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

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.     private RouteProviderInterface $provider;
  26.     private PageRegistry $pageRegistry;
  27.     public function __construct(RouteProviderInterface $providerPageRegistry $pageRegistryLoggerInterface $logger null)
  28.     {
  29.         parent::__construct(new RouteCollection(), new RequestContext(), $logger);
  30.         $this->provider $provider;
  31.         $this->pageRegistry $pageRegistry;
  32.     }
  33.     public function generate(string $name, array $parameters = [], int $referenceType self::ABSOLUTE_PATH): string
  34.     {
  35.         if (
  36.             RouteObjectInterface::OBJECT_BASED_ROUTE_NAME === $name
  37.             && \array_key_exists(RouteObjectInterface::CONTENT_OBJECT$parameters)
  38.             && $parameters[RouteObjectInterface::CONTENT_OBJECT] instanceof PageModel
  39.         ) {
  40.             $route $this->pageRegistry->getRoute($parameters[RouteObjectInterface::CONTENT_OBJECT]);
  41.             unset($parameters[RouteObjectInterface::CONTENT_OBJECT]);
  42.         } elseif (
  43.             RouteObjectInterface::OBJECT_BASED_ROUTE_NAME === $name
  44.             && \array_key_exists(RouteObjectInterface::ROUTE_OBJECT$parameters)
  45.             && $parameters[RouteObjectInterface::ROUTE_OBJECT] instanceof PageRoute
  46.         ) {
  47.             $route $parameters[RouteObjectInterface::ROUTE_OBJECT];
  48.             unset($parameters[RouteObjectInterface::ROUTE_OBJECT]);
  49.         } else {
  50.             $route $this->provider->getRouteByName($name);
  51.         }
  52.         /** @var CompiledRoute $compiledRoute */
  53.         $compiledRoute $route->compile();
  54.         if (
  55.             $route instanceof PageRoute
  56.             && === \count(array_intersect_key(
  57.                 array_filter(array_merge($route->getDefaults(), $parameters)),
  58.                 array_flip($compiledRoute->getVariables())
  59.             ))
  60.         ) {
  61.             $staticPrefix $compiledRoute->getStaticPrefix();
  62.             $indexPath = ($route->getUrlPrefix() ? '/'.$route->getUrlPrefix() : '').'/index';
  63.             if ($staticPrefix === $indexPath || $staticPrefix === $indexPath.$route->getUrlSuffix()) {
  64.                 $route->setPath('/');
  65.                 $route->setUrlSuffix('');
  66.                 $compiledRoute $route->compile();
  67.             }
  68.         }
  69.         try {
  70.             return $this->doGenerate(
  71.                 $compiledRoute->getVariables(),
  72.                 $route->getDefaults(),
  73.                 $route->getRequirements(),
  74.                 $compiledRoute->getTokens(),
  75.                 $parameters,
  76.                 $route->getDefault('_canonical_route') ?: $name,
  77.                 $referenceType,
  78.                 $compiledRoute->getHostTokens(),
  79.                 $route->getSchemes()
  80.             );
  81.         } catch (ExceptionInterface $exception) {
  82.             throw new RouteParametersException($route$parameters$referenceType$exception);
  83.         }
  84.     }
  85. }