8a131c3a8b4fe2a5d7563230b524934b60b0dd60
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Core / Environment.php
1 <?php
2 declare(strict_types = 1);
3 namespace TYPO3\CMS\Core\Core;
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 /**
19 * This class is initialized once in the SystemEnvironmentBuilder, and can then
20 * be used throughout the application to access common variables
21 * related to path-resolving and OS-/PHP-application specific information.
22 *
23 * It's main design goal is to remove any access to constants within TYPO3 code and to provide a static,
24 * for TYPO3 core and extensions non-changeable information.
25 *
26 * This class does not contain any HTTP related information, as this is handled in NormalizedParams functionality.
27 *
28 * All path-related methods do return the realpath to the paths without (!) the trailing slash.
29 *
30 * This class only defines what is configured through the environment, does not do any checks if paths exist
31 * etc. This should be part of the application or the SystemEnvironmentBuilder.
32 *
33 * In your application, use it like this:
34 *
35 * Instead of writing "PATH_site" call "Environment::getPublicPath() . '/'"
36 * Instead of writing "TYPO3_REQUESTTYPE & TYPO3_REQUESTTYPE_CLI" call "Environment::isCli()"
37 */
38 class Environment
39 {
40 protected static $cli;
41 protected static $composerMode;
42 protected static $context;
43 protected static $projectPath;
44 protected static $publicPath;
45 protected static $currentScript;
46 protected static $os;
47 protected static $varPath;
48 protected static $configPath;
49
50 /**
51 * Sets up the Environment. Please note that this is not public API and only used within the very early
52 * Set up of TYPO3, or to be used within tests. If you ever call this method in your extension, you're probably
53 * doing something wrong. Never call this method! Never rely on it!
54 *
55 * @param ApplicationContext $context
56 * @param bool $cli
57 * @param bool $composerMode
58 * @param string $projectPath
59 * @param string $publicPath
60 * @param string $varPath
61 * @param string $configPath
62 * @param string $currentScript
63 * @param string $os
64 * @internal
65 */
66 public static function initialize(
67 ApplicationContext $context,
68 bool $cli,
69 bool $composerMode,
70 string $projectPath,
71 string $publicPath,
72 string $varPath,
73 string $configPath,
74 string $currentScript,
75 string $os
76 ) {
77 self::$cli = $cli;
78 self::$composerMode = $composerMode;
79 self::$context = $context;
80 self::$projectPath = $projectPath;
81 self::$publicPath = $publicPath;
82 self::$varPath = $varPath;
83 self::$configPath = $configPath;
84 self::$currentScript = $currentScript;
85 self::$os = $os;
86 }
87
88 /**
89 * Delivers the ApplicationContext object, usually defined in TYPO3_CONTEXT environment variables.
90 * This is something like "Production", "Testing", or "Development" or any additional information
91 * "Production/Staging".
92 *
93 * @return ApplicationContext
94 */
95 public static function getContext(): ApplicationContext
96 {
97 return self::$context;
98 }
99
100 /**
101 * Informs whether TYPO3 has been installed via composer or not. Typically this is useful inside the
102 * Maintenance Modules, or the Extension Manager.
103 *
104 * @return bool
105 */
106 public static function isComposerMode(): bool
107 {
108 return self::$composerMode;
109 }
110
111 /**
112 * Whether the current PHP request is handled by a CLI SAPI module or not.
113 *
114 * @return bool
115 */
116 public static function isCli(): bool
117 {
118 return self::$cli;
119 }
120
121 /**
122 * The root path to the project. For installations set up via composer, this is the path where your
123 * composer.json file is stored. For non-composer-setups, this is (due to legacy reasons) the public web folder
124 * where the TYPO3 installation has been unzipped (something like htdocs/ or public/ on your webfolder).
125 * However, non-composer-mode installations define an environment variable called "TYPO3_PATH_APP"
126 * to define a different folder (usually a parent folder) to allow TYPO3 to access and store data outside
127 * of the public web folder.
128 *
129 * @return string The absolute path to the project without the trailing slash
130 */
131 public static function getProjectPath(): string
132 {
133 return self::$projectPath;
134 }
135
136 /**
137 * The public web folder where index.php (= the frontend application) is put. This is equal to the legacy constant
138 * PATH_site, without the trailing slash. For non-composer installations, the project path = the public path.
139 *
140 * @return string
141 */
142 public static function getPublicPath(): string
143 {
144 return self::$publicPath;
145 }
146
147 /**
148 * The folder where variable data like logs, sessions, locks, and cache files can be stored.
149 * When project path = public path, then this folder is usually typo3temp/var/, otherwise it's set to
150 * $project_path/var.
151 *
152 * @return string
153 */
154 public static function getVarPath(): string
155 {
156 return self::$varPath;
157 }
158
159 /**
160 * The folder where all global (= installation-wide) configuration like
161 * - LocalConfiguration.php,
162 * - AdditionalConfiguration.php, and
163 * - PackageStates.php
164 * is put.
165 * This folder usually has to be writable for TYPO3 in order to work.
166 *
167 * When project path = public path, then this folder is usually typo3conf/, otherwise it's set to
168 * $project_path/config.
169 *
170 * @return string
171 */
172 public static function getConfigPath(): string
173 {
174 return self::$configPath;
175 }
176
177 /**
178 * The path + filename to the current PHP script.
179 *
180 * @return string
181 */
182 public static function getCurrentScript(): string
183 {
184 return self::$currentScript;
185 }
186
187 /**
188 * Helper methods to easily find occurrences, however as these properties are not computed
189 * it is very possible that these methods will become obsolete in the near future.
190 */
191
192 /**
193 * Previously found under typo3conf/l10n/
194 * Please note that this might be gone at some point
195 *
196 * @return string
197 */
198 public static function getLabelsPath(): string
199 {
200 if (self::$publicPath === self::$projectPath) {
201 return self::getPublicPath() . '/typo3conf/l10n';
202 }
203 return self::getVarPath() . '/labels';
204 }
205
206 /**
207 * Previously known as PATH_typo3
208 * Please note that this might be gone at some point
209 *
210 * @return string
211 */
212 public static function getBackendPath(): string
213 {
214 return self::getPublicPath() . '/typo3';
215 }
216
217 /**
218 * Previously known as PATH_typo3 . 'sysext/'
219 * Please note that this might be gone at some point
220 *
221 * @return string
222 */
223 public static function getFrameworkBasePath(): string
224 {
225 return self::getPublicPath() . '/typo3/sysext';
226 }
227
228 /**
229 * Previously known as PATH_site . 'typo3conf/ext/'
230 * Please note that this might be gone at some point
231 *
232 * @return string
233 */
234 public static function getExtensionsPath(): string
235 {
236 return self::getPublicPath() . '/typo3conf/ext';
237 }
238
239 /**
240 * Previously known as PATH_typo3conf
241 * Please note that this might be gone at some point
242 *
243 * @return string
244 */
245 public static function getLegacyConfigPath(): string
246 {
247 return self::getPublicPath() . '/typo3conf';
248 }
249
250 /**
251 * Whether this TYPO3 installation runs on windows
252 *
253 * @return bool
254 */
255 public static function isWindows(): bool
256 {
257 return self::$os === 'WINDOWS';
258 }
259
260 /**
261 * Whether this TYPO3 installation runs on unix (= non-windows machines)
262 *
263 * @return bool
264 */
265 public static function isUnix(): bool
266 {
267 return self::$os === 'UNIX';
268 }
269 }