Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions Model/Command/ConfigUpdate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace oat\generis\Model\Command;

use oat\generis\Model\Config\ConfigInitializer;
use oat\generis\Model\Console\ConsoleCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

/**
* Class ConfigUpdate
*
* @author Martijn Swinkels <m.swinkels@taotesting.com>
*/
class ConfigUpdate extends ConsoleCommand
{

/**
* @inheritdoc
*/
protected function configure()
{
$this->setName('config:update')
->setDescription('Rebuilds the configuration for the platform');
}

/**
* @inheritdoc
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$this->writeln('Rebuilding config');
$configInitializer = new ConfigInitializer();
$configInitializer->initialize(true);
$this->success('Config has been rebuilt!');
}
}
127 changes: 127 additions & 0 deletions Model/Config/ConfigInitializer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
<?php

namespace oat\generis\Model\Config;

use Symfony\Component\Config\ConfigCache;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\Yaml\Exception\ParseException;
use Symfony\Component\Yaml\Yaml;

/**
* Class used to initialize the configuration files for the platform
*
* @author Martijn Swinkels <m.swinkels@taotesting.com>
*/
class ConfigInitializer
{

const CONFIG_DIR = 'config';
const CONFIG_CACHE_DIR = 'config' . DIRECTORY_SEPARATOR . 'cache' . DIRECTORY_SEPARATOR;
const CACHE_LIFETIME = 18000;

/**
* @var string
*/
private $fileName;

/**
* @var ConfigCache
*/
private $configCache;

/**
* @var string
*/
private $rootPath;

/**
* @var string[]
*/
private static $excludedPaths = [
'bin', 'vendor', 'composer', 'config', 'data', 'tests'
];

/**
* AbstractInitializer constructor.
*
* @param string $fileName
*/
public function __construct($fileName = 'config.yml')
{
$rootPathParts = explode(DIRECTORY_SEPARATOR, __DIR__);
$rootPathParts = array_splice($rootPathParts, 0, -3);
$this->rootPath = implode(DIRECTORY_SEPARATOR, $rootPathParts) . DIRECTORY_SEPARATOR;
$this->fileName = $fileName;
$this->configCache = new ConfigCache($this->rootPath . self::CONFIG_CACHE_DIR . $this->fileName, false);
}


/**
* Read configuration for the project
*
* @param bool $rebuild
* @return array
*/
public function initialize($rebuild = false)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function initialize has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring.

{
if (!$rebuild && $this->configCache->isFresh() && !$this->isCacheExpired()) {
return Yaml::parseFile($this->rootPath . self::CONFIG_CACHE_DIR . $this->fileName);
}

$configPaths = $this->getConfigPaths();
$fileLocator = new FileLocator($configPaths);
$fileList = $fileLocator->locate(self::CONFIG_DIR . DIRECTORY_SEPARATOR . $this->fileName, null, false);

if (!count($fileList)) {
return [];
}

$config = [];
foreach ($fileList as $file) {
try {
$config = array_merge_recursive($config, Yaml::parseFile($file));
} catch (ParseException $e) {
throw new ParseException(
sprintf(
'The Yaml config in file "%s" is invalid:' . "\n%s\nVerify the files contents and try again.",
$file, $e->getMessage()
)
);
}
}

$this->configCache->write(Yaml::dump($config));

return $config;
}

/**
* @return string[]
*/
private function getConfigPaths()
{
$configPaths = array_filter(glob('*'), 'is_dir');

foreach ($configPaths as $key => $configPath) {
if (in_array($configPath, self::$excludedPaths, true)) {
unset($configPaths[$key]);
continue;
}
$configPaths[$key] = $this->rootPath . $configPath;
}

return $configPaths;
}

/**
* Check if the cached config file is expired
*
* @return bool
*/
private function isCacheExpired()
{
$lastModificationTime = filemtime($this->rootPath . self::CONFIG_CACHE_DIR . $this->fileName);

return time() > $lastModificationTime + self::CACHE_LIFETIME;
}
}
54 changes: 54 additions & 0 deletions Model/Config/Reader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

namespace oat\generis\Model\Config;

/**
* Reader used to get values from config.
*
* @author Martijn Swinkels <m.swinkels@taotesting.com>
*/
class Reader
{

/**
* @var array
*/
private $config;

/**
* Read configuration for the project
*
* @return array
*/
private function getConfig()
{
if ($this->config === null) {
$initializer = new ConfigInitializer();
$this->config = $initializer->initialize();
}

return $this->config;
}

/**
* Get a configuration.
*
* @param string $path
* @return mixed|null
*/
public function get($path)
{
$config = $this->getConfig();
$keys = explode('/', $path);

foreach ($keys as $key) {
if (!isset($config[$key])) {
return null;
}

$config = $config[$key];
}

return $config;
}
}
Loading