|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* This file is part of the php-vfs package. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) Michael Donat <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace VirtualFileSystem; |
|
12
|
|
|
|
|
13
|
|
|
use VirtualFileSystem\Exception\NotDirectoryException; |
|
14
|
|
|
use VirtualFileSystem\Exception\NotFileException; |
|
15
|
|
|
use VirtualFileSystem\Exception\NotFoundException; |
|
16
|
|
|
use VirtualFileSystem\Structure\Directory; |
|
17
|
|
|
use VirtualFileSystem\Structure\File; |
|
18
|
|
|
use VirtualFileSystem\Structure\Root; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Class to hold the filesystem structure as object representation. It also provides access and factory methods for |
|
22
|
|
|
* file system management. |
|
23
|
|
|
* |
|
24
|
|
|
* An instance of Container is registered as a default stream options when FileSystem class is instantiated - it is |
|
25
|
|
|
* later used by streamWrapper implementation to interact with underlying object representation. |
|
26
|
|
|
* |
|
27
|
|
|
* @author Michael Donat <[email protected]> |
|
28
|
|
|
* @package php-vfs |
|
29
|
|
|
* @api |
|
30
|
|
|
*/ |
|
31
|
|
|
class Container |
|
32
|
|
|
{ |
|
33
|
|
|
/** |
|
34
|
|
|
* @var Root |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $root; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var Factory |
|
40
|
|
|
*/ |
|
41
|
|
|
protected $factory; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @var Wrapper\PermissionHelper |
|
45
|
|
|
*/ |
|
46
|
|
|
protected $permissionHelper; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Class constructor. Sets factory and root object on init. |
|
50
|
|
|
* |
|
51
|
|
|
* @param Factory $factory |
|
52
|
|
|
*/ |
|
53
|
|
|
public function __construct(Factory $factory) |
|
54
|
|
|
{ |
|
55
|
|
|
$this->setFactory($factory); |
|
56
|
|
|
$this->root = $this->factory()->getRoot(); |
|
57
|
|
|
$this->setPermissionHelper(new Wrapper\PermissionHelper()); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Sets Factory instance |
|
62
|
|
|
* |
|
63
|
|
|
* @param \VirtualFileSystem\Factory $factory |
|
64
|
|
|
*/ |
|
65
|
|
|
public function setFactory($factory) |
|
66
|
|
|
{ |
|
67
|
|
|
$this->factory = $factory; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Returns Factory instance |
|
72
|
|
|
* |
|
73
|
|
|
* @return \VirtualFileSystem\Factory |
|
74
|
|
|
*/ |
|
75
|
|
|
public function factory() |
|
76
|
|
|
{ |
|
77
|
|
|
return $this->factory; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Returns Root instance |
|
82
|
|
|
* |
|
83
|
|
|
* @return Root |
|
84
|
|
|
*/ |
|
85
|
|
|
public function root() |
|
86
|
|
|
{ |
|
87
|
|
|
return $this->root; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Returns filesystem Node|Directory|File|Root at given path. |
|
92
|
|
|
* |
|
93
|
|
|
* @param string $path |
|
94
|
|
|
* |
|
95
|
|
|
* @return Structure\Node |
|
96
|
|
|
* |
|
97
|
|
|
* @throws NotFoundException |
|
98
|
|
|
*/ |
|
99
|
|
|
public function nodeAt($path) |
|
100
|
|
|
{ |
|
101
|
|
|
$pathParts = array_filter(explode('/', str_replace('\\', '/', $path)), 'strlen'); |
|
102
|
|
|
|
|
103
|
|
|
$node = $this->root(); |
|
104
|
|
|
|
|
105
|
|
|
foreach ($pathParts as $level) { |
|
106
|
|
|
if ($node instanceof File) { |
|
107
|
|
|
throw new NotFoundException(); |
|
108
|
|
|
} |
|
109
|
|
|
$node = $node->childAt($level); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
return $node; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Checks whether filesystem has Node at given path |
|
117
|
|
|
* |
|
118
|
|
|
* @param string $path |
|
119
|
|
|
* |
|
120
|
|
|
* @return bool |
|
121
|
|
|
*/ |
|
122
|
|
|
public function hasNodeAt($path) |
|
123
|
|
|
{ |
|
124
|
|
|
try { |
|
125
|
|
|
$this->nodeAt($path); |
|
126
|
|
|
|
|
127
|
|
|
return true; |
|
128
|
|
|
} catch (NotFoundException $e) { |
|
129
|
|
|
return false; |
|
130
|
|
|
} |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* Returns directory at given path |
|
135
|
|
|
* |
|
136
|
|
|
* @param string $path |
|
137
|
|
|
* |
|
138
|
|
|
* @return Structure\Directory |
|
139
|
|
|
* |
|
140
|
|
|
* @throws NotDirectoryException |
|
141
|
|
|
* @throws NotFoundException |
|
142
|
|
|
*/ |
|
143
|
|
|
public function directoryAt($path) |
|
144
|
|
|
{ |
|
145
|
|
|
$file = $this->nodeAt($path); |
|
146
|
|
|
|
|
147
|
|
|
if (!$file instanceof Directory) { |
|
148
|
|
|
throw new NotDirectoryException(); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
return $file; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* Returns file at given path |
|
156
|
|
|
* |
|
157
|
|
|
* @param string $path |
|
158
|
|
|
* |
|
159
|
|
|
* @return Structure\File |
|
160
|
|
|
* |
|
161
|
|
|
* @throws NotFileException |
|
162
|
|
|
* @throws NotFoundException |
|
163
|
|
|
*/ |
|
164
|
|
|
public function fileAt($path) |
|
165
|
|
|
{ |
|
166
|
|
|
$file = $this->nodeAt($path); |
|
167
|
|
|
|
|
168
|
|
|
if (!$file instanceof File) { |
|
169
|
|
|
throw new NotFileException(); |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
return $file; |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
/** |
|
176
|
|
|
* Creates Directory at given path. |
|
177
|
|
|
* |
|
178
|
|
|
* @param string $path |
|
179
|
|
|
* @param bool $recursive |
|
180
|
|
|
* @param null|integer $mode |
|
181
|
|
|
* |
|
182
|
|
|
* @return Structure\Directory |
|
183
|
|
|
* |
|
184
|
|
|
* @throws NotFoundException |
|
185
|
|
|
*/ |
|
186
|
|
|
public function createDir($path, $recursive = false, $mode = null) |
|
187
|
|
|
{ |
|
188
|
|
|
$parentPath = dirname($path); |
|
189
|
|
|
$name = basename($path); |
|
190
|
|
|
|
|
191
|
|
|
try { |
|
192
|
|
|
$parent = $this->directoryAt($parentPath); |
|
193
|
|
|
} catch (NotFoundException $e) { |
|
194
|
|
|
if (!$recursive) { |
|
195
|
|
|
throw new NotFoundException(sprintf('createDir: %s: No such file or directory', $parentPath)); |
|
196
|
|
|
} |
|
197
|
|
|
$parent = $this->createDir($parentPath, $recursive, $mode); |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
$parent->addDirectory($newDirectory = $this->factory()->getDir($name)); |
|
201
|
|
|
|
|
202
|
|
|
if (!is_null($mode)) { |
|
203
|
|
|
$newDirectory->chmod($mode); |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
return $newDirectory; |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
/** |
|
210
|
|
|
* Creates link at given path |
|
211
|
|
|
* |
|
212
|
|
|
* @param string $path |
|
213
|
|
|
* @param string $destination |
|
214
|
|
|
* |
|
215
|
|
|
* @return Structure\Link |
|
216
|
|
|
* |
|
217
|
|
|
*/ |
|
218
|
|
|
public function createLink($path, $destination) |
|
219
|
|
|
{ |
|
220
|
|
|
|
|
221
|
|
|
$destination = $this->nodeAt($destination); |
|
222
|
|
|
|
|
223
|
|
|
if ($this->hasNodeAt($path)) { |
|
224
|
|
|
throw new \RuntimeException(sprintf('%s already exists', $path)); |
|
225
|
|
|
} |
|
226
|
|
|
|
|
227
|
|
|
$parent = $this->directoryAt(dirname($path)); |
|
228
|
|
|
|
|
229
|
|
|
$parent->addLink($newLink = $this->factory()->getLink(basename($path), $destination)); |
|
230
|
|
|
|
|
231
|
|
|
return $newLink; |
|
232
|
|
|
} |
|
233
|
|
|
|
|
234
|
|
|
/** |
|
235
|
|
|
* Creates file at given path |
|
236
|
|
|
* |
|
237
|
|
|
* @param string $path |
|
238
|
|
|
* @param string|null $data |
|
239
|
|
|
* |
|
240
|
|
|
* @return Structure\File |
|
241
|
|
|
* |
|
242
|
|
|
* @throws \RuntimeException |
|
243
|
|
|
*/ |
|
244
|
|
|
public function createFile($path, $data = null) |
|
245
|
|
|
{ |
|
246
|
|
|
if ($this->hasNodeAt($path)) { |
|
247
|
|
|
throw new \RuntimeException(sprintf('%s already exists', $path)); |
|
248
|
|
|
} |
|
249
|
|
|
|
|
250
|
|
|
$parent = $this->directoryAt(dirname($path)); |
|
251
|
|
|
|
|
252
|
|
|
$parent->addFile($newFile = $this->factory()->getFile(basename($path))); |
|
253
|
|
|
|
|
254
|
|
|
$newFile->setData($data); |
|
255
|
|
|
|
|
256
|
|
|
return $newFile; |
|
257
|
|
|
|
|
258
|
|
|
} |
|
259
|
|
|
|
|
260
|
|
|
/** |
|
261
|
|
|
* Creates struture |
|
262
|
|
|
* |
|
263
|
|
|
* @param array $structure |
|
264
|
|
|
* @param string $parent |
|
265
|
|
|
* @throws NotFoundException |
|
266
|
|
|
*/ |
|
267
|
|
|
public function createStructure(array $structure, $parent = '/') |
|
268
|
|
|
{ |
|
269
|
|
|
foreach ($structure as $key => $value) { |
|
270
|
|
|
if (is_array($value)) { |
|
271
|
|
|
$this->createDir($parent.$key); |
|
272
|
|
|
$this->createStructure($value, $parent.$key.'/'); |
|
273
|
|
|
} else { |
|
274
|
|
|
$this->createFile($parent.$key, $value); |
|
275
|
|
|
} |
|
276
|
|
|
} |
|
277
|
|
|
} |
|
278
|
|
|
|
|
279
|
|
|
/** |
|
280
|
|
|
* Moves Node from source to destination |
|
281
|
|
|
* |
|
282
|
|
|
* @param string $fromPath |
|
283
|
|
|
* @param string $toPath |
|
284
|
|
|
* |
|
285
|
|
|
* @throws \RuntimeException |
|
286
|
|
|
*/ |
|
287
|
|
|
public function move($fromPath, $toPath) |
|
288
|
|
|
{ |
|
289
|
|
|
$fromNode = $this->nodeAt($fromPath); |
|
290
|
|
|
|
|
291
|
|
|
try { |
|
292
|
|
|
$nodeToOverride = $this->nodeAt($toPath); |
|
293
|
|
|
|
|
294
|
|
|
if (!is_a($nodeToOverride, get_class($fromNode))) { |
|
295
|
|
|
//nodes of a different type |
|
296
|
|
|
throw new \RuntimeException('Can\'t move.'); |
|
297
|
|
|
} |
|
298
|
|
|
|
|
299
|
|
|
if ($nodeToOverride instanceof Directory) { |
|
300
|
|
|
if ($nodeToOverride->size()) { |
|
301
|
|
|
//nodes of a different type |
|
302
|
|
|
throw new \RuntimeException('Can\'t override non empty directory.'); |
|
303
|
|
|
} |
|
304
|
|
|
} |
|
305
|
|
|
|
|
306
|
|
|
$this->remove($toPath, true); |
|
307
|
|
|
|
|
308
|
|
|
} catch (NotFoundException $e) { |
|
309
|
|
|
//nothing at destination, we're good |
|
310
|
|
|
} |
|
311
|
|
|
|
|
312
|
|
|
$toParent = $this->directoryAt(dirname($toPath)); |
|
313
|
|
|
|
|
314
|
|
|
$fromNode->setBasename(basename($toPath)); |
|
315
|
|
|
|
|
316
|
|
|
$toParent->addNode($fromNode); |
|
317
|
|
|
|
|
318
|
|
|
$this->remove($fromPath, true); |
|
319
|
|
|
|
|
320
|
|
|
} |
|
321
|
|
|
|
|
322
|
|
|
/** |
|
323
|
|
|
* Removes node at $path |
|
324
|
|
|
* |
|
325
|
|
|
* @param string $path |
|
326
|
|
|
* @param bool $recursive |
|
327
|
|
|
* |
|
328
|
|
|
* @throws \RuntimeException |
|
329
|
|
|
*/ |
|
330
|
|
|
public function remove($path, $recursive = false) |
|
331
|
|
|
{ |
|
332
|
|
|
$fileToRemove = $this->nodeAt($path); |
|
333
|
|
|
|
|
334
|
|
|
if (!$recursive && $fileToRemove instanceof Directory) { |
|
335
|
|
|
throw new \RuntimeException('Won\'t non-recursively remove directory'); |
|
336
|
|
|
} |
|
337
|
|
|
|
|
338
|
|
|
$this->directoryAt(dirname($path))->remove(basename($path)); |
|
339
|
|
|
|
|
340
|
|
|
clearstatcache(true, $path); |
|
341
|
|
|
} |
|
342
|
|
|
|
|
343
|
|
|
/** |
|
344
|
|
|
* Returns PermissionHelper with given node in context |
|
345
|
|
|
* |
|
346
|
|
|
* @param Structure\Node $node |
|
347
|
|
|
* |
|
348
|
|
|
* @return \VirtualFileSystem\Wrapper\PermissionHelper |
|
349
|
|
|
*/ |
|
350
|
|
|
public function getPermissionHelper(Structure\Node $node) |
|
351
|
|
|
{ |
|
352
|
|
|
return $this->permissionHelper->setNode($node); |
|
353
|
|
|
} |
|
354
|
|
|
|
|
355
|
|
|
/** |
|
356
|
|
|
* Sets permission helper instance |
|
357
|
|
|
* |
|
358
|
|
|
* @param \VirtualFileSystem\Wrapper\PermissionHelper $permissionHelper |
|
359
|
|
|
*/ |
|
360
|
|
|
public function setPermissionHelper($permissionHelper) |
|
361
|
|
|
{ |
|
362
|
|
|
$this->permissionHelper = $permissionHelper; |
|
363
|
|
|
} |
|
364
|
|
|
} |
|
365
|
|
|
|