@@ -23,8 +23,8 @@ |
||
23 | 23 | */ |
24 | 24 | interface EngineFinder extends Finder |
25 | 25 | { |
26 | - // Constants to be used for the Config file sections. |
|
27 | - const CLASS_NAME_KEY = 'ClassName'; |
|
28 | - const ENGINES_KEY = 'Engines'; |
|
29 | - const NULL_OBJECT = 'NullObject'; |
|
26 | + // Constants to be used for the Config file sections. |
|
27 | + const CLASS_NAME_KEY = 'ClassName'; |
|
28 | + const ENGINES_KEY = 'Engines'; |
|
29 | + const NULL_OBJECT = 'NullObject'; |
|
30 | 30 | } |
@@ -15,35 +15,35 @@ |
||
15 | 15 | * Engine finder default configuration. |
16 | 16 | */ |
17 | 17 | $engineFinder = [ |
18 | - // Class to use for instantiating the EngineFinder implementation. |
|
19 | - Engine\EngineFinder::CLASS_NAME_KEY => Engine\BaseEngineFinder::class, |
|
20 | - // Engine implementations to register with the EngineFinder. |
|
21 | - Engine\EngineFinder::ENGINES_KEY => [ |
|
22 | - 'PHPEngine' => Engine\PHPEngine::class, |
|
23 | - ], |
|
24 | - // Null object implementation to use with the EngineFinder. |
|
25 | - Engine\EngineFinder::NULL_OBJECT => Engine\NullEngine::class, |
|
18 | + // Class to use for instantiating the EngineFinder implementation. |
|
19 | + Engine\EngineFinder::CLASS_NAME_KEY => Engine\BaseEngineFinder::class, |
|
20 | + // Engine implementations to register with the EngineFinder. |
|
21 | + Engine\EngineFinder::ENGINES_KEY => [ |
|
22 | + 'PHPEngine' => Engine\PHPEngine::class, |
|
23 | + ], |
|
24 | + // Null object implementation to use with the EngineFinder. |
|
25 | + Engine\EngineFinder::NULL_OBJECT => Engine\NullEngine::class, |
|
26 | 26 | ]; |
27 | 27 | |
28 | 28 | /* |
29 | 29 | * View finder default configuration. |
30 | 30 | */ |
31 | 31 | $viewFinder = [ |
32 | - // Class to use for instantiating the ViewFinder implementation. |
|
33 | - View\ViewFinder::CLASS_NAME_KEY => View\BaseViewFinder::class, |
|
34 | - // View implementations to register with the ViewFinder. |
|
35 | - View\ViewFinder::VIEWS_KEY => [ |
|
36 | - 'BaseView' => View\BaseView::class, |
|
37 | - ], |
|
38 | - // Null object implementation to use with the ViewFinder. |
|
39 | - View\ViewFinder::NULL_OBJECT => View\NullView::class, |
|
32 | + // Class to use for instantiating the ViewFinder implementation. |
|
33 | + View\ViewFinder::CLASS_NAME_KEY => View\BaseViewFinder::class, |
|
34 | + // View implementations to register with the ViewFinder. |
|
35 | + View\ViewFinder::VIEWS_KEY => [ |
|
36 | + 'BaseView' => View\BaseView::class, |
|
37 | + ], |
|
38 | + // Null object implementation to use with the ViewFinder. |
|
39 | + View\ViewFinder::NULL_OBJECT => View\NullView::class, |
|
40 | 40 | ]; |
41 | 41 | |
42 | 42 | return [ |
43 | - 'BrightNucleus' => [ |
|
44 | - 'View' => [ |
|
45 | - 'EngineFinder' => $engineFinder, |
|
46 | - 'ViewFinder' => $viewFinder, |
|
47 | - ], |
|
48 | - ], |
|
43 | + 'BrightNucleus' => [ |
|
44 | + 'View' => [ |
|
45 | + 'EngineFinder' => $engineFinder, |
|
46 | + 'ViewFinder' => $viewFinder, |
|
47 | + ], |
|
48 | + ], |
|
49 | 49 | ]; |
@@ -28,160 +28,160 @@ |
||
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 | - ? $this->getURIs($criteria)->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()); |
|
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 | - $criterion = URIHelper::getFilename($criterion); |
|
129 | - |
|
130 | - return empty($extension) || URIHelper::hasExtension($criterion, $extension) |
|
131 | - ? $criterion |
|
132 | - : $criterion . $extension; |
|
133 | - }, $criteria)[0]; |
|
134 | - |
|
135 | - return $this->arrayToRegexPattern(array_unique($names)); |
|
136 | - } |
|
137 | - |
|
138 | - /** |
|
139 | - * Get the path pattern to pass to the file finder. |
|
140 | - * |
|
141 | - * @since 0.1.3 |
|
142 | - * |
|
143 | - * @return string Path pattern to pass to the file finder. |
|
144 | - */ |
|
145 | - protected function getPathPattern(): string |
|
146 | - { |
|
147 | - return $this->path; |
|
148 | - } |
|
149 | - |
|
150 | - /** |
|
151 | - * Get an array as a regular expression pattern string. |
|
152 | - * |
|
153 | - * @since 0.1.3 |
|
154 | - * |
|
155 | - * @param array $array Array to generate the pattern for. |
|
156 | - * |
|
157 | - * @return string Generated regular expression pattern. |
|
158 | - */ |
|
159 | - protected function arrayToRegexPattern(array $array): string |
|
160 | - { |
|
161 | - $array = array_map('preg_quote', $array); |
|
162 | - |
|
163 | - return '/' . implode('|', $array) . '/'; |
|
164 | - } |
|
165 | - |
|
166 | - /** |
|
167 | - * Validate the extensions and return a collection. |
|
168 | - * |
|
169 | - * @since 0.1.1 |
|
170 | - * |
|
171 | - * @param Extensions|array|string|null $extensions Extensions to validate. |
|
172 | - * |
|
173 | - * @return Extensions Validated extensions collection. |
|
174 | - */ |
|
175 | - protected function validateExtensions($extensions): Extensions |
|
176 | - { |
|
177 | - if (empty($extensions)) { |
|
178 | - $extensions = new Extensions(['']); |
|
179 | - } |
|
180 | - |
|
181 | - if (! $extensions instanceof Extensions) { |
|
182 | - $extensions = new Extensions((array)$extensions); |
|
183 | - } |
|
184 | - |
|
185 | - return $extensions; |
|
186 | - } |
|
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 | + ? $this->getURIs($criteria)->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()); |
|
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 | + $criterion = URIHelper::getFilename($criterion); |
|
129 | + |
|
130 | + return empty($extension) || URIHelper::hasExtension($criterion, $extension) |
|
131 | + ? $criterion |
|
132 | + : $criterion . $extension; |
|
133 | + }, $criteria)[0]; |
|
134 | + |
|
135 | + return $this->arrayToRegexPattern(array_unique($names)); |
|
136 | + } |
|
137 | + |
|
138 | + /** |
|
139 | + * Get the path pattern to pass to the file finder. |
|
140 | + * |
|
141 | + * @since 0.1.3 |
|
142 | + * |
|
143 | + * @return string Path pattern to pass to the file finder. |
|
144 | + */ |
|
145 | + protected function getPathPattern(): string |
|
146 | + { |
|
147 | + return $this->path; |
|
148 | + } |
|
149 | + |
|
150 | + /** |
|
151 | + * Get an array as a regular expression pattern string. |
|
152 | + * |
|
153 | + * @since 0.1.3 |
|
154 | + * |
|
155 | + * @param array $array Array to generate the pattern for. |
|
156 | + * |
|
157 | + * @return string Generated regular expression pattern. |
|
158 | + */ |
|
159 | + protected function arrayToRegexPattern(array $array): string |
|
160 | + { |
|
161 | + $array = array_map('preg_quote', $array); |
|
162 | + |
|
163 | + return '/' . implode('|', $array) . '/'; |
|
164 | + } |
|
165 | + |
|
166 | + /** |
|
167 | + * Validate the extensions and return a collection. |
|
168 | + * |
|
169 | + * @since 0.1.1 |
|
170 | + * |
|
171 | + * @param Extensions|array|string|null $extensions Extensions to validate. |
|
172 | + * |
|
173 | + * @return Extensions Validated extensions collection. |
|
174 | + */ |
|
175 | + protected function validateExtensions($extensions): Extensions |
|
176 | + { |
|
177 | + if (empty($extensions)) { |
|
178 | + $extensions = new Extensions(['']); |
|
179 | + } |
|
180 | + |
|
181 | + if (! $extensions instanceof Extensions) { |
|
182 | + $extensions = new Extensions((array)$extensions); |
|
183 | + } |
|
184 | + |
|
185 | + return $extensions; |
|
186 | + } |
|
187 | 187 | } |
@@ -124,12 +124,12 @@ 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 | $criterion = URIHelper::getFilename($criterion); |
129 | 129 | |
130 | 130 | return empty($extension) || URIHelper::hasExtension($criterion, $extension) |
131 | 131 | ? $criterion |
132 | - : $criterion . $extension; |
|
132 | + : $criterion.$extension; |
|
133 | 133 | }, $criteria)[0]; |
134 | 134 | |
135 | 135 | return $this->arrayToRegexPattern(array_unique($names)); |
@@ -160,7 +160,7 @@ discard block |
||
160 | 160 | { |
161 | 161 | $array = array_map('preg_quote', $array); |
162 | 162 | |
163 | - return '/' . implode('|', $array) . '/'; |
|
163 | + return '/'.implode('|', $array).'/'; |
|
164 | 164 | } |
165 | 165 | |
166 | 166 | /** |
@@ -178,8 +178,8 @@ discard block |
||
178 | 178 | $extensions = new Extensions(['']); |
179 | 179 | } |
180 | 180 | |
181 | - if (! $extensions instanceof Extensions) { |
|
182 | - $extensions = new Extensions((array)$extensions); |
|
181 | + if ( ! $extensions instanceof Extensions) { |
|
182 | + $extensions = new Extensions((array) $extensions); |
|
183 | 183 | } |
184 | 184 | |
185 | 185 | return $extensions; |