‪TYPO3CMS  9.5
SiteFinder.php
Go to the documentation of this file.
1 <?php
2 declare(strict_types = 1);
3 
4 namespace ‪TYPO3\CMS\Core\Site;
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 
26 
31 {
35  protected ‪$sites = [];
36 
42  protected ‪$mappingRootPageIdToIdentifier = [];
43 
47  protected ‪$siteConfiguration;
48 
55  {
56  $this->siteConfiguration = ‪$siteConfiguration ?? GeneralUtility::makeInstance(
57  SiteConfiguration::class,
59  );
60  $this->‪fetchAllSites();
61  }
62 
69  public function ‪getAllSites(bool $useCache = true): array
70  {
71  if ($useCache === false) {
72  $this->‪fetchAllSites($useCache);
73  }
74  return ‪$this->sites;
75  }
76 
85  public function ‪getSiteByRootPageId(int $rootPageId): ‪Site
86  {
87  if (isset($this->mappingRootPageIdToIdentifier[$rootPageId])) {
88  return $this->sites[$this->mappingRootPageIdToIdentifier[$rootPageId]];
89  }
90  throw new ‪SiteNotFoundException('No site found for root page id ' . $rootPageId, 1521668882);
91  }
92 
100  public function ‪getSiteByIdentifier(string $identifier): ‪Site
101  {
102  if (isset($this->sites[$identifier])) {
103  return $this->sites[$identifier];
104  }
105  throw new ‪SiteNotFoundException('No site found for identifier ' . $identifier, 1521716628);
106  }
107 
117  public function ‪getSiteByPageId(int $pageId, array $rootLine = null, string $mountPointParameter = null): ‪Site
118  {
119  if ($pageId === 0) {
120  // page uid 0 has no root line. We don't need to ask the root line resolver to know that.
121  $rootLine = [];
122  }
123  if (!is_array($rootLine)) {
124  try {
125  $rootLine = GeneralUtility::makeInstance(RootlineUtility::class, $pageId, $mountPointParameter)->get();
126  } catch (‪PageNotFoundException $e) {
127  // Usually when a page was hidden or disconnected
128  // This could be improved by handing in a Context object and decide whether hidden pages
129  // Should be linkeable too
130  $rootLine = [];
131  }
132  }
133  foreach ($rootLine as $pageInRootLine) {
134  if (isset($this->mappingRootPageIdToIdentifier[(int)$pageInRootLine['uid']])) {
135  return $this->sites[$this->mappingRootPageIdToIdentifier[(int)$pageInRootLine['uid']]];
136  }
137  }
138  throw new ‪SiteNotFoundException('No site found in root line of page ' . $pageId, 1521716622);
139  }
140 
144  protected function ‪fetchAllSites(bool $useCache = true): void
145  {
146  $this->sites = $this->siteConfiguration->getAllExistingSites($useCache);
147  foreach ($this->sites as $identifier => $site) {
148  $this->mappingRootPageIdToIdentifier[$site->getRootPageId()] = $identifier;
149  }
150  }
151 }
‪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:82
‪TYPO3\CMS\Core\Site\SiteFinder\__construct
‪__construct(SiteConfiguration $siteConfiguration=null)
Definition: SiteFinder.php:51
‪TYPO3\CMS\Core\Site\SiteFinder\getSiteByPageId
‪Site getSiteByPageId(int $pageId, array $rootLine=null, string $mountPointParameter=null)
Definition: SiteFinder.php:114
‪TYPO3\CMS\Core\Site\SiteFinder\$siteConfiguration
‪SiteConfiguration $siteConfiguration
Definition: SiteFinder.php:44
‪TYPO3\CMS\Core\Utility\RootlineUtility
Definition: RootlineUtility.php:36
‪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:38
‪TYPO3\CMS\Core\Site\SiteFinder\fetchAllSites
‪fetchAllSites(bool $useCache=true)
Definition: SiteFinder.php:141
‪TYPO3\CMS\Core\Site\Entity\Site
Definition: Site.php:39
‪TYPO3\CMS\Core\Site
‪TYPO3\CMS\Core\Site\SiteFinder\getSiteByIdentifier
‪Site getSiteByIdentifier(string $identifier)
Definition: SiteFinder.php:97
‪TYPO3\CMS\Core\Site\SiteFinder\getAllSites
‪Site[] getAllSites(bool $useCache=true)
Definition: SiteFinder.php:66
‪TYPO3\CMS\Core\Core\Environment
Definition: Environment.php:39
‪TYPO3\CMS\Core\Core\Environment\getConfigPath
‪static string getConfigPath()
Definition: Environment.php:183
‪TYPO3\CMS\Core\Exception\Page\PageNotFoundException
Definition: PageNotFoundException.php:23
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45
‪TYPO3\CMS\Core\Site\SiteFinder\$sites
‪Site[] $sites
Definition: SiteFinder.php:34