1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the SVN-Buddy library. |
4
|
|
|
* For the full copyright and license information, please view |
5
|
|
|
* the LICENSE file that was distributed with this source code. |
6
|
|
|
* |
7
|
|
|
* @copyright Alexander Obuhovich <[email protected]> |
8
|
|
|
* @link https://github.com/console-helpers/svn-buddy |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace ConsoleHelpers\SVNBuddy\Cache; |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Caches information about repository. |
16
|
|
|
*/ |
17
|
|
|
class FileCacheStorage implements ICacheStorage |
18
|
|
|
{ |
19
|
|
|
|
20
|
|
|
const COMPRESS_THRESHOLD = 10240; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Cache file. |
24
|
|
|
* |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
private $_file; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Creates instance of revision log cache. |
31
|
|
|
* |
32
|
|
|
* @param string $file Cache file. |
33
|
|
|
*/ |
34
|
21 |
|
public function __construct($file) |
35
|
|
|
{ |
36
|
21 |
|
$this->_file = $file; |
37
|
|
|
|
38
|
21 |
|
$parent_path = dirname($this->_file); |
39
|
|
|
|
40
|
21 |
|
if ( !file_exists($parent_path) ) { |
41
|
2 |
|
mkdir($parent_path, 0777, true); |
42
|
|
|
} |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @inheritDoc |
47
|
|
|
*/ |
48
|
2 |
|
public function getUniqueId() |
49
|
|
|
{ |
50
|
2 |
|
return $this->_file; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @inheritDoc |
55
|
|
|
* |
56
|
|
|
* @throws \RuntimeException When cache file doesn't exist. |
57
|
|
|
*/ |
58
|
1 |
|
public function getSize() |
59
|
|
|
{ |
60
|
1 |
|
if ( !file_exists($this->_file) ) { |
61
|
|
|
throw new \RuntimeException('File "' . $this->_file . '" does not exist.'); |
62
|
|
|
} |
63
|
|
|
|
64
|
1 |
|
return filesize($this->_file); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Gets information from cache. |
69
|
|
|
* |
70
|
|
|
* @return array|null |
71
|
|
|
*/ |
72
|
14 |
|
public function get() |
73
|
|
|
{ |
74
|
14 |
|
if ( !file_exists($this->_file) ) { |
75
|
4 |
|
return null; |
76
|
|
|
} |
77
|
|
|
|
78
|
11 |
|
$file_contents = file_get_contents($this->_file); |
79
|
11 |
|
$first_symbol = substr($file_contents, 0, 1); |
80
|
|
|
|
81
|
11 |
|
if ( !in_array($first_symbol, array('{', '[')) ) { |
82
|
1 |
|
$file_contents = gzuncompress($file_contents); |
83
|
|
|
} |
84
|
|
|
|
85
|
11 |
|
$cache = json_decode($file_contents, true); |
86
|
|
|
|
87
|
11 |
|
if ( $cache ) { |
88
|
10 |
|
return $cache; |
89
|
|
|
} |
90
|
|
|
|
91
|
1 |
|
return null; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Stores information in cache. |
96
|
|
|
* |
97
|
|
|
* @param array $cache Cache. |
98
|
|
|
* |
99
|
|
|
* @return void |
100
|
|
|
*/ |
101
|
15 |
|
public function set(array $cache) |
102
|
|
|
{ |
103
|
15 |
|
$file_contents = json_encode($cache); |
104
|
|
|
|
105
|
15 |
|
if ( strlen($file_contents) > self::COMPRESS_THRESHOLD ) { |
106
|
1 |
|
$file_contents = gzcompress($file_contents); |
107
|
|
|
} |
108
|
|
|
|
109
|
15 |
|
file_put_contents($this->_file, $file_contents); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Invalidates cache. |
114
|
|
|
* |
115
|
|
|
* @return void |
116
|
|
|
*/ |
117
|
10 |
|
public function invalidate() |
118
|
|
|
{ |
119
|
10 |
|
if ( file_exists($this->_file) ) { |
120
|
6 |
|
unlink($this->_file); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
} |
125
|
|
|
|