"Fossies" - the Fresh Open Source Software Archive

Member "grav/system/src/Grav/Common/GPM/Remote/AbstractPackageCollection.php" (8 Jun 2020, 2399 Bytes) of package /linux/www/grav-v1.6.26.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "AbstractPackageCollection.php" see the Fossies "Dox" file reference documentation.

    1 <?php
    2 
    3 /**
    4  * @package    Grav\Common\GPM
    5  *
    6  * @copyright  Copyright (C) 2015 - 2019 Trilby Media, LLC. All rights reserved.
    7  * @license    MIT License; see LICENSE file for details.
    8  */
    9 
   10 namespace Grav\Common\GPM\Remote;
   11 
   12 use Grav\Common\Grav;
   13 use Grav\Common\GPM\Common\AbstractPackageCollection as BaseCollection;
   14 use Grav\Common\GPM\Response;
   15 use \Doctrine\Common\Cache\FilesystemCache;
   16 
   17 class AbstractPackageCollection extends BaseCollection
   18 {
   19     /**
   20      * The cached data previously fetched
   21      * @var string
   22      */
   23     protected $raw;
   24 
   25     /**
   26      * The lifetime to store the entry in seconds
   27      * @var int
   28      */
   29     private $lifetime = 86400;
   30 
   31     protected $repository;
   32 
   33     protected $cache;
   34 
   35     /**
   36      * AbstractPackageCollection constructor.
   37      *
   38      * @param null $repository
   39      * @param bool $refresh
   40      * @param null $callback
   41      */
   42     public function __construct($repository = null, $refresh = false, $callback = null)
   43     {
   44         parent::__construct();
   45         if ($repository === null) {
   46             throw new \RuntimeException('A repository is required to indicate the origin of the remote collection');
   47         }
   48 
   49         $channel = Grav::instance()['config']->get('system.gpm.releases', 'stable');
   50         $cache_dir = Grav::instance()['locator']->findResource('cache://gpm', true, true);
   51         $this->cache = new FilesystemCache($cache_dir);
   52 
   53         $this->repository = $repository . '?v=' . GRAV_VERSION . '&' . $channel . '=1';
   54         $this->raw        = $this->cache->fetch(md5($this->repository));
   55 
   56         $this->fetch($refresh, $callback);
   57         foreach (json_decode($this->raw, true) as $slug => $data) {
   58             // Temporarily fix for using multisites
   59             if (isset($data['install_path'])) {
   60                 $path = preg_replace('~^user/~i', 'user://', $data['install_path']);
   61                 $data['install_path'] = Grav::instance()['locator']->findResource($path, false, true);
   62             }
   63             $this->items[$slug] = new Package($data, $this->type);
   64         }
   65     }
   66 
   67     public function fetch($refresh = false, $callback = null)
   68     {
   69         if (!$this->raw || $refresh) {
   70             $response  = Response::get($this->repository, [], $callback);
   71             $this->raw = $response;
   72             $this->cache->save(md5($this->repository), $this->raw, $this->lifetime);
   73         }
   74 
   75         return $this->raw;
   76     }
   77 }