custom/plugins/SheStorefrontUrls/src/SheStorefrontUrls.php line 16

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace She\StorefrontUrls;
  3. use She\StorefrontUrls\Components\RequestMatcher;
  4. use She\StorefrontUrls\Components\UrlGenerator;
  5. use Shopware\Core\Framework\Context;
  6. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  8. use Shopware\Core\Framework\Plugin;
  9. use Shopware\Core\System\SystemConfig\SystemConfigService;
  10. use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
  11. use Symfony\Component\DependencyInjection\ContainerBuilder;
  12. use Symfony\Component\Routing\RouteCollectionBuilder;
  13. class SheStorefrontUrls extends Plugin implements CompilerPassInterface
  14. {
  15.     public const CONFIG_NAME 'SheStorefrontUrls.config';
  16.     public function build(ContainerBuilder $container): void
  17.     {
  18.         parent::build($container);
  19.         $container->addCompilerPass($this);
  20.     }
  21.     public function configureRoutes(RouteCollectionBuilder $routeBuilderstring $environment): void
  22.     {
  23.         if (!$this->isActive()) {
  24.             return;
  25.         }
  26.         $this->buildRoutes($routeBuilder);
  27.     }
  28.     public function process(ContainerBuilder $container)
  29.     {
  30.         $router $container->getDefinition('router.default');
  31.         $router->addMethodCall('setOption', ['generator_class'UrlGenerator::class]);
  32.     }
  33.     private function buildRoutes(RouteCollectionBuilder $routeBuilder)
  34.     {
  35.         $routes $routeBuilder->build()->all();
  36.         /** @var EntityRepositoryInterface $repository */
  37.         $repository $this->container->get('sales_channel.repository');
  38.         $salesChannelIds $repository->searchIds(new Criteria(), Context::createDefaultContext())->getIds();
  39.         $salesChannelIds[] = null;
  40.         foreach ($salesChannelIds as $salesChannelId) {
  41.             $config $this->container->get(SystemConfigService::class)->get(self::CONFIG_NAME$salesChannelId);
  42.             if (empty($config)) {
  43.                 continue;
  44.             }
  45.             foreach ($config as $key => $paths) {
  46.                 if (empty($paths)) {
  47.                     continue;
  48.                 }
  49.                 $key strtolower(substr(preg_replace('/(?<!^)[A-Z]/''.$0'$key), 6));
  50.                 if (!isset($routes[$key])) {
  51.                     continue;
  52.                 }
  53.                 $paths explode('&'$paths);
  54.                 foreach ($paths as $path) {
  55.                     $name $key;
  56.                     $routes[$key]->setDefault('_canonical_route'$key);
  57.                     $route = clone $routes[$key];
  58.                     if ($salesChannelId !== null) {
  59.                         $name .= '.' $salesChannelId;
  60.                         $route->setDefault('_channel'$salesChannelId);
  61.                     }
  62.                     $path explode('='$path);
  63.                     if (count($path) > 1) {
  64.                         $name .= '.' $path[0];
  65.                         $route->setDefault('_locale'$path[0]);
  66.                         $route->setPath($path[1]);
  67.                     } else {
  68.                         $route->setPath($path[0]);
  69.                     }
  70.                     $routeBuilder->addRoute($route$name);
  71.                     $routeBuilder->addRoute($routes[$key], $key '.original');
  72.                 }
  73.             }
  74.         }
  75.     }
  76. }