Completed
Push — master ( d577b1...045d4d )
by
unknown
20:39
created

Environment::isUnix()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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