|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
/** |
|
3
|
|
|
* Bright Nucleus View Component. |
|
4
|
|
|
* |
|
5
|
|
|
* @package BrightNucleus\View |
|
6
|
|
|
* @author Alain Schlesser <[email protected]> |
|
7
|
|
|
* @license MIT |
|
8
|
|
|
* @link http://www.brightnucleus.com/ |
|
9
|
|
|
* @copyright 2016-2017 Alain Schlesser, Bright Nucleus |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace BrightNucleus\View\Location; |
|
13
|
|
|
|
|
14
|
|
|
use BrightNucleus\View\Support\Extensions; |
|
15
|
|
|
use BrightNucleus\View\Support\URIHelper; |
|
16
|
|
|
use Exception; |
|
17
|
|
|
use Symfony\Component\Finder\Finder; |
|
18
|
|
|
use Symfony\Component\Finder\SplFileInfo; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Class FilesystemLocation. |
|
22
|
|
|
* |
|
23
|
|
|
* @since 0.1.0 |
|
24
|
|
|
* |
|
25
|
|
|
* @package BrightNucleus\View\Location |
|
26
|
|
|
* @author Alain Schlesser <[email protected]> |
|
27
|
|
|
*/ |
|
28
|
|
|
class FilesystemLocation implements Location |
|
29
|
|
|
{ |
|
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
|
45 |
|
public function __construct(string $path, $extensions = null) |
|
58
|
|
|
{ |
|
59
|
45 |
|
$this->path = $path; |
|
60
|
45 |
|
$this->extensions = $this->validateExtensions($extensions); |
|
61
|
45 |
|
} |
|
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
|
44 |
|
public function getURI(array $criteria) |
|
73
|
|
|
{ |
|
74
|
44 |
|
$uris = $this->getURIs($criteria); |
|
75
|
|
|
|
|
76
|
44 |
|
return $uris->count() > 0 |
|
77
|
43 |
|
? $uris->first() |
|
78
|
44 |
|
: 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
|
44 |
|
public function getURIs(array $criteria): URIs |
|
91
|
|
|
{ |
|
92
|
44 |
|
$uris = new URIs(); |
|
93
|
|
|
|
|
94
|
44 |
|
foreach ($this->extensions as $extension) { |
|
95
|
44 |
|
$finder = new Finder(); |
|
96
|
|
|
|
|
97
|
|
|
try { |
|
98
|
44 |
|
bdump( $this->getPathPattern($this->getRelativePath($criteria)) ); |
|
99
|
44 |
|
$finder->files() |
|
100
|
44 |
|
->name($this->getNamePattern($criteria, $extension)) |
|
101
|
43 |
|
->in($this->getPathPattern($this->getRelativePath($criteria))); |
|
102
|
|
|
foreach ($finder as $file) { |
|
103
|
43 |
|
/** @var SplFileInfo $file */ |
|
104
|
|
|
$uris->add($file->getPathname()); |
|
105
|
44 |
|
} |
|
106
|
|
|
} catch (Exception $exception) { |
|
107
|
|
|
// Fail silently; |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
44 |
|
|
|
111
|
|
|
return $uris; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Get the name pattern to pass to the file finder. |
|
116
|
|
|
* |
|
117
|
|
|
* @since 0.1.3 |
|
118
|
|
|
* |
|
119
|
|
|
* @param array $criteria Criteria to match. |
|
120
|
|
|
* @param string $extension Extension to match. |
|
121
|
|
|
* |
|
122
|
|
|
* @return string Name pattern to pass to the file finder. |
|
123
|
44 |
|
*/ |
|
124
|
|
|
protected function getNamePattern(array $criteria, string $extension): string |
|
125
|
44 |
|
{ |
|
126
|
|
|
$names = []; |
|
127
|
|
|
|
|
128
|
44 |
|
$names[] = array_map(function ($criterion) use ($extension) { |
|
129
|
|
|
$uriExtension = URIHelper::containsExtension($criterion); |
|
130
|
44 |
|
if (! empty($extension)) { |
|
131
|
20 |
|
$extension = ltrim($extension, '.'); |
|
|
|
|
|
|
132
|
44 |
|
|
|
133
|
44 |
|
if ($uriExtension === $extension) { |
|
134
|
|
|
$criterion = substr($criterion,0,-strlen(".{$extension}")); |
|
135
|
44 |
|
} |
|
136
|
|
|
} else { |
|
137
|
|
|
$extension = URIHelper::containsExtension($criterion); |
|
|
|
|
|
|
138
|
|
|
if (!empty($extension)) { |
|
139
|
|
|
$criterion = substr($criterion,0,-strlen(".{$extension}")); |
|
140
|
|
|
} |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
$criterion = preg_quote(URIHelper::getFilename($criterion), chr(1)); |
|
144
|
|
|
|
|
145
|
44 |
|
return (empty($extension) || URIHelper::hasExtension($criterion, $extension)) |
|
146
|
|
|
? "{$criterion}(?:\..*?)$" |
|
147
|
44 |
|
: "{$criterion}\.{$extension}$"; |
|
148
|
|
|
}, $criteria)[0]; |
|
149
|
|
|
|
|
150
|
|
|
return chr(1) . implode('|', array_unique($names)) . chr(1); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* Get the path pattern to pass to the file finder. |
|
155
|
|
|
* |
|
156
|
|
|
* @since 0.4.7 |
|
157
|
|
|
* |
|
158
|
|
|
* @param string $relativePath Relative path that was included in the |
|
159
|
|
|
* criterion. |
|
160
|
|
|
* @return string Path pattern to pass to the file finder. |
|
161
|
44 |
|
*/ |
|
162
|
44 |
|
protected function getPathPattern(string $relativePath): string |
|
163
|
44 |
|
{ |
|
164
|
|
|
if (empty($relativePath)) { |
|
165
|
44 |
|
return $this->path; |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
return rtrim($this->path,'/') . '/' . ltrim($relativePath, '/'); |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
/** |
|
172
|
|
|
* Get the relative path that is included in the criterion. |
|
173
|
|
|
* |
|
174
|
|
|
* @since 0.4.7 |
|
175
|
|
|
* |
|
176
|
|
|
* @param array $criteria Criteria to extract the relative path from. |
|
177
|
45 |
|
* @return string Relative path included in the criterion. |
|
178
|
|
|
*/ |
|
179
|
45 |
|
protected function getRelativePath($criteria): string |
|
180
|
8 |
|
{ |
|
181
|
|
|
$criterion = current($criteria); |
|
182
|
|
|
$components = explode('/', $criterion); |
|
183
|
45 |
|
|
|
184
|
38 |
|
if (count($components) < 2) { |
|
185
|
|
|
return ''; |
|
186
|
|
|
} |
|
187
|
45 |
|
|
|
188
|
|
|
return implode('/', array_slice($components, 0, -1)); |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
/** |
|
192
|
|
|
* Validate the extensions and return a collection. |
|
193
|
|
|
* |
|
194
|
|
|
* @since 0.1.1 |
|
195
|
|
|
* |
|
196
|
|
|
* @param Extensions|array|string|null $extensions Extensions to validate. |
|
197
|
|
|
* |
|
198
|
|
|
* @return Extensions Validated extensions collection. |
|
199
|
|
|
*/ |
|
200
|
|
|
protected function validateExtensions($extensions): Extensions |
|
201
|
|
|
{ |
|
202
|
|
|
if (empty($extensions)) { |
|
203
|
|
|
$extensions = new Extensions(['']); |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
if (! $extensions instanceof Extensions) { |
|
207
|
|
|
$extensions = new Extensions((array)$extensions); |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
return $extensions; |
|
211
|
|
|
} |
|
212
|
|
|
} |
|
213
|
|
|
|
It seems like you are assigning to a variable which was imported through a
usestatement which was not imported by reference.For clarity, we suggest to use a different name or import by reference depending on whether you would like to have the change visibile in outer-scope.
Change not visible in outer-scope
Change visible in outer-scope