mirror of
https://github.com/alchemy-fr/Phraseanet.git
synced 2025-10-07 01:54:27 +00:00
Add configuration editor command in setup
This commit is contained in:
14
bin/setup
14
bin/setup
@@ -11,6 +11,7 @@
|
||||
|
||||
namespace KonsoleKommander;
|
||||
|
||||
use Alchemy\Phrasea\Command\Setup\ConfigurationEditor;
|
||||
use Alchemy\Phrasea\Core\Version;
|
||||
use Alchemy\Phrasea\Command\UpgradeDBDatas;
|
||||
use Alchemy\Phrasea\Command\Setup\Install;
|
||||
@@ -24,6 +25,7 @@ use Alchemy\Phrasea\Command\Plugin\DisablePlugin;
|
||||
use Alchemy\Phrasea\CLI;
|
||||
use Alchemy\Phrasea\Command\Setup\CheckEnvironment;
|
||||
use Alchemy\Phrasea\Core\CLIProvider\DoctrineMigrationServiceProvider;
|
||||
use Alchemy\Phrasea\Setup\ConfigurationTester;
|
||||
|
||||
require_once __DIR__ . '/../vendor/autoload.php';
|
||||
|
||||
@@ -53,16 +55,16 @@ $app->register(new DoctrineMigrationServiceProvider());
|
||||
$app->command(new \module_console_aboutAuthors('about:authors'));
|
||||
$app->command(new \module_console_aboutLicense('about:license'));
|
||||
|
||||
if(
|
||||
$app['phraseanet.configuration-tester']->isMigrable()
|
||||
|| $app['phraseanet.configuration-tester']->isUpgradable()
|
||||
|| $app['phraseanet.configuration-tester']->isInstalled()
|
||||
) {
|
||||
/** @var ConfigurationTester $configurationTester */
|
||||
$configurationTester = $app['phraseanet.configuration-tester'];
|
||||
|
||||
if($configurationTester->isMigrable() || $configurationTester->isUpgradable() || $configurationTester->isInstalled()) {
|
||||
$app->command(new \module_console_systemUpgrade('system:upgrade'));
|
||||
}
|
||||
|
||||
if ($app['phraseanet.configuration-tester']->isInstalled()) {
|
||||
if ($configurationTester->isInstalled()) {
|
||||
$app->command(new UpgradeDBDatas('system:upgrade-datas'));
|
||||
$app->command(new ConfigurationEditor('system:config'));
|
||||
}
|
||||
|
||||
$app->command(new AddPlugin());
|
||||
|
178
lib/Alchemy/Phrasea/Command/Setup/ConfigurationEditor.php
Normal file
178
lib/Alchemy/Phrasea/Command/Setup/ConfigurationEditor.php
Normal file
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user