‪TYPO3CMS  ‪main
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 array ‪$sites = [];
36 
42  protected array ‪$mappingRootPageIdToIdentifier = [];
43 
45 
50  {
51  $this->siteConfiguration = ‪$siteConfiguration ?? GeneralUtility::makeInstance(SiteConfiguration::class);
52  $this->‪fetchAllSites();
53  }
54 
60  public function ‪getAllSites(bool $useCache = true): array
61  {
62  if ($useCache === false) {
63  $this->‪fetchAllSites($useCache);
64  }
65  return ‪$this->sites;
66  }
67 
75  public function ‪getSiteByRootPageId(int $rootPageId): ‪Site
76  {
77  if (isset($this->mappingRootPageIdToIdentifier[$rootPageId])) {
78  return $this->sites[$this->mappingRootPageIdToIdentifier[$rootPageId]];
79  }
80  throw new ‪SiteNotFoundException('No site found for root page id ' . $rootPageId, 1521668882);
81  }
82 
89  {
90  if (isset($this->sites[‪$identifier])) {
91  return $this->sites[‪$identifier];
92  }
93  throw new ‪SiteNotFoundException('No site found for identifier ' . ‪$identifier, 1521716628);
94  }
95 
102  public function ‪getSiteByPageId(int $pageId, array $rootLine = null, string $mountPointParameter = null): ‪Site
103  {
104  if ($pageId === 0) {
105  // page uid 0 has no root line. We don't need to ask the root line resolver to know that.
106  $rootLine = [];
107  }
108  if (!is_array($rootLine)) {
109  try {
110  $rootLine = GeneralUtility::makeInstance(RootlineUtility::class, $pageId, (string)$mountPointParameter)->get();
111  } catch (‪PageNotFoundException $e) {
112  // Usually when a page was hidden or disconnected
113  // This could be improved by handing in a Context object and decide whether hidden pages
114  // Should be linkable too
115  $rootLine = [];
116  }
117  }
118  foreach ($rootLine as $pageInRootLine) {
119  if (isset($this->mappingRootPageIdToIdentifier[(int)$pageInRootLine['uid']])) {
120  return $this->sites[$this->mappingRootPageIdToIdentifier[(int)$pageInRootLine['uid']]];
121  }
122  }
123  throw new ‪SiteNotFoundException('No site found in root line of page ' . $pageId, 1521716622);
124  }
125 
126  protected function ‪fetchAllSites(bool $useCache = true): void
127  {
128  $this->sites = $this->siteConfiguration->getAllExistingSites($useCache);
129  foreach ($this->sites as ‪$identifier => $site) {
130  $this->mappingRootPageIdToIdentifier[$site->getRootPageId()] = ‪$identifier;
131  }
132  }
133 }
‪TYPO3\CMS\Core\Site\SiteFinder\$mappingRootPageIdToIdentifier
‪array $mappingRootPageIdToIdentifier
Definition: SiteFinder.php:42
‪TYPO3\CMS\Core\Site\SiteFinder\__construct
‪__construct(SiteConfiguration $siteConfiguration=null)
Definition: SiteFinder.php:49
‪TYPO3\CMS\Core\Site\SiteFinder\$siteConfiguration
‪SiteConfiguration $siteConfiguration
Definition: SiteFinder.php:44
‪TYPO3\CMS\Core\Site\SiteFinder\getSiteByRootPageId
‪getSiteByRootPageId(int $rootPageId)
Definition: SiteFinder.php:75
‪TYPO3\CMS\Core\Utility\RootlineUtility
Definition: RootlineUtility.php:40
‪TYPO3\CMS\Core\Exception\SiteNotFoundException
Definition: SiteNotFoundException.php:25
‪TYPO3\CMS\Core\Site\SiteFinder
Definition: SiteFinder.php:31
‪TYPO3\CMS\Core\Site\SiteFinder\getSiteByPageId
‪getSiteByPageId(int $pageId, array $rootLine=null, string $mountPointParameter=null)
Definition: SiteFinder.php:102
‪TYPO3\CMS\Core\Configuration\SiteConfiguration
Definition: SiteConfiguration.php:47
‪TYPO3\CMS\Core\Site\SiteFinder\fetchAllSites
‪fetchAllSites(bool $useCache=true)
Definition: SiteFinder.php:126
‪TYPO3\CMS\Core\Site\Entity\Site
Definition: Site.php:42
‪TYPO3\CMS\Core\Site
‪TYPO3\CMS\Core\Site\SiteFinder\$sites
‪array $sites
Definition: SiteFinder.php:35
‪TYPO3\CMS\Core\Site\SiteFinder\getSiteByIdentifier
‪getSiteByIdentifier(string $identifier)
Definition: SiteFinder.php:88
‪TYPO3\CMS\Core\Site\SiteFinder\getAllSites
‪Site[] getAllSites(bool $useCache=true)
Definition: SiteFinder.php:60
‪TYPO3\CMS\Core\Exception\Page\PageNotFoundException
Definition: PageNotFoundException.php:23
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Webhooks\Message\$identifier
‪identifier readonly string $identifier
Definition: FileAddedMessage.php:37