forked from KnpLabs/Gaufrette
/
Local.php
253 lines (218 loc) · 6.35 KB
/
Local.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
<?php
namespace Gaufrette\Adapter;
use Gaufrette\Checksum;
use Gaufrette\Path;
use Gaufrette\Filesystem;
use Gaufrette\FileStream;
/**
* Adapter for the local filesystem
*
* @author Antoine Hérault <antoine.herault@gmail.com>
*/
class Local extends Base
{
protected $directory;
/**
* Constructor
*
* @param string $directory Directory where the filesystem is located
* @param boolean $create Whether to create the directory if it does not
* exist (default FALSE)
*
* @throws RuntimeException if the specified directory does not exist and
* could not be created
*/
public function __construct($directory, $create = false)
{
$this->directory = $this->normalizePath($directory);
if (is_link($this->directory)) {
$this->directory = readlink($this->directory);
}
$this->ensureDirectoryExists($this->directory, $create);
}
/**
* {@inheritDoc}
*/
public function read($key)
{
$content = file_get_contents($this->computePath($key));
if (false === $content) {
throw new \RuntimeException(sprintf('Could not read the \'%s\' file.', $key));
}
return $content;
}
/**
* {@inheritDoc}
*/
public function write($key, $content, array $metadata = null)
{
$path = $this->computePath($key);
$this->ensureDirectoryExists(dirname($path), true);
$numBytes = file_put_contents($this->computePath($key), $content);
if (false === $numBytes) {
throw new \RuntimeException(sprintf('Could not write the \'%s\' file.', $key));
}
return $numBytes;
}
/**
* {@inheritDoc}
*/
public function rename($key, $new)
{
if (!rename($this->computePath($key), $this->computePath($new))) {
throw new \RuntimeException(sprintf('Could not rename the \'%s\' file to \'%s\'.', $key, $new));
}
}
/**
* {@inheritDoc}
*/
public function exists($key)
{
return is_file($this->computePath($key));
}
/**
* {@inheritDoc}
*/
public function keys($prefix = null)
{
if (null !== $prefix) {
throw new \BadMethodCallException("Usage of prefix filter not implemented yet.");
}
$iterator = new \RecursiveIteratorIterator(
new \RecursiveDirectoryIterator(
$this->directory,
\FilesystemIterator::SKIP_DOTS | \FilesystemIterator::UNIX_PATHS
)
);
$files = iterator_to_array($iterator);
$self = $this;
return array_values(
array_map(
function($file) use ($self) {
return $self->computeKey(strval($file));
},
$files
)
);
}
/**
* {@inheritDoc}
*/
public function mtime($key)
{
return filemtime($this->computePath($key));
}
/**
* {@inheritDoc}
*/
public function checksum($key)
{
return Checksum::fromFile($this->computePath($key));
}
/**
* {@inheritDoc}
*/
public function delete($key)
{
if (!unlink($this->computePath($key))) {
throw new \RuntimeException(sprintf('Could not remove the \'%s\' file.', $key));
}
}
/**
* Computes the path from the specified key
*
* @param string $key The key which for to compute the path
*
* @return string A path
*
* @throws OutOfBoundsException If the computed path is out of the
* directory
*/
public function computePath($key)
{
$path = $this->normalizePath($this->directory . '/' . $key);
if (0 !== strpos($path, $this->directory)) {
throw new \OutOfBoundsException(sprintf('The file \'%s\' is out of the filesystem.', $key));
}
return $path;
}
/**
* Normalizes the given path
*
* @param string $path
*
* @return string
*/
public function normalizePath($path)
{
return Path::normalize($path);
}
/**
* Computes the key from the specified path
*
* @param string $path
*
* return string
*/
public function computeKey($path)
{
$path = $this->normalizePath($path);
if (0 !== strpos($path, $this->directory)) {
throw new \OutOfBoundsException(sprintf('The path \'%s\' is out of the filesystem.', $path));
}
return ltrim(substr($path, strlen($this->directory)), '/');
}
/**
* Ensures the specified directory exists, creates it if it does not
*
* @param string $directory Path of the directory to test
* @param boolean $create Whether to create the directory if it does
* not exist
*
* @throws RuntimeException if the directory does not exists and could not
* be created
*/
public function ensureDirectoryExists($directory, $create = false)
{
if (!is_dir($directory)) {
if (!$create) {
throw new \RuntimeException(sprintf('The directory \'%s\' does not exist.', $directory));
}
$this->createDirectory($directory);
}
}
/**
* Creates the specified directory and its parents
*
* @param string $directory Path of the directory to create
*
* @throws InvalidArgumentException if the directory already exists
* @throws RuntimeException if the directory could not be created
*/
public function createDirectory($directory)
{
if (is_dir($directory)) {
throw new \InvalidArgumentException(sprintf('The directory \'%s\' already exists.', $directory));
}
$umask = umask(0);
$created = mkdir($directory, 0777, true);
umask($umask);
if (!$created) {
throw new \RuntimeException(sprintf('The directory \'%s\' could not be created.', $directory));
}
}
/**
* {@inheritDoc}
*/
public function supportsMetadata()
{
return false;
}
/**
* {@inheritDoc}
*/
public function createFileStream($key, Filesystem $filesystem)
{
return new FileStream\Local($this->computePath($key));
}
}