From 9c541ee20b36b79079f01251ed12c044c2958804 Mon Sep 17 00:00:00 2001 From: Gert Date: Sat, 4 Apr 2015 14:00:52 +0200 Subject: [PATCH 1/3] add filter function to iterator --- system/src/Grav/Common/Iterator.php | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/system/src/Grav/Common/Iterator.php b/system/src/Grav/Common/Iterator.php index 9105c0630..c9b0ee776 100644 --- a/system/src/Grav/Common/Iterator.php +++ b/system/src/Grav/Common/Iterator.php @@ -197,4 +197,23 @@ class Iterator implements \ArrayAccess, \Iterator, \Countable, \Serializable return $this; } + + /** + * Filter elements from the list + * @param callable|null $callback A function the receives ($value, $key) and must return a boolean to indicate filter status + * @return $this + */ + public function filter(callable $callback = null) + { + foreach ($this->items as $key => $value) { + if ( + ($callback && !call_user_func($callback, $value, $key)) || + (!$callback && !(bool) $value) + ) { + unset($this->items[$key]); + } + } + + return $this; + } } From d92d9bafc62257cdedde6b78e0632988bbd3ffec Mon Sep 17 00:00:00 2001 From: Gert Date: Sat, 4 Apr 2015 14:01:46 +0200 Subject: [PATCH 2/3] refactor Common/GPM namespace for more unified approach to installed vs remote packages --- .../Collection.php => AbstractCollection.php} | 14 ++-- .../GPM/Common/AbstractPackageCollection.php | 34 ++++++++ .../Common/GPM/Common/CachedCollection.php | 21 +++++ system/src/Grav/Common/GPM/Common/Package.php | 42 ++++++++++ .../GPM/Local/AbstractPackageCollection.php | 16 ++++ system/src/Grav/Common/GPM/Local/Package.php | 84 ++++--------------- system/src/Grav/Common/GPM/Local/Packages.php | 25 ++---- system/src/Grav/Common/GPM/Local/Plugins.php | 10 +-- system/src/Grav/Common/GPM/Local/Themes.php | 21 +++-- .../src/Grav/Common/GPM/PackageInterface.php | 58 +++++++++++++ ...tion.php => AbstractPackageCollection.php} | 43 +++------- system/src/Grav/Common/GPM/Remote/Grav.php | 18 ++-- system/src/Grav/Common/GPM/Remote/Package.php | 32 ++----- .../src/Grav/Common/GPM/Remote/Packages.php | 25 ++---- system/src/Grav/Common/GPM/Remote/Plugins.php | 27 +++--- system/src/Grav/Common/GPM/Remote/Themes.php | 27 +++--- system/src/Grav/Common/GPM/Response.php | 1 - 17 files changed, 283 insertions(+), 215 deletions(-) rename system/src/Grav/Common/GPM/{Local/Collection.php => AbstractCollection.php} (50%) create mode 100644 system/src/Grav/Common/GPM/Common/AbstractPackageCollection.php create mode 100644 system/src/Grav/Common/GPM/Common/CachedCollection.php create mode 100644 system/src/Grav/Common/GPM/Common/Package.php create mode 100644 system/src/Grav/Common/GPM/Local/AbstractPackageCollection.php create mode 100644 system/src/Grav/Common/GPM/PackageInterface.php rename system/src/Grav/Common/GPM/Remote/{Collection.php => AbstractPackageCollection.php} (61%) diff --git a/system/src/Grav/Common/GPM/Local/Collection.php b/system/src/Grav/Common/GPM/AbstractCollection.php similarity index 50% rename from system/src/Grav/Common/GPM/Local/Collection.php rename to system/src/Grav/Common/GPM/AbstractCollection.php index 9e3cf3638..7c1974d22 100644 --- a/system/src/Grav/Common/GPM/Local/Collection.php +++ b/system/src/Grav/Common/GPM/AbstractCollection.php @@ -1,19 +1,19 @@ items as $name => $theme) { - $items[$name] = $theme->toArray(); + foreach ($this->items as $name => $package) { + $items[$name] = $package->toArray(); } return json_encode($items); @@ -23,8 +23,8 @@ class Collection extends Iterator { $items = []; - foreach ($this->items as $name => $theme) { - $items[$name] = $theme->toArray(); + foreach ($this->items as $name => $package) { + $items[$name] = $package->toArray(); } return $items; diff --git a/system/src/Grav/Common/GPM/Common/AbstractPackageCollection.php b/system/src/Grav/Common/GPM/Common/AbstractPackageCollection.php new file mode 100644 index 000000000..46c9e894a --- /dev/null +++ b/system/src/Grav/Common/GPM/Common/AbstractPackageCollection.php @@ -0,0 +1,34 @@ +items as $name => $package) { + $items[$name] = $package->toArray(); + } + + return json_encode($items); + } + + public function toArray() + { + $items = []; + + foreach ($this->items as $name => $package) { + $items[$name] = $package->toArray(); + } + + return $items; + } +} diff --git a/system/src/Grav/Common/GPM/Common/CachedCollection.php b/system/src/Grav/Common/GPM/Common/CachedCollection.php new file mode 100644 index 000000000..fd6ae9420 --- /dev/null +++ b/system/src/Grav/Common/GPM/Common/CachedCollection.php @@ -0,0 +1,21 @@ + $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 new file mode 100644 index 000000000..ff9a087c9 --- /dev/null +++ b/system/src/Grav/Common/GPM/Common/Package.php @@ -0,0 +1,42 @@ +data = $package; + + if ($type) { + $this->data->set('package_type', $type); + } + } + + public function getData() { + return $this->data; + } + + public function __get($key) { + return $this->data->get($key); + } + + public function __isset($key) { + return isset($this->data->$key); + } + + public function __toString() { + return $this->toJson(); + } + + public function toJson() { + return $this->data->toJson(); + } + + public function toArray() { + return $this->data->toArray(); + } + +} diff --git a/system/src/Grav/Common/GPM/Local/AbstractPackageCollection.php b/system/src/Grav/Common/GPM/Local/AbstractPackageCollection.php new file mode 100644 index 000000000..314829378 --- /dev/null +++ b/system/src/Grav/Common/GPM/Local/AbstractPackageCollection.php @@ -0,0 +1,16 @@ + $data) { + $this->items[$name] = new Package($data, $this->type); + } + } +} diff --git a/system/src/Grav/Common/GPM/Local/Package.php b/system/src/Grav/Common/GPM/Local/Package.php index 4ba7bc0b4..6af4a4e1a 100644 --- a/system/src/Grav/Common/GPM/Local/Package.php +++ b/system/src/Grav/Common/GPM/Local/Package.php @@ -2,39 +2,24 @@ namespace Grav\Common\GPM\Local; use Grav\Common\Data\Data; +use Grav\Common\GPM\Common\Package as BasePackage; -/** - * Class Package - * @package Grav\Common\GPM\Local - */ -class Package +class Package extends BasePackage { - /** - * @var Data - */ - protected $data; - /** - * @var \Grav\Common\Data\Blueprint - */ - protected $blueprints; + protected $settings; - /** - * @param Data $package - * @param bool $package_type - */ - public function __construct(Data $package, $package_type = false) + public function __construct(Data $package, $package_type = null) { - $this->data = $package; - $this->blueprints = $this->data->blueprints(); + $data = new Data($package->blueprints()->toArray()); + parent::__construct($data, $package_type); - if ($package_type) { - $html_description = \Parsedown::instance()->line($this->blueprints->get('description')); - $this->blueprints->set('package_type', $package_type); - $this->blueprints->set('slug', $this->blueprints->name); - $this->blueprints->set('description_html', $html_description); - $this->blueprints->set('description_plain', strip_tags($html_description)); - $this->blueprints->set('symlink', is_link(USER_DIR . $package_type . DS . $this->blueprints->name)); - } + $this->settings = $package->toArray(); + + $html_description = \Parsedown::instance()->line($this->description); + $this->data->set('slug', $this->name); + $this->data->set('description_html', $html_description); + $this->data->set('description_plain', strip_tags($html_description)); + $this->data->set('symlink', is_link(USER_DIR . $package_type . DS . $this->name)); } /** @@ -42,47 +27,6 @@ class Package */ public function isEnabled() { - return $this->data['enabled']; - } - - /** - * @return Data - */ - public function getData() - { - return $this->data; - } - - /** - * @param $key - * @return mixed - */ - public function __get($key) - { - return $this->blueprints->get($key); - } - - /** - * @return string - */ - public function __toString() - { - return $this->toJson(); - } - - /** - * @return string - */ - public function toJson() - { - return $this->blueprints->toJson(); - } - - /** - * @return array - */ - public function toArray() - { - return $this->blueprints->toArray(); + return $this->settings['enabled']; } } diff --git a/system/src/Grav/Common/GPM/Local/Packages.php b/system/src/Grav/Common/GPM/Local/Packages.php index 4498e6895..a8ed6a455 100644 --- a/system/src/Grav/Common/GPM/Local/Packages.php +++ b/system/src/Grav/Common/GPM/Local/Packages.php @@ -1,28 +1,17 @@ new Plugins(), - 'themes' => new Themes() - ]; - } + $items = [ + 'plugins' => new Plugins(), + 'themes' => new Themes() + ]; - $this->plugins = self::$cache[__METHOD__]['plugins']; - $this->themes = self::$cache[__METHOD__]['themes']; - - $this->append(['plugins' => $this->plugins]); - $this->append(['themes' => $this->themes]); + parent::__construct($items); } } diff --git a/system/src/Grav/Common/GPM/Local/Plugins.php b/system/src/Grav/Common/GPM/Local/Plugins.php index b52efea93..b1c3c64a7 100644 --- a/system/src/Grav/Common/GPM/Local/Plugins.php +++ b/system/src/Grav/Common/GPM/Local/Plugins.php @@ -5,22 +5,18 @@ namespace Grav\Common\GPM\Local; * Class Plugins * @package Grav\Common\GPM\Local */ -class Plugins extends Collection +class Plugins extends AbstractPackageCollection { /** * @var string */ - private $type = 'plugins'; + protected $type = 'plugins'; /** * Local Plugins Constructor */ public function __construct() { - $grav = self::getGrav(); - - foreach ($grav['plugins']->all() as $name => $data) { - $this->items[$name] = new Package($data, $this->type); - } + parent::__construct(self::getGrav()['plugins']->all()); } } diff --git a/system/src/Grav/Common/GPM/Local/Themes.php b/system/src/Grav/Common/GPM/Local/Themes.php index 673490144..64d23b903 100644 --- a/system/src/Grav/Common/GPM/Local/Themes.php +++ b/system/src/Grav/Common/GPM/Local/Themes.php @@ -1,15 +1,22 @@ all() as $name => $data) { - $this->items[$name] = new Package($data, $this->type); - } + parent::__construct(self::getGrav()['themes']->all()); } } diff --git a/system/src/Grav/Common/GPM/PackageInterface.php b/system/src/Grav/Common/GPM/PackageInterface.php new file mode 100644 index 000000000..ad856c08f --- /dev/null +++ b/system/src/Grav/Common/GPM/PackageInterface.php @@ -0,0 +1,58 @@ +findResource('cache://gpm', true, true); @@ -35,28 +35,11 @@ class Collection extends Iterator { $this->repository = $repository; $this->raw = $this->cache->fetch(md5($this->repository)); - } - public function toJson() - { - $items = []; - - foreach ($this->items as $name => $theme) { - $items[$name] = $theme->toArray(); + $this->fetch($refresh, $callback); + foreach (json_decode($this->raw, true) as $slug => $data) { + $this->items[$slug] = new Package($data, $this->type); } - - return json_encode($items); - } - - public function toArray() - { - $items = []; - - foreach ($this->items as $name => $theme) { - $items[$name] = $theme->toArray(); - } - - return $items; } public function fetch($refresh = false, $callback = null) diff --git a/system/src/Grav/Common/GPM/Remote/Grav.php b/system/src/Grav/Common/GPM/Remote/Grav.php index 8e6510060..ed5582d57 100644 --- a/system/src/Grav/Common/GPM/Remote/Grav.php +++ b/system/src/Grav/Common/GPM/Remote/Grav.php @@ -1,9 +1,11 @@ repository); + $cache_dir = self::getGrav()['locator']->findResource('cache://gpm', true, true); + $this->cache = new FilesystemCache($cache_dir); + $this->raw = $this->cache->fetch(md5($this->repository)); $this->fetch($refresh, $callback); - $this->data = json_decode($this->raw); - $this->version = @$this->data->version ?: '-'; - $this->date = @$this->data->date ?: '-'; + $this->data = json_decode($this->raw, true); + $this->version = @$this->data['version'] ?: '-'; + $this->date = @$this->data['date'] ?: '-'; - foreach ($this->data->assets as $slug => $data) { + foreach ($this->data['assets'] as $slug => $data) { $this->items[$slug] = new Package($data); } } diff --git a/system/src/Grav/Common/GPM/Remote/Package.php b/system/src/Grav/Common/GPM/Remote/Package.php index 45da29478..9b0367f66 100644 --- a/system/src/Grav/Common/GPM/Remote/Package.php +++ b/system/src/Grav/Common/GPM/Remote/Package.php @@ -1,32 +1,12 @@ data = $package; - if ($package_type) { - $this->data->package_type = $package_type; - } - } +use Grav\Common\Data\Data; +use Grav\Common\GPM\Common\Package as BasePackage; - public function getData() { - return $this->data; +class Package extends BasePackage { + public function __construct($package, $package_type = null) { + $data = new Data($package); + parent::__construct($data, $package_type); } - - public function __get($key) { - return $this->data->$key; - } - - public function __toString() { - return $this->toJson(); - } - - public function toJson() { - return json_encode($this->data); - } - - public function toArray() { - return $this->data; - } - } diff --git a/system/src/Grav/Common/GPM/Remote/Packages.php b/system/src/Grav/Common/GPM/Remote/Packages.php index f78e9c67e..75f397fa3 100644 --- a/system/src/Grav/Common/GPM/Remote/Packages.php +++ b/system/src/Grav/Common/GPM/Remote/Packages.php @@ -1,28 +1,17 @@ new Plugins($refresh, $callback), - 'themes' => new Themes($refresh, $callback) - ]; - } + $items = [ + 'plugins' => new Plugins($refresh, $callback), + 'themes' => new Themes($refresh, $callback) + ]; - $this->plugins = self::$cache[__METHOD__]['plugins']->toArray(); - $this->themes = self::$cache[__METHOD__]['themes']->toArray(); - - $this->append(['plugins' => $this->plugins]); - $this->append(['themes' => $this->themes]); + parent::__construct($items); } } diff --git a/system/src/Grav/Common/GPM/Remote/Plugins.php b/system/src/Grav/Common/GPM/Remote/Plugins.php index fa71fbab6..ec6d64521 100644 --- a/system/src/Grav/Common/GPM/Remote/Plugins.php +++ b/system/src/Grav/Common/GPM/Remote/Plugins.php @@ -1,21 +1,24 @@ repository); - - $this->fetch($refresh, $callback); - $this->data = json_decode($this->raw); - - foreach ($this->data as $slug => $data) { - $this->items[$slug] = new Package($data, $this->type); - } + parent::__construct($this->repository, $refresh, $callback); } } diff --git a/system/src/Grav/Common/GPM/Remote/Themes.php b/system/src/Grav/Common/GPM/Remote/Themes.php index fcb5a34c4..759b7e10a 100644 --- a/system/src/Grav/Common/GPM/Remote/Themes.php +++ b/system/src/Grav/Common/GPM/Remote/Themes.php @@ -1,21 +1,24 @@ repository); - - $this->fetch($refresh, $callback); - $this->data = json_decode($this->raw); - - foreach ($this->data as $slug => $data) { - $this->items[$slug] = new Package($data, $this->type); - } + parent::__construct($this->repository, $refresh, $callback); } } diff --git a/system/src/Grav/Common/GPM/Response.php b/system/src/Grav/Common/GPM/Response.php index 211b2a415..530415870 100644 --- a/system/src/Grav/Common/GPM/Response.php +++ b/system/src/Grav/Common/GPM/Response.php @@ -78,7 +78,6 @@ class Response $method = 'get' . ucfirst(strtolower(self::$method)); self::$callback = $callback; - return static::$method($uri, $options, $callback); } From 82bd54eb0bbeabc65d682e9cce91ced01c15c861 Mon Sep 17 00:00:00 2001 From: Gert Date: Sat, 4 Apr 2015 14:02:28 +0200 Subject: [PATCH 3/3] update Console/GPM for refactored Common/GPM --- system/src/Grav/Console/Gpm/InfoCommand.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/system/src/Grav/Console/Gpm/InfoCommand.php b/system/src/Grav/Console/Gpm/InfoCommand.php index 721583032..53290b8ec 100644 --- a/system/src/Grav/Console/Gpm/InfoCommand.php +++ b/system/src/Grav/Console/Gpm/InfoCommand.php @@ -78,12 +78,12 @@ class InfoCommand extends Command $this->output->writeln(''); $packageURL = ''; - if (isset($foundPackage->author->url)) { - $packageURL = '<' . $foundPackage->author->url . '>'; + if (isset($foundPackage->author['url'])) { + $packageURL = '<' . $foundPackage->author['url'] . '>'; } $this->output->writeln("" . str_pad("Author", - 12) . ": " . $foundPackage->author->name . ' <' . $foundPackage->author->email . '> ' . $packageURL); + 12) . ": " . $foundPackage->author['name'] . ' <' . $foundPackage->author['email'] . '> ' . $packageURL); foreach (array( 'version',