[TASK] Re-work/simplify copyright header in PHP files - Part 3
[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 * @author Steffen Müller <typo3@t3node.com> (Backport to TYPO3 CMS)
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 if (strstr($contextString, '/') === FALSE) {
66 $this->rootContextString = $contextString;
67 $this->parentContext = NULL;
68 } else {
69 $contextStringParts = explode('/', $contextString);
70 $this->rootContextString = $contextStringParts[0];
71 array_pop($contextStringParts);
72 $this->parentContext = new ApplicationContext(implode('/', $contextStringParts));
73 }
74
75 if (!in_array($this->rootContextString, array('Development', 'Production', 'Testing'))) {
76 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);
77 }
78
79 $this->contextString = $contextString;
80 }
81
82 /**
83 * Returns the full context string, for example "Development", or "Production/LiveSystem"
84 *
85 * @return string
86 * @api
87 */
88 public function __toString() {
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 boolean
96 * @api
97 */
98 public function isDevelopment() {
99 return ($this->rootContextString === 'Development');
100 }
101
102 /**
103 * Returns TRUE if this context is the Production context or a sub-context of it
104 *
105 * @return boolean
106 * @api
107 */
108
109 public function isProduction() {
110 return ($this->rootContextString === 'Production');
111 }
112
113 /**
114 * Returns TRUE if this context is the Testing context or a sub-context of it
115 *
116 * @return boolean
117 * @api
118 */
119 public function isTesting() {
120 return ($this->rootContextString === 'Testing');
121 }
122
123 /**
124 * Returns the parent context object, if any
125 *
126 * @return \TYPO3\CMS\Core\Core\ApplicationContext the parent context or NULL, if there is none
127 * @api
128 */
129 public function getParent() {
130 return $this->parentContext;
131 }
132 }