1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the php-phantomjs. |
5
|
|
|
* |
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
7
|
|
|
* file that was distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
namespace JonnyW\PhantomJs\Cache; |
10
|
|
|
|
11
|
|
|
use JonnyW\PhantomJs\Exception\NotWritableException; |
12
|
|
|
use JonnyW\PhantomJs\Exception\NotExistsException; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* PHP PhantomJs |
16
|
|
|
* |
17
|
|
|
* @author Jon Wenmoth <[email protected]> |
18
|
|
|
*/ |
19
|
|
|
class FileCache implements CacheInterface |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* Default write directory |
23
|
|
|
* |
24
|
|
|
* @var string |
25
|
|
|
* @access protected |
26
|
|
|
*/ |
27
|
|
|
protected $directory; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Default write extension |
31
|
|
|
* |
32
|
|
|
* @var string |
33
|
|
|
* @access protected |
34
|
|
|
*/ |
35
|
|
|
protected $extension; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Internal constructor. |
39
|
|
|
* |
40
|
|
|
* @access public |
41
|
|
|
* @param string $directory |
42
|
|
|
* @param string $extension |
43
|
|
|
*/ |
44
|
22 |
|
public function __construct($directory, $extension) |
45
|
|
|
{ |
46
|
22 |
|
$this->directory = rtrim($directory, DIRECTORY_SEPARATOR); |
47
|
22 |
|
$this->extension = $extension; |
48
|
22 |
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Write data to storage. |
52
|
|
|
* |
53
|
|
|
* @access public |
54
|
|
|
* @param string $id |
55
|
|
|
* @param string $data |
56
|
|
|
* @return string |
57
|
|
|
* @throws \JonnyW\PhantomJs\Exception\NotWritableException |
58
|
|
|
*/ |
59
|
45 |
|
public function save($id, $data) |
60
|
|
|
{ |
61
|
45 |
|
$file = $this->getFilename($id); |
62
|
|
|
|
63
|
44 |
|
if (!$this->isWritable($file)) { |
64
|
2 |
|
throw new NotWritableException(sprintf('File could not be written to system as target is not writable: %s', $file)); |
65
|
|
|
} |
66
|
|
|
|
67
|
42 |
|
if ($this->writeData($file, $data) === false) { |
68
|
|
|
|
69
|
|
|
$this->delete($file); |
70
|
|
|
|
71
|
|
|
throw new NotWritableException(sprintf('Data could not be written to file on system. Please make sure that file is writeable: %s', $file)); |
72
|
|
|
} |
73
|
|
|
|
74
|
42 |
|
return $file; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Fetch data from file. |
79
|
|
|
* |
80
|
|
|
* @access public |
81
|
|
|
* @param string $id |
82
|
|
|
* @return mixed|void |
83
|
|
|
* @throws \JonnyW\PhantomJs\Exception\NotExistsException |
84
|
|
|
*/ |
85
|
23 |
|
public function fetch($id) |
86
|
|
|
{ |
87
|
23 |
|
$file = $this->getFilename($id); |
88
|
|
|
|
89
|
23 |
|
if (!$this->exists($id)) { |
90
|
1 |
|
throw new NotExistsException(sprintf('Could not fetch data from file as file does not exist: %s', $file)); |
91
|
|
|
} |
92
|
|
|
|
93
|
22 |
|
return $this->readData($file); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Delete data from storage. |
98
|
|
|
* |
99
|
|
|
* @access public |
100
|
|
|
* @param string $id |
101
|
|
|
* @return void |
102
|
|
|
*/ |
103
|
38 |
|
public function delete($id) |
104
|
|
|
{ |
105
|
38 |
|
$files = glob($this->getFilename($id)); |
106
|
|
|
|
107
|
38 |
|
if (count($files)) { |
108
|
38 |
|
array_map('unlink', $files); |
109
|
|
|
} |
110
|
38 |
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Data exists in storage. |
114
|
|
|
* |
115
|
|
|
* @access public |
116
|
|
|
* @param string $id |
117
|
|
|
* @return boolean |
118
|
|
|
*/ |
119
|
33 |
|
public function exists($id) |
120
|
|
|
{ |
121
|
33 |
|
return (bool) (file_exists($this->getFilename($id))); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Is data writeable. |
126
|
|
|
* |
127
|
|
|
* @access protected |
128
|
|
|
* @param $file |
129
|
|
|
* @return boolean |
130
|
|
|
*/ |
131
|
44 |
|
protected function isWritable($file) |
132
|
|
|
{ |
133
|
44 |
|
return (bool) ((file_exists($file) && is_writable($file)) || (!file_exists($file) && is_writable(dirname($file)))); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Write data to file. |
138
|
|
|
* |
139
|
|
|
* @access protected |
140
|
|
|
* @param string $file |
141
|
|
|
* @param string $data |
142
|
|
|
* @return boolean |
143
|
|
|
*/ |
144
|
42 |
|
protected function writeData($file, $data) |
145
|
|
|
{ |
146
|
42 |
|
return file_put_contents($file, $data); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Read data from file. |
151
|
|
|
* |
152
|
|
|
* @access protected |
153
|
|
|
* @param string $file |
154
|
|
|
* @return mixed |
155
|
|
|
*/ |
156
|
22 |
|
protected function readData($file) |
157
|
|
|
{ |
158
|
22 |
|
return file_get_contents($file); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Get filename |
163
|
|
|
* |
164
|
|
|
* @access protected |
165
|
|
|
* @param string $id |
166
|
|
|
* @return string |
167
|
|
|
* @throws \JonnyW\PhantomJs\Exception\NotWritableException |
168
|
|
|
*/ |
169
|
48 |
|
protected function getFileName($id) |
170
|
|
|
{ |
171
|
48 |
|
if (is_dir($id)) { |
172
|
1 |
|
return sprintf('%1$s/%2$s.%3$s', rtrim($id, DIRECTORY_SEPARATOR), uniqid(), $this->extension); |
173
|
|
|
} |
174
|
|
|
|
175
|
47 |
|
$dirName = dirname($id); |
176
|
|
|
|
177
|
47 |
|
if (!file_exists($id) && $dirName === '.') { |
178
|
45 |
|
return sprintf('%1$s/%2$s', $this->directory, $id); |
179
|
|
|
} |
180
|
|
|
|
181
|
38 |
|
if (!file_exists($id) && !is_writable($dirName)) { |
182
|
1 |
|
throw new NotWritableException(sprintf('File could not be written to system as target is not writable: %s', $id)); |
183
|
|
|
} |
184
|
|
|
|
185
|
37 |
|
return $id; |
186
|
|
|
} |
187
|
|
|
} |
188
|
|
|
|