Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1954 from alchemy-fr/feature/edit-config-command
Add configuration editor command in setup
- Loading branch information
Showing
2 changed files
with
186 additions
and
6 deletions.
There are no files selected for viewing
This file contains 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
178 changes: 178 additions & 0 deletions
178
lib/Alchemy/Phrasea/Command/Setup/ConfigurationEditor.php
This file contains 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,178 @@ | ||
<?php | ||
|
||
namespace Alchemy\Phrasea\Command\Setup; | ||
|
||
use Alchemy\Phrasea\Command\Command; | ||
use Alchemy\Phrasea\Core\Configuration\Configuration; | ||
use Symfony\Component\Console\Input\InputArgument; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
use Symfony\Component\Yaml\Yaml; | ||
|
||
class ConfigurationEditor extends Command | ||
{ | ||
|
||
public function __construct($name) | ||
{ | ||
parent::__construct($name); | ||
} | ||
|
||
protected function configure() | ||
{ | ||
$this->addArgument( | ||
'operation', | ||
InputArgument::REQUIRED, | ||
'The operation to execute (get, set, or add)' | ||
); | ||
|
||
$this->addArgument( | ||
'parameter', | ||
InputArgument::REQUIRED, | ||
'The name of the configuration parameter to get or set' | ||
); | ||
|
||
$this->addArgument( | ||
'value', | ||
InputArgument::OPTIONAL, | ||
'The value to set when operation is "set" or "add", in YAML syntax' | ||
); | ||
} | ||
|
||
protected function doExecute(InputInterface $input, OutputInterface $output) | ||
{ | ||
$command = $input->getArgument('operation'); | ||
$parameter = $input->getArgument('parameter'); | ||
|
||
$parameterNodes = explode('.', $parameter); | ||
|
||
if ($command == 'get') { | ||
$this->readConfigurationValue($output, $parameter, $parameterNodes); | ||
} | ||
elseif ($command == 'set') { | ||
$this->writeConfigurationValue($output, $parameter, $parameterNodes, $input->getArgument('value')); | ||
} | ||
elseif ($command == 'add') { | ||
$this->appendConfigurationValue($output, $parameter, $parameterNodes, $input->getArgument('value')); | ||
} | ||
} | ||
|
||
private function readConfigurationValue(OutputInterface $output, $parameter, array $parameterNodes) | ||
{ | ||
$app = $this->getContainer(); | ||
/** @var Configuration $config */ | ||
$config = $app['configuration.store']; | ||
$values = $config->getConfig(); | ||
$current = $values; | ||
|
||
foreach ($parameterNodes as $paramName) { | ||
$current = $current[$paramName]; | ||
} | ||
|
||
$output->writeln('<info>Getting configuration entry</info> ' . $parameter); | ||
|
||
$this->printConfigurationValue($output, $parameter, $current); | ||
} | ||
|
||
private function writeConfigurationValue(OutputInterface $output, $parameter, array $parameterNodes, $value) | ||
{ | ||
$app = $this->getContainer(); | ||
/** @var Configuration $configurationStore */ | ||
$configurationStore = $app['configuration.store']; | ||
$lastParameter = end($parameterNodes); | ||
|
||
$configurationRoot = $configurationStore->getConfig(); | ||
$configurationCurrent = & $configurationRoot; | ||
|
||
$output->writeln('<info>Writing configuration entry</info> ' . $parameter); | ||
|
||
foreach ($parameterNodes as $paramName) { | ||
if (! isset($mergeConfiguration[$paramName])) { | ||
$configurationCurrent[$paramName] = array(); | ||
} | ||
|
||
if ($lastParameter == $paramName) { | ||
$configurationCurrent[$paramName] = Yaml::parse($value); | ||
} | ||
else { | ||
$configurationCurrent = & $configurationCurrent[$paramName]; | ||
} | ||
} | ||
|
||
$configurationStore->setConfig($configurationRoot); | ||
$configurationStore->compileAndWrite(); | ||
|
||
$output->writeln('<comment>Reading updated configuration value</comment>'); | ||
|
||
$this->readConfigurationValue($output, $parameter, $parameterNodes); | ||
} | ||
|
||
private function appendConfigurationValue(OutputInterface $output, $parameter, array $parameterNodes, $value) | ||
{ | ||
$app = $this->getContainer(); | ||
/** @var Configuration $configurationStore */ | ||
$configurationStore = $app['configuration.store']; | ||
$lastParameter = end($parameterNodes); | ||
|
||
$configurationRoot = $configurationStore->getConfig(); | ||
$configurationCurrent = & $configurationRoot; | ||
|
||
$output->writeln('<info>Appending value to configuration entry</info> ' . $parameter); | ||
|
||
foreach ($parameterNodes as $paramName) { | ||
if (! isset($configurationCurrent[$paramName])) { | ||
$configurationCurrent[$paramName] = array(); | ||
} | ||
|
||
if ($lastParameter == $paramName) { | ||
if (! is_array($configurationCurrent[$paramName])) { | ||
$configurationCurrent[$paramName] = array($configurationCurrent[$paramName]); | ||
} | ||
|
||
$parsedValue = Yaml::parse($value); | ||
|
||
if (! is_array($parsedValue)) { | ||
$parsedValue = [ $parsedValue ]; | ||
} | ||
|
||
$configurationCurrent[$paramName] = array_merge($configurationCurrent[$paramName], $parsedValue); | ||
$configurationCurrent[$paramName] = array_unique($configurationCurrent[$paramName]); | ||
} | ||
else { | ||
$configurationCurrent = & $configurationCurrent[$paramName]; | ||
} | ||
} | ||
|
||
$configurationStore->setConfig($configurationRoot); | ||
$configurationStore->compileAndWrite(); | ||
|
||
$output->writeln('<comment>Reading updated configuration value</comment>'); | ||
|
||
$this->readConfigurationValue($output, $parameter, $parameterNodes); | ||
} | ||
|
||
private function printConfigurationValue(OutputInterface $output, $name, $value, $indent = 0) | ||
{ | ||
if ($indent > 0) { | ||
$output->write(PHP_EOL); | ||
} | ||
|
||
$output->write(str_repeat(' ', $indent * 4) . (is_numeric($name) ? '- ' : $name . ': ')); | ||
|
||
if (is_array($value)) { | ||
if (empty($value)) { | ||
$output->write('[]'); | ||
} | ||
|
||
foreach ($value as $valueName => $valueItem) { | ||
$this->printConfigurationValue($output, $valueName, $valueItem, $indent + 1); | ||
} | ||
} | ||
else { | ||
$output->write(var_export($value)); | ||
} | ||
|
||
if ($indent == 0) { | ||
$output->write(PHP_EOL); | ||
} | ||
} | ||
} |