Skip to content

Commit

Permalink
[fixup] Add ability to set additionnal parameters to install hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
cedric-anne committed May 14, 2019
1 parent a90a82a commit 74d12ae
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 9 deletions.
37 changes: 36 additions & 1 deletion inc/console/plugin/installcommand.class.php
Expand Up @@ -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',
Expand Down Expand Up @@ -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(
'<info>' . sprintf(__('Processing plugin "%s"...'), $directory) . '</info>',
Expand All @@ -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])) {
Expand Down Expand Up @@ -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;
}
}
19 changes: 11 additions & 8 deletions inc/plugin.class.php
Expand Up @@ -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;

Expand All @@ -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']);
Expand Down

0 comments on commit 74d12ae

Please sign in to comment.