vendor/shopware/core/Framework/Bundle.php line 74

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Framework;
  3. use Shopware\Core\Framework\Adapter\Asset\AssetPackageService;
  4. use Shopware\Core\Framework\Adapter\Filesystem\PrefixFilesystem;
  5. use Shopware\Core\Framework\DependencyInjection\CompilerPass\AddCoreMigrationPathCompilerPass;
  6. use Shopware\Core\Framework\DependencyInjection\CompilerPass\BusinessEventRegisterCompilerPass;
  7. use Shopware\Core\Framework\Migration\MigrationSource;
  8. use Shopware\Core\Kernel;
  9. use Symfony\Component\Config\FileLocator;
  10. use Symfony\Component\Config\Loader\DelegatingLoader;
  11. use Symfony\Component\Config\Loader\LoaderResolver;
  12. use Symfony\Component\DependencyInjection\ContainerBuilder;
  13. use Symfony\Component\DependencyInjection\Definition;
  14. use Symfony\Component\DependencyInjection\Loader\PhpFileLoader;
  15. use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
  16. use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
  17. use Symfony\Component\DependencyInjection\Reference;
  18. use Symfony\Component\Filesystem\Filesystem;
  19. use Symfony\Component\HttpKernel\Bundle\Bundle as SymfonyBundle;
  20. use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator;
  21. use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter;
  22. abstract class Bundle extends SymfonyBundle
  23. {
  24.     public function build(ContainerBuilder $container): void
  25.     {
  26.         parent::build($container);
  27.         $this->registerContainerFile($container);
  28.         $this->registerMigrationPath($container);
  29.         $this->registerFilesystem($container'private');
  30.         $this->registerFilesystem($container'public');
  31.         $this->registerEvents($container);
  32.     }
  33.     public function boot(): void
  34.     {
  35.         $this->container->get(AssetPackageService::class)->addAssetPackage($this->getName(), $this->getPath());
  36.         parent::boot();
  37.     }
  38.     public function getMigrationNamespace(): string
  39.     {
  40.         return $this->getNamespace() . '\Migration';
  41.     }
  42.     public function getMigrationPath(): string
  43.     {
  44.         $migrationSuffix str_replace(
  45.             $this->getNamespace(),
  46.             '',
  47.             $this->getMigrationNamespace()
  48.         );
  49.         return $this->getPath() . str_replace('\\''/'$migrationSuffix);
  50.     }
  51.     final public function getContainerPrefix(): string
  52.     {
  53.         return (new CamelCaseToSnakeCaseNameConverter())->normalize($this->getName());
  54.     }
  55.     public function configureRoutes(RoutingConfigurator $routesstring $environment): void
  56.     {
  57.         $fileSystem = new Filesystem();
  58.         $confDir $this->getPath() . '/Resources/config';
  59.         if ($fileSystem->exists($confDir)) {
  60.             $routes->import($confDir '/{routes}/*' Kernel::CONFIG_EXTS'glob');
  61.             $routes->import($confDir '/{routes}/' $environment '/**/*' Kernel::CONFIG_EXTS'glob');
  62.             $routes->import($confDir '/{routes}' Kernel::CONFIG_EXTS'glob');
  63.             $routes->import($confDir '/{routes}_' $environment Kernel::CONFIG_EXTS'glob');
  64.         }
  65.     }
  66.     public function configureRouteOverwrites(RoutingConfigurator $routesstring $environment): void
  67.     {
  68.         $fileSystem = new Filesystem();
  69.         $confDir $this->getPath() . '/Resources/config';
  70.         if ($fileSystem->exists($confDir)) {
  71.             $routes->import($confDir '/{routes_overwrite}' Kernel::CONFIG_EXTS'glob');
  72.         }
  73.     }
  74.     public function getTemplatePriority(): int
  75.     {
  76.         return 0;
  77.     }
  78.     /**
  79.      * Returns a list of all action event class references of this bundle. The events will be registered inside the `\Shopware\Core\Framework\Event\BusinessEventRegistry`.
  80.      *
  81.      * @return array<string>
  82.      */
  83.     protected function getActionEventClasses(): array
  84.     {
  85.         return [];
  86.     }
  87.     protected function registerMigrationPath(ContainerBuilder $container): void
  88.     {
  89.         $migrationPath $this->getMigrationPath();
  90.         if (!is_dir($migrationPath)) {
  91.             return;
  92.         }
  93.         $container->register(MigrationSource::class . '_' $this->getName(), MigrationSource::class)
  94.             ->addArgument($this->getName())
  95.             ->addArgument([$migrationPath => $this->getMigrationNamespace()])
  96.             ->addTag('shopware.migration_source');
  97.     }
  98.     /**
  99.      * @deprecated tag:v6.5.0 - Use own migration source instead
  100.      */
  101.     protected function addCoreMigrationPath(ContainerBuilder $containerstring $pathstring $namespace): void
  102.     {
  103.         Feature::triggerDeprecationOrThrow(
  104.             'v6.5.0.0',
  105.             Feature::deprecatedMethodMessage(__CLASS____METHOD__'v6.5.0.0')
  106.         );
  107.         $container->addCompilerPass(new AddCoreMigrationPathCompilerPass($path$namespace));
  108.     }
  109.     private function registerFilesystem(ContainerBuilder $containerstring $key): void
  110.     {
  111.         $containerPrefix $this->getContainerPrefix();
  112.         $parameterKey sprintf('shopware.filesystem.%s'$key);
  113.         $serviceId sprintf('%s.filesystem.%s'$containerPrefix$key);
  114.         $filesystem = new Definition(
  115.             PrefixFilesystem::class,
  116.             [
  117.                 new Reference($parameterKey),
  118.                 'plugins/' $containerPrefix,
  119.             ]
  120.         );
  121.         $filesystem->setPublic(true);
  122.         $container->setDefinition($serviceId$filesystem);
  123.     }
  124.     private function registerEvents(ContainerBuilder $container): void
  125.     {
  126.         $classes $this->getActionEventClasses();
  127.         if ($classes === []) {
  128.             return;
  129.         }
  130.         $container->addCompilerPass(new BusinessEventRegisterCompilerPass($classes));
  131.     }
  132.     /**
  133.      * Looks for service definition files inside the `Resources/config`
  134.      * directory and loads either xml or yml files.
  135.      */
  136.     private function registerContainerFile(ContainerBuilder $container): void
  137.     {
  138.         $fileLocator = new FileLocator($this->getPath());
  139.         $loaderResolver = new LoaderResolver([
  140.             new XmlFileLoader($container$fileLocator),
  141.             new YamlFileLoader($container$fileLocator),
  142.             new PhpFileLoader($container$fileLocator),
  143.         ]);
  144.         $delegatingLoader = new DelegatingLoader($loaderResolver);
  145.         foreach ($this->getServicesFilePathArray($this->getPath() . '/Resources/config/services.*') as $path) {
  146.             $delegatingLoader->load($path);
  147.         }
  148.         if ($container->getParameter('kernel.environment') === 'test') {
  149.             foreach ($this->getServicesFilePathArray($this->getPath() . '/Resources/config/services_test.*') as $testPath) {
  150.                 $delegatingLoader->load($testPath);
  151.             }
  152.         }
  153.     }
  154.     private function getServicesFilePathArray(string $path): array
  155.     {
  156.         $pathArray glob($path);
  157.         if ($pathArray === false) {
  158.             return [];
  159.         }
  160.         return $pathArray;
  161.     }
  162. }