From 0d97ec4783736583d2fdbe390761d20681389f28 Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Fri, 6 Apr 2012 22:39:43 +0200 Subject: [PATCH] Add cache to the composer repositories --- src/Composer/Cache.php | 70 +++++++++++++++++++ .../Repository/ComposerRepository.php | 28 ++++++-- 2 files changed, 94 insertions(+), 4 deletions(-) create mode 100644 src/Composer/Cache.php diff --git a/src/Composer/Cache.php b/src/Composer/Cache.php new file mode 100644 index 000000000..1b0ad4770 --- /dev/null +++ b/src/Composer/Cache.php @@ -0,0 +1,70 @@ + + * Jordi Boggiano + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Composer; + +use Composer\IO\IOInterface; + +/** + * Reads/writes to a filesystem cache + * + * @author Jordi Boggiano + */ +class Cache +{ + private $io; + private $root; + private $enabled = true; + + public function __construct(IOInterface $io, $cacheKey = null) + { + $this->io = $io; + + if (defined('PHP_WINDOWS_VERSION_MAJOR')) { + $this->root = getenv('APPDATA') . rtrim('/Composer/cache/' . $cacheKey, '/') . '/'; + } else { + $this->root = getenv('HOME') . rtrim('/.composer/cache/' . $cacheKey, '/') . '/'; + } + + if (!is_dir($this->root)) { + if (!@mkdir($this->root, 0777, true)) { + $this->enabled = false; + } + } + } + + public function getRoot() + { + return $this->root; + } + + public function read($file) + { + if ($this->enabled && file_exists($this->root . $file)) { + return file_get_contents($this->root . $file); + } + } + + public function write($file, $contents) + { + if ($this->enabled) { + file_put_contents($this->root . $file, $contents); + } + } + + public function sha1($file) + { + if ($this->enabled && file_exists($this->root . $file)) { + return sha1_file($this->root . $file); + } + } +} diff --git a/src/Composer/Repository/ComposerRepository.php b/src/Composer/Repository/ComposerRepository.php index 055c25f81..450dfdf33 100644 --- a/src/Composer/Repository/ComposerRepository.php +++ b/src/Composer/Repository/ComposerRepository.php @@ -15,6 +15,7 @@ namespace Composer\Repository; use Composer\Package\Loader\ArrayLoader; use Composer\Package\LinkConstraint\VersionConstraint; use Composer\Json\JsonFile; +use Composer\Cache; use Composer\IO\IOInterface; use Composer\Util\RemoteFilesystem; @@ -26,6 +27,7 @@ class ComposerRepository extends ArrayRepository protected $url; protected $io; protected $packages; + protected $cache; public function __construct(array $config, IOInterface $io) { @@ -40,13 +42,25 @@ class ComposerRepository extends ArrayRepository $this->url = $config['url']; $this->io = $io; + $this->cache = new Cache($io, preg_replace('{[^a-z0-9.]}', '-', $this->url)); } protected function initialize() { parent::initialize(); - $json = new JsonFile($this->url.'/packages.json', new RemoteFilesystem($this->io)); - $data = $json->read(); + + try { + $json = new JsonFile($this->url.'/packages.json', new RemoteFilesystem($this->io)); + $data = $json->read(); + $this->cache->write('packages.json', json_encode($data)); + } catch (\Exception $e) { + if ($contents = $this->cache->read('packages.json')) { + $this->io->write(''.$this->url.' could not be loaded, package information was loaded from the local cache and may be out of date'); + $data = json_decode($contents, true); + } else { + throw $e; + } + } $loader = new ArrayLoader(); $this->loadRepository($loader, $data); @@ -75,8 +89,14 @@ class ComposerRepository extends ArrayRepository if (isset($data['includes'])) { foreach ($data['includes'] as $include => $metadata) { - $json = new JsonFile($this->url.'/'.$include, new RemoteFilesystem($this->io)); - $this->loadRepository($loader, $json->read()); + if ($this->cache->sha1($include) === $metadata['sha1']) { + $includedData = json_decode($this->cache->read($include), true); + } else { + $json = new JsonFile($this->url.'/'.$include, new RemoteFilesystem($this->io)); + $includedData = $json->read(); + $this->cache->write($include, json_encode($includedData)); + } + $this->loadRepository($loader, $includedData); } } }