1 | <?php |
||
44 | class InMemoryRepository extends AbstractEditableRepository |
||
45 | { |
||
46 | /** |
||
47 | * @var PuliResource[] |
||
48 | */ |
||
49 | private $resources = array(); |
||
50 | |||
51 | /** |
||
52 | * Create the repository. |
||
53 | * |
||
54 | * @param ChangeStream|null $changeStream If provided, the repository will log |
||
55 | * resources changes in this change stream. |
||
56 | */ |
||
57 | 122 | public function __construct(ChangeStream $changeStream = null) |
|
58 | { |
||
59 | 122 | parent::__construct($changeStream); |
|
60 | |||
61 | 122 | $this->clear(); |
|
62 | 122 | } |
|
63 | |||
64 | /** |
||
65 | * {@inheritdoc} |
||
66 | */ |
||
67 | 49 | public function get($path) |
|
68 | { |
||
69 | 49 | $path = $this->sanitizePath($path); |
|
70 | |||
71 | 40 | if (!isset($this->resources[$path])) { |
|
72 | 4 | throw ResourceNotFoundException::forPath($path); |
|
73 | } |
||
74 | |||
75 | 36 | return $this->resources[$path]; |
|
76 | } |
||
77 | |||
78 | /** |
||
79 | * {@inheritdoc} |
||
80 | */ |
||
81 | 27 | public function find($query, $language = 'glob') |
|
82 | { |
||
83 | 27 | $this->failUnlessGlob($language); |
|
84 | |||
85 | 25 | $query = $this->sanitizePath($query); |
|
86 | 19 | $resources = array(); |
|
87 | |||
88 | 19 | if (Glob::isDynamic($query)) { |
|
89 | 7 | $resources = $this->getGlobIterator($query); |
|
90 | 19 | } elseif (isset($this->resources[$query])) { |
|
91 | 12 | $resources = array($this->resources[$query]); |
|
92 | 12 | } |
|
93 | |||
94 | 19 | return new ArrayResourceCollection($resources); |
|
|
|||
95 | } |
||
96 | |||
97 | /** |
||
98 | * {@inheritdoc} |
||
99 | */ |
||
100 | 16 | public function contains($query, $language = 'glob') |
|
101 | { |
||
102 | 16 | $this->failUnlessGlob($language); |
|
103 | |||
104 | 15 | $query = $this->sanitizePath($query); |
|
105 | |||
106 | 12 | if (Glob::isDynamic($query)) { |
|
107 | 1 | $iterator = $this->getGlobIterator($query); |
|
108 | 1 | $iterator->rewind(); |
|
109 | |||
110 | 1 | return $iterator->valid(); |
|
111 | } |
||
112 | |||
113 | 11 | return isset($this->resources[$query]); |
|
114 | 1 | } |
|
115 | |||
116 | /** |
||
117 | * {@inheritdoc} |
||
118 | */ |
||
119 | 84 | public function add($path, $resource) |
|
120 | { |
||
121 | 84 | $path = $this->sanitizePath($path); |
|
122 | |||
123 | 81 | if ($resource instanceof ResourceCollection) { |
|
124 | 2 | $this->ensureDirectoryExists($path); |
|
125 | 2 | foreach ($resource as $child) { |
|
126 | 2 | $this->addResource($path.'/'.$child->getName(), $child); |
|
127 | 2 | } |
|
128 | |||
129 | // Keep the resources sorted by file name |
||
130 | 2 | ksort($this->resources); |
|
131 | |||
132 | 2 | return; |
|
133 | } |
||
134 | |||
135 | 79 | if ($resource instanceof PuliResource) { |
|
136 | 78 | $this->ensureDirectoryExists(Path::getDirectory($path)); |
|
137 | 78 | $this->addResource($path, $resource); |
|
138 | |||
139 | 78 | ksort($this->resources); |
|
140 | |||
141 | 78 | return; |
|
142 | } |
||
143 | |||
144 | 1 | throw new UnsupportedResourceException(sprintf( |
|
145 | 1 | 'The passed resource must be a PuliResource or ResourceCollection. Got: %s', |
|
146 | 1 | is_object($resource) ? get_class($resource) : gettype($resource) |
|
147 | 1 | )); |
|
148 | } |
||
149 | |||
150 | /** |
||
151 | * {@inheritdoc} |
||
152 | */ |
||
153 | 16 | public function remove($query, $language = 'glob') |
|
167 | |||
168 | /** |
||
169 | * {@inheritdoc} |
||
170 | */ |
||
171 | 122 | public function clear() |
|
172 | { |
||
186 | |||
187 | /** |
||
188 | * {@inheritdoc} |
||
189 | */ |
||
190 | 10 | public function listChildren($path) |
|
196 | |||
197 | /** |
||
198 | * {@inheritdoc} |
||
199 | */ |
||
200 | 6 | public function hasChildren($path) |
|
207 | |||
208 | /** |
||
209 | * Recursively creates a directory for a path. |
||
210 | * |
||
211 | * @param string $path A directory path. |
||
212 | */ |
||
213 | 80 | private function ensureDirectoryExists($path) |
|
227 | |||
228 | 80 | private function addResource($path, PuliResource $resource) |
|
252 | |||
253 | 10 | private function removeResource(PuliResource $resource) |
|
274 | |||
275 | /** |
||
276 | * Returns an iterator for the children of a resource. |
||
277 | * |
||
278 | * @param PuliResource $resource The resource. |
||
279 | * |
||
280 | * @return RegexFilterIterator The iterator. |
||
281 | */ |
||
282 | 17 | private function getChildIterator(PuliResource $resource) |
|
294 | |||
295 | /** |
||
296 | * Returns an iterator for a glob. |
||
297 | * |
||
298 | * @param string $glob The glob. |
||
299 | * |
||
300 | * @return GlobFilterIterator The iterator. |
||
301 | */ |
||
302 | 8 | protected function getGlobIterator($glob) |
|
310 | } |
||
311 |
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.