-
Notifications
You must be signed in to change notification settings - Fork 10
Feature - TT-362 Symfony console implementation #582
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
martijn-tao
wants to merge
14
commits into
oat-sa:develop
Choose a base branch
from
martijn-tao:feature/TT-362-symfony-console-implementation
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
fe7e882
Autoload map added
martijn-tao 19e9b90
Merge pull request #1 from martijn-tao/feature/console-commands
martijn-tao c4ce5ae
Added default console command, and executable
martijn-tao d3bf3ba
Added helper functions so this class can pass on styled messages dire…
martijn-tao 445feac
Moved command configuration, added listing and progressbar
martijn-tao 7629ebc
Added dynamic configuration loading
martijn-tao fd66226
WIP - Extension registration testing
martijn-tao fbb3566
WIP - testing DI
martijn-tao de7b2c5
Autowiring DI implementation
martijn-tao b70c0b7
DI Testing
martijn-tao 1b63d43
cleanup
martijn-tao 7c0e67e
version bump
martijn-tao 078c708
Cleaned up config update command
martijn-tao f7ecd43
Cleaned up testing data
martijn-tao File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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!'); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | ||
| { | ||
| 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; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function
initializehas a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring.