vendor/presta/sitemap-bundle/src/EventListener/RouteAnnotationEventListener.php line 60

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the PrestaSitemapBundle package.
  4.  *
  5.  * (c) PrestaConcept <https://prestaconcept.net>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Presta\SitemapBundle\EventListener;
  11. use Presta\SitemapBundle\Event\SitemapAddUrlEvent;
  12. use Presta\SitemapBundle\Event\SitemapPopulateEvent;
  13. use Presta\SitemapBundle\Routing\RouteOptionParser;
  14. use Presta\SitemapBundle\Service\UrlContainerInterface;
  15. use Presta\SitemapBundle\Sitemap\Url\UrlConcrete;
  16. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  17. use Symfony\Component\Routing\Exception\MissingMandatoryParametersException;
  18. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  19. use Symfony\Component\Routing\RouterInterface;
  20. /**
  21.  * Listen to {@see SitemapPopulateEvent} event.
  22.  * Populate sitemap with configured static routes.
  23.  *
  24.  * @phpstan-import-type RouteOptions from RouteOptionParser
  25.  */
  26. class RouteAnnotationEventListener
  27. {
  28.     /**
  29.      * @var RouterInterface
  30.      */
  31.     protected $router;
  32.     /**
  33.      * @var EventDispatcherInterface
  34.      */
  35.     private $dispatcher;
  36.     /**
  37.      * @var string
  38.      */
  39.     private $defaultSection;
  40.     public function __construct(
  41.         RouterInterface $router,
  42.         EventDispatcherInterface $eventDispatcher,
  43.         string $defaultSection
  44.     ) {
  45.         $this->router $router;
  46.         $this->dispatcher $eventDispatcher;
  47.         $this->defaultSection $defaultSection;
  48.     }
  49.     /**
  50.      * @param SitemapPopulateEvent $event
  51.      */
  52.     public function registerRouteAnnotation(SitemapPopulateEvent $event): void
  53.     {
  54.         $this->addUrlsFromRoutes($event->getUrlContainer(), $event->getSection());
  55.     }
  56.     /**
  57.      * @param UrlContainerInterface $container
  58.      * @param string|null           $section
  59.      *
  60.      * @throws \InvalidArgumentException
  61.      */
  62.     private function addUrlsFromRoutes(UrlContainerInterface $container, ?string $section): void
  63.     {
  64.         $collection $this->router->getRouteCollection();
  65.         foreach ($collection->all() as $name => $route) {
  66.             $options RouteOptionParser::parse($name$route);
  67.             if (!$options) {
  68.                 continue;
  69.             }
  70.             $routeSection $options['section'] ?? $this->defaultSection;
  71.             if ($section !== null && $routeSection !== $section) {
  72.                 continue;
  73.             }
  74.             $event = new SitemapAddUrlEvent($name$options$this->router);
  75.             $this->dispatcher->dispatch($event);
  76.             if (!$event->shouldBeRegistered()) {
  77.                 continue;
  78.             }
  79.             $container->addUrl(
  80.                 $event->getUrl() ?? $this->getUrlConcrete($name$options),
  81.                 $routeSection
  82.             );
  83.         }
  84.     }
  85.     /**
  86.      * @param string       $name    Route name
  87.      * @param RouteOptions $options Node options
  88.      *
  89.      * @return UrlConcrete
  90.      * @throws \InvalidArgumentException
  91.      */
  92.     protected function getUrlConcrete(string $name, array $options): UrlConcrete
  93.     {
  94.         try {
  95.             return new UrlConcrete(
  96.                 $this->getRouteUri($name),
  97.                 $options['lastmod'],
  98.                 $options['changefreq'],
  99.                 $options['priority']
  100.             );
  101.         } catch (\Exception $e) {
  102.             throw new \InvalidArgumentException(
  103.                 sprintf(
  104.                     'Invalid argument for route "%s": %s',
  105.                     $name,
  106.                     $e->getMessage()
  107.                 ),
  108.                 0,
  109.                 $e
  110.             );
  111.         }
  112.     }
  113.     /**
  114.      * @param string               $name   Route name
  115.      * @param array<string, mixed> $params Route additional parameters
  116.      *
  117.      * @return string
  118.      * @throws \InvalidArgumentException
  119.      */
  120.     protected function getRouteUri(string $name, array $params = []): string
  121.     {
  122.         // If the route needs additional parameters, we can't add it
  123.         try {
  124.             return $this->router->generate($name$paramsUrlGeneratorInterface::ABSOLUTE_URL);
  125.         } catch (MissingMandatoryParametersException $e) {
  126.             throw new \InvalidArgumentException(
  127.                 sprintf(
  128.                     'The route "%s" cannot have the sitemap option because it requires parameters other than "%s"',
  129.                     $name,
  130.                     implode('", "'array_keys($params))
  131.                 ),
  132.                 0,
  133.                 $e
  134.             );
  135.         }
  136.     }
  137. }