vendor/contao/core-bundle/src/Routing/PageUrlGenerator.php line 53

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4.  * This file is part of Contao.
  5.  *
  6.  * (c) Leo Feyer
  7.  *
  8.  * @license LGPL-3.0-or-later
  9.  */
  10. namespace Contao\CoreBundle\Routing;
  11. use Contao\CoreBundle\Exception\RouteParametersException;
  12. use Contao\CoreBundle\Routing\Page\PageRegistry;
  13. use Contao\CoreBundle\Routing\Page\PageRoute;
  14. use Contao\PageModel;
  15. use Psr\Log\LoggerInterface;
  16. use Symfony\Cmf\Component\Routing\RouteObjectInterface;
  17. use Symfony\Cmf\Component\Routing\RouteProviderInterface;
  18. use Symfony\Component\Routing\CompiledRoute;
  19. use Symfony\Component\Routing\Exception\ExceptionInterface;
  20. use Symfony\Component\Routing\Generator\UrlGenerator as SymfonyUrlGenerator;
  21. use Symfony\Component\Routing\RequestContext;
  22. use Symfony\Component\Routing\RouteCollection;
  23. class PageUrlGenerator extends SymfonyUrlGenerator
  24. {
  25.     /**
  26.      * @var RouteProviderInterface
  27.      */
  28.     private $provider;
  29.     /**
  30.      * @var PageRegistry
  31.      */
  32.     private $pageRegistry;
  33.     public function __construct(RouteProviderInterface $providerPageRegistry $pageRegistryLoggerInterface $logger null)
  34.     {
  35.         parent::__construct(new RouteCollection(), new RequestContext(), $logger);
  36.         $this->provider $provider;
  37.         $this->pageRegistry $pageRegistry;
  38.     }
  39.     /**
  40.      * @param string $name
  41.      * @param array  $parameters
  42.      * @param int    $referenceType
  43.      */
  44.     public function generate($name$parameters = [], $referenceType self::ABSOLUTE_PATH): string
  45.     {
  46.         if (
  47.             RouteObjectInterface::OBJECT_BASED_ROUTE_NAME === $name
  48.             && \array_key_exists(RouteObjectInterface::CONTENT_OBJECT$parameters)
  49.             && $parameters[RouteObjectInterface::CONTENT_OBJECT] instanceof PageModel
  50.         ) {
  51.             $route $this->pageRegistry->getRoute($parameters[RouteObjectInterface::CONTENT_OBJECT]);
  52.             unset($parameters[RouteObjectInterface::CONTENT_OBJECT]);
  53.         } else {
  54.             $route $this->provider->getRouteByName($name);
  55.         }
  56.         /** @var CompiledRoute $compiledRoute */
  57.         $compiledRoute $route->compile();
  58.         if (
  59.             $route instanceof PageRoute
  60.             && === \count(array_intersect_key(
  61.                 array_filter(array_merge($route->getDefaults(), $parameters)),
  62.                 array_flip($compiledRoute->getVariables())
  63.             ))
  64.         ) {
  65.             $staticPrefix $compiledRoute->getStaticPrefix();
  66.             $indexPath = ($route->getUrlPrefix() ? '/'.$route->getUrlPrefix() : '').'/index';
  67.             if ($staticPrefix === $indexPath || $staticPrefix === $indexPath.$route->getUrlSuffix()) {
  68.                 $route->setPath('/');
  69.                 $route->setUrlSuffix('');
  70.                 $compiledRoute $route->compile();
  71.             }
  72.         }
  73.         try {
  74.             return $this->doGenerate(
  75.                 $compiledRoute->getVariables(),
  76.                 $route->getDefaults(),
  77.                 $route->getRequirements(),
  78.                 $compiledRoute->getTokens(),
  79.                 $parameters,
  80.                 $name,
  81.                 $referenceType,
  82.                 $compiledRoute->getHostTokens(),
  83.                 $route->getSchemes()
  84.             );
  85.         } catch (ExceptionInterface $exception) {
  86.             throw new RouteParametersException($route$parameters$referenceType$exception);
  87.         }
  88.     }
  89. }