‪TYPO3CMS  ‪main
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 
42  public function setSite(‪Site $site): void
43  {
44  $this->site = $site;
45  }
46 
47  public function getSite(): ‪Site
48  {
49  return $this->site;
50  }
51 
62  protected function filterContainedInSite(array $results): array
63  {
64  if (empty($results)) {
65  return $results;
66  }
67  return array_filter(
68  $results,
69  function (array $result) {
70  // default FrontendRestrictionContainer retrieves only live records
71  // (no specific workspace & move-placeholder resolving required here)
72  $pageId = (int)$result['pid'];
73  return $this->isPageIdContainedInSite($pageId);
74  }
75  );
76  }
77 
82  protected function isPageIdContainedInSite(int $pageId): bool
83  {
84  try {
85  $expectedSite = $this->getSiteMatcher()->matchByPageId($pageId);
86  return $expectedSite->getRootPageId() === $this->site->getRootPageId();
87  } catch (SiteNotFoundException $exception) {
88  // Same as in \TYPO3\CMS\Core\DataHandling\SlugHelper::isUniqueInSite
89  // where it is assumed that a record, that is not in site context,
90  // but still configured uniqueInSite is unique. We therefore must assume
91  // the resolved record to be rightfully part of the current site.
92  return true;
93  }
94  }
95 
96  protected function getSiteMatcher(): ‪SiteMatcher
97  {
98  if (!isset($this->siteMatcher)) {
99  $this->siteMatcher = GeneralUtility::makeInstance(SiteMatcher::class);
100  }
101  return $this->siteMatcher;
102  }
103 }
‪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:25
‪TYPO3\CMS\Core\Routing\Aspect\SiteAccessorTrait\getSite
‪getSite()
Definition: SiteAccessorTrait.php:45
‪TYPO3\CMS\Core\Site\Entity\Site
Definition: Site.php:42
‪TYPO3\CMS\Core\Routing\Aspect\SiteAccessorTrait\isPageIdContainedInSite
‪isPageIdContainedInSite(int $pageId)
Definition: SiteAccessorTrait.php:80
‪TYPO3\CMS\Core\Routing\Aspect\SiteAccessorTrait\filterContainedInSite
‪filterContainedInSite(array $results)
Definition: SiteAccessorTrait.php:60
‪TYPO3\CMS\Core\Routing\Aspect
Definition: AspectFactory.php:18
‪TYPO3\CMS\Core\Routing\Aspect\SiteAccessorTrait\getSiteMatcher
‪getSiteMatcher()
Definition: SiteAccessorTrait.php:94
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Core\Routing\Aspect\SiteAccessorTrait\$site
‪Site $site
Definition: SiteAccessorTrait.php:34
‪TYPO3\CMS\Core\Routing\SiteMatcher
Definition: SiteMatcher.php:52
‪TYPO3\CMS\Core\Routing\Aspect\SiteAccessorTrait\setSite
‪setSite(Site $site)
Definition: SiteAccessorTrait.php:40