36205a03dba1920224397e51eddb88d51a11337c
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Site / Entity / Site.php
1 <?php
2 declare(strict_types = 1);
3
4 namespace TYPO3\CMS\Core\Site\Entity;
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\Utility\GeneralUtility;
20 use TYPO3\CMS\Frontend\PageErrorHandler\FluidPageErrorHandler;
21 use TYPO3\CMS\Frontend\PageErrorHandler\PageContentErrorHandler;
22 use TYPO3\CMS\Frontend\PageErrorHandler\PageErrorHandlerInterface;
23
24 /**
25 * Entity representing a single site with available languages
26 */
27 class Site
28 {
29 protected const ERRORHANDLER_TYPE_PAGE = 'Page';
30 protected const ERRORHANDLER_TYPE_FLUID = 'Fluid';
31 protected const ERRORHANDLER_TYPE_PHP = 'PHP';
32
33 /**
34 * @var string
35 */
36 protected $identifier;
37
38 /**
39 * @var string
40 */
41 protected $base;
42
43 /**
44 * @var int
45 */
46 protected $rootPageId;
47
48 /**
49 * Any attributes for this site
50 * @var array
51 */
52 protected $configuration;
53
54 /**
55 * @var SiteLanguage[]
56 */
57 protected $languages;
58
59 /**
60 * @var array
61 */
62 protected $errorHandlers;
63
64 /**
65 * Sets up a site object, and its languages and error handlers
66 *
67 * @param string $identifier
68 * @param int $rootPageId
69 * @param array $configuration
70 */
71 public function __construct(string $identifier, int $rootPageId, array $configuration)
72 {
73 $this->identifier = $identifier;
74 $this->rootPageId = $rootPageId;
75 $this->configuration = $configuration;
76 $configuration['languages'] = $configuration['languages'] ?: [0 => [
77 'languageId' => 0,
78 'title' => 'Default',
79 'navigationTitle' => '',
80 'typo3Language' => 'default',
81 'flag' => 'us',
82 'locale' => 'en_US.UTF-8',
83 'iso-639-1' => 'en',
84 'hreflang' => 'en-US',
85 'direction' => '',
86 ]];
87 $this->base = $configuration['base'] ?? '';
88 foreach ($configuration['languages'] as $languageConfiguration) {
89 $languageUid = (int)$languageConfiguration['languageId'];
90 $base = '/';
91 if (!empty($languageConfiguration['base'])) {
92 $base = $languageConfiguration['base'];
93 }
94 $baseParts = parse_url($base);
95 if (empty($baseParts['scheme'])) {
96 $base = rtrim($this->base, '/') . '/' . ltrim($base, '/');
97 }
98 $this->languages[$languageUid] = new SiteLanguage(
99 $this,
100 $languageUid,
101 $languageConfiguration['locale'],
102 $base,
103 $languageConfiguration
104 );
105 }
106 foreach ($configuration['errorHandling'] ?? [] as $errorHandlingConfiguration) {
107 $code = $errorHandlingConfiguration['errorCode'];
108 unset($errorHandlingConfiguration['errorCode']);
109 $this->errorHandlers[(int)$code] = $errorHandlingConfiguration;
110 }
111 }
112
113 /**
114 * Gets the identifier of this site
115 *
116 * @return string
117 */
118 public function getIdentifier(): string
119 {
120 return $this->identifier;
121 }
122
123 /**
124 * Returns the base URL of this site
125 *
126 * @return string
127 */
128 public function getBase(): string
129 {
130 return $this->base;
131 }
132
133 /**
134 * Returns the root page ID of this site
135 *
136 * @return int
137 */
138 public function getRootPageId(): int
139 {
140 return $this->rootPageId;
141 }
142
143 /**
144 * Returns all available langauges of this site
145 *
146 * @return SiteLanguage[]
147 */
148 public function getLanguages(): array
149 {
150 return $this->languages;
151 }
152
153 /**
154 * Returns a language of this site, given by the sys_language_uid
155 *
156 * @param int $languageId
157 * @return SiteLanguage
158 * @throws \InvalidArgumentException
159 */
160 public function getLanguageById(int $languageId): SiteLanguage
161 {
162 if (isset($this->languages[$languageId])) {
163 return $this->languages[$languageId];
164 }
165 throw new \InvalidArgumentException(
166 'Language ' . $languageId . ' does not exist on site ' . $this->identifier . '.',
167 1522960188
168 );
169 }
170
171 /**
172 * Returns a ready-to-use error handler, to be used within the ErrorController
173 *
174 * @param int $type
175 * @return PageErrorHandlerInterface
176 * @throws \RuntimeException
177 */
178 public function getErrorHandler(int $type): PageErrorHandlerInterface
179 {
180 $errorHandler = $this->errorHandlers[$type];
181 switch ($errorHandler['errorHandler']) {
182 case self::ERRORHANDLER_TYPE_FLUID:
183 return new FluidPageErrorHandler($type, $errorHandler);
184 case self::ERRORHANDLER_TYPE_PAGE:
185 return new PageContentErrorHandler($type, $errorHandler);
186 case self::ERRORHANDLER_TYPE_PHP:
187 // Check if the interface is implemented
188 $handler = GeneralUtility::makeInstance($errorHandler['errorPhpClassFQCN'], $type, $errorHandler);
189 if (!($handler instanceof PageErrorHandlerInterface)) {
190 // @todo throw new exception
191 }
192 return $handler;
193 }
194 throw new \RuntimeException('Not implemented', 1522495914);
195 }
196
197 /**
198 * Returns the whole configuration for this site
199 *
200 * @return array
201 */
202 public function getConfiguration(): array
203 {
204 return $this->configuration;
205 }
206
207 /**
208 * Returns a single configuration attribute
209 *
210 * @param string $attributeName
211 * @return mixed
212 * @throws \InvalidArgumentException
213 */
214 public function getAttribute(string $attributeName)
215 {
216 if (isset($this->configuration[$attributeName])) {
217 return $this->configuration[$attributeName];
218 }
219 throw new \InvalidArgumentException(
220 'Attribute ' . $attributeName . ' does not exist on site ' . $this->identifier . '.',
221 1522495954
222 );
223 }
224 }