|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the puli/repository package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Bernhard Schussek <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Puli\Repository; |
|
13
|
|
|
|
|
14
|
|
|
use ArrayIterator; |
|
15
|
|
|
use Puli\Repository\Api\EditableRepository; |
|
16
|
|
|
use Puli\Repository\Api\Resource\PuliResource; |
|
17
|
|
|
use Puli\Repository\Api\ResourceCollection; |
|
18
|
|
|
use Puli\Repository\Api\UnsupportedResourceException; |
|
19
|
|
|
use Puli\Repository\Resource\Collection\ArrayResourceCollection; |
|
20
|
|
|
use Puli\Repository\Resource\GenericResource; |
|
21
|
|
|
use Puli\Repository\Resource\LinkResource; |
|
22
|
|
|
use Webmozart\Assert\Assert; |
|
23
|
|
|
use Webmozart\Glob\Glob; |
|
24
|
|
|
use Webmozart\Glob\Iterator\GlobFilterIterator; |
|
25
|
|
|
use Webmozart\Glob\Iterator\RegexFilterIterator; |
|
26
|
|
|
use Webmozart\PathUtil\Path; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* An in-memory resource repository. |
|
30
|
|
|
* |
|
31
|
|
|
* Resources can be added with the method {@link add()}: |
|
32
|
|
|
* |
|
33
|
|
|
* ```php |
|
34
|
|
|
* use Puli\Repository\InMemoryRepository; |
|
35
|
|
|
* |
|
36
|
|
|
* $repo = new InMemoryRepository(); |
|
37
|
|
|
* $repo->add('/css', new DirectoryResource('/path/to/project/res/css')); |
|
38
|
|
|
* ``` |
|
39
|
|
|
* |
|
40
|
|
|
* @since 1.0 |
|
41
|
|
|
* |
|
42
|
|
|
* @author Bernhard Schussek <[email protected]> |
|
43
|
|
|
*/ |
|
44
|
|
|
class InMemoryRepository extends AbstractRepository implements EditableRepository |
|
45
|
|
|
{ |
|
46
|
|
|
/** |
|
47
|
|
|
* @var PuliResource[] |
|
48
|
|
|
*/ |
|
49
|
|
|
private $resources = array(); |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* This array contains a copy of each LinkResource in $resources |
|
53
|
|
|
* for performance reasons. |
|
54
|
|
|
* |
|
55
|
|
|
* @var LinkResource[] |
|
56
|
|
|
*/ |
|
57
|
|
|
private $links = array(); |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Creates a new repository. |
|
61
|
|
|
*/ |
|
62
|
83 |
|
public function __construct() |
|
63
|
|
|
{ |
|
64
|
83 |
|
$this->clear(); |
|
65
|
83 |
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* {@inheritdoc} |
|
69
|
|
|
*/ |
|
70
|
51 |
|
public function get($path) |
|
71
|
|
|
{ |
|
72
|
51 |
|
$path = $this->sanitizePath($path); |
|
73
|
|
|
|
|
74
|
42 |
|
if (!isset($this->resources[$path])) { |
|
75
|
6 |
|
return $this->followLinksOrFail($path); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
39 |
|
return $this->resources[$path]; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* {@inheritdoc} |
|
83
|
|
|
*/ |
|
84
|
24 |
|
public function find($query, $language = 'glob') |
|
85
|
|
|
{ |
|
86
|
24 |
|
$this->validateSearchLanguage($language); |
|
87
|
|
|
|
|
88
|
22 |
|
$query = $this->sanitizePath($query); |
|
89
|
16 |
|
$resources = array(); |
|
90
|
|
|
|
|
91
|
16 |
|
if (Glob::isDynamic($query)) { |
|
92
|
7 |
|
$resources = $this->getGlobIterator($query); |
|
93
|
16 |
|
} elseif (isset($this->resources[$query])) { |
|
94
|
9 |
|
$resources = array($this->resources[$query]); |
|
95
|
9 |
|
} |
|
96
|
|
|
|
|
97
|
16 |
|
return new ArrayResourceCollection($resources); |
|
|
|
|
|
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* {@inheritdoc} |
|
102
|
|
|
*/ |
|
103
|
16 |
|
public function contains($query, $language = 'glob') |
|
104
|
|
|
{ |
|
105
|
16 |
|
$this->validateSearchLanguage($language); |
|
106
|
|
|
|
|
107
|
15 |
|
$query = $this->sanitizePath($query); |
|
108
|
|
|
|
|
109
|
12 |
|
if (Glob::isDynamic($query)) { |
|
110
|
1 |
|
$iterator = $this->getGlobIterator($query); |
|
111
|
1 |
|
$iterator->rewind(); |
|
112
|
|
|
|
|
113
|
1 |
|
return $iterator->valid(); |
|
114
|
6 |
|
} |
|
115
|
|
|
|
|
116
|
11 |
|
return isset($this->resources[$query]); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* {@inheritdoc} |
|
121
|
|
|
*/ |
|
122
|
73 |
|
public function add($path, $resource) |
|
123
|
|
|
{ |
|
124
|
73 |
|
$path = $this->sanitizePath($path); |
|
125
|
|
|
|
|
126
|
70 |
|
if ($resource instanceof ResourceCollection) { |
|
127
|
2 |
|
$this->ensureDirectoryExists($path); |
|
128
|
2 |
|
foreach ($resource as $child) { |
|
129
|
2 |
|
$this->addResource($path.'/'.$child->getName(), $child); |
|
130
|
2 |
|
} |
|
131
|
|
|
|
|
132
|
|
|
// Keep the resources sorted by file name |
|
133
|
2 |
|
ksort($this->resources); |
|
134
|
|
|
|
|
135
|
2 |
|
return; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
68 |
|
if ($resource instanceof PuliResource) { |
|
139
|
67 |
|
$this->ensureDirectoryExists(Path::getDirectory($path)); |
|
140
|
67 |
|
$this->addResource($path, $resource); |
|
141
|
|
|
|
|
142
|
67 |
|
ksort($this->resources); |
|
143
|
|
|
|
|
144
|
67 |
|
return; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
1 |
|
throw new UnsupportedResourceException(sprintf( |
|
148
|
1 |
|
'The passed resource must be a PuliResource or ResourceCollection. Got: %s', |
|
149
|
1 |
|
is_object($resource) ? get_class($resource) : gettype($resource) |
|
150
|
1 |
|
)); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* {@inheritdoc} |
|
155
|
|
|
*/ |
|
156
|
13 |
|
public function remove($query, $language = 'glob') |
|
157
|
|
|
{ |
|
158
|
13 |
|
$resources = $this->find($query, $language); |
|
159
|
9 |
|
$nbOfResources = count($this->resources); |
|
160
|
|
|
|
|
161
|
|
|
// Run the assertion after find(), so that we know that $query is valid |
|
162
|
9 |
|
Assert::notEmpty(trim($query, '/'), 'The root directory cannot be removed.'); |
|
163
|
|
|
|
|
164
|
7 |
|
foreach ($resources as $resource) { |
|
165
|
7 |
|
$this->removeResource($resource); |
|
166
|
7 |
|
} |
|
167
|
|
|
|
|
168
|
7 |
|
return $nbOfResources - count($this->resources); |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
/** |
|
172
|
|
|
* {@inheritdoc} |
|
173
|
|
|
*/ |
|
174
|
83 |
|
public function clear() |
|
175
|
|
|
{ |
|
176
|
83 |
|
$root = new GenericResource('/'); |
|
177
|
83 |
|
$root->attachTo($this); |
|
178
|
|
|
|
|
179
|
|
|
// Subtract root |
|
180
|
83 |
|
$removed = count($this->resources) - 1; |
|
181
|
|
|
|
|
182
|
83 |
|
$this->resources = array('/' => $root); |
|
183
|
83 |
|
$this->links = array(); |
|
184
|
|
|
|
|
185
|
83 |
|
return $removed; |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
/** |
|
189
|
|
|
* {@inheritdoc} |
|
190
|
|
|
*/ |
|
191
|
10 |
|
public function listChildren($path) |
|
192
|
|
|
{ |
|
193
|
10 |
|
$iterator = $this->getChildIterator($this->get($path)); |
|
194
|
|
|
|
|
195
|
6 |
|
return new ArrayResourceCollection($iterator); |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
/** |
|
199
|
|
|
* {@inheritdoc} |
|
200
|
|
|
*/ |
|
201
|
6 |
|
public function hasChildren($path) |
|
202
|
|
|
{ |
|
203
|
6 |
|
$iterator = $this->getChildIterator($this->get($path)); |
|
204
|
2 |
|
$iterator->rewind(); |
|
205
|
|
|
|
|
206
|
2 |
|
return $iterator->valid(); |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
/** |
|
210
|
|
|
* Recursively creates a directory for a path. |
|
211
|
|
|
* |
|
212
|
|
|
* @param string $path A directory path. |
|
213
|
|
|
*/ |
|
214
|
69 |
|
private function ensureDirectoryExists($path) |
|
215
|
|
|
{ |
|
216
|
69 |
|
if (!isset($this->resources[$path])) { |
|
217
|
|
|
// Recursively initialize parent directories |
|
218
|
23 |
|
if ($path !== '/') { |
|
219
|
23 |
|
$this->ensureDirectoryExists(Path::getDirectory($path)); |
|
220
|
23 |
|
} |
|
221
|
|
|
|
|
222
|
23 |
|
$this->resources[$path] = new GenericResource($path); |
|
223
|
23 |
|
$this->resources[$path]->attachTo($this); |
|
224
|
|
|
|
|
225
|
23 |
|
return; |
|
226
|
|
|
} |
|
227
|
69 |
|
} |
|
228
|
|
|
|
|
229
|
69 |
|
private function addResource($path, PuliResource $resource) |
|
230
|
|
|
{ |
|
231
|
|
|
// Don't modify resources attached to other repositories |
|
232
|
69 |
|
if ($resource->isAttached()) { |
|
233
|
2 |
|
$resource = clone $resource; |
|
234
|
2 |
|
} |
|
235
|
|
|
|
|
236
|
69 |
|
$basePath = '/' === $path ? $path : $path.'/'; |
|
237
|
|
|
|
|
238
|
|
|
// Read children before attaching the resource to this repository |
|
239
|
69 |
|
$children = $resource->listChildren(); |
|
240
|
|
|
|
|
241
|
69 |
|
$resource->attachTo($this, $path); |
|
242
|
|
|
|
|
243
|
|
|
// Add the resource before adding its children, so that the array |
|
244
|
|
|
// stays sorted |
|
245
|
69 |
|
$this->resources[$path] = $resource; |
|
246
|
|
|
|
|
247
|
69 |
|
if ($resource instanceof LinkResource) { |
|
248
|
7 |
|
$this->links[$path] = $resource; |
|
249
|
7 |
|
} |
|
250
|
|
|
|
|
251
|
69 |
|
foreach ($children as $name => $child) { |
|
252
|
34 |
|
$this->addResource($basePath.$name, $child); |
|
253
|
69 |
|
} |
|
254
|
69 |
|
} |
|
255
|
|
|
|
|
256
|
7 |
|
private function removeResource(PuliResource $resource) |
|
257
|
|
|
{ |
|
258
|
7 |
|
$path = $resource->getPath(); |
|
259
|
|
|
|
|
260
|
|
|
// Ignore non-existing resources |
|
261
|
7 |
|
if (!isset($this->resources[$path])) { |
|
262
|
|
|
return; |
|
263
|
|
|
} |
|
264
|
|
|
|
|
265
|
|
|
// Recursively register directory contents |
|
266
|
7 |
|
foreach ($this->getChildIterator($resource) as $child) { |
|
267
|
5 |
|
$this->removeResource($child); |
|
268
|
7 |
|
} |
|
269
|
|
|
|
|
270
|
7 |
|
unset($this->resources[$path]); |
|
271
|
|
|
|
|
272
|
7 |
|
if ($resource instanceof LinkResource && array_key_exists($path, $this->links)) { |
|
273
|
|
|
unset($this->links[$path]); |
|
274
|
|
|
} |
|
275
|
|
|
|
|
276
|
|
|
// Detach from locator |
|
277
|
7 |
|
$resource->detach(); |
|
278
|
7 |
|
} |
|
279
|
|
|
|
|
280
|
|
|
/** |
|
281
|
|
|
* {@inheritdoc} |
|
282
|
|
|
*/ |
|
283
|
6 |
|
protected function followLinks($path) |
|
284
|
|
|
{ |
|
285
|
6 |
|
foreach ($this->links as $link) { |
|
286
|
3 |
|
$linkPath = rtrim($link->getPath(), '/'); |
|
287
|
|
|
|
|
288
|
3 |
|
if (0 === strpos($path, $linkPath.'/')) { |
|
289
|
3 |
|
$realTargetPath = rtrim($link->getTarget(true)->getPath(), '/'); |
|
290
|
3 |
|
$realPath = substr_replace($path, $realTargetPath, 0, strlen($linkPath)); |
|
291
|
|
|
|
|
292
|
3 |
|
if (isset($this->resources[$realPath])) { |
|
293
|
3 |
|
return $this->resources[$realPath]; |
|
294
|
|
|
} |
|
295
|
|
|
} |
|
296
|
5 |
|
} |
|
297
|
|
|
|
|
298
|
3 |
|
return null; |
|
299
|
|
|
} |
|
300
|
|
|
|
|
301
|
|
|
/** |
|
302
|
|
|
* Returns an iterator for the children of a resource. |
|
303
|
|
|
* |
|
304
|
|
|
* @param PuliResource $resource The resource. |
|
305
|
|
|
* |
|
306
|
|
|
* @return RegexFilterIterator The iterator. |
|
307
|
|
|
*/ |
|
308
|
14 |
View Code Duplication |
private function getChildIterator(PuliResource $resource) |
|
309
|
|
|
{ |
|
310
|
14 |
|
$staticPrefix = rtrim($resource->getPath(), '/').'/'; |
|
311
|
14 |
|
$regExp = '~^'.preg_quote($staticPrefix, '~').'[^/]+$~'; |
|
312
|
|
|
|
|
313
|
14 |
|
return new RegexFilterIterator( |
|
314
|
14 |
|
$regExp, |
|
315
|
14 |
|
$staticPrefix, |
|
316
|
14 |
|
new ArrayIterator($this->resources), |
|
317
|
|
|
RegexFilterIterator::FILTER_KEY |
|
318
|
14 |
|
); |
|
319
|
|
|
} |
|
320
|
|
|
|
|
321
|
|
|
/** |
|
322
|
|
|
* Returns an iterator for a glob. |
|
323
|
|
|
* |
|
324
|
|
|
* @param string $glob The glob. |
|
325
|
|
|
* |
|
326
|
|
|
* @return GlobFilterIterator The iterator. |
|
327
|
|
|
*/ |
|
328
|
8 |
|
protected function getGlobIterator($glob) |
|
329
|
|
|
{ |
|
330
|
8 |
|
return new GlobFilterIterator( |
|
331
|
8 |
|
$glob, |
|
332
|
8 |
|
new ArrayIterator($this->resources), |
|
333
|
|
|
GlobFilterIterator::FILTER_KEY |
|
334
|
8 |
|
); |
|
335
|
|
|
} |
|
336
|
|
|
} |
|
337
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.