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

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