‪TYPO3CMS  11.5
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 SymfonyRoute ‪$route;
29  protected array ‪$routeResult;
30  protected array ‪$hostMatches = [];
31  protected array ‪$pathMatches = [];
32 
33  public function ‪__construct(SymfonyRoute ‪$route, array ‪$routeResult)
34  {
35  $this->route = ‪$route;
36  $this->routeResult = ‪$routeResult;
37  }
38 
39  public function ‪withPathMatches(array ‪$pathMatches): self
40  {
41  $target = clone $this;
42  $target->pathMatches = ‪$pathMatches;
43  return $target;
44  }
45 
46  public function ‪withHostMatches(array ‪$hostMatches): self
47  {
48  $target = clone $this;
49  $target->hostMatches = ‪$hostMatches;
50  return $target;
51  }
52 
53  public function ‪getRoute(): SymfonyRoute
54  {
55  return ‪$this->route;
56  }
57 
61  public function ‪getRouteResult(): array
62  {
63  return ‪$this->routeResult;
64  }
65 
66  public function ‪getFallbackScore(): int
67  {
68  return $this->route->getOption('fallback') === true ? 1 : 0;
69  }
70 
71  public function ‪getHostMatchScore(): int
72  {
73  return empty($this->hostMatches[0]) ? 0 : 1;
74  }
75 
76  public function ‪getPathMatchScore(int $index): int
77  {
78  $completeMatch = $this->pathMatches[0];
79  $tailMatch = $this->pathMatches[$index] ?? '';
80  // no tail, it's a complete match
81  if ($tailMatch === '') {
82  return strlen($completeMatch);
83  }
84  // otherwise, find length of complete match that does not contain tail
85  // example: complete: `/french/other`, tail: `/other` -> `strlen` of `/french`
86  return strpos($completeMatch, $tailMatch);
87  }
88 
89  public function ‪getSiteIdentifier(): string
90  {
91  $site = $this->route->getDefault('site');
92  return $site instanceof ‪SiteInterface ? $site->‪getIdentifier() : '';
93  }
94 }
‪TYPO3\CMS\Core\Site\Entity\SiteInterface
Definition: SiteInterface.php:26
‪TYPO3\CMS\Core\Routing\MatchedRoute\withPathMatches
‪withPathMatches(array $pathMatches)
Definition: MatchedRoute.php:39
‪TYPO3\CMS\Core\Routing\MatchedRoute\withHostMatches
‪withHostMatches(array $hostMatches)
Definition: MatchedRoute.php:46
‪TYPO3\CMS\Core\Routing\MatchedRoute\$hostMatches
‪array $hostMatches
Definition: MatchedRoute.php:30
‪TYPO3\CMS\Core\Routing\MatchedRoute\getSiteIdentifier
‪getSiteIdentifier()
Definition: MatchedRoute.php:89
‪TYPO3\CMS\Core\Routing\MatchedRoute\getRoute
‪getRoute()
Definition: MatchedRoute.php:53
‪TYPO3\CMS\Core\Routing
‪TYPO3\CMS\Core\Routing\MatchedRoute\$pathMatches
‪array $pathMatches
Definition: MatchedRoute.php:31
‪TYPO3\CMS\Core\Routing\MatchedRoute
Definition: MatchedRoute.php:27
‪TYPO3\CMS\Core\Routing\MatchedRoute\__construct
‪__construct(SymfonyRoute $route, array $routeResult)
Definition: MatchedRoute.php:33
‪TYPO3\CMS\Core\Site\Entity\SiteInterface\getIdentifier
‪string getIdentifier()
‪TYPO3\CMS\Core\Routing\MatchedRoute\getRouteResult
‪array getRouteResult()
Definition: MatchedRoute.php:61
‪TYPO3\CMS\Core\Routing\MatchedRoute\getFallbackScore
‪getFallbackScore()
Definition: MatchedRoute.php:66
‪TYPO3\CMS\Core\Routing\MatchedRoute\$route
‪SymfonyRoute $route
Definition: MatchedRoute.php:28
‪TYPO3\CMS\Core\Routing\MatchedRoute\$routeResult
‪array $routeResult
Definition: MatchedRoute.php:29
‪TYPO3\CMS\Core\Routing\MatchedRoute\getHostMatchScore
‪getHostMatchScore()
Definition: MatchedRoute.php:71
‪TYPO3\CMS\Core\Routing\MatchedRoute\getPathMatchScore
‪getPathMatchScore(int $index)
Definition: MatchedRoute.php:76