forked from composer/composer
/
Cache.php
65 lines (55 loc) · 1.35 KB
/
Cache.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<?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;
public function __construct(IOInterface $io, $cacheDir)
{
$this->io = $io;
$this->root = rtrim($cacheDir, '/\\') . '/';
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);
}
}
}