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

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