vendor/contao/core-bundle/src/Routing/Page/PageRegistry.php line 79

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\Page;
  11. use Contao\PageModel;
  12. use Doctrine\DBAL\Connection;
  13. class PageRegistry
  14. {
  15.     private const DISABLE_CONTENT_COMPOSITION = ['redirect''forward''logout'];
  16.     /**
  17.      * @var Connection
  18.      */
  19.     private $connection;
  20.     /**
  21.      * @var array<RouteConfig>
  22.      */
  23.     private $routeConfigs = [];
  24.     /**
  25.      * @var array<DynamicRouteInterface>
  26.      */
  27.     private $routeEnhancers = [];
  28.     /**
  29.      * @var array<ContentCompositionInterface|bool>
  30.      */
  31.     private $contentComposition = [];
  32.     /**
  33.      * @var array<string>|null
  34.      */
  35.     private $urlPrefixes;
  36.     /**
  37.      * @var array<string>|null
  38.      */
  39.     private $urlSuffixes;
  40.     public function __construct(Connection $connection)
  41.     {
  42.         $this->connection $connection;
  43.     }
  44.     /**
  45.      * Returns the route for a page.
  46.      *
  47.      * If no path is configured (is null), the route will accept
  48.      * any parameters after the page alias (e.g. "en/page-alias/foo/bar.html").
  49.      *
  50.      * A route enhancer might enhance the route for a specific page.
  51.      */
  52.     public function getRoute(PageModel $pageModel): PageRoute
  53.     {
  54.         $type $pageModel->type;
  55.         $config $this->routeConfigs[$type] ?? new RouteConfig();
  56.         $defaults $config->getDefaults();
  57.         $requirements $config->getRequirements();
  58.         $path $config->getPath();
  59.         if (null === $path) {
  60.             $path '/'.($pageModel->alias ?: $pageModel->id).'{!parameters}';
  61.             $defaults['parameters'] = '';
  62.             $requirements['parameters'] = $pageModel->requireItem '/.+' '(/.+?)?';
  63.         }
  64.         $route = new PageRoute($pageModel$path$defaults$requirements$config->getOptions(), $config->getMethods());
  65.         if (null !== $config->getUrlSuffix()) {
  66.             $route->setUrlSuffix($config->getUrlSuffix());
  67.         }
  68.         if (!isset($this->routeEnhancers[$type])) {
  69.             return $route;
  70.         }
  71.         /** @var DynamicRouteInterface $enhancer */
  72.         $enhancer $this->routeEnhancers[$type];
  73.         $enhancer->configurePageRoute($route);
  74.         return $route;
  75.     }
  76.     public function getPathRegex(): array
  77.     {
  78.         $prefixes = [];
  79.         foreach ($this->routeConfigs as $type => $config) {
  80.             $regex $config->getPathRegex();
  81.             if (null !== $regex) {
  82.                 $prefixes[$type] = $regex;
  83.             }
  84.         }
  85.         return $prefixes;
  86.     }
  87.     public function supportsContentComposition(PageModel $pageModel): bool
  88.     {
  89.         if (!isset($this->contentComposition[$pageModel->type])) {
  90.             return !\in_array($pageModel->typeself::DISABLE_CONTENT_COMPOSITIONtrue);
  91.         }
  92.         $service $this->contentComposition[$pageModel->type];
  93.         if ($service instanceof ContentCompositionInterface) {
  94.             return $service->supportsContentComposition($pageModel);
  95.         }
  96.         return (bool) $service;
  97.     }
  98.     /**
  99.      * @return array<string>
  100.      */
  101.     public function getUrlPrefixes(): array
  102.     {
  103.         $this->initializePrefixAndSuffix();
  104.         return $this->urlPrefixes;
  105.     }
  106.     /**
  107.      * @return array<string>
  108.      */
  109.     public function getUrlSuffixes(): array
  110.     {
  111.         $this->initializePrefixAndSuffix();
  112.         return $this->urlSuffixes;
  113.     }
  114.     /**
  115.      * @param ContentCompositionInterface|bool $contentComposition
  116.      */
  117.     public function add(string $typeRouteConfig $configDynamicRouteInterface $routeEnhancer null$contentComposition true): self
  118.     {
  119.         // Override existing pages with the same identifier
  120.         $this->routeConfigs[$type] = $config;
  121.         if (null !== $routeEnhancer) {
  122.             $this->routeEnhancers[$type] = $routeEnhancer;
  123.         }
  124.         if (null !== $contentComposition) {
  125.             $this->contentComposition[$type] = $contentComposition;
  126.         }
  127.         $this->urlPrefixes $this->urlSuffixes null;
  128.         return $this;
  129.     }
  130.     public function remove(string $type): self
  131.     {
  132.         unset(
  133.             $this->routeConfigs[$type],
  134.             $this->routeEnhancers[$type],
  135.             $this->contentComposition[$type]
  136.         );
  137.         $this->urlPrefixes $this->urlSuffixes null;
  138.         return $this;
  139.     }
  140.     public function keys(): array
  141.     {
  142.         return array_keys($this->routeConfigs);
  143.     }
  144.     private function initializePrefixAndSuffix(): void
  145.     {
  146.         if (null !== $this->urlPrefixes || null !== $this->urlSuffixes) {
  147.             return;
  148.         }
  149.         $results $this->connection->fetchAllAssociative("SELECT urlPrefix, urlSuffix FROM tl_page WHERE type='root'");
  150.         $urlSuffixes = [
  151.             array_column($results'urlSuffix'),
  152.             array_filter(array_map(
  153.                 static function (RouteConfig $config) {
  154.                     return $config->getUrlSuffix();
  155.                 },
  156.                 $this->routeConfigs
  157.             )),
  158.         ];
  159.         foreach ($this->routeConfigs as $config) {
  160.             if (null !== ($suffix $config->getUrlSuffix())) {
  161.                 $urlSuffixes[] = [$suffix];
  162.             }
  163.         }
  164.         foreach ($this->routeEnhancers as $enhancer) {
  165.             $urlSuffixes[] = $enhancer->getUrlSuffixes();
  166.         }
  167.         $this->urlSuffixes array_values(array_unique(array_merge(...$urlSuffixes)));
  168.         $this->urlPrefixes array_values(array_unique(array_column($results'urlPrefix')));
  169.     }
  170. }