src/Core/Framework/Framework.php line 47

  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Framework;
  3. use Shopware\Core\Framework\DataAbstractionLayer\DefinitionInstanceRegistry;
  4. use Shopware\Core\Framework\DataAbstractionLayer\ExtensionRegistry;
  5. use Shopware\Core\Framework\DependencyInjection\CompilerPass\ActionEventCompilerPass;
  6. use Shopware\Core\Framework\DependencyInjection\CompilerPass\AssetRegistrationCompilerPass;
  7. use Shopware\Core\Framework\DependencyInjection\CompilerPass\DefaultTransportCompilerPass;
  8. use Shopware\Core\Framework\DependencyInjection\CompilerPass\DemodataCompilerPass;
  9. use Shopware\Core\Framework\DependencyInjection\CompilerPass\DisableTwigCacheWarmerCompilerPass;
  10. use Shopware\Core\Framework\DependencyInjection\CompilerPass\EntityCompilerPass;
  11. use Shopware\Core\Framework\DependencyInjection\CompilerPass\FeatureFlagCompilerPass;
  12. use Shopware\Core\Framework\DependencyInjection\CompilerPass\FilesystemConfigMigrationCompilerPass;
  13. use Shopware\Core\Framework\DependencyInjection\CompilerPass\FrameworkMigrationReplacementCompilerPass;
  14. use Shopware\Core\Framework\DependencyInjection\CompilerPass\RateLimiterCompilerPass;
  15. use Shopware\Core\Framework\DependencyInjection\CompilerPass\RedisPrefixCompilerPass;
  16. use Shopware\Core\Framework\DependencyInjection\CompilerPass\RouteScopeCompilerPass;
  17. use Shopware\Core\Framework\DependencyInjection\CompilerPass\TwigEnvironmentCompilerPass;
  18. use Shopware\Core\Framework\DependencyInjection\CompilerPass\TwigLoaderConfigCompilerPass;
  19. use Shopware\Core\Framework\DependencyInjection\FrameworkExtension;
  20. use Shopware\Core\Framework\Increment\IncrementerGatewayCompilerPass;
  21. use Shopware\Core\Framework\Log\Package;
  22. use Shopware\Core\Framework\Migration\MigrationCompilerPass;
  23. use Shopware\Core\Framework\Test\DependencyInjection\CompilerPass\ContainerVisibilityCompilerPass;
  24. use Shopware\Core\Framework\Test\RateLimiter\DisableRateLimiterCompilerPass;
  25. use Shopware\Core\Kernel;
  26. use Shopware\Core\System\SalesChannel\Entity\SalesChannelDefinitionInstanceRegistry;
  27. use Symfony\Component\Config\FileLocator;
  28. use Symfony\Component\Config\Loader\DelegatingLoader;
  29. use Symfony\Component\Config\Loader\LoaderResolver;
  30. use Symfony\Component\DependencyInjection\Compiler\PassConfig;
  31. use Symfony\Component\DependencyInjection\ContainerBuilder;
  32. use Symfony\Component\DependencyInjection\Extension\Extension;
  33. use Symfony\Component\DependencyInjection\Loader\ClosureLoader;
  34. use Symfony\Component\DependencyInjection\Loader\DirectoryLoader;
  35. use Symfony\Component\DependencyInjection\Loader\GlobFileLoader;
  36. use Symfony\Component\DependencyInjection\Loader\IniFileLoader;
  37. use Symfony\Component\DependencyInjection\Loader\PhpFileLoader;
  38. use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
  39. use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
  40. /**
  41.  * @internal
  42.  */
  43. #[Package('core')]
  44. class Framework extends Bundle
  45. {
  46.     public function getTemplatePriority(): int
  47.     {
  48.         return -1;
  49.     }
  50.     public function getContainerExtension(): Extension
  51.     {
  52.         return new FrameworkExtension();
  53.     }
  54.     /**
  55.      * {@inheritdoc}
  56.      */
  57.     public function build(ContainerBuilder $container): void
  58.     {
  59.         $container->setParameter('locale''en-GB');
  60.         $environment = (string) $container->getParameter('kernel.environment');
  61.         $this->buildConfig($container$environment);
  62.         $loader = new XmlFileLoader($container, new FileLocator(__DIR__ '/DependencyInjection/'));
  63.         $loader->load('services.xml');
  64.         $loader->load('acl.xml');
  65.         $loader->load('api.xml');
  66.         $loader->load('app.xml');
  67.         $loader->load('custom-field.xml');
  68.         $loader->load('data-abstraction-layer.xml');
  69.         $loader->load('demodata.xml');
  70.         $loader->load('event.xml');
  71.         $loader->load('hydrator.xml');
  72.         $loader->load('filesystem.xml');
  73.         $loader->load('message-queue.xml');
  74.         $loader->load('plugin.xml');
  75.         $loader->load('rule.xml');
  76.         $loader->load('scheduled-task.xml');
  77.         $loader->load('store.xml');
  78.         $loader->load('script.xml');
  79.         $loader->load('language.xml');
  80.         $loader->load('update.xml');
  81.         $loader->load('seo.xml');
  82.         $loader->load('webhook.xml');
  83.         $loader->load('rate-limiter.xml');
  84.         $loader->load('increment.xml');
  85.         if ($container->getParameter('kernel.environment') === 'test') {
  86.             $loader->load('services_test.xml');
  87.             $loader->load('store_test.xml');
  88.             $loader->load('seo_test.xml');
  89.         }
  90.         // make sure to remove services behind a feature flag, before some other compiler passes may reference them, therefore the high priority
  91.         $container->addCompilerPass(new FeatureFlagCompilerPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION1000);
  92.         $container->addCompilerPass(new EntityCompilerPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION0);
  93.         $container->addCompilerPass(new MigrationCompilerPass(), PassConfig::TYPE_AFTER_REMOVING0);
  94.         $container->addCompilerPass(new ActionEventCompilerPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION0);
  95.         $container->addCompilerPass(new DisableTwigCacheWarmerCompilerPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION0);
  96.         $container->addCompilerPass(new DefaultTransportCompilerPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION0);
  97.         $container->addCompilerPass(new TwigLoaderConfigCompilerPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION0);
  98.         $container->addCompilerPass(new TwigEnvironmentCompilerPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION0);
  99.         $container->addCompilerPass(new RouteScopeCompilerPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION0);
  100.         $container->addCompilerPass(new AssetRegistrationCompilerPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION0);
  101.         $container->addCompilerPass(new FilesystemConfigMigrationCompilerPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION0);
  102.         $container->addCompilerPass(new RateLimiterCompilerPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION0);
  103.         $container->addCompilerPass(new IncrementerGatewayCompilerPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION0);
  104.         $container->addCompilerPass(new RedisPrefixCompilerPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION0);
  105.         if ($container->getParameter('kernel.environment') === 'test') {
  106.             $container->addCompilerPass(new DisableRateLimiterCompilerPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION0);
  107.             $container->addCompilerPass(new ContainerVisibilityCompilerPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION0);
  108.         }
  109.         $container->addCompilerPass(new FrameworkMigrationReplacementCompilerPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION0);
  110.         $container->addCompilerPass(new DemodataCompilerPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION0);
  111.         parent::build($container);
  112.     }
  113.     public function boot(): void
  114.     {
  115.         parent::boot();
  116.         $featureFlags $this->container->getParameter('shopware.feature.flags');
  117.         if (!\is_array($featureFlags)) {
  118.             throw new \RuntimeException('Container parameter "shopware.feature.flags" needs to be an array');
  119.         }
  120.         Feature::registerFeatures($featureFlags);
  121.         $cacheDir $this->container->getParameter('kernel.cache_dir');
  122.         if (!\is_string($cacheDir)) {
  123.             throw new \RuntimeException('Container parameter "kernel.cache_dir" needs to be a string');
  124.         }
  125.         $this->registerEntityExtensions(
  126.             $this->container->get(DefinitionInstanceRegistry::class),
  127.             $this->container->get(SalesChannelDefinitionInstanceRegistry::class),
  128.             $this->container->get(ExtensionRegistry::class)
  129.         );
  130.     }
  131.     /**
  132.      * @return string[]
  133.      */
  134.     protected function getCoreMigrationPaths(): array
  135.     {
  136.         return [
  137.             __DIR__ '/../Migration' => 'Shopware\Core\Migration',
  138.         ];
  139.     }
  140.     private function buildConfig(ContainerBuilder $containerstring $environment): void
  141.     {
  142.         $cacheDir $container->getParameter('kernel.cache_dir');
  143.         if (!\is_string($cacheDir)) {
  144.             throw new \RuntimeException('Container parameter "kernel.cache_dir" needs to be a string');
  145.         }
  146.         $locator = new FileLocator('Resources/config');
  147.         $resolver = new LoaderResolver([
  148.             new XmlFileLoader($container$locator),
  149.             new YamlFileLoader($container$locator),
  150.             new IniFileLoader($container$locator),
  151.             new PhpFileLoader($container$locator),
  152.             new GlobFileLoader($container$locator),
  153.             new DirectoryLoader($container$locator),
  154.             new ClosureLoader($container),
  155.         ]);
  156.         $configLoader = new DelegatingLoader($resolver);
  157.         $confDir $this->getPath() . '/Resources/config';
  158.         $configLoader->load($confDir '/{packages}/*' Kernel::CONFIG_EXTS'glob');
  159.         $configLoader->load($confDir '/{packages}/' $environment '/*' Kernel::CONFIG_EXTS'glob');
  160.         if ($environment === 'e2e') {
  161.             $configLoader->load($confDir '/{packages}/prod/*' Kernel::CONFIG_EXTS'glob');
  162.         }
  163.     }
  164.     private function registerEntityExtensions(
  165.         DefinitionInstanceRegistry $definitionRegistry,
  166.         SalesChannelDefinitionInstanceRegistry $salesChannelRegistry,
  167.         ExtensionRegistry $registry
  168.     ): void {
  169.         foreach ($registry->getExtensions() as $extension) {
  170.             /** @var string $class */
  171.             $class $extension->getDefinitionClass();
  172.             $definition $definitionRegistry->get($class);
  173.             $definition->addExtension($extension);
  174.             $salesChannelDefinition $salesChannelRegistry->get($class);
  175.             // same definition? do not added extension
  176.             if ($salesChannelDefinition !== $definition) {
  177.                 $salesChannelDefinition->addExtension($extension);
  178.             }
  179.         }
  180.     }
  181. }