-
Notifications
You must be signed in to change notification settings - Fork 2
/
ContextLoader.php
257 lines (224 loc) · 5.89 KB
/
ContextLoader.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
<?php
namespace Cron\CronContext;
use TYPO3\CMS\Core\Core\Environment;
/**
* Additional configuration loader (based on context)
*
* Examples:
*
* TYPO3_CONTEXT=Production
* -> config/system/additional/Production.php
*
* TYPO3_CONTEXT=Testing
* -> config/system/additional/Testing.php
*
* TYPO3_CONTEXT=Development
* -> config/system/additional/Development.php
*
* TYPO3_CONTEXT=Production/Staging
* -> config/system/additional/Production.php
* -> config/system/additional/Production/Staging.php
*
* TYPO3_CONTEXT=Production/Live
* -> config/system/additional/Production.php
* -> config/system/additional/Production/Live.php
*
* TYPO3_CONTEXT=Production/Live/Server4711
* -> config/system/additional/Production.php
* -> config/system/additional/Production/Live.php
* -> config/system/additional/Production/Live/Server4711.php
*
*/
class ContextLoader
{
/**
* Application context
*
* @var \TYPO3\CMS\Core\Core\ApplicationContext
*/
protected $applicationContext;
/**
* Context list (reversed)
*
* @var array
*/
protected $contextList = [];
/**
* Configuration path list (simple files)
*
* @var array
*/
protected $confPathList = [];
/**
* Construct
*/
public function __construct()
{
define('CRON_TYPO3_ADDITIONALCONFIGURATION', 1);
$this
->init()
->checkEnvironment()
->buildContextList();
}
/**
* Init
*
* @return $this
*/
public function init()
{
$this->applicationContext = Environment::getContext();
return $this;
}
/**
* Check environment
*
* @return $this
*/
public function checkEnvironment()
{
// Check CLI mode
if (!Environment::isCli() &&
empty(getenv('TYPO3_CONTEXT'))
) {
echo '[ERROR] TYPO3_CONTEXT not set or found for additional.php' . "\n";
exit(1);
}
return $this;
}
/**
* Add path for automatic context loader
*
* @param string $path Path to file
*
* @return $this
*/
public function addContextConfiguration($path)
{
$this->confPathList['context'][] = $path;
return $this;
}
/**
* Add configuration to loader
*
* @param string $path Path to file
*
* @return $this
*/
public function addConfiguration($path)
{
$this->confPathList['file'][] = $path;
return $this;
}
/**
* Load configuration
*
* @return $this
*/
public function loadConfiguration()
{
$this
->loadContextConfiguration()
->loadFileConfiguration();
return $this;
}
/**
* Build context list
*
* @return $this
*/
protected function buildContextList()
{
$contextList = [];
$currentContext = $this->applicationContext;
do {
$contextList[] = (string)$currentContext;
} while ($currentContext = $currentContext->getParent());
// Reverse list, general first (eg. PRODUCTION), then specific last (eg. SERVER)
$this->contextList = array_reverse($contextList);
return $this;
}
/**
* Load configuration based on current context
*
* @return $this
*/
protected function loadContextConfiguration()
{
if (!empty($this->confPathList['context'])) {
foreach ($this->confPathList['context'] as $path) {
foreach ($this->contextList as $context) {
// Sanitize context name
$context = preg_replace('/[^-_\.a-zA-Z0-9\/]/', '', $context);
// Build config file name
$this->loadConfigurationFile($path . '/' . $context . '.php');
}
}
}
return $this;
}
/**
* Load simple file configuration
*
* @return $this
*/
protected function loadFileConfiguration()
{
if (!empty($this->confPathList['file'])) {
foreach ($this->confPathList['file'] as $path) {
$this->loadConfigurationFile($path);
}
}
return $this;
}
/**
* Load configuration file
*
* @param string $configurationFile Configuration file
*
* @return $this
*/
protected function loadConfigurationFile($configurationFile)
{
// Load config file
if (file_exists($configurationFile)) {
// Keep this variable for automatic injection into requried files!
$contextLoader = $this;
// Load configuration file
$retConf = require $configurationFile;
// Apply return'ed configuration (if available)
if (!empty($retConf) && is_array($retConf)) {
$GLOBALS['TYPO3_CONF_VARS'] = array_replace_recursive($GLOBALS['TYPO3_CONF_VARS'], $retConf);
}
}
return $this;
}
/**
* Append context name to sitename (if not production)
*
* @return $this
*/
public function appendContextNameToSitename()
{
if (!$this->applicationContext->isProduction()) {
$GLOBALS['TYPO3_CONF_VARS']['SYS']['sitename'] = sprintf(
'%s [[%s]]',
$GLOBALS['TYPO3_CONF_VARS']['SYS']['sitename'],
strtoupper((string)$this->applicationContext
));
}
return $this;
}
/**
* Set extension configuration value (by list)
*
* @param string $extension Extension name
* @param array $settingList List of settings
*
* @return $this
*/
public function setExtensionConfigurationList($extension, array $settingList)
{
$this->setExtensionConfiguration($extension, key($settingList), current($settingList));
}
}