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