Skip to content

Commit

Permalink
Cleanup (eg. PSR-2)
Browse files Browse the repository at this point in the history
  • Loading branch information
Markus Blaschke committed Oct 7, 2015
1 parent 98a7ce0 commit 853b893
Showing 1 changed file with 97 additions and 31 deletions.
128 changes: 97 additions & 31 deletions ContextLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
* -> typo3conf/AdditionalConfiguration/Production/Live/Server4711.php
*
*/
class ContextLoader {
class ContextLoader
{

/**
* Application context
Expand Down Expand Up @@ -72,20 +73,35 @@ class ContextLoader {
/**
* Construct
*/
public function __construct() {
public function __construct()
{
define('CRON_TYPO3_ADDITIONALCONFIGURATION', 1);

$this
->init()
->checkEnvironment()
->buildContextList();
}

/**
* Init
*
* @return $this
*/
public function init()
{
$this->applicationContext = GeneralUtility::getApplicationContext();
$this->checkEnvironment();
$this->buildContextList();

return $this;
}

/**
* Check environment
*
* @return $this
*/
public function checkEnvironment() {
public function checkEnvironment()
{
// Check CLI mode
if (defined('TYPO3_cliMode')) {
$contextEnv = getenv('TYPO3_CONTEXT');
Expand All @@ -104,9 +120,11 @@ public function checkEnvironment() {
*
* return $this;
*/
public function useCache() {
public function useCache()
{
// TODO: maybe the caching is not safe for race conditions
$this->cacheFile = PATH_site . '/typo3temp/Cache/Code/cache_phpcode/cron_context_conf.php';

return $this;
}

Expand All @@ -115,32 +133,40 @@ public function useCache() {
*
* @return $this
*/
public function useCacheInProduction() {
public function useCacheInProduction()
{
if ($this->applicationContext->isProduction()) {
$this->useCache();
}

return $this;
}

/**
* Add path for automatic context loader
*
* @param string $path Path to file
*
* @return $this
*/
public function addContextConfiguration($path) {
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) {
public function addConfiguration($path)
{
$this->confPathList['file'][] = $path;

return $this;
}

Expand All @@ -149,43 +175,51 @@ public function addConfiguration($path) {
*
* @return $this
*/
public function loadConfiguration() {
public function loadConfiguration()
{
if (!$this->loadCache()) {
$this->loadContextConfiguration();
$this->loadFileConfiguration();
$this->injectExtensionConfiguration();
$this->buildCache();
$this
->loadContextConfiguration()
->loadFileConfiguration()
->injectExtensionConfiguration()
->buildCache();
}

return $this;
}

/**
* Build context list
*
* @return $this
*/
protected function buildContextList() {
$contextList = array();
protected function buildContextList()
{
$contextList = array();
$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 from cache
*/
protected function loadCache() {
protected function loadCache()
{
$ret = false;

if ($this->cacheFile && file_exists($this->cacheFile)) {
$conf = unserialize(file_get_contents($this->cacheFile));

if (!empty($conf)) {
$GLOBALS['TYPO3_CONF_VARS'] = $conf;
$ret = true;
$ret = true;
}
}

Expand All @@ -194,17 +228,25 @@ protected function loadCache() {

/**
* Build context config cache
*
* @return $this
*/
protected function buildCache() {
protected function buildCache()
{
if ($this->cacheFile) {
file_put_contents($this->cacheFile, serialize($GLOBALS['TYPO3_CONF_VARS']));
}

return $this;
}

/**
* Load configuration based on current context
*
* @return $this
*/
protected function loadContextConfiguration() {
protected function loadContextConfiguration()
{
if (!empty($this->confPathList['context'])) {
foreach ($this->confPathList['context'] as $path) {
foreach ($this->contextList as $context) {
Expand All @@ -216,26 +258,35 @@ protected function loadContextConfiguration() {
}
}
}

return $this;
}

/**
* Load simple file configuration
*
* @return $this
*/
protected function loadFileConfiguration() {
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) {
protected function loadConfigurationFile($configurationFile)
{
// Load config file
if (file_exists($configurationFile)) {
// Keep this variable for automatic injection into requried files!
Expand All @@ -255,30 +306,41 @@ protected function loadConfigurationFile($configurationFile) {

/**
* Inject dynamic extension configuration
*
* @return $this
*/
protected function injectExtensionConfiguration() {
protected function injectExtensionConfiguration()
{
if (!empty($this->extensionConfList)) {
$extConf = &$GLOBALS['TYPO3_CONF_VARS']['EXT']['extConf'];

foreach ($this->extensionConfList as $extension => $settingList) {
if (!empty($extConf[$extension])) {
$conf = unserialize($extConf[$extension]);
$conf = array_merge($conf, $settingList);
$conf = unserialize($extConf[$extension]);
$conf = array_merge($conf, $settingList);
$extConf[$extension] = serialize($conf);
}
}
}

return $this;
}

/**
* Append context name to sitename (if not production)
*
* @return $this
*/
public function appendContextNameToSitename() {
public function appendContextNameToSitename()
{
if (!$this->applicationContext->isProduction()) {
$GLOBALS['TYPO3_CONF_VARS']['SYS']['sitename'] .= ' [[' . strtoupper((string)$this->applicationContext) . ']]';
$GLOBALS['TYPO3_CONF_VARS']['SYS']['sitename'] = sprintf(
'%s [[%s]]',
$GLOBALS['TYPO3_CONF_VARS']['SYS']['sitename'],
strtoupper((string)$this->applicationContext
));
}

return $this;
}

Expand All @@ -288,9 +350,11 @@ public function appendContextNameToSitename() {
* @param string $extension Extension name
* @param string $setting Configuration setting name
* @param mixed $value Configuration value
*
* @return $this
*/
public function setExtensionConfiguration($extension, $setting, $value = null) {
public function setExtensionConfiguration($extension, $setting, $value = null)
{
$this->extensionConfList[$extension][$setting] = $value;

return $this;
Expand All @@ -299,11 +363,13 @@ public function setExtensionConfiguration($extension, $setting, $value = null) {
/**
* Set extension configuration value (by list)
*
* @param string $extension Extension name
* @param array $settingList List of settings
* @param string $extension Extension name
* @param array $settingList List of settings
*
* @return $this
*/
public function setExtensionConfigurationList($extension, array $settingList) {
public function setExtensionConfigurationList($extension, array $settingList)
{
if (empty($this->extensionConfList[$extension])) {
$this->extensionConfList[$extension] = $settingList;
} else {
Expand Down

0 comments on commit 853b893

Please sign in to comment.