[FEATURE] Backport ApplicationContext from Flow
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Core / ApplicationContext.php
1 <?php
2 namespace TYPO3\CMS\Core\Core;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2013 The respective TYPO3 Flow framework authors
8 * (c) 2013 Steffen Müller <typo3@t3node.com> (Backport to TYPO3 CMS)
9 * All rights reserved
10 *
11 * This script is part of the TYPO3 project. The TYPO3 project is
12 * free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * The GNU General Public License can be found at
18 * http://www.gnu.org/copyleft/gpl.html.
19 *
20 * This script is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
27
28 /**
29 * The TYPO3 Context object.
30 *
31 * A TYPO3 Application context is something like "Production", "Development",
32 * "Production/StagingSystem", and is set using the TYPO3_CONTEXT environment variable.
33 *
34 * A context can contain arbitrary sub-contexts, which are delimited with slash
35 * ("Production/StagingSystem", "Production/Staging/Server1"). The top-level
36 * contexts, however, must be one of "Testing", "Development" and "Production".
37 *
38 * Mainly, you will use $context->isProduction(), $context->isTesting() and
39 * $context->isDevelopment() inside your custom code.
40 *
41 * This class is derived from the TYPO3 Flow framework.
42 * Credits go to the respective authors.
43 *
44 * @author Steffen Müller <typo3@t3node.com> (Backport to TYPO3 CMS)
45 */
46 class ApplicationContext {
47
48 /**
49 * The (internal) context string; could be something like "Development" or "Development/MyLocalMacBook"
50 *
51 * @var string
52 */
53 protected $contextString;
54
55 /**
56 * The root context; must be one of "Development", "Testing" or "Production"
57 *
58 * @var string
59 */
60 protected $rootContextString;
61
62 /**
63 * The parent context, or NULL if there is no parent context
64 *
65 * @var \TYPO3\CMS\Core\Core\ApplicationContext
66 */
67 protected $parentContext;
68
69 /**
70 * Initialize the context object.
71 *
72 * @param string $contextString
73 * @throws \Exception if the parent context is none of "Development", "Production" or "Testing"
74 */
75 public function __construct($contextString) {
76 if (strstr($contextString, '/') === FALSE) {
77 $this->rootContextString = $contextString;
78 $this->parentContext = NULL;
79 } else {
80 $contextStringParts = explode('/', $contextString);
81 $this->rootContextString = $contextStringParts[0];
82 array_pop($contextStringParts);
83 $this->parentContext = new ApplicationContext(implode('/', $contextStringParts));
84 }
85
86 if (!in_array($this->rootContextString, array('Development', 'Production', 'Testing'))) {
87 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);
88 }
89
90 $this->contextString = $contextString;
91 }
92
93 /**
94 * Returns the full context string, for example "Development", or "Production/LiveSystem"
95 *
96 * @return string
97 * @api
98 */
99 public function __toString() {
100 return $this->contextString;
101 }
102
103 /**
104 * Returns TRUE if this context is the Development context or a sub-context of it
105 *
106 * @return boolean
107 * @api
108 */
109 public function isDevelopment() {
110 return ($this->rootContextString === 'Development');
111 }
112
113 /**
114 * Returns TRUE if this context is the Production context or a sub-context of it
115 *
116 * @return boolean
117 * @api
118 */
119
120 public function isProduction() {
121 return ($this->rootContextString === 'Production');
122 }
123
124 /**
125 * Returns TRUE if this context is the Testing context or a sub-context of it
126 *
127 * @return boolean
128 * @api
129 */
130 public function isTesting() {
131 return ($this->rootContextString === 'Testing');
132 }
133
134 /**
135 * Returns the parent context object, if any
136 *
137 * @return \TYPO3\CMS\Core\Core\ApplicationContext the parent context or NULL, if there is none
138 * @api
139 */
140 public function getParent() {
141 return $this->parentContext;
142 }
143 }
144 ?>