[TASK] Remove blank line between docblock and the documented element
[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 /**
18 * The TYPO3 Context object.
19 *
20 * A TYPO3 Application context is something like "Production", "Development",
21 * "Production/StagingSystem", and is set using the TYPO3_CONTEXT environment variable.
22 *
23 * A context can contain arbitrary sub-contexts, which are delimited with slash
24 * ("Production/StagingSystem", "Production/Staging/Server1"). The top-level
25 * contexts, however, must be one of "Testing", "Development" and "Production".
26 *
27 * Mainly, you will use $context->isProduction(), $context->isTesting() and
28 * $context->isDevelopment() inside your custom code.
29 *
30 * This class is derived from the TYPO3 Flow framework.
31 * Credits go to the respective authors.
32 */
33 class ApplicationContext
34 {
35 /**
36 * The (internal) context string; could be something like "Development" or "Development/MyLocalMacBook"
37 *
38 * @var string
39 */
40 protected $contextString;
41
42 /**
43 * The root context; must be one of "Development", "Testing" or "Production"
44 *
45 * @var string
46 */
47 protected $rootContextString;
48
49 /**
50 * The parent context, or NULL if there is no parent context
51 *
52 * @var \TYPO3\CMS\Core\Core\ApplicationContext
53 */
54 protected $parentContext;
55
56 /**
57 * Initialize the context object.
58 *
59 * @param string $contextString
60 * @throws \Exception if the parent context is none of "Development", "Production" or "Testing"
61 */
62 public function __construct($contextString)
63 {
64 if (strstr($contextString, '/') === false) {
65 $this->rootContextString = $contextString;
66 $this->parentContext = null;
67 } else {
68 $contextStringParts = explode('/', $contextString);
69 $this->rootContextString = $contextStringParts[0];
70 array_pop($contextStringParts);
71 $this->parentContext = new ApplicationContext(implode('/', $contextStringParts));
72 }
73
74 if (!in_array($this->rootContextString, array('Development', 'Production', 'Testing'))) {
75 throw new \TYPO3\CMS\Core\Exception('The given context "' . $contextString . '" was not valid. Only allowed are Development, Production and Testing, including their sub-contexts', 1335436551);
76 }
77
78 $this->contextString = $contextString;
79 }
80
81 /**
82 * Returns the full context string, for example "Development", or "Production/LiveSystem"
83 *
84 * @return string
85 * @api
86 */
87 public function __toString()
88 {
89 return $this->contextString;
90 }
91
92 /**
93 * Returns TRUE if this context is the Development context or a sub-context of it
94 *
95 * @return bool
96 * @api
97 */
98 public function isDevelopment()
99 {
100 return ($this->rootContextString === 'Development');
101 }
102
103 /**
104 * Returns TRUE if this context is the Production context or a sub-context of it
105 *
106 * @return bool
107 * @api
108 */
109 public function isProduction()
110 {
111 return ($this->rootContextString === 'Production');
112 }
113
114 /**
115 * Returns TRUE if this context is the Testing context or a sub-context of it
116 *
117 * @return bool
118 * @api
119 */
120 public function isTesting()
121 {
122 return ($this->rootContextString === 'Testing');
123 }
124
125 /**
126 * Returns the parent context object, if any
127 *
128 * @return \TYPO3\CMS\Core\Core\ApplicationContext the parent context or NULL, if there is none
129 * @api
130 */
131 public function getParent()
132 {
133 return $this->parentContext;
134 }
135 }