‪TYPO3CMS  9.5
SiteAccessorTrait.php
Go to the documentation of this file.
1 <?php
2 declare(strict_types = 1);
3 
5 
6 /*
7  * This file is part of the TYPO3 CMS project.
8  *
9  * It is free software; you can redistribute it and/or modify it under
10  * the terms of the GNU General Public License, either version 2
11  * of the License, or any later version.
12  *
13  * For the full copyright and license information, please read the
14  * LICENSE.txt file that was distributed with this source code.
15  *
16  * The TYPO3 project - inspiring people to share!
17  */
18 
23 
30 {
34  protected $site;
35 
39  protected $siteMatcher;
40 
44  public function setSite(‪Site $site): void
45  {
46  $this->site = $site;
47  }
48 
52  public function getSite(): ‪Site
53  {
54  return $this->site;
55  }
56 
68  protected function filterContainedInSite(array $results): array
69  {
70  if (empty($results)) {
71  return $results;
72  }
73  return array_filter(
74  $results,
75  function (array $result) {
76  // default FrontendRestrictionContainer retrieves only live records
77  // (no specific workspace & move-placeholder resolving required here)
78  $pageId = (int)$result['pid'];
79  return $this->isPageIdContainedInSite($pageId);
80  }
81  );
82  }
83 
91  protected function isPageIdContainedInSite(int $pageId): bool
92  {
93  try {
94  $expectedSite = $this->getSiteMatcher()->matchByPageId($pageId);
95  return $expectedSite->getRootPageId() === $this->site->getRootPageId();
96  } catch (‪SiteNotFoundException $exception) {
97  // Same as in \TYPO3\CMS\Core\DataHandling\SlugHelper::isUniqueInSite
98  // where it is assumed that a record, that is not in site context,
99  // but still configured uniqueInSite is unique. We therefore must assume
100  // the resolved record to be rightfully part of the current site.
101  return true;
102  }
103  }
104 
105  protected function getSiteMatcher(): ‪SiteMatcher
106  {
107  if (!isset($this->siteMatcher)) {
108  $this->siteMatcher = GeneralUtility::makeInstance(SiteMatcher::class);
109  }
110  return $this->siteMatcher;
111  }
112 }
‪TYPO3\CMS\Core\Routing\Aspect\SiteAccessorTrait\$siteMatcher
‪SiteMatcher null $siteMatcher
Definition: SiteAccessorTrait.php:37
‪TYPO3\CMS\Core\Routing\Aspect\SiteAccessorTrait
Definition: SiteAccessorTrait.php:30
‪TYPO3\CMS\Core\Exception\SiteNotFoundException
Definition: SiteNotFoundException.php:25
‪TYPO3\CMS\Core\Site\Entity\Site
Definition: Site.php:39
‪TYPO3\CMS\Core\Routing\Aspect\SiteAccessorTrait\getSite
‪Site getSite()
Definition: SiteAccessorTrait.php:50
‪TYPO3\CMS\Core\Routing\Aspect\SiteAccessorTrait\filterContainedInSite
‪array filterContainedInSite(array $results)
Definition: SiteAccessorTrait.php:66
‪TYPO3\CMS\Core\Routing\Aspect
Definition: AspectFactory.php:4
‪TYPO3\CMS\Core\Routing\Aspect\SiteAccessorTrait\getSiteMatcher
‪getSiteMatcher()
Definition: SiteAccessorTrait.php:103
‪TYPO3\CMS\Core\Routing\Aspect\SiteAccessorTrait\isPageIdContainedInSite
‪bool isPageIdContainedInSite(int $pageId)
Definition: SiteAccessorTrait.php:89
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45
‪TYPO3\CMS\Core\Routing\Aspect\SiteAccessorTrait\$site
‪Site $site
Definition: SiteAccessorTrait.php:33
‪TYPO3\CMS\Core\Routing\SiteMatcher
Definition: SiteMatcher.php:53
‪TYPO3\CMS\Core\Routing\Aspect\SiteAccessorTrait\setSite
‪setSite(Site $site)
Definition: SiteAccessorTrait.php:42