From ee379a512ae624481e256646f6928be176558811 Mon Sep 17 00:00:00 2001 From: Jeremy Gonyea Date: Fri, 29 Jan 2021 23:06:20 -0500 Subject: [PATCH] Added ability to filter enabled or disabled with bin/gpm index --- system/src/Grav/Common/GPM/GPM.php | 51 ++++++++++++++- system/src/Grav/Console/Gpm/IndexCommand.php | 66 +++++++++++++++++++- 2 files changed, 112 insertions(+), 5 deletions(-) diff --git a/system/src/Grav/Common/GPM/GPM.php b/system/src/Grav/Common/GPM/GPM.php index 80fea081a..ec4cbf87a 100644 --- a/system/src/Grav/Common/GPM/GPM.php +++ b/system/src/Grav/Common/GPM/GPM.php @@ -139,6 +139,32 @@ class GPM extends Iterator return $this->installed['plugins']; } + + /** + * Returns the plugin's enabled state + * + * @param string $slug + * @return bool True if the Plugin is Enabled. False if manually set to enable:false. Null otherwise. + */ + public function isPluginEnabled($slug) + { + $result = null; + $grav = new Grav(); + $config = $grav::instance()['config']['plugins']; + $installed = $this->isPluginInstalled($slug); + + $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; + } + /** * Checks if a Plugin is installed * @@ -182,6 +208,28 @@ class GPM extends Iterator return $this->installed['themes']; } + /** + * 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. + */ + public function isThemeEnabled($slug) + { + $grav = new Grav(); + $result = null; + $current_theme = $grav::instance()['config']['system']['pages']['theme']; + $theme_installed = $this->isThemeInstalled($slug); + + if ($current_theme == $slug) { + $result = true; + } elseif ($theme_installed) { + $result = false; + } + return $result; + } + /** * Checks if a Theme is installed * @@ -1023,7 +1071,6 @@ class GPM extends Iterator //Factor in the package dependencies too $dependencies = $this->calculateMergedDependenciesOfPackage($dependencyName, $dependencies); - } elseif ($dependencyVersion !== '*') { // Dependency already added by another package // If this package requires a version higher than the currently stored one, store this requirement instead @@ -1059,7 +1106,7 @@ class GPM extends Iterator $dependencies[$dependencyName] = $dependencyVersion; } } else { - $compatible = $this->checkNextSignificantReleasesAreCompatible($currently_stored_version_number,$current_package_version_number); + $compatible = $this->checkNextSignificantReleasesAreCompatible($currently_stored_version_number, $current_package_version_number); if (!$compatible) { throw new RuntimeException("Dependency {$dependencyName} is required in two incompatible versions", 2); } diff --git a/system/src/Grav/Console/Gpm/IndexCommand.php b/system/src/Grav/Console/Gpm/IndexCommand.php index 9f4634410..163afb48b 100644 --- a/system/src/Grav/Console/Gpm/IndexCommand.php +++ b/system/src/Grav/Console/Gpm/IndexCommand.php @@ -90,6 +90,18 @@ class IndexCommand extends GpmCommand InputOption::VALUE_NONE, 'Reverses the order of the output.' ) + ->addOption( + 'enabled', + 'e', + InputOption::VALUE_NONE, + 'Filters the results to only enabled Themes and Plugins.' + ) + ->addOption( + 'disabled', + 'd', + InputOption::VALUE_NONE, + 'Filters the results to only disabled Themes and Plugins.' + ) ->setDescription('Lists the plugins and themes available for installation') ->setHelp('The index command lists the plugins and themes available for installation') ; @@ -129,7 +141,7 @@ class IndexCommand extends GpmCommand if (!empty($packages)) { $io->section('Packages table'); $table = new Table($io); - $table->setHeaders(['Count', 'Name', 'Slug', 'Version', 'Installed']); + $table->setHeaders(['Count', 'Name', 'Slug', 'Version', 'Installed', 'Enabled']); $index = 0; foreach ($packages as $slug => $package) { @@ -138,7 +150,8 @@ class IndexCommand extends GpmCommand 'Name' => '' . Utils::truncate($package->name, 20, false, ' ', '...') . ' ', 'Slug' => $slug, 'Version'=> $this->version($package), - 'Installed' => $this->installed($package) + 'Installed' => $this->installed($package), + 'Enabled' => $this->enabled($package), ]; $table->addRow($row); @@ -195,6 +208,28 @@ class IndexCommand extends GpmCommand return !$installed ? 'not installed' : 'installed'; } + /** + * @param Package $package + * @return string + */ + private function enabled(Package $package): string + { + $package = $list[$package->slug] ?? $package; + $type = ucfirst(preg_replace('/s$/', '', $package->package_type)); + $method = 'is' . $type . 'Enabled'; + $enabled = $this->gpm->{$method}($package->slug); + + if ($enabled === null) { + $result = ''; + } elseif ($enabled === true) { + $result = 'enabled'; + } elseif ($enabled === false) { + $result = 'disabled'; + } + + return $result; + } + /** * @param Packages $data * @return Packages @@ -210,10 +245,12 @@ class IndexCommand extends GpmCommand } $filter = [ + $this->options['desc'], + $this->options['disabled'], + $this->options['enabled'], $this->options['filter'], $this->options['installed-only'], $this->options['updates-only'], - $this->options['desc'] ]; if (count(array_filter($filter))) { @@ -240,6 +277,29 @@ class IndexCommand extends GpmCommand $filter = $this->gpm->{$function}($package->slug); } + // Filtering enabled only + if ($filter && $this->options['enabled']) { + $method = ucfirst(preg_replace('/s$/', '', $package->package_type)); + + // Check if packaged is enabled. + $function = 'is' . $method . 'Enabled'; + $filter = $this->gpm->{$function}($package->slug); + } + + // Filtering disabled only + if ($filter && $this->options['disabled']) { + $method = ucfirst(preg_replace('/s$/', '', $package->package_type)); + + // Check if package is disabled. + $function = 'is' . $method . 'Enabled'; + $enabled_filter = $this->gpm->{$function}($package->slug); + + // Apply filtering results. + if (!( $enabled_filter === false)) { + $filter = false; + } + } + if (!$filter) { unset($data[$type][$slug]); }