From 74d12aeed2229ccaceb303367cbbb0c4e4c802fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Anne?= Date: Tue, 14 May 2019 12:46:05 +0200 Subject: [PATCH] [fixup] Add ability to set additionnal parameters to install hooks --- inc/console/plugin/installcommand.class.php | 37 ++++++++++++++++++++- inc/plugin.class.php | 19 ++++++----- 2 files changed, 47 insertions(+), 9 deletions(-) diff --git a/inc/console/plugin/installcommand.class.php b/inc/console/plugin/installcommand.class.php index 19aeabd35641..5e86c6dbadd6 100644 --- a/inc/console/plugin/installcommand.class.php +++ b/inc/console/plugin/installcommand.class.php @@ -56,6 +56,19 @@ protected function configure() { $this->setAliases(['plugin:install']); $this->setDescription('Run plugin(s) installation script'); + $this->addOption( + 'param', + 'p', + InputOption::VALUE_IS_ARRAY | InputOption::VALUE_OPTIONAL, + __('Additionnal parameters to pass to the plugin install hook function') + . PHP_EOL + . __('"-p foo" will set "foo" param value to true') + . PHP_EOL + . __('"-p foo=bar" will set "foo" param value to "bar"') + . PHP_EOL + ); + $this->addUsage('-p foo=bar -p force myplugin'); + $this->addOption( 'username', 'u', @@ -96,6 +109,8 @@ protected function execute(InputInterface $input, OutputInterface $output) { $directories = $input->getArgument('directory'); $force = $input->getOption('force'); + $params = $this->getAdditionnalParameters($input); + foreach ($directories as $directory) { $output->writeln( '' . sprintf(__('Processing plugin "%s"...'), $directory) . '', @@ -115,7 +130,7 @@ protected function execute(InputInterface $input, OutputInterface $output) { ); continue; } - $plugin->install($plugin->fields['id']); + $plugin->install($plugin->fields['id'], $params); // Check state after installation if (!in_array($plugin->fields['state'], [Plugin::NOTACTIVATED, Plugin::TOBECONFIGURED])) { @@ -318,4 +333,24 @@ private function canRunInstallMethod($directory, $allow_reinstall) { return true; } + + /** + * Extract additionnal parameters from input. + * + * @param InputInterface $input + * + * @return array + */ + private function getAdditionnalParameters(InputInterface $input) { + + $input_params = $input->getOption('param'); + + $params = []; + foreach ($input_params as $input_param) { + $parts = explode('=', $input_param); + $params[$parts[0]] = isset($parts[1]) ? $parts[1] : true; + } + + return $params; + } } diff --git a/inc/plugin.class.php b/inc/plugin.class.php index bc9dc49d350b..1e6896781ba5 100644 --- a/inc/plugin.class.php +++ b/inc/plugin.class.php @@ -509,11 +509,14 @@ function uninstall($ID) { /** * Install a plugin * - * @param int $ID ID of the plugin + * @param int $ID ID of the plugin + * @param array $params Additionnal params to pass to install hook. * * @return void + * + * @since 9.5.0 Added $param parameter **/ - function install($ID) { + function install($ID, array $params = []) { global $DB; @@ -528,15 +531,15 @@ function install($ID) { } self::load($this->fields['directory'], true); - $function = 'plugin_' . $this->fields['directory'] . '_install'; - if (function_exists($function)) { + $install_function = 'plugin_' . $this->fields['directory'] . '_install'; + if (function_exists($install_function)) { $this->setLoaded('temp', $this->fields['directory']); // For autoloader $DB->disableTableCaching(); //prevents issues on table/fieldExists upgrading from old versions - if ($function()) { + if ($install_function($params)) { $type = INFO; - $function = 'plugin_' . $this->fields['directory'] . '_check_config'; - if (function_exists($function)) { - if ($function()) { + $check_function = 'plugin_' . $this->fields['directory'] . '_check_config'; + if (function_exists($check_function)) { + if ($check_function()) { $this->update(['id' => $ID, 'state' => self::NOTACTIVATED]); $message = sprintf(__('Plugin %1$s has been installed!'), $this->fields['name']);