Skip to content

Commit

Permalink
Merge branch 'feature/gpm-index-enhancements' of https://github.com/j…
Browse files Browse the repository at this point in the history
…gonyea/grav into jgonyea-feature/gpm-index-enhancements
  • Loading branch information
mahagr committed Feb 8, 2021
2 parents 1347c76 + ee379a5 commit 7964028
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 5 deletions.
51 changes: 49 additions & 2 deletions system/src/Grav/Common/GPM/GPM.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,32 @@ public function getInstalledPlugins()
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
*
Expand Down Expand Up @@ -182,6 +208,28 @@ public function getInstalledThemes()
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
*
Expand Down Expand Up @@ -1023,7 +1071,6 @@ private function calculateMergedDependenciesOfPackage($packageName, $dependencie

//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
Expand Down Expand Up @@ -1059,7 +1106,7 @@ private function calculateMergedDependenciesOfPackage($packageName, $dependencie
$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);
}
Expand Down
66 changes: 63 additions & 3 deletions system/src/Grav/Console/Gpm/IndexCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,18 @@ protected function configure(): void
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 <info>index</info> command lists the plugins and themes available for installation')
;
Expand Down Expand Up @@ -129,7 +141,7 @@ protected function serve(): int
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) {
Expand All @@ -138,7 +150,8 @@ protected function serve(): int
'Name' => '<cyan>' . Utils::truncate($package->name, 20, false, ' ', '...') . '</cyan> ',
'Slug' => $slug,
'Version'=> $this->version($package),
'Installed' => $this->installed($package)
'Installed' => $this->installed($package),
'Enabled' => $this->enabled($package),
];

$table->addRow($row);
Expand Down Expand Up @@ -195,6 +208,28 @@ private function installed(Package $package): string
return !$installed ? '<magenta>not installed</magenta>' : '<cyan>installed</cyan>';
}

/**
* @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 = '<cyan>enabled</cyan>';
} elseif ($enabled === false) {
$result = '<red>disabled</red>';
}

return $result;
}

/**
* @param Packages $data
* @return Packages
Expand All @@ -210,10 +245,12 @@ public function filter(Packages $data): Packages
}

$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))) {
Expand All @@ -240,6 +277,29 @@ public function filter(Packages $data): Packages
$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]);
}
Expand Down

0 comments on commit 7964028

Please sign in to comment.