vendor/vich/uploader-bundle/src/Metadata/Driver/AnnotationDriver.php line 86

  1. <?php
  2. namespace Vich\UploaderBundle\Metadata\Driver;
  3. use Doctrine\Common\Annotations\Reader as AnnotationReader;
  4. use Doctrine\Persistence\ManagerRegistry;
  5. use Metadata\ClassMetadata as JMSClassMetadata;
  6. use Metadata\Driver\AdvancedDriverInterface;
  7. use Vich\UploaderBundle\Mapping\Annotation\Uploadable;
  8. use Vich\UploaderBundle\Mapping\Annotation\UploadableField;
  9. use Vich\UploaderBundle\Metadata\ClassMetadata;
  10. /**
  11.  * @author Kévin Gomez <contact@kevingomez.fr>
  12.  * @author Konstantin Myakshin <koc-dp@yandex.ru>
  13.  */
  14. class AnnotationDriver implements AdvancedDriverInterface
  15. {
  16.     /**
  17.      * @deprecated
  18.      */
  19.     public const UPLOADABLE_ANNOTATION Uploadable::class;
  20.     /**
  21.      * @deprecated
  22.      */
  23.     public const UPLOADABLE_FIELD_ANNOTATION UploadableField::class;
  24.     /** @var AnnotationReader|AttributeReader */
  25.     protected $reader;
  26.     /** @var ManagerRegistry[] */
  27.     private $managerRegistryList;
  28.     public function __construct(AnnotationReader $reader, array $managerRegistryList)
  29.     {
  30.         $this->reader $reader;
  31.         $this->managerRegistryList $managerRegistryList;
  32.     }
  33.     public function loadMetadataForClass(\ReflectionClass $class): ?JMSClassMetadata
  34.     {
  35.         if (!$this->isUploadable($class)) {
  36.             return null;
  37.         }
  38.         $classMetadata = new ClassMetadata($class->name);
  39.         $classMetadata->fileResources[] = $class->getFileName();
  40.         $classes = [];
  41.         do {
  42.             $classes[] = $class;
  43.             $class $class->getParentClass();
  44.         } while (false !== $class);
  45.         $classes \array_reverse($classesfalse);
  46.         $properties = [];
  47.         foreach ($classes as $class) {
  48.             $properties \array_merge($properties$class->getProperties());
  49.         }
  50.         foreach ($properties as $property) {
  51.             $uploadableField $this->reader->getPropertyAnnotation($propertyUploadableField::class);
  52.             if (null === $uploadableField) {
  53.                 continue;
  54.             }
  55.             /* @var $uploadableField UploadableField */
  56.             // TODO: try automatically determinate target fields if embeddable used
  57.             $fieldMetadata = [
  58.                 'mapping' => $uploadableField->getMapping(),
  59.                 'propertyName' => $property->getName(),
  60.                 'fileNameProperty' => $uploadableField->getFileNameProperty(),
  61.                 'size' => $uploadableField->getSize(),
  62.                 'mimeType' => $uploadableField->getMimeType(),
  63.                 'originalName' => $uploadableField->getOriginalName(),
  64.                 'dimensions' => $uploadableField->getDimensions(),
  65.             ];
  66.             // TODO: store UploadableField object instead of array
  67.             $classMetadata->fields[$property->getName()] = $fieldMetadata;
  68.         }
  69.         return $classMetadata;
  70.     }
  71.     public function getAllClassNames(): array
  72.     {
  73.         $classes = [];
  74.         $metadata = [];
  75.         foreach ($this->managerRegistryList as $managerRegisty) {
  76.             $managers $managerRegisty->getManagers();
  77.             foreach ($managers as $manager) {
  78.                 $metadata[] = $manager->getMetadataFactory()->getAllMetadata();
  79.             }
  80.         }
  81.         $metadata \array_merge(...$metadata);
  82.         /** @var \Doctrine\Persistence\Mapping\ClassMetadata $classMeta */
  83.         foreach ($metadata as $classMeta) {
  84.             if ($this->isUploadable(new \ReflectionClass($classMeta->getName()))) {
  85.                 $classes[] = $classMeta->getName();
  86.             }
  87.         }
  88.         return $classes;
  89.     }
  90.     protected function isUploadable(\ReflectionClass $class): bool
  91.     {
  92.         return null !== $this->reader->getClassAnnotation($classUploadable::class);
  93.     }
  94. }