vendor/easycorp/easyadmin-bundle/src/Configuration/MetadataConfigPass.php line 38

Open in your IDE?
  1. <?php
  2. namespace EasyCorp\Bundle\EasyAdminBundle\Configuration;
  3. use Doctrine\Common\Persistence\ManagerRegistry;
  4. use Doctrine\ORM\Mapping\ClassMetadata;
  5. use Symfony\Component\Config\Definition\Exception\InvalidTypeException;
  6. /**
  7.  * Introspects the metadata of the Doctrine entities to complete the
  8.  * configuration of the properties.
  9.  *
  10.  * @author Javier Eguiluz <javier.eguiluz@gmail.com>
  11.  */
  12. class MetadataConfigPass implements ConfigPassInterface
  13. {
  14.     /** @var ManagerRegistry */
  15.     private $doctrine;
  16.     public function __construct(ManagerRegistry $doctrine)
  17.     {
  18.         $this->doctrine $doctrine;
  19.     }
  20.     public function process(array $backendConfig)
  21.     {
  22.         foreach ($backendConfig['entities'] as $entityName => $entityConfig) {
  23.             try {
  24.                 $em $this->doctrine->getManagerForClass($entityConfig['class']);
  25.             } catch (\ReflectionException $e) {
  26.                 throw new InvalidTypeException(sprintf('The configured class "%s" for the path "easy_admin.entities.%s" does not exist. Did you forget to create the entity class or to define its namespace?'$entityConfig['class'], $entityName));
  27.             }
  28.             if (null === $em) {
  29.                 throw new InvalidTypeException(sprintf('The configured class "%s" for the path "easy_admin.entities.%s" is no mapped entity.'$entityConfig['class'], $entityName));
  30.             }
  31.             $entityMetadata $em->getMetadataFactory()->getMetadataFor($entityConfig['class']);
  32.             $entityConfig['primary_key_field_name'] = $entityMetadata->getSingleIdentifierFieldName();
  33.             $entityConfig['properties'] = $this->processEntityPropertiesMetadata($entityMetadata);
  34.             $backendConfig['entities'][$entityName] = $entityConfig;
  35.         }
  36.         return $backendConfig;
  37.     }
  38.     /**
  39.      * Takes the entity metadata introspected via Doctrine and completes its
  40.      * contents to simplify data processing for the rest of the application.
  41.      *
  42.      * @param ClassMetadata $entityMetadata The entity metadata introspected via Doctrine
  43.      *
  44.      * @return array The entity properties metadata provided by Doctrine
  45.      *
  46.      * @throws \RuntimeException
  47.      */
  48.     private function processEntityPropertiesMetadata(ClassMetadata $entityMetadata)
  49.     {
  50.         $entityPropertiesMetadata = [];
  51.         if ($entityMetadata->isIdentifierComposite) {
  52.             throw new \RuntimeException(sprintf("The '%s' entity isn't valid because it contains a composite primary key."$entityMetadata->name));
  53.         }
  54.         // introspect regular entity fields
  55.         foreach ($entityMetadata->fieldMappings as $fieldName => $fieldMetadata) {
  56.             $entityPropertiesMetadata[$fieldName] = $fieldMetadata;
  57.         }
  58.         // introspect fields for entity associations
  59.         foreach ($entityMetadata->associationMappings as $fieldName => $associationMetadata) {
  60.             $entityPropertiesMetadata[$fieldName] = array_merge($associationMetadata, [
  61.                 'type' => 'association',
  62.                 'associationType' => $associationMetadata['type'],
  63.             ]);
  64.             // associations different from *-to-one cannot be sorted
  65.             if ($associationMetadata['type'] & ClassMetadata::TO_MANY) {
  66.                 $entityPropertiesMetadata[$fieldName]['sortable'] = false;
  67.             }
  68.         }
  69.         return $entityPropertiesMetadata;
  70.     }
  71. }