From 8ea4216672448e1d89ff247d9aeaf5aa99d4a2c2 Mon Sep 17 00:00:00 2001 From: Matias Griese Date: Mon, 8 Feb 2021 13:40:39 +0200 Subject: [PATCH] Added ability to filter enabled or disabled with bin/gpm index [#3187] --- CHANGELOG.md | 1 + system/src/Grav/Common/GPM/GPM.php | 41 ++++++-------------- system/src/Grav/Console/Gpm/IndexCommand.php | 20 ++++++---- 3 files changed, 24 insertions(+), 38 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d9cb70326..21d206ff9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ * Updated bundled `composer.phar` binary to latest version `2.0.9` * Improved session fixation handling in PHP 7.4+ (cannot fix it in PHP 7.3 due to PHP bug) * Added optional password/database attributes for redis in `system.yaml` + * Added ability to filter enabled or disabled with bin/gpm index [#3187](https://github.com/getgrav/grav/pull/3187) 1. [](#bugfix) * Fixed CLI progressbar in `backup` and `security` commands to use styled output [#3198](https://github.com/getgrav/grav/issues/3198) * Fixed page save failing because of uploaded images [#3191](https://github.com/getgrav/grav/issues/3191) diff --git a/system/src/Grav/Common/GPM/GPM.php b/system/src/Grav/Common/GPM/GPM.php index 56c4ebbd7..1b15e5de8 100644 --- a/system/src/Grav/Common/GPM/GPM.php +++ b/system/src/Grav/Common/GPM/GPM.php @@ -146,23 +146,11 @@ class GPM extends Iterator * @param string $slug * @return bool True if the Plugin is Enabled. False if manually set to enable:false. Null otherwise. */ - public function isPluginEnabled($slug) + public function isPluginEnabled($slug): bool { - $result = null; - $grav = new Grav(); - $config = $grav::instance()['config']['plugins']; - $installed = $this->isPluginInstalled($slug); + $grav = Grav::instance(); - $enabled = isset($config[$slug]) && ((bool)$config[$slug]['enabled'] == true); - $disabled = isset($config[$slug]['enabled']) && ((bool)$config[$slug]['enabled'] === false); - - if ($enabled) { - $result = true; - } - if ($disabled) { - $result = false; - } - return $result; + return ($grav['config']['plugins'][$slug]['enabled'] ?? false) === true; } /** @@ -171,7 +159,7 @@ class GPM extends Iterator * @param string $slug The slug of the Plugin * @return bool True if the Plugin has been installed. False otherwise */ - public function isPluginInstalled($slug) + public function isPluginInstalled($slug): bool { return isset($this->installed['plugins'][$slug]); } @@ -212,22 +200,15 @@ class GPM extends Iterator * Checks if a Theme is enabled * * @param string $slug The slug of the Theme - * @return bool - * True if the Theme has been set to the default theme. False if installed, but not enabled. Null otherwise. + * @return bool True if the Theme has been set to the default theme. False if installed, but not enabled. Null otherwise. */ - public function isThemeEnabled($slug) + public function isThemeEnabled($slug): bool { - $grav = new Grav(); - $result = null; - $current_theme = $grav::instance()['config']['system']['pages']['theme']; - $theme_installed = $this->isThemeInstalled($slug); + $grav = Grav::instance(); - if ($current_theme == $slug) { - $result = true; - } elseif ($theme_installed) { - $result = false; - } - return $result; + $current_theme = $grav['config']['system']['pages']['theme'] ?? null; + + return $current_theme === $slug; } /** @@ -236,7 +217,7 @@ class GPM extends Iterator * @param string $slug The slug of the Theme * @return bool True if the Theme has been installed. False otherwise */ - public function isThemeInstalled($slug) + public function isThemeInstalled($slug): bool { return isset($this->installed['themes'][$slug]); } diff --git a/system/src/Grav/Console/Gpm/IndexCommand.php b/system/src/Grav/Console/Gpm/IndexCommand.php index 33e1b28ab..97a71bdf8 100644 --- a/system/src/Grav/Console/Gpm/IndexCommand.php +++ b/system/src/Grav/Console/Gpm/IndexCommand.php @@ -216,15 +216,19 @@ class IndexCommand extends GpmCommand { $package = $list[$package->slug] ?? $package; $type = ucfirst(preg_replace('/s$/', '', $package->package_type)); - $method = 'is' . $type . 'Enabled'; - $enabled = $this->gpm->{$method}($package->slug); + $method = 'is' . $type . 'Installed'; + $installed = $this->gpm->{$method}($package->slug); - if ($enabled === null) { + if ($installed) { + $method = 'is' . $type . 'Enabled'; + $enabled = $this->gpm->{$method}($package->slug); + if ($enabled === true) { + $result = 'enabled'; + } elseif ($enabled === false) { + $result = 'disabled'; + } + } else { $result = ''; - } elseif ($enabled === true) { - $result = 'enabled'; - } elseif ($enabled === false) { - $result = 'disabled'; } return $result; @@ -264,7 +268,7 @@ class IndexCommand extends GpmCommand } // Filtering updatables only - if ($filter && $this->options['installed-only']) { + if ($filter && ($this->options['installed-only'] || $this->options['enabled'] || $this->options['disabled'])) { $method = ucfirst(preg_replace('/s$/', '', $package->package_type)); $function = 'is' . $method . 'Installed'; $filter = $this->gpm->{$function}($package->slug);