‪TYPO3CMS  11.5
SiteFinder.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 
18 namespace ‪TYPO3\CMS\Core\Site;
19 
26 
31 {
35  protected ‪$sites = [];
36 
42  protected ‪$mappingRootPageIdToIdentifier = [];
43 
47  protected ‪$siteConfiguration;
48 
55  {
56  $this->siteConfiguration = ‪$siteConfiguration ?? GeneralUtility::makeInstance(SiteConfiguration::class);
57  $this->‪fetchAllSites();
58  }
59 
66  public function ‪getAllSites(bool $useCache = true): array
67  {
68  if ($useCache === false) {
69  $this->‪fetchAllSites($useCache);
70  }
71  return ‪$this->sites;
72  }
73 
82  public function ‪getSiteByRootPageId(int $rootPageId): ‪Site
83  {
84  if (isset($this->mappingRootPageIdToIdentifier[$rootPageId])) {
85  return $this->sites[$this->mappingRootPageIdToIdentifier[$rootPageId]];
86  }
87  throw new ‪SiteNotFoundException('No site found for root page id ' . $rootPageId, 1521668882);
88  }
89 
97  public function ‪getSiteByIdentifier(string $identifier): ‪Site
98  {
99  if (isset($this->sites[$identifier])) {
100  return $this->sites[$identifier];
101  }
102  throw new ‪SiteNotFoundException('No site found for identifier ' . $identifier, 1521716628);
103  }
104 
114  public function ‪getSiteByPageId(int $pageId, ?array $rootLine = null, ?string $mountPointParameter = null): ‪Site
115  {
116  if ($pageId === 0) {
117  // page uid 0 has no root line. We don't need to ask the root line resolver to know that.
118  $rootLine = [];
119  }
120  if (!is_array($rootLine)) {
121  try {
122  $rootLine = GeneralUtility::makeInstance(RootlineUtility::class, $pageId, (string)$mountPointParameter)->get();
123  } catch (‪PageNotFoundException $e) {
124  // Usually when a page was hidden or disconnected
125  // This could be improved by handing in a Context object and decide whether hidden pages
126  // Should be linkable too
127  $rootLine = [];
128  }
129  }
130  foreach ($rootLine as $pageInRootLine) {
131  if (isset($this->mappingRootPageIdToIdentifier[(int)$pageInRootLine['uid']])) {
132  return $this->sites[$this->mappingRootPageIdToIdentifier[(int)$pageInRootLine['uid']]];
133  }
134  }
135  throw new ‪SiteNotFoundException('No site found in root line of page ' . $pageId, 1521716622);
136  }
137 
141  protected function ‪fetchAllSites(bool $useCache = true): void
142  {
143  $this->sites = $this->siteConfiguration->getAllExistingSites($useCache);
144  foreach ($this->sites as $identifier => $site) {
145  $this->mappingRootPageIdToIdentifier[$site->getRootPageId()] = $identifier;
146  }
147  }
148 }
‪TYPO3\CMS\Core\Site\SiteFinder\$mappingRootPageIdToIdentifier
‪array $mappingRootPageIdToIdentifier
Definition: SiteFinder.php:40
‪TYPO3\CMS\Core\Site\SiteFinder\getSiteByRootPageId
‪Site getSiteByRootPageId(int $rootPageId)
Definition: SiteFinder.php:79
‪TYPO3\CMS\Core\Site\SiteFinder\$siteConfiguration
‪SiteConfiguration $siteConfiguration
Definition: SiteFinder.php:44
‪TYPO3\CMS\Core\Utility\RootlineUtility
Definition: RootlineUtility.php:38
‪TYPO3\CMS\Core\Exception\SiteNotFoundException
Definition: SiteNotFoundException.php:25
‪TYPO3\CMS\Core\Site\SiteFinder
Definition: SiteFinder.php:31
‪TYPO3\CMS\Core\Configuration\SiteConfiguration
Definition: SiteConfiguration.php:43
‪TYPO3\CMS\Core\Site\SiteFinder\fetchAllSites
‪fetchAllSites(bool $useCache=true)
Definition: SiteFinder.php:138
‪TYPO3\CMS\Core\Site\Entity\Site
Definition: Site.php:42
‪TYPO3\CMS\Core\Site\SiteFinder\getSiteByPageId
‪Site getSiteByPageId(int $pageId, ?array $rootLine=null, ?string $mountPointParameter=null)
Definition: SiteFinder.php:111
‪TYPO3\CMS\Core\Site
‪TYPO3\CMS\Core\Site\SiteFinder\getSiteByIdentifier
‪Site getSiteByIdentifier(string $identifier)
Definition: SiteFinder.php:94
‪TYPO3\CMS\Core\Site\SiteFinder\getAllSites
‪Site[] getAllSites(bool $useCache=true)
Definition: SiteFinder.php:63
‪TYPO3\CMS\Core\Site\SiteFinder\__construct
‪__construct(?SiteConfiguration $siteConfiguration=null)
Definition: SiteFinder.php:51
‪TYPO3\CMS\Core\Exception\Page\PageNotFoundException
Definition: PageNotFoundException.php:23
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:50
‪TYPO3\CMS\Core\Site\SiteFinder\$sites
‪Site[] $sites
Definition: SiteFinder.php:34