Skip to content

Commit 981cdd5

Browse files
committed
Initial revision
1 parent 1afe6cb commit 981cdd5

17 files changed

Lines changed: 611 additions & 0 deletions

File tree

Classes/ContextLoader.php

Lines changed: 364 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,364 @@
1+
<?php
2+
3+
namespace WebDevOps\ContextLoader;
4+
5+
use TYPO3\CMS\Core\Utility\GeneralUtility;
6+
7+
/**
8+
* Context loader (TYPO3_CONTEXT)
9+
*
10+
* Examples:
11+
*
12+
* TYPO3_CONTEXT=Production
13+
* -> typo3conf/AdditionalConfiguration/Production.php
14+
*
15+
* TYPO3_CONTEXT=Testing
16+
* -> typo3conf/AdditionalConfiguration/Testing.php
17+
*
18+
* TYPO3_CONTEXT=Development
19+
* -> typo3conf/AdditionalConfiguration/Development.php
20+
*
21+
* TYPO3_CONTEXT=Production/Staging
22+
* -> typo3conf/AdditionalConfiguration/Production.php
23+
* -> typo3conf/AdditionalConfiguration/Production/Staging.php
24+
*
25+
* TYPO3_CONTEXT=Production/Live
26+
* -> typo3conf/AdditionalConfiguration/Production.php
27+
* -> typo3conf/AdditionalConfiguration/Production/Live.php
28+
*
29+
* TYPO3_CONTEXT=Production/Live/Server4711
30+
* -> typo3conf/AdditionalConfiguration/Production.php
31+
* -> typo3conf/AdditionalConfiguration/Production/Live.php
32+
* -> typo3conf/AdditionalConfiguration/Production/Live/Server123.php
33+
*
34+
*/
35+
class ContextLoader
36+
{
37+
/**
38+
* @var ContextLoader
39+
*/
40+
static $instance;
41+
42+
/**
43+
* Load indicator
44+
*
45+
* @var bool
46+
*/
47+
static $configurationLoaded = false;
48+
49+
/**
50+
* @var \TYPO3\CMS\Core\Core\ApplicationContext
51+
*/
52+
protected $applicationContext;
53+
54+
/**
55+
* Context list (reversed)
56+
*
57+
* @var array
58+
*/
59+
protected $contextList = [];
60+
61+
/**
62+
* Configuration path list (simple files)
63+
*
64+
* @var array
65+
*/
66+
protected $confPathList = [];
67+
68+
/**
69+
* List of extension configuration directives (overwrites)
70+
*
71+
* @var array
72+
*/
73+
protected $extensionConfList = [];
74+
75+
/**
76+
* Check if configuration is already loaded
77+
*
78+
* @return bool
79+
*/
80+
public function isConfigurationLoaded()
81+
{
82+
return $this::$configurationLoaded;
83+
}
84+
85+
/**
86+
* @return ContextLoader
87+
*/
88+
public static function getInstance()
89+
{
90+
if (!self::$instance) {
91+
self::$instance = new self();
92+
}
93+
94+
return self::$instance;
95+
}
96+
97+
/**
98+
* Constructor
99+
*/
100+
protected function __construct()
101+
{
102+
103+
}
104+
105+
/**
106+
* @return $this
107+
*/
108+
public function init()
109+
{
110+
$this
111+
->checkEnvironment()
112+
->buildContextList();
113+
114+
return $this;
115+
}
116+
117+
/**
118+
* @return $this
119+
*/
120+
public function checkEnvironment()
121+
{
122+
// Check CLI mode
123+
if (defined('TYPO3_cliMode')) {
124+
$contextEnv = getenv('TYPO3_CONTEXT');
125+
126+
if (empty($contextEnv)) {
127+
echo '[ERROR] TYPO3_CONTEXT not set or found for AdditionalConfiguration.php' . "\n";
128+
exit(1);
129+
}
130+
}
131+
132+
return $this;
133+
}
134+
135+
/**
136+
* @param string $path Path to directory
137+
*
138+
* @return $this
139+
*/
140+
public function addContextConfigurationDirectory($path)
141+
{
142+
$this->confPathList['context'][] = $path;
143+
144+
return $this;
145+
}
146+
147+
/**
148+
* @param string $path Path to configuration file
149+
*
150+
* @return $this
151+
*/
152+
public function addConfigurationFile($path)
153+
{
154+
$this->confPathList['file'][] = $path;
155+
156+
return $this;
157+
}
158+
159+
/**
160+
* @return $this
161+
*/
162+
public function loadConfiguration()
163+
{
164+
$this
165+
->loadConfigurationFromContextDirectories()
166+
->loadConfigurationFromConfigurationFiles()
167+
->applyExtensionConfiguration();
168+
169+
$this::$configurationLoaded = true;
170+
171+
return $this;
172+
}
173+
174+
/**
175+
* Build context list
176+
*
177+
* @return $this
178+
*/
179+
protected function buildContextList()
180+
{
181+
$this->applicationContext = GeneralUtility::getApplicationContext();
182+
183+
$contextList = [];
184+
$currentContext = $this->applicationContext;
185+
do {
186+
$contextList[] = (string)$currentContext;
187+
} while ($currentContext = $currentContext->getParent());
188+
189+
// Reverse list, general first (eg. PRODUCTION), then specific last (eg. SERVER)
190+
$this->contextList = array_reverse($contextList);
191+
192+
return $this;
193+
}
194+
195+
/**
196+
* Load configuration based on current context
197+
*
198+
* @return $this
199+
*/
200+
protected function loadConfigurationFromContextDirectories()
201+
{
202+
if (!empty($this->confPathList['context'])) {
203+
foreach ($this->confPathList['context'] as $path) {
204+
foreach ($this->contextList as $context) {
205+
// Sanitize context name
206+
$context = preg_replace('/[^-_\.a-zA-Z0-9\/]/', '', $context);
207+
208+
// Build config file name
209+
$this->loadAndApplyConfigurationFile($path . '/' . $context . '.php');
210+
}
211+
}
212+
}
213+
214+
return $this;
215+
}
216+
217+
/**
218+
* Load simple file configuration
219+
*
220+
* @return $this
221+
*/
222+
protected function loadConfigurationFromConfigurationFiles()
223+
{
224+
if (!empty($this->confPathList['file'])) {
225+
foreach ($this->confPathList['file'] as $path) {
226+
$this->loadAndApplyConfigurationFile($path);
227+
}
228+
}
229+
230+
return $this;
231+
}
232+
233+
/**
234+
* @param string $configurationFile Configuration file
235+
*
236+
* @return $this
237+
*/
238+
protected function loadAndApplyConfigurationFile($configurationFile)
239+
{
240+
// Load config file
241+
if (file_exists($configurationFile)) {
242+
// Keep this variable for automatic injection into requried files!
243+
$contextLoader = $this;
244+
245+
// Load configuration file
246+
$retConf = require $configurationFile;
247+
248+
// Apply return'ed configuration (if available)
249+
if (!empty($retConf) && is_array($retConf)) {
250+
$GLOBALS['TYPO3_CONF_VARS'] = array_replace_recursive($GLOBALS['TYPO3_CONF_VARS'], $retConf);
251+
}
252+
}
253+
254+
return $this;
255+
}
256+
257+
/**
258+
* @return $this
259+
*/
260+
protected function applyExtensionConfiguration()
261+
{
262+
if (!empty($this->extensionConfList)) {
263+
$extConf = &$GLOBALS['TYPO3_CONF_VARS']['EXT']['extConf'];
264+
265+
foreach ($this->extensionConfList as $extension => $settingList) {
266+
$conf = [];
267+
268+
if (!empty($extConf[$extension])) {
269+
$conf = unserialize($extConf[$extension]);
270+
}
271+
272+
$conf = array_merge($conf, $settingList);
273+
$extConf[$extension] = serialize($conf);
274+
}
275+
}
276+
277+
return $this;
278+
}
279+
280+
/**
281+
* @return $this
282+
*/
283+
public function appendContextNameToSitename()
284+
{
285+
$GLOBALS['TYPO3_CONF_VARS']['SYS']['sitename'] = sprintf(
286+
'%s [[%s]]',
287+
$GLOBALS['TYPO3_CONF_VARS']['SYS']['sitename'],
288+
strtoupper((string)$this->applicationContext
289+
));
290+
291+
return $this;
292+
}
293+
294+
/**
295+
* @return $this
296+
*/
297+
public function appendContextNameToSitenameInDevelopment()
298+
{
299+
if ($this->isDevelopmentContext()) {
300+
$this->appendContextNameToSitename();
301+
}
302+
303+
return $this;
304+
}
305+
306+
/**
307+
* @return bool
308+
*/
309+
public function isDevelopmentContext()
310+
{
311+
return $this->applicationContext->isDevelopment();
312+
}
313+
314+
/**
315+
* @return bool
316+
*/
317+
public function isProductionContext()
318+
{
319+
return $this->applicationContext->isProduction();
320+
}
321+
322+
/**
323+
* @return bool
324+
*/
325+
public function isTestingContext()
326+
{
327+
return $this->applicationContext->isTesting();
328+
}
329+
330+
/**
331+
* Set extension configuration value
332+
*
333+
* @param string $extension Extension name
334+
* @param string $setting Configuration setting name
335+
* @param mixed $value Configuration value
336+
*
337+
* @return $this
338+
*/
339+
public function setExtensionConfiguration($extension, $setting, $value = null)
340+
{
341+
$this->extensionConfList[$extension][$setting] = $value;
342+
343+
return $this;
344+
}
345+
346+
/**
347+
* Set extension configuration value (by list)
348+
*
349+
* @param string $extension Extension name
350+
* @param array $settingList List of settings
351+
*
352+
* @return $this
353+
*/
354+
public function setExtensionConfigurationList($extension, array $settingList)
355+
{
356+
if (empty($this->extensionConfList[$extension])) {
357+
$this->extensionConfList[$extension] = $settingList;
358+
} else {
359+
$this->extensionConfList[$extension] = array_merge($this->extensionConfList[$extension], $settingList);
360+
}
361+
362+
return $this;
363+
}
364+
}

Configuration/Development.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
// Disable frontend caching (single hit caching)
4+
$GLOBALS['TYPO3_CONF_VARS']['SYS']['caching']['cacheConfigurations']['cache_pagesection']['options'] =
5+
$GLOBALS['TYPO3_CONF_VARS']['SYS']['caching']['cacheConfigurations']['cache_hash']['options'] =
6+
$GLOBALS['TYPO3_CONF_VARS']['SYS']['caching']['cacheConfigurations']['cache_pages']['options'] = [];
7+
$GLOBALS['TYPO3_CONF_VARS']['SYS']['caching']['cacheConfigurations']['cache_pagesection']['backend'] =
8+
$GLOBALS['TYPO3_CONF_VARS']['SYS']['caching']['cacheConfigurations']['cache_hash']['backend'] =
9+
$GLOBALS['TYPO3_CONF_VARS']['SYS']['caching']['cacheConfigurations']['cache_pages']['backend'] =
10+
'TYPO3\\CMS\\Core\\Cache\\Backend\\TransientMemoryBackend';
11+
12+
return [
13+
'SYS' => [
14+
'trustedHostsPattern' => '.*',
15+
'devIPmask' => '*',
16+
'sqlDebug' => 1,
17+
'displayErrors' => 1,
18+
'enableDeprecationLog' => 'file',
19+
'systemLogLevel' => 0,
20+
],
21+
'BE' => [
22+
// set installer password to 'dev'
23+
'installToolPassword' => '$P$C4a1FXXNaZmkWi6LUxrDKSXjMBToX0/',
24+
'debug' => true,
25+
'sessionTimeout' => '360000'
26+
],
27+
'FE' => [
28+
'disableNoCacheParameter' => false,
29+
'debug' => true,
30+
],
31+
];

0 commit comments

Comments
 (0)