vendor/shopware/core/System/CustomEntity/CustomEntityRegistrar.php line 35

  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\System\CustomEntity;
  3. use Doctrine\DBAL\Connection;
  4. use Doctrine\DBAL\Exception;
  5. use Shopware\Core\Framework\DataAbstractionLayer\DefinitionInstanceRegistry;
  6. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEventFactory;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Read\EntityReaderInterface;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Search\EntityAggregatorInterface;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Search\EntitySearcherInterface;
  11. use Shopware\Core\Framework\DataAbstractionLayer\VersionManager;
  12. use Shopware\Core\Framework\Log\Package;
  13. use Shopware\Core\System\CustomEntity\Schema\DynamicEntityDefinition;
  14. use Symfony\Component\DependencyInjection\ContainerInterface;
  15. /**
  16.  * @internal
  17.  */
  18. #[Package('core')]
  19. class CustomEntityRegistrar
  20. {
  21.     public function __construct(private readonly ContainerInterface $container)
  22.     {
  23.     }
  24.     public function register(): void
  25.     {
  26.         if (!$this->container->get(Connection::class)->isConnected()) {
  27.             return;
  28.         }
  29.         try {
  30.             $entities $this->container->get(Connection::class)->fetchAllAssociative('
  31.                 SELECT custom_entity.name, custom_entity.fields, custom_entity.flags
  32.                 FROM custom_entity
  33.                     LEFT JOIN app ON app.id = custom_entity.app_id
  34.                 WHERE custom_entity.app_id IS NULL OR app.active = 1
  35.             ');
  36.         } catch (Exception) {
  37.             // kernel booted without database connection, or booted for migration and custom entity table not created yet
  38.             return;
  39.         }
  40.         $definitions = [];
  41.         $registry $this->container->get(DefinitionInstanceRegistry::class);
  42.         foreach ($entities as $entity) {
  43.             $fields json_decode((string) $entity['fields'], true512\JSON_THROW_ON_ERROR);
  44.             try {
  45.                 $flags json_decode((string) $entity['flags'], true512\JSON_THROW_ON_ERROR);
  46.             } catch (\JsonException $e) {
  47.                 $flags = [];
  48.             }
  49.             $definition DynamicEntityDefinition::create($entity['name'], $fields$flags$this->container);
  50.             $definitions[] = $definition;
  51.             $this->container->set($definition->getEntityName(), $definition);
  52.             $this->container->set($definition->getEntityName() . '.repository'$this->createRepository($definition));
  53.             $registry->register($definition$definition->getEntityName());
  54.         }
  55.         foreach ($definitions as $definition) {
  56.             // triggers field generation to generate reverse foreign keys, translation definitions and mapping definitions
  57.             $definition->getFields();
  58.         }
  59.     }
  60.     private function createRepository(DynamicEntityDefinition $definition): EntityRepository
  61.     {
  62.         return new EntityRepository(
  63.             $definition,
  64.             $this->container->get(EntityReaderInterface::class),
  65.             $this->container->get(VersionManager::class),
  66.             $this->container->get(EntitySearcherInterface::class),
  67.             $this->container->get(EntityAggregatorInterface::class),
  68.             $this->container->get('event_dispatcher'),
  69.             $this->container->get(EntityLoadedEventFactory::class)
  70.         );
  71.     }
  72. }