From 01b85f19bc19c26e8f5bdc4cf749d1034b11e4dd Mon Sep 17 00:00:00 2001 From: Matias Griese Date: Thu, 16 May 2019 11:51:30 +0300 Subject: [PATCH] Fixed GPM errors from blueprints not being logged [#2505] --- CHANGELOG.md | 1 + system/src/Grav/Common/Data/Blueprints.php | 14 +++++++++-- .../Common/GPM/Common/CachedCollection.php | 13 ++++++---- system/src/Grav/Common/GPM/Common/Package.php | 4 +-- system/src/Grav/Common/GPM/GPM.php | 25 ++++++++++--------- .../GPM/Local/AbstractPackageCollection.php | 1 + system/src/Grav/Common/GPM/Local/Plugins.php | 1 + .../GPM/Remote/AbstractPackageCollection.php | 2 +- .../src/Grav/Common/GPM/Remote/GravCore.php | 6 ++--- system/src/Grav/Common/Plugins.php | 17 +++++++++++-- system/src/Grav/Common/Themes.php | 14 ++++++++++- 11 files changed, 70 insertions(+), 28 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d02bda507b..b2ff74b413 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ * Optimizations for Plugin/Theme loading 1. [](#bugfix) * Force question to install demo content in theme update [#2493](https://github.com/getgrav/grav/issues/2493) + * Fixed GPM errors from blueprints not being logged [#2505](https://github.com/getgrav/grav/issues/2505) # v1.6.9 ## 05/09/2019 diff --git a/system/src/Grav/Common/Data/Blueprints.php b/system/src/Grav/Common/Data/Blueprints.php index 0b0719d09c..1d0fcc689c 100644 --- a/system/src/Grav/Common/Data/Blueprints.php +++ b/system/src/Grav/Common/Data/Blueprints.php @@ -39,7 +39,8 @@ public function __construct($search = 'blueprints://') public function get($type) { if (!isset($this->instances[$type])) { - $this->instances[$type] = $this->loadFile($type); + $blueprint = $this->loadFile($type); + $this->instances[$type] = $blueprint; } return $this->instances[$type]; @@ -99,6 +100,15 @@ protected function loadFile($name) $blueprint->setContext($this->search); } - return $blueprint->load()->init(); + try { + $blueprint->load()->init(); + } catch (\RuntimeException $e) { + $log = Grav::instance()['log']; + $log->error(sprintf('Blueprint %s cannot be loaded: %s', $name, $e->getMessage())); + + throw $e; + } + + return $blueprint; } } diff --git a/system/src/Grav/Common/GPM/Common/CachedCollection.php b/system/src/Grav/Common/GPM/Common/CachedCollection.php index 7c769752eb..a6fca4db0d 100644 --- a/system/src/Grav/Common/GPM/Common/CachedCollection.php +++ b/system/src/Grav/Common/GPM/Common/CachedCollection.php @@ -11,19 +11,22 @@ use Grav\Common\Iterator; -class CachedCollection extends Iterator { - +class CachedCollection extends Iterator +{ protected static $cache; public function __construct($items) { parent::__construct(); + + $method = static::class . __METHOD__; + // local cache to speed things up - if (!isset(self::$cache[get_called_class() . __METHOD__])) { - self::$cache[get_called_class() . __METHOD__] = $items; + if (!isset(self::$cache[$method])) { + self::$cache[$method] = $items; } - foreach (self::$cache[get_called_class() . __METHOD__] as $name => $item) { + foreach (self::$cache[$method] as $name => $item) { $this->append([$name => $item]); } } diff --git a/system/src/Grav/Common/GPM/Common/Package.php b/system/src/Grav/Common/GPM/Common/Package.php index d205f1ee1f..40d3759128 100644 --- a/system/src/Grav/Common/GPM/Common/Package.php +++ b/system/src/Grav/Common/GPM/Common/Package.php @@ -11,8 +11,8 @@ use Grav\Common\Data\Data; -class Package { - +class Package +{ /** * @var Data */ diff --git a/system/src/Grav/Common/GPM/GPM.php b/system/src/Grav/Common/GPM/GPM.php index 94eec7d994..6c9e7b0e4a 100644 --- a/system/src/Grav/Common/GPM/GPM.php +++ b/system/src/Grav/Common/GPM/GPM.php @@ -770,7 +770,7 @@ public function getVersionOfDependencyRequiredByPackage($package_slug, $dependen * @param array $ignore_packages_list * * @return bool - * @throws \Exception + * @throws \RuntimeException */ public function checkNoOtherPackageNeedsThisDependencyInALowerVersion( $slug, @@ -793,8 +793,8 @@ public function checkNoOtherPackageNeedsThisDependencyInALowerVersion( $compatible = $this->checkNextSignificantReleasesAreCompatible($version, $other_dependency_version); if (!$compatible) { - if (!in_array($dependent_package, $ignore_packages_list)) { - throw new \Exception("Package $slug is required in an older version by package $dependent_package. This package needs a newer version, and because of this it cannot be installed. The $dependent_package package must be updated to use a newer release of $slug.", + if (!in_array($dependent_package, $ignore_packages_list, true)) { + throw new \RuntimeException("Package $slug is required in an older version by package $dependent_package. This package needs a newer version, and because of this it cannot be installed. The $dependent_package package must be updated to use a newer release of $slug.", 2); } } @@ -850,10 +850,10 @@ public function getDependencies($packages) ) { //Needs a Grav update first throw new \RuntimeException("One of the packages require PHP {$dependencies['php']}. Please update PHP to resolve this"); - } else { - unset($dependencies[$dependency_slug]); - continue; } + + unset($dependencies[$dependency_slug]); + continue; } //First, check for Grav dependency. If a dependency requires Grav > the current version, abort and tell. @@ -863,10 +863,10 @@ public function getDependencies($packages) ) { //Needs a Grav update first throw new \RuntimeException("One of the packages require Grav {$dependencies['grav']}. Please update Grav to the latest release."); - } else { - unset($dependencies[$dependency_slug]); - continue; } + + unset($dependencies[$dependency_slug]); + continue; } if ($this->isPluginInstalled($dependency_slug)) { @@ -1092,6 +1092,7 @@ public function calculateVersionNumberFromDependencyVersion($version) if ($this->versionFormatIsEqualOrHigher($version)) { return trim(substr($version, 2)); } + return $version; } @@ -1104,7 +1105,7 @@ public function calculateVersionNumberFromDependencyVersion($version) * * @return bool */ - public function versionFormatIsNextSignificantRelease($version) + public function versionFormatIsNextSignificantRelease($version): bool { return strpos($version, '~') === 0; } @@ -1118,7 +1119,7 @@ public function versionFormatIsNextSignificantRelease($version) * * @return bool */ - public function versionFormatIsEqualOrHigher($version) + public function versionFormatIsEqualOrHigher($version): bool { return strpos($version, '>=') === 0; } @@ -1136,7 +1137,7 @@ public function versionFormatIsEqualOrHigher($version) * * @return bool */ - public function checkNextSignificantReleasesAreCompatible($version1, $version2) + public function checkNextSignificantReleasesAreCompatible($version1, $version2): bool { $version1array = explode('.', $version1); $version2array = explode('.', $version2); diff --git a/system/src/Grav/Common/GPM/Local/AbstractPackageCollection.php b/system/src/Grav/Common/GPM/Local/AbstractPackageCollection.php index 9acc08385b..be565c9c96 100644 --- a/system/src/Grav/Common/GPM/Local/AbstractPackageCollection.php +++ b/system/src/Grav/Common/GPM/Local/AbstractPackageCollection.php @@ -16,6 +16,7 @@ abstract class AbstractPackageCollection extends BaseCollection public function __construct($items) { parent::__construct(); + foreach ($items as $name => $data) { $data->set('slug', $name); $this->items[$name] = new Package($data, $this->type); diff --git a/system/src/Grav/Common/GPM/Local/Plugins.php b/system/src/Grav/Common/GPM/Local/Plugins.php index 9c8e6dd132..19adfb88ea 100644 --- a/system/src/Grav/Common/GPM/Local/Plugins.php +++ b/system/src/Grav/Common/GPM/Local/Plugins.php @@ -25,6 +25,7 @@ public function __construct() { /** @var \Grav\Common\Plugins $plugins */ $plugins = Grav::instance()['plugins']; + parent::__construct($plugins->all()); } } diff --git a/system/src/Grav/Common/GPM/Remote/AbstractPackageCollection.php b/system/src/Grav/Common/GPM/Remote/AbstractPackageCollection.php index b46dc04dad..429c5b1dc5 100644 --- a/system/src/Grav/Common/GPM/Remote/AbstractPackageCollection.php +++ b/system/src/Grav/Common/GPM/Remote/AbstractPackageCollection.php @@ -43,7 +43,7 @@ public function __construct($repository = null, $refresh = false, $callback = nu { parent::__construct(); if ($repository === null) { - throw new \RuntimeException("A repository is required to indicate the origin of the remote collection"); + throw new \RuntimeException('A repository is required to indicate the origin of the remote collection'); } $channel = Grav::instance()['config']->get('system.gpm.releases', 'stable'); diff --git a/system/src/Grav/Common/GPM/Remote/GravCore.php b/system/src/Grav/Common/GPM/Remote/GravCore.php index 168f82f8ee..1d30120d02 100644 --- a/system/src/Grav/Common/GPM/Remote/GravCore.php +++ b/system/src/Grav/Common/GPM/Remote/GravCore.php @@ -37,9 +37,9 @@ public function __construct($refresh = false, $callback = null) $this->fetch($refresh, $callback); $this->data = json_decode($this->raw, true); - $this->version = isset($this->data['version']) ? $this->data['version'] : '-'; - $this->date = isset($this->data['date']) ? $this->data['date'] : '-'; - $this->min_php = isset($this->data['min_php']) ? $this->data['min_php'] : null; + $this->version = $this->data['version'] ?? '-'; + $this->date = $this->data['date'] ?? '-'; + $this->min_php = $this->data['min_php'] ?? null; if (isset($this->data['assets'])) { foreach ((array)$this->data['assets'] as $slug => $data) { diff --git a/system/src/Grav/Common/Plugins.php b/system/src/Grav/Common/Plugins.php index 87908517ab..358727f33f 100644 --- a/system/src/Grav/Common/Plugins.php +++ b/system/src/Grav/Common/Plugins.php @@ -133,12 +133,25 @@ public function add($plugin) */ public static function all() { - $plugins = Grav::instance()['plugins']; + $grav = Grav::instance(); + $plugins = $grav['plugins']; $list = []; foreach ($plugins as $instance) { $name = $instance->name; - $result = self::get($name); + + try { + $result = self::get($name); + } catch (\Exception $e) { + $exception = new \RuntimeException(sprintf('Plugin %s: %s', $name, $e->getMessage()), $e->getCode(), $e); + + /** @var Debugger $debugger */ + $debugger = $grav['debugger']; + $debugger->addMessage("Plugin {$name} cannot be loaded, please check Exceptions tab", 'error'); + $debugger->addException($exception); + + continue; + } if ($result) { $list[$name] = $result; diff --git a/system/src/Grav/Common/Themes.php b/system/src/Grav/Common/Themes.php index 2d12b3399b..6720d462dc 100644 --- a/system/src/Grav/Common/Themes.php +++ b/system/src/Grav/Common/Themes.php @@ -100,7 +100,19 @@ public function all() } $theme = $directory->getFilename(); - $result = $this->get($theme); + + try { + $result = $this->get($theme); + } catch (\Exception $e) { + $exception = new \RuntimeException(sprintf('Theme %s: %s', $theme, $e->getMessage()), $e->getCode(), $e); + + /** @var Debugger $debugger */ + $debugger = $this->grav['debugger']; + $debugger->addMessage("Theme {$theme} cannot be loaded, please check Exceptions tab", 'error'); + $debugger->addException($exception); + + continue; + } if ($result) { $list[$theme] = $result;