"Fossies" - the Fresh Open Source Software Archive

Member "contao-4.11.5/core-bundle/src/Routing/AbstractPageRouteProvider.php" (23 Jun 2021, 5493 Bytes) of package /linux/www/contao-4.11.5.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "AbstractPageRouteProvider.php" see the Fossies "Dox" file reference documentation.

    1 <?php
    2 
    3 declare(strict_types=1);
    4 
    5 /*
    6  * This file is part of Contao.
    7  *
    8  * (c) Leo Feyer
    9  *
   10  * @license LGPL-3.0-or-later
   11  */
   12 
   13 namespace Contao\CoreBundle\Routing;
   14 
   15 use Contao\CoreBundle\Framework\ContaoFramework;
   16 use Contao\CoreBundle\Routing\Page\PageRoute;
   17 use Contao\Model\Collection;
   18 use Contao\PageModel;
   19 use Symfony\Cmf\Component\Routing\Candidates\CandidatesInterface;
   20 use Symfony\Cmf\Component\Routing\RouteProviderInterface;
   21 use Symfony\Component\HttpFoundation\Request;
   22 use Symfony\Component\Routing\Route;
   23 
   24 abstract class AbstractPageRouteProvider implements RouteProviderInterface
   25 {
   26     /**
   27      * @var ContaoFramework
   28      */
   29     protected $framework;
   30 
   31     /**
   32      * @var CandidatesInterface
   33      */
   34     protected $candidates;
   35 
   36     public function __construct(ContaoFramework $framework, CandidatesInterface $candidates)
   37     {
   38         $this->framework = $framework;
   39         $this->candidates = $candidates;
   40     }
   41 
   42     /**
   43      * @return array<PageModel>
   44      */
   45     protected function findCandidatePages(Request $request): array
   46     {
   47         $candidates = $this->candidates->getCandidates($request);
   48 
   49         if (empty($candidates)) {
   50             return [];
   51         }
   52 
   53         $ids = [];
   54         $aliases = [];
   55 
   56         foreach ($candidates as $candidate) {
   57             if (preg_match('/^[1-9]\d*$/', $candidate)) {
   58                 $ids[] = (int) $candidate;
   59             } else {
   60                 $aliases[] = $candidate;
   61             }
   62         }
   63 
   64         $conditions = [];
   65 
   66         if (!empty($ids)) {
   67             $conditions[] = 'tl_page.id IN ('.implode(',', $ids).')';
   68         }
   69 
   70         if (!empty($aliases)) {
   71             $conditions[] = 'tl_page.alias IN ('.implode(',', array_fill(0, \count($aliases), '?')).')';
   72         }
   73 
   74         /** @var PageModel $pageModel */
   75         $pageModel = $this->framework->getAdapter(PageModel::class);
   76         $pages = $pageModel->findBy([implode(' OR ', $conditions)], $aliases);
   77 
   78         if (!$pages instanceof Collection) {
   79             return [];
   80         }
   81 
   82         /** @var array<PageModel> */
   83         return $pages->getModels();
   84     }
   85 
   86     /**
   87      * @return array<int>
   88      */
   89     protected function getPageIdsFromNames(array $names): array
   90     {
   91         $ids = [];
   92 
   93         foreach ($names as $name) {
   94             if (0 !== strncmp($name, 'tl_page.', 8)) {
   95                 continue;
   96             }
   97 
   98             [, $id] = explode('.', $name);
   99 
  100             if (!preg_match('/^[1-9]\d*$/', $id)) {
  101                 continue;
  102             }
  103 
  104             $ids[] = (int) $id;
  105         }
  106 
  107         return array_unique($ids);
  108     }
  109 
  110     protected function compareRoutes(Route $a, Route $b, array $languages = null): int
  111     {
  112         if ('' !== $a->getHost() && '' === $b->getHost()) {
  113             return -1;
  114         }
  115 
  116         if ('' === $a->getHost() && '' !== $b->getHost()) {
  117             return 1;
  118         }
  119 
  120         /** @var PageModel|null $pageA */
  121         $pageA = $a->getDefault('pageModel');
  122 
  123         /** @var PageModel|null $pageB */
  124         $pageB = $b->getDefault('pageModel');
  125 
  126         // Check if the page models are valid (should always be the case, as routes are generated from pages)
  127         if (!$pageA instanceof PageModel || !$pageB instanceof PageModel) {
  128             return 0;
  129         }
  130 
  131         $langA = null;
  132         $langB = null;
  133 
  134         if (null !== $languages && $pageA->rootLanguage !== $pageB->rootLanguage) {
  135             $langA = $languages[$pageA->rootLanguage] ?? null;
  136             $langB = $languages[$pageB->rootLanguage] ?? null;
  137         }
  138 
  139         if (null === $langA && null === $langB) {
  140             if ($pageA->rootIsFallback && !$pageB->rootIsFallback) {
  141                 return -1;
  142             }
  143 
  144             if ($pageB->rootIsFallback && !$pageA->rootIsFallback) {
  145                 return 1;
  146             }
  147         } else {
  148             if (null === $langA && null !== $langB) {
  149                 return 1;
  150             }
  151 
  152             if (null !== $langA && null === $langB) {
  153                 return -1;
  154             }
  155 
  156             if ($langA < $langB) {
  157                 return -1;
  158             }
  159 
  160             if ($langA > $langB) {
  161                 return 1;
  162             }
  163         }
  164 
  165         if ('root' !== $pageA->type && 'root' === $pageB->type) {
  166             return -1;
  167         }
  168 
  169         if ('root' === $pageA->type && 'root' !== $pageB->type) {
  170             return 1;
  171         }
  172 
  173         $pathA = $a instanceof PageRoute && $a->getUrlSuffix() ? substr($a->getPath(), 0, -\strlen($a->getUrlSuffix())) : $a->getPath();
  174         $pathB = $b instanceof PageRoute && $b->getUrlSuffix() ? substr($b->getPath(), 0, -\strlen($b->getUrlSuffix())) : $b->getPath();
  175 
  176         $countA = \count(explode('/', $pathA));
  177         $countB = \count(explode('/', $pathB));
  178 
  179         if ($countA > $countB) {
  180             return -1;
  181         }
  182 
  183         if ($countB > $countA) {
  184             return 1;
  185         }
  186 
  187         return strnatcasecmp($pathA, $pathB);
  188     }
  189 
  190     protected function convertLanguagesForSorting(array $languages): array
  191     {
  192         foreach ($languages as &$language) {
  193             $language = str_replace('_', '-', $language);
  194 
  195             if (5 === \strlen($language)) {
  196                 $lng = substr($language, 0, 2);
  197 
  198                 // Append the language if only language plus dialect is given (see #430)
  199                 if (!\in_array($lng, $languages, true)) {
  200                     $languages[] = $lng;
  201                 }
  202             }
  203         }
  204 
  205         return array_flip(array_values($languages));
  206     }
  207 }