8251063522ba6014a7b9316cdc263aaf37dff9fb
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Core / ApplicationContext.php
1 <?php
2 namespace TYPO3\CMS\Core\Core;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Core\Exception;
18
19 /**
20 * The TYPO3 Context object.
21 *
22 * A TYPO3 Application context is something like "Production", "Development",
23 * "Production/StagingSystem", and is set using the TYPO3_CONTEXT environment variable.
24 *
25 * A context can contain arbitrary sub-contexts, which are delimited with slash
26 * ("Production/StagingSystem", "Production/Staging/Server1"). The top-level
27 * contexts, however, must be one of "Testing", "Development" and "Production".
28 *
29 * Mainly, you will use $context->isProduction(), $context->isTesting() and
30 * $context->isDevelopment() inside your custom code.
31 *
32 * This class is derived from the TYPO3 Flow framework.
33 * Credits go to the respective authors.
34 */
35 class ApplicationContext
36 {
37 /**
38 * The (internal) context string; could be something like "Development" or "Development/MyLocalMacBook"
39 *
40 * @var string
41 */
42 protected $contextString;
43
44 /**
45 * The root context; must be one of "Development", "Testing" or "Production"
46 *
47 * @var string
48 */
49 protected $rootContextString;
50
51 /**
52 * The parent context, or NULL if there is no parent context
53 *
54 * @var \TYPO3\CMS\Core\Core\ApplicationContext
55 */
56 protected $parentContext;
57
58 /**
59 * Initialize the context object.
60 *
61 * @param string $contextString
62 * @throws Exception if the parent context is none of "Development", "Production" or "Testing"
63 */
64 public function __construct($contextString)
65 {
66 if (strpos($contextString, '/') === false) {
67 $this->rootContextString = $contextString;
68 $this->parentContext = null;
69 } else {
70 $contextStringParts = explode('/', $contextString);
71 $this->rootContextString = $contextStringParts[0];
72 array_pop($contextStringParts);
73 $this->parentContext = new ApplicationContext(implode('/', $contextStringParts));
74 }
75
76 if (!in_array($this->rootContextString, ['Development', 'Production', 'Testing'], true)) {
77 throw new Exception('The given context "' . $contextString . '" was not valid. Only allowed are Development, Production and Testing, including their sub-contexts', 1335436551);
78 }
79
80 $this->contextString = $contextString;
81 }
82
83 /**
84 * Returns the full context string, for example "Development", or "Production/LiveSystem"
85 *
86 * @return string
87 * @api
88 */
89 public function __toString()
90 {
91 return $this->contextString;
92 }
93
94 /**
95 * Returns TRUE if this context is the Development context or a sub-context of it
96 *
97 * @return bool
98 * @api
99 */
100 public function isDevelopment()
101 {
102 return ($this->rootContextString === 'Development');
103 }
104
105 /**
106 * Returns TRUE if this context is the Production context or a sub-context of it
107 *
108 * @return bool
109 * @api
110 */
111 public function isProduction()
112 {
113 return ($this->rootContextString === 'Production');
114 }
115
116 /**
117 * Returns TRUE if this context is the Testing context or a sub-context of it
118 *
119 * @return bool
120 * @api
121 */
122 public function isTesting()
123 {
124 return ($this->rootContextString === 'Testing');
125 }
126
127 /**
128 * Returns the parent context object, if any
129 *
130 * @return \TYPO3\CMS\Core\Core\ApplicationContext the parent context or NULL, if there is none
131 * @api
132 */
133 public function getParent()
134 {
135 return $this->parentContext;
136 }
137 }