@@ -28,184 +28,184 @@ |
||
28 | 28 | class FilesystemLocation implements Location |
29 | 29 | { |
30 | 30 | |
31 | - /** |
|
32 | - * Path that this location points to. |
|
33 | - * |
|
34 | - * @since 0.1.0 |
|
35 | - * |
|
36 | - * @var string |
|
37 | - */ |
|
38 | - protected $path; |
|
39 | - |
|
40 | - /** |
|
41 | - * Extensions that this location can accept. |
|
42 | - * |
|
43 | - * @since 0.1.0 |
|
44 | - * |
|
45 | - * @var Extensions |
|
46 | - */ |
|
47 | - protected $extensions; |
|
48 | - |
|
49 | - /** |
|
50 | - * Instantiate a FilesystemLocation object. |
|
51 | - * |
|
52 | - * @since 0.1.0 |
|
53 | - * |
|
54 | - * @param string $path Path that this location points to. |
|
55 | - * @param Extensions|array|string|null $extensions Optional. Extensions that this location can accept. |
|
56 | - */ |
|
57 | - public function __construct(string $path, $extensions = null) |
|
58 | - { |
|
59 | - $this->path = $path; |
|
60 | - $this->extensions = $this->validateExtensions($extensions); |
|
61 | - } |
|
62 | - |
|
63 | - /** |
|
64 | - * Get the first URI that matches the given criteria. |
|
65 | - * |
|
66 | - * @since 0.1.0 |
|
67 | - * |
|
68 | - * @param array $criteria Criteria to match. |
|
69 | - * |
|
70 | - * @return string|false URI that matches the criteria or false if none found. |
|
71 | - */ |
|
72 | - public function getURI(array $criteria) |
|
73 | - { |
|
74 | - $uris = $this->getURIs($criteria); |
|
75 | - |
|
76 | - return $uris->count() > 0 |
|
77 | - ? $uris->first() |
|
78 | - : false; |
|
79 | - } |
|
80 | - |
|
81 | - /** |
|
82 | - * Get all URIs that match the given criteria. |
|
83 | - * |
|
84 | - * @since 0.1.1 |
|
85 | - * |
|
86 | - * @param array $criteria Criteria to match. |
|
87 | - * |
|
88 | - * @return URIs URIs that match the criteria or an empty collection if none found. |
|
89 | - */ |
|
90 | - public function getURIs(array $criteria): URIs |
|
91 | - { |
|
92 | - $uris = new URIs(); |
|
93 | - |
|
94 | - foreach ($this->extensions as $extension) { |
|
95 | - $finder = new Finder(); |
|
96 | - |
|
97 | - try { |
|
98 | - $finder->files() |
|
99 | - ->name($this->getNamePattern($criteria, $extension)) |
|
100 | - ->in($this->getPathPattern($this->getRelativePath($criteria))); |
|
101 | - foreach ($finder as $file) { |
|
102 | - /** @var SplFileInfo $file */ |
|
103 | - $uris->add($file->getPathname()); |
|
104 | - } |
|
105 | - } catch (Exception $exception) { |
|
106 | - // Fail silently; |
|
107 | - } |
|
108 | - } |
|
109 | - |
|
110 | - return $uris; |
|
111 | - } |
|
112 | - |
|
113 | - /** |
|
114 | - * Get the name pattern to pass to the file finder. |
|
115 | - * |
|
116 | - * @since 0.1.3 |
|
117 | - * |
|
118 | - * @param array $criteria Criteria to match. |
|
119 | - * @param string $extension Extension to match. |
|
120 | - * |
|
121 | - * @return string Name pattern to pass to the file finder. |
|
122 | - */ |
|
123 | - protected function getNamePattern(array $criteria, string $extension): string |
|
124 | - { |
|
125 | - $names = []; |
|
126 | - |
|
127 | - $names[] = array_map(function ($criterion) use ($extension) { |
|
128 | - $uriExtension = URIHelper::containsExtension($criterion); |
|
129 | - if (! empty($extension)) { |
|
130 | - $extension = ltrim($extension, '.'); |
|
131 | - |
|
132 | - if ($uriExtension === $extension) { |
|
133 | - $criterion = substr($criterion,0,-strlen(".{$extension}")); |
|
134 | - } |
|
135 | - } else { |
|
136 | - $extension = URIHelper::containsExtension($criterion); |
|
137 | - if (!empty($extension)) { |
|
138 | - $criterion = substr($criterion,0,-strlen(".{$extension}")); |
|
139 | - } |
|
140 | - } |
|
141 | - |
|
142 | - $criterion = preg_quote(URIHelper::getFilename($criterion), chr(1)); |
|
143 | - |
|
144 | - return (empty($extension) || URIHelper::hasExtension($criterion, $extension)) |
|
145 | - ? "{$criterion}(?:\..*?)$" |
|
146 | - : "{$criterion}\.{$extension}$"; |
|
147 | - }, $criteria)[0]; |
|
148 | - |
|
149 | - return chr(1) . implode('|', array_unique($names)) . chr(1); |
|
150 | - } |
|
151 | - |
|
152 | - /** |
|
153 | - * Get the path pattern to pass to the file finder. |
|
154 | - * |
|
155 | - * @since 0.4.7 |
|
156 | - * |
|
157 | - * @param string $relativePath Relative path that was included in the |
|
158 | - * criterion. |
|
159 | - * @return string Path pattern to pass to the file finder. |
|
160 | - */ |
|
161 | - protected function getPathPattern(string $relativePath): string |
|
162 | - { |
|
163 | - if (empty($relativePath)) { |
|
164 | - return $this->path; |
|
165 | - } |
|
166 | - |
|
167 | - return rtrim($this->path,'/') . '/' . ltrim($relativePath, '/'); |
|
168 | - } |
|
169 | - |
|
170 | - /** |
|
171 | - * Get the relative path that is included in the criterion. |
|
172 | - * |
|
173 | - * @since 0.4.7 |
|
174 | - * |
|
175 | - * @param array $criteria Criteria to extract the relative path from. |
|
176 | - * @return string Relative path included in the criterion. |
|
177 | - */ |
|
178 | - protected function getRelativePath($criteria): string |
|
179 | - { |
|
180 | - $criterion = current($criteria); |
|
181 | - $components = explode('/', $criterion); |
|
182 | - |
|
183 | - if (count($components) < 2) { |
|
184 | - return ''; |
|
185 | - } |
|
186 | - |
|
187 | - return implode('/', array_slice($components, 0, -1)); |
|
188 | - } |
|
189 | - |
|
190 | - /** |
|
191 | - * Validate the extensions and return a collection. |
|
192 | - * |
|
193 | - * @since 0.1.1 |
|
194 | - * |
|
195 | - * @param Extensions|array|string|null $extensions Extensions to validate. |
|
196 | - * |
|
197 | - * @return Extensions Validated extensions collection. |
|
198 | - */ |
|
199 | - protected function validateExtensions($extensions): Extensions |
|
200 | - { |
|
201 | - if (empty($extensions)) { |
|
202 | - $extensions = new Extensions(['']); |
|
203 | - } |
|
204 | - |
|
205 | - if (! $extensions instanceof Extensions) { |
|
206 | - $extensions = new Extensions((array)$extensions); |
|
207 | - } |
|
208 | - |
|
209 | - return $extensions; |
|
210 | - } |
|
31 | + /** |
|
32 | + * Path that this location points to. |
|
33 | + * |
|
34 | + * @since 0.1.0 |
|
35 | + * |
|
36 | + * @var string |
|
37 | + */ |
|
38 | + protected $path; |
|
39 | + |
|
40 | + /** |
|
41 | + * Extensions that this location can accept. |
|
42 | + * |
|
43 | + * @since 0.1.0 |
|
44 | + * |
|
45 | + * @var Extensions |
|
46 | + */ |
|
47 | + protected $extensions; |
|
48 | + |
|
49 | + /** |
|
50 | + * Instantiate a FilesystemLocation object. |
|
51 | + * |
|
52 | + * @since 0.1.0 |
|
53 | + * |
|
54 | + * @param string $path Path that this location points to. |
|
55 | + * @param Extensions|array|string|null $extensions Optional. Extensions that this location can accept. |
|
56 | + */ |
|
57 | + public function __construct(string $path, $extensions = null) |
|
58 | + { |
|
59 | + $this->path = $path; |
|
60 | + $this->extensions = $this->validateExtensions($extensions); |
|
61 | + } |
|
62 | + |
|
63 | + /** |
|
64 | + * Get the first URI that matches the given criteria. |
|
65 | + * |
|
66 | + * @since 0.1.0 |
|
67 | + * |
|
68 | + * @param array $criteria Criteria to match. |
|
69 | + * |
|
70 | + * @return string|false URI that matches the criteria or false if none found. |
|
71 | + */ |
|
72 | + public function getURI(array $criteria) |
|
73 | + { |
|
74 | + $uris = $this->getURIs($criteria); |
|
75 | + |
|
76 | + return $uris->count() > 0 |
|
77 | + ? $uris->first() |
|
78 | + : false; |
|
79 | + } |
|
80 | + |
|
81 | + /** |
|
82 | + * Get all URIs that match the given criteria. |
|
83 | + * |
|
84 | + * @since 0.1.1 |
|
85 | + * |
|
86 | + * @param array $criteria Criteria to match. |
|
87 | + * |
|
88 | + * @return URIs URIs that match the criteria or an empty collection if none found. |
|
89 | + */ |
|
90 | + public function getURIs(array $criteria): URIs |
|
91 | + { |
|
92 | + $uris = new URIs(); |
|
93 | + |
|
94 | + foreach ($this->extensions as $extension) { |
|
95 | + $finder = new Finder(); |
|
96 | + |
|
97 | + try { |
|
98 | + $finder->files() |
|
99 | + ->name($this->getNamePattern($criteria, $extension)) |
|
100 | + ->in($this->getPathPattern($this->getRelativePath($criteria))); |
|
101 | + foreach ($finder as $file) { |
|
102 | + /** @var SplFileInfo $file */ |
|
103 | + $uris->add($file->getPathname()); |
|
104 | + } |
|
105 | + } catch (Exception $exception) { |
|
106 | + // Fail silently; |
|
107 | + } |
|
108 | + } |
|
109 | + |
|
110 | + return $uris; |
|
111 | + } |
|
112 | + |
|
113 | + /** |
|
114 | + * Get the name pattern to pass to the file finder. |
|
115 | + * |
|
116 | + * @since 0.1.3 |
|
117 | + * |
|
118 | + * @param array $criteria Criteria to match. |
|
119 | + * @param string $extension Extension to match. |
|
120 | + * |
|
121 | + * @return string Name pattern to pass to the file finder. |
|
122 | + */ |
|
123 | + protected function getNamePattern(array $criteria, string $extension): string |
|
124 | + { |
|
125 | + $names = []; |
|
126 | + |
|
127 | + $names[] = array_map(function ($criterion) use ($extension) { |
|
128 | + $uriExtension = URIHelper::containsExtension($criterion); |
|
129 | + if (! empty($extension)) { |
|
130 | + $extension = ltrim($extension, '.'); |
|
131 | + |
|
132 | + if ($uriExtension === $extension) { |
|
133 | + $criterion = substr($criterion,0,-strlen(".{$extension}")); |
|
134 | + } |
|
135 | + } else { |
|
136 | + $extension = URIHelper::containsExtension($criterion); |
|
137 | + if (!empty($extension)) { |
|
138 | + $criterion = substr($criterion,0,-strlen(".{$extension}")); |
|
139 | + } |
|
140 | + } |
|
141 | + |
|
142 | + $criterion = preg_quote(URIHelper::getFilename($criterion), chr(1)); |
|
143 | + |
|
144 | + return (empty($extension) || URIHelper::hasExtension($criterion, $extension)) |
|
145 | + ? "{$criterion}(?:\..*?)$" |
|
146 | + : "{$criterion}\.{$extension}$"; |
|
147 | + }, $criteria)[0]; |
|
148 | + |
|
149 | + return chr(1) . implode('|', array_unique($names)) . chr(1); |
|
150 | + } |
|
151 | + |
|
152 | + /** |
|
153 | + * Get the path pattern to pass to the file finder. |
|
154 | + * |
|
155 | + * @since 0.4.7 |
|
156 | + * |
|
157 | + * @param string $relativePath Relative path that was included in the |
|
158 | + * criterion. |
|
159 | + * @return string Path pattern to pass to the file finder. |
|
160 | + */ |
|
161 | + protected function getPathPattern(string $relativePath): string |
|
162 | + { |
|
163 | + if (empty($relativePath)) { |
|
164 | + return $this->path; |
|
165 | + } |
|
166 | + |
|
167 | + return rtrim($this->path,'/') . '/' . ltrim($relativePath, '/'); |
|
168 | + } |
|
169 | + |
|
170 | + /** |
|
171 | + * Get the relative path that is included in the criterion. |
|
172 | + * |
|
173 | + * @since 0.4.7 |
|
174 | + * |
|
175 | + * @param array $criteria Criteria to extract the relative path from. |
|
176 | + * @return string Relative path included in the criterion. |
|
177 | + */ |
|
178 | + protected function getRelativePath($criteria): string |
|
179 | + { |
|
180 | + $criterion = current($criteria); |
|
181 | + $components = explode('/', $criterion); |
|
182 | + |
|
183 | + if (count($components) < 2) { |
|
184 | + return ''; |
|
185 | + } |
|
186 | + |
|
187 | + return implode('/', array_slice($components, 0, -1)); |
|
188 | + } |
|
189 | + |
|
190 | + /** |
|
191 | + * Validate the extensions and return a collection. |
|
192 | + * |
|
193 | + * @since 0.1.1 |
|
194 | + * |
|
195 | + * @param Extensions|array|string|null $extensions Extensions to validate. |
|
196 | + * |
|
197 | + * @return Extensions Validated extensions collection. |
|
198 | + */ |
|
199 | + protected function validateExtensions($extensions): Extensions |
|
200 | + { |
|
201 | + if (empty($extensions)) { |
|
202 | + $extensions = new Extensions(['']); |
|
203 | + } |
|
204 | + |
|
205 | + if (! $extensions instanceof Extensions) { |
|
206 | + $extensions = new Extensions((array)$extensions); |
|
207 | + } |
|
208 | + |
|
209 | + return $extensions; |
|
210 | + } |
|
211 | 211 | } |
@@ -124,18 +124,18 @@ discard block |
||
124 | 124 | { |
125 | 125 | $names = []; |
126 | 126 | |
127 | - $names[] = array_map(function ($criterion) use ($extension) { |
|
127 | + $names[] = array_map(function($criterion) use ($extension) { |
|
128 | 128 | $uriExtension = URIHelper::containsExtension($criterion); |
129 | - if (! empty($extension)) { |
|
129 | + if ( ! empty($extension)) { |
|
130 | 130 | $extension = ltrim($extension, '.'); |
131 | 131 | |
132 | 132 | if ($uriExtension === $extension) { |
133 | - $criterion = substr($criterion,0,-strlen(".{$extension}")); |
|
133 | + $criterion = substr($criterion, 0, -strlen(".{$extension}")); |
|
134 | 134 | } |
135 | 135 | } else { |
136 | 136 | $extension = URIHelper::containsExtension($criterion); |
137 | - if (!empty($extension)) { |
|
138 | - $criterion = substr($criterion,0,-strlen(".{$extension}")); |
|
137 | + if ( ! empty($extension)) { |
|
138 | + $criterion = substr($criterion, 0, -strlen(".{$extension}")); |
|
139 | 139 | } |
140 | 140 | } |
141 | 141 | |
@@ -146,7 +146,7 @@ discard block |
||
146 | 146 | : "{$criterion}\.{$extension}$"; |
147 | 147 | }, $criteria)[0]; |
148 | 148 | |
149 | - return chr(1) . implode('|', array_unique($names)) . chr(1); |
|
149 | + return chr(1).implode('|', array_unique($names)).chr(1); |
|
150 | 150 | } |
151 | 151 | |
152 | 152 | /** |
@@ -164,7 +164,7 @@ discard block |
||
164 | 164 | return $this->path; |
165 | 165 | } |
166 | 166 | |
167 | - return rtrim($this->path,'/') . '/' . ltrim($relativePath, '/'); |
|
167 | + return rtrim($this->path, '/').'/'.ltrim($relativePath, '/'); |
|
168 | 168 | } |
169 | 169 | |
170 | 170 | /** |
@@ -202,8 +202,8 @@ discard block |
||
202 | 202 | $extensions = new Extensions(['']); |
203 | 203 | } |
204 | 204 | |
205 | - if (! $extensions instanceof Extensions) { |
|
206 | - $extensions = new Extensions((array)$extensions); |
|
205 | + if ( ! $extensions instanceof Extensions) { |
|
206 | + $extensions = new Extensions((array) $extensions); |
|
207 | 207 | } |
208 | 208 | |
209 | 209 | return $extensions; |