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