‪TYPO3CMS  10.4
SiteAccessorTrait.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 
24 
31 {
35  protected $site;
36 
40  protected $siteMatcher;
41 
45  public function setSite(‪Site $site): void
46  {
47  $this->site = $site;
48  }
49 
53  public function getSite(): ‪Site
54  {
55  return $this->site;
56  }
57 
69  protected function filterContainedInSite(array $results): array
70  {
71  if (empty($results)) {
72  return $results;
73  }
74  return array_filter(
75  $results,
76  function (array $result) {
77  // default FrontendRestrictionContainer retrieves only live records
78  // (no specific workspace & move-placeholder resolving required here)
79  $pageId = (int)$result['pid'];
80  return $this->isPageIdContainedInSite($pageId);
81  }
82  );
83  }
84 
92  protected function isPageIdContainedInSite(int $pageId): bool
93  {
94  try {
95  $expectedSite = $this->getSiteMatcher()->matchByPageId($pageId);
96  return $expectedSite->getRootPageId() === $this->site->getRootPageId();
97  } catch (‪SiteNotFoundException $exception) {
98  // Same as in \TYPO3\CMS\Core\DataHandling\SlugHelper::isUniqueInSite
99  // where it is assumed that a record, that is not in site context,
100  // but still configured uniqueInSite is unique. We therefore must assume
101  // the resolved record to be rightfully part of the current site.
102  return true;
103  }
104  }
105 
106  protected function getSiteMatcher(): ‪SiteMatcher
107  {
108  if (!isset($this->siteMatcher)) {
109  $this->siteMatcher = GeneralUtility::makeInstance(SiteMatcher::class);
110  }
111  return $this->siteMatcher;
112  }
113 }
‪TYPO3\CMS\Core\Routing\Aspect\SiteAccessorTrait\$siteMatcher
‪SiteMatcher null $siteMatcher
Definition: SiteAccessorTrait.php:38
‪TYPO3\CMS\Core\Routing\Aspect\SiteAccessorTrait
Definition: SiteAccessorTrait.php:31
‪TYPO3\CMS\Core\Exception\SiteNotFoundException
Definition: SiteNotFoundException.php:26
‪TYPO3\CMS\Core\Site\Entity\Site
Definition: Site.php:40
‪TYPO3\CMS\Core\Routing\Aspect\SiteAccessorTrait\getSite
‪Site getSite()
Definition: SiteAccessorTrait.php:51
‪TYPO3\CMS\Core\Routing\Aspect\SiteAccessorTrait\filterContainedInSite
‪array filterContainedInSite(array $results)
Definition: SiteAccessorTrait.php:67
‪TYPO3\CMS\Core\Routing\Aspect
Definition: AspectFactory.php:18
‪TYPO3\CMS\Core\Routing\Aspect\SiteAccessorTrait\getSiteMatcher
‪getSiteMatcher()
Definition: SiteAccessorTrait.php:104
‪TYPO3\CMS\Core\Routing\Aspect\SiteAccessorTrait\isPageIdContainedInSite
‪bool isPageIdContainedInSite(int $pageId)
Definition: SiteAccessorTrait.php:90
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Core\Routing\Aspect\SiteAccessorTrait\$site
‪Site $site
Definition: SiteAccessorTrait.php:34
‪TYPO3\CMS\Core\Routing\SiteMatcher
Definition: SiteMatcher.php:51
‪TYPO3\CMS\Core\Routing\Aspect\SiteAccessorTrait\setSite
‪setSite(Site $site)
Definition: SiteAccessorTrait.php:43