2012-04-06 20:39:43 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of Composer.
|
|
|
|
*
|
|
|
|
* (c) Nils Adermann <naderman@naderman.de>
|
|
|
|
* Jordi Boggiano <j.boggiano@seld.be>
|
|
|
|
*
|
|
|
|
* 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 <j.boggiano@seld.be>
|
|
|
|
*/
|
|
|
|
class Cache
|
|
|
|
{
|
|
|
|
private $io;
|
|
|
|
private $root;
|
|
|
|
private $enabled = true;
|
|
|
|
|
2012-04-09 14:36:06 +00:00
|
|
|
public function __construct(IOInterface $io, $cacheDir)
|
2012-04-06 20:39:43 +00:00
|
|
|
{
|
|
|
|
$this->io = $io;
|
2012-04-09 14:36:06 +00:00
|
|
|
$this->root = rtrim($cacheDir, '/\\') . '/';
|
2012-04-06 20:39:43 +00:00
|
|
|
|
|
|
|
if (!is_dir($this->root)) {
|
|
|
|
if (!@mkdir($this->root, 0777, true)) {
|
|
|
|
$this->enabled = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getRoot()
|
|
|
|
{
|
|
|
|
return $this->root;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function read($file)
|
|
|
|
{
|
2012-06-18 13:34:08 +00:00
|
|
|
$file = preg_replace('{[^a-z0-9.]}i', '-', $file);
|
2012-04-06 20:39:43 +00:00
|
|
|
if ($this->enabled && file_exists($this->root . $file)) {
|
|
|
|
return file_get_contents($this->root . $file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function write($file, $contents)
|
|
|
|
{
|
|
|
|
if ($this->enabled) {
|
2012-06-18 13:34:08 +00:00
|
|
|
$file = preg_replace('{[^a-z0-9.]}i', '-', $file);
|
2012-04-06 20:39:43 +00:00
|
|
|
file_put_contents($this->root . $file, $contents);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function sha1($file)
|
|
|
|
{
|
2012-06-18 13:34:08 +00:00
|
|
|
$file = preg_replace('{[^a-z0-9.]}i', '-', $file);
|
2012-04-06 20:39:43 +00:00
|
|
|
if ($this->enabled && file_exists($this->root . $file)) {
|
|
|
|
return sha1_file($this->root . $file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|