vendor/shopware/core/Framework/DataAbstractionLayer/DefinitionInstanceRegistry.php line 117

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Framework\DataAbstractionLayer;
  3. use Shopware\Core\Framework\DataAbstractionLayer\Dbal\FieldAccessorBuilder\FieldAccessorBuilderInterface;
  4. use Shopware\Core\Framework\DataAbstractionLayer\Dbal\FieldResolver\AbstractFieldResolver;
  5. use Shopware\Core\Framework\DataAbstractionLayer\Exception\DefinitionNotFoundException;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Exception\EntityRepositoryNotFoundException;
  7. use Shopware\Core\Framework\DataAbstractionLayer\FieldSerializer\FieldSerializerInterface;
  8. use Symfony\Component\DependencyInjection\ContainerInterface;
  9. class DefinitionInstanceRegistry
  10. {
  11.     /**
  12.      * @var ContainerInterface
  13.      */
  14.     protected $container;
  15.     /**
  16.      * @var array
  17.      */
  18.     protected $repositoryMap;
  19.     /**
  20.      * @var array
  21.      */
  22.     protected $definitions;
  23.     /**
  24.      * @var array
  25.      */
  26.     protected $entityClassMapping;
  27.     /**
  28.      * @internal
  29.      *
  30.      * @param array $definitionMap array of $entityName => $definitionServiceId,
  31.      *                             eg. 'product' => '\Shopware\Core\Content\Product\ProductDefinition'
  32.      * @param array $repositoryMap array of $entityName => $repositoryServiceId, eg. 'product' => 'product.repository'
  33.      */
  34.     public function __construct(ContainerInterface $container, array $definitionMap, array $repositoryMap)
  35.     {
  36.         $this->container $container;
  37.         $this->definitions $definitionMap;
  38.         $this->repositoryMap $repositoryMap;
  39.     }
  40.     /**
  41.      * @throws EntityRepositoryNotFoundException
  42.      */
  43.     public function getRepository(string $entityName): EntityRepositoryInterface
  44.     {
  45.         $entityRepositoryClass $this->getEntityRepositoryClassByEntityName($entityName);
  46.         /** @var EntityRepositoryInterface $entityRepository */
  47.         $entityRepository $this->container->get($entityRepositoryClass);
  48.         return $entityRepository;
  49.     }
  50.     public function get(string $class): EntityDefinition
  51.     {
  52.         if ($this->container->has($class)) {
  53.             $definition $this->container->get($class);
  54.             /** @var EntityDefinition $definition */
  55.             return $definition;
  56.         }
  57.         throw new DefinitionNotFoundException($class);
  58.     }
  59.     /**
  60.      * Shorthand to get the definition instance by class and use provided key as entity name as fallback
  61.      */
  62.     public function getByClassOrEntityName(string $key): EntityDefinition
  63.     {
  64.         try {
  65.             return $this->get($key);
  66.         } catch (DefinitionNotFoundException $e) {
  67.             return $this->getByEntityName($key);
  68.         }
  69.     }
  70.     public function has(string $name): bool
  71.     {
  72.         return isset($this->definitions[$name]);
  73.     }
  74.     /**
  75.      * @throws DefinitionNotFoundException
  76.      */
  77.     public function getByEntityName(string $entityName): EntityDefinition
  78.     {
  79.         $definitionClass $this->getDefinitionClassByEntityName($entityName);
  80.         if ($this->container->has($definitionClass)) {
  81.             return $this->get($definitionClass);
  82.         }
  83.         throw new DefinitionNotFoundException($entityName);
  84.     }
  85.     /**
  86.      * @return EntityDefinition[]
  87.      */
  88.     public function getDefinitions(): array
  89.     {
  90.         return array_map(function (string $name): EntityDefinition {
  91.             return $this->get($name);
  92.         }, $this->definitions);
  93.     }
  94.     public function getSerializer(string $serializerClass): FieldSerializerInterface
  95.     {
  96.         /** @var FieldSerializerInterface $fieldSerializer */
  97.         $fieldSerializer $this->container->get($serializerClass);
  98.         return $fieldSerializer;
  99.     }
  100.     /**
  101.      * @return AbstractFieldResolver
  102.      */
  103.     public function getResolver(string $resolverClass)
  104.     {
  105.         /** @var AbstractFieldResolver $fieldResolver */
  106.         $fieldResolver $this->container->get($resolverClass);
  107.         return $fieldResolver;
  108.     }
  109.     public function getAccessorBuilder(string $accessorBuilderClass): FieldAccessorBuilderInterface
  110.     {
  111.         /** @var FieldAccessorBuilderInterface $fieldAccessorBuilder */
  112.         $fieldAccessorBuilder $this->container->get($accessorBuilderClass);
  113.         return $fieldAccessorBuilder;
  114.     }
  115.     public function getByEntityClass(Entity $entity): ?EntityDefinition
  116.     {
  117.         $map $this->loadClassMapping();
  118.         $source \get_class($entity);
  119.         return $map[$source] ?? null;
  120.     }
  121.     public function register(EntityDefinition $definition, ?string $serviceId null): void
  122.     {
  123.         if (!$serviceId) {
  124.             $serviceId \get_class($definition);
  125.         }
  126.         if (!$this->container->has($serviceId)) {
  127.             $this->container->set($serviceId$definition);
  128.         }
  129.         if ($this->entityClassMapping !== null) {
  130.             $this->entityClassMapping[$definition->getEntityClass()] = $definition;
  131.         }
  132.         $this->definitions[$definition->getEntityName()] = $serviceId;
  133.         $this->repositoryMap[$definition->getEntityName()] = $definition->getEntityName() . '.repository';
  134.         $definition->compile($this);
  135.     }
  136.     private function loadClassMapping(): array
  137.     {
  138.         if ($this->entityClassMapping !== null) {
  139.             return $this->entityClassMapping;
  140.         }
  141.         $this->entityClassMapping = [];
  142.         foreach ($this->definitions as $element) {
  143.             $definition $this->container->get($element);
  144.             if (!$definition) {
  145.                 continue;
  146.             }
  147.             try {
  148.                 $class $definition->getEntityClass();
  149.                 $this->entityClassMapping[$class] = $definition;
  150.             } catch (\Throwable $e) {
  151.             }
  152.         }
  153.         return $this->entityClassMapping;
  154.     }
  155.     /**
  156.      * @throws DefinitionNotFoundException
  157.      */
  158.     private function getDefinitionClassByEntityName(string $entityName): string
  159.     {
  160.         if (!isset($this->definitions[$entityName])) {
  161.             throw new DefinitionNotFoundException($entityName);
  162.         }
  163.         return $this->definitions[$entityName];
  164.     }
  165.     /**
  166.      * @throws EntityRepositoryNotFoundException
  167.      */
  168.     private function getEntityRepositoryClassByEntityName(string $entityName): string
  169.     {
  170.         if (!isset($this->repositoryMap[$entityName])) {
  171.             throw new EntityRepositoryNotFoundException($entityName);
  172.         }
  173.         return $this->repositoryMap[$entityName];
  174.     }
  175. }