‪TYPO3CMS  ‪main
MatchedRoute.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
20 use Symfony\Component\Routing\Route as SymfonyRoute;
22 
27 {
28  protected array ‪$hostMatches = [];
29  protected array ‪$pathMatches = [];
30 
31  public function ‪__construct(protected SymfonyRoute $route, protected array $routeResult) {}
32 
33  public function ‪withPathMatches(array ‪$pathMatches): self
34  {
35  $target = clone $this;
36  $target->pathMatches = ‪$pathMatches;
37  return $target;
38  }
39 
40  public function ‪withHostMatches(array ‪$hostMatches): self
41  {
42  $target = clone $this;
43  $target->hostMatches = ‪$hostMatches;
44  return $target;
45  }
46 
47  public function ‪getRoute(): SymfonyRoute
48  {
49  return $this->route;
50  }
51 
52  public function ‪getRouteResult(): array
53  {
54  return $this->routeResult;
55  }
56 
57  public function ‪getFallbackScore(): int
58  {
59  return $this->route->getOption('fallback') === true ? 1 : 0;
60  }
61 
62  public function ‪getHostMatchScore(): int
63  {
64  return empty($this->hostMatches[0]) ? 0 : 1;
65  }
66 
67  public function ‪getPathMatchScore(int $index): int
68  {
69  $completeMatch = $this->pathMatches[0];
70  $tailMatch = $this->pathMatches[$index] ?? '';
71  // no tail, it's a complete match
72  if ($tailMatch === '') {
73  return strlen($completeMatch);
74  }
75  // otherwise, find length of complete match that does not contain tail
76  // example: complete: `/french/other`, tail: `/other` -> `strlen` of `/french`
77  return strpos($completeMatch, $tailMatch);
78  }
79 
80  public function ‪getSiteIdentifier(): string
81  {
82  $site = $this->route->getDefault('site');
83  return $site instanceof ‪SiteInterface ? $site->‪getIdentifier() : '';
84  }
85 }
‪TYPO3\CMS\Core\Site\Entity\SiteInterface
Definition: SiteInterface.php:26
‪TYPO3\CMS\Core\Routing\MatchedRoute\withPathMatches
‪withPathMatches(array $pathMatches)
Definition: MatchedRoute.php:33
‪TYPO3\CMS\Core\Routing\MatchedRoute\withHostMatches
‪withHostMatches(array $hostMatches)
Definition: MatchedRoute.php:40
‪TYPO3\CMS\Core\Routing\MatchedRoute\$hostMatches
‪array $hostMatches
Definition: MatchedRoute.php:28
‪TYPO3\CMS\Core\Routing\MatchedRoute\getSiteIdentifier
‪getSiteIdentifier()
Definition: MatchedRoute.php:80
‪TYPO3\CMS\Core\Routing\MatchedRoute\getRoute
‪getRoute()
Definition: MatchedRoute.php:47
‪TYPO3\CMS\Core\Routing
‪TYPO3\CMS\Core\Routing\MatchedRoute\$pathMatches
‪array $pathMatches
Definition: MatchedRoute.php:29
‪TYPO3\CMS\Core\Routing\MatchedRoute
Definition: MatchedRoute.php:27
‪TYPO3\CMS\Core\Routing\MatchedRoute\__construct
‪__construct(protected SymfonyRoute $route, protected array $routeResult)
Definition: MatchedRoute.php:31
‪TYPO3\CMS\Core\Routing\MatchedRoute\getRouteResult
‪getRouteResult()
Definition: MatchedRoute.php:52
‪TYPO3\CMS\Core\Routing\MatchedRoute\getFallbackScore
‪getFallbackScore()
Definition: MatchedRoute.php:57
‪TYPO3\CMS\Core\Routing\MatchedRoute\getHostMatchScore
‪getHostMatchScore()
Definition: MatchedRoute.php:62
‪TYPO3\CMS\Core\Site\Entity\SiteInterface\getIdentifier
‪getIdentifier()
‪TYPO3\CMS\Core\Routing\MatchedRoute\getPathMatchScore
‪getPathMatchScore(int $index)
Definition: MatchedRoute.php:67