[BUGFIX] Fix several typos in php comments
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Site / SiteFinder.php
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
19 use TYPO3\CMS\Core\Configuration\SiteConfiguration;
20 use TYPO3\CMS\Core\Core\Environment;
21 use TYPO3\CMS\Core\Exception\Page\PageNotFoundException;
22 use TYPO3\CMS\Core\Exception\SiteNotFoundException;
23 use TYPO3\CMS\Core\Site\Entity\Site;
24 use TYPO3\CMS\Core\Utility\GeneralUtility;
25 use TYPO3\CMS\Core\Utility\RootlineUtility;
26
27 /**
28 * Is used in backend and frontend for all places where to read / identify sites and site languages.
29 */
30 class SiteFinder
31 {
32 /**
33 * @var Site[]
34 */
35 protected $sites = [];
36
37 /**
38 * Short-hand to quickly fetch a site based on a rootPageId
39 *
40 * @var array
41 */
42 protected $mappingRootPageIdToIdentifier = [];
43
44 /**
45 * Fetches all existing configurations as Site objects
46 *
47 * @param SiteConfiguration $siteConfiguration
48 */
49 public function __construct(SiteConfiguration $siteConfiguration = null)
50 {
51 $siteConfiguration = $siteConfiguration ?? GeneralUtility::makeInstance(
52 SiteConfiguration::class,
53 Environment::getConfigPath() . '/sites'
54 );
55 $sites = $siteConfiguration->getAllExistingSites();
56 foreach ($sites as $identifier => $site) {
57 $this->sites[$identifier] = $site;
58 $this->mappingRootPageIdToIdentifier[$site->getRootPageId()] = $identifier;
59 }
60 }
61
62 /**
63 * Return a list of all configured sites
64 *
65 * @return Site[]
66 */
67 public function getAllSites(): array
68 {
69 return $this->sites;
70 }
71
72 /**
73 * Find a site by given root page id
74 *
75 * @param int $rootPageId the page ID (default language)
76 * @return Site
77 * @throws SiteNotFoundException
78 * @internal only for usage in some places for managing Site Configuration, might be removed without further notice
79 */
80 public function getSiteByRootPageId(int $rootPageId): Site
81 {
82 if (isset($this->mappingRootPageIdToIdentifier[$rootPageId])) {
83 return $this->sites[$this->mappingRootPageIdToIdentifier[$rootPageId]];
84 }
85 throw new SiteNotFoundException('No site found for root page id ' . $rootPageId, 1521668882);
86 }
87
88 /**
89 * Find a site by given identifier
90 *
91 * @param string $identifier
92 * @return Site
93 * @throws SiteNotFoundException
94 */
95 public function getSiteByIdentifier(string $identifier): Site
96 {
97 if (isset($this->sites[$identifier])) {
98 return $this->sites[$identifier];
99 }
100 throw new SiteNotFoundException('No site found for identifier ' . $identifier, 1521716628);
101 }
102
103 /**
104 * Traverses the rootline of a page up until a Site was found.
105 *
106 * @param int $pageId
107 * @param array $rootLine
108 * @return Site
109 * @throws SiteNotFoundException
110 */
111 public function getSiteByPageId(int $pageId, array $rootLine = null): Site
112 {
113 if ($pageId === 0) {
114 // page uid 0 has no root line. We don't need to ask the root line resolver to know that.
115 $rootLine = [];
116 }
117 if (!is_array($rootLine)) {
118 try {
119 $rootLine = GeneralUtility::makeInstance(RootlineUtility::class, $pageId)->get();
120 } catch (PageNotFoundException $e) {
121 // Usually when a page was hidden or disconnected
122 // This could be improved by handing in a Context object and decide whether hidden pages
123 // Should be linkable too
124 $rootLine = [];
125 }
126 }
127 foreach ($rootLine as $pageInRootLine) {
128 if (isset($this->mappingRootPageIdToIdentifier[(int)$pageInRootLine['uid']])) {
129 return $this->sites[$this->mappingRootPageIdToIdentifier[(int)$pageInRootLine['uid']]];
130 }
131 }
132 throw new SiteNotFoundException('No site found in root line of page ' . $pageId, 1521716622);
133 }
134 }