@@ -46,7 +46,7 @@ |
||
46 | 46 | * @param string $uri URI to render. |
47 | 47 | * @param array $context Context in which to render. |
48 | 48 | * |
49 | - * @return callable Rendering callback. |
|
49 | + * @return \Closure Rendering callback. |
|
50 | 50 | */ |
51 | 51 | public function getRenderCallback(string $uri, array $context = []): callable |
52 | 52 | { |
@@ -24,32 +24,32 @@ |
||
24 | 24 | class NullEngine implements Engine, NullFindable |
25 | 25 | { |
26 | 26 | |
27 | - /** |
|
28 | - * Check whether the Findable can handle an individual criterion. |
|
29 | - * |
|
30 | - * @since 0.1.0 |
|
31 | - * |
|
32 | - * @param mixed $criterion Criterion to check. |
|
33 | - * |
|
34 | - * @return bool Whether the Findable can handle the criterion. |
|
35 | - */ |
|
36 | - public function canHandle($criterion): bool |
|
37 | - { |
|
38 | - return true; |
|
39 | - } |
|
27 | + /** |
|
28 | + * Check whether the Findable can handle an individual criterion. |
|
29 | + * |
|
30 | + * @since 0.1.0 |
|
31 | + * |
|
32 | + * @param mixed $criterion Criterion to check. |
|
33 | + * |
|
34 | + * @return bool Whether the Findable can handle the criterion. |
|
35 | + */ |
|
36 | + public function canHandle($criterion): bool |
|
37 | + { |
|
38 | + return true; |
|
39 | + } |
|
40 | 40 | |
41 | - /** |
|
42 | - * Get the rendering callback for a given URI. |
|
43 | - * |
|
44 | - * @since 0.1.0 |
|
45 | - * |
|
46 | - * @param string $uri URI to render. |
|
47 | - * @param array $context Context in which to render. |
|
48 | - * |
|
49 | - * @return callable Rendering callback. |
|
50 | - */ |
|
51 | - public function getRenderCallback(string $uri, array $context = []): callable |
|
52 | - { |
|
53 | - return function () { return ''; }; |
|
54 | - } |
|
41 | + /** |
|
42 | + * Get the rendering callback for a given URI. |
|
43 | + * |
|
44 | + * @since 0.1.0 |
|
45 | + * |
|
46 | + * @param string $uri URI to render. |
|
47 | + * @param array $context Context in which to render. |
|
48 | + * |
|
49 | + * @return callable Rendering callback. |
|
50 | + */ |
|
51 | + public function getRenderCallback(string $uri, array $context = []): callable |
|
52 | + { |
|
53 | + return function () { return ''; }; |
|
54 | + } |
|
55 | 55 | } |
@@ -50,6 +50,6 @@ |
||
50 | 50 | */ |
51 | 51 | public function getRenderCallback(string $uri, array $context = []): callable |
52 | 52 | { |
53 | - return function () { return ''; }; |
|
53 | + return function() { return ''; }; |
|
54 | 54 | } |
55 | 55 | } |
@@ -33,274 +33,274 @@ |
||
33 | 33 | class ViewBuilder |
34 | 34 | { |
35 | 35 | |
36 | - use ConfigTrait; |
|
36 | + use ConfigTrait; |
|
37 | 37 | |
38 | - const ENGINE_FINDER_KEY = 'EngineFinder'; |
|
39 | - const VIEW_FINDER_KEY = 'ViewFinder'; |
|
38 | + const ENGINE_FINDER_KEY = 'EngineFinder'; |
|
39 | + const VIEW_FINDER_KEY = 'ViewFinder'; |
|
40 | 40 | |
41 | - /** |
|
42 | - * BaseViewFinder instance. |
|
43 | - * |
|
44 | - * @since 0.1.0 |
|
45 | - * |
|
46 | - * @var ViewFinder |
|
47 | - */ |
|
48 | - protected $viewFinder; |
|
41 | + /** |
|
42 | + * BaseViewFinder instance. |
|
43 | + * |
|
44 | + * @since 0.1.0 |
|
45 | + * |
|
46 | + * @var ViewFinder |
|
47 | + */ |
|
48 | + protected $viewFinder; |
|
49 | 49 | |
50 | - /** |
|
51 | - * BaseEngineFinder instance. |
|
52 | - * |
|
53 | - * @since 0.1.0 |
|
54 | - * |
|
55 | - * @var EngineFinder |
|
56 | - */ |
|
57 | - protected $engineFinder; |
|
50 | + /** |
|
51 | + * BaseEngineFinder instance. |
|
52 | + * |
|
53 | + * @since 0.1.0 |
|
54 | + * |
|
55 | + * @var EngineFinder |
|
56 | + */ |
|
57 | + protected $engineFinder; |
|
58 | 58 | |
59 | - /** |
|
60 | - * Locations to scan for views. |
|
61 | - * |
|
62 | - * @since 0.1.0 |
|
63 | - * |
|
64 | - * @var Locations |
|
65 | - */ |
|
66 | - protected $locations; |
|
59 | + /** |
|
60 | + * Locations to scan for views. |
|
61 | + * |
|
62 | + * @since 0.1.0 |
|
63 | + * |
|
64 | + * @var Locations |
|
65 | + */ |
|
66 | + protected $locations; |
|
67 | 67 | |
68 | - /** |
|
69 | - * Instantiate a ViewBuilder object. |
|
70 | - * |
|
71 | - * @since 0.1.0 |
|
72 | - * |
|
73 | - * @param ConfigInterface $config Optional. Configuration settings. |
|
74 | - * @param ViewFinder|null $viewFinder Optional. ViewFinder instance. |
|
75 | - * @param EngineFinder|null $engineFinder Optional. EngineFinder instance. |
|
76 | - * |
|
77 | - * @throws FailedToProcessConfigException If the config could not be processed. |
|
78 | - */ |
|
79 | - public function __construct( |
|
80 | - ConfigInterface $config = null, |
|
81 | - ViewFinder $viewFinder = null, |
|
82 | - EngineFinder $engineFinder = null |
|
83 | - ) { |
|
84 | - $this->processConfig($this->getConfig($config)); |
|
85 | - $this->viewFinder = $viewFinder ?? $this->getViewFinder(); |
|
86 | - $this->engineFinder = $engineFinder ?? $this->getEngineFinder(); |
|
87 | - $this->locations = new Locations(); |
|
88 | - } |
|
68 | + /** |
|
69 | + * Instantiate a ViewBuilder object. |
|
70 | + * |
|
71 | + * @since 0.1.0 |
|
72 | + * |
|
73 | + * @param ConfigInterface $config Optional. Configuration settings. |
|
74 | + * @param ViewFinder|null $viewFinder Optional. ViewFinder instance. |
|
75 | + * @param EngineFinder|null $engineFinder Optional. EngineFinder instance. |
|
76 | + * |
|
77 | + * @throws FailedToProcessConfigException If the config could not be processed. |
|
78 | + */ |
|
79 | + public function __construct( |
|
80 | + ConfigInterface $config = null, |
|
81 | + ViewFinder $viewFinder = null, |
|
82 | + EngineFinder $engineFinder = null |
|
83 | + ) { |
|
84 | + $this->processConfig($this->getConfig($config)); |
|
85 | + $this->viewFinder = $viewFinder ?? $this->getViewFinder(); |
|
86 | + $this->engineFinder = $engineFinder ?? $this->getEngineFinder(); |
|
87 | + $this->locations = new Locations(); |
|
88 | + } |
|
89 | 89 | |
90 | - /** |
|
91 | - * Create a new view for a given URI. |
|
92 | - * |
|
93 | - * @since 0.1.0 |
|
94 | - * |
|
95 | - * @param string $view View identifier to create a view for. |
|
96 | - * @param mixed $type Type of view to create. |
|
97 | - * |
|
98 | - * @return View Instance of the requested view. |
|
99 | - * @throws FailedToInstantiateView If the view could not be instantiated. |
|
100 | - */ |
|
101 | - public function create(string $view, $type = null): View |
|
102 | - { |
|
103 | - $uri = $this->scanLocations([$view]); |
|
90 | + /** |
|
91 | + * Create a new view for a given URI. |
|
92 | + * |
|
93 | + * @since 0.1.0 |
|
94 | + * |
|
95 | + * @param string $view View identifier to create a view for. |
|
96 | + * @param mixed $type Type of view to create. |
|
97 | + * |
|
98 | + * @return View Instance of the requested view. |
|
99 | + * @throws FailedToInstantiateView If the view could not be instantiated. |
|
100 | + */ |
|
101 | + public function create(string $view, $type = null): View |
|
102 | + { |
|
103 | + $uri = $this->scanLocations([$view]); |
|
104 | 104 | |
105 | - $engine = $uri |
|
106 | - ? $this->getEngine($uri) |
|
107 | - : false; |
|
105 | + $engine = $uri |
|
106 | + ? $this->getEngine($uri) |
|
107 | + : false; |
|
108 | 108 | |
109 | - return ($uri && $engine) |
|
110 | - ? $this->getView($uri, $engine, $type) |
|
111 | - : $this->getViewFinder()->getNullObject(); |
|
112 | - } |
|
109 | + return ($uri && $engine) |
|
110 | + ? $this->getView($uri, $engine, $type) |
|
111 | + : $this->getViewFinder()->getNullObject(); |
|
112 | + } |
|
113 | 113 | |
114 | - /** |
|
115 | - * Get an Engine that can deal with the given URI. |
|
116 | - * |
|
117 | - * @since 0.1.0 |
|
118 | - * |
|
119 | - * @param string $uri URI to get an engine for. |
|
120 | - * |
|
121 | - * @return Engine Instance of an engine that can deal with the given URI. |
|
122 | - */ |
|
123 | - public function getEngine(string $uri): Engine |
|
124 | - { |
|
125 | - return $this->getEngineFinder()->find([$uri]); |
|
126 | - } |
|
114 | + /** |
|
115 | + * Get an Engine that can deal with the given URI. |
|
116 | + * |
|
117 | + * @since 0.1.0 |
|
118 | + * |
|
119 | + * @param string $uri URI to get an engine for. |
|
120 | + * |
|
121 | + * @return Engine Instance of an engine that can deal with the given URI. |
|
122 | + */ |
|
123 | + public function getEngine(string $uri): Engine |
|
124 | + { |
|
125 | + return $this->getEngineFinder()->find([$uri]); |
|
126 | + } |
|
127 | 127 | |
128 | - /** |
|
129 | - * Get a view for a given URI, engine and type. |
|
130 | - * |
|
131 | - * @since 0.1.0 |
|
132 | - * |
|
133 | - * @param string $uri URI to get a view for. |
|
134 | - * @param Engine $engine Engine to use for the view. |
|
135 | - * @param mixed $type Type of view to get. |
|
136 | - * |
|
137 | - * @return View View that matches the given requirements. |
|
138 | - * @throws FailedToInstantiateView If the view could not be instantiated. |
|
139 | - */ |
|
140 | - public function getView(string $uri, Engine $engine, $type = null): View |
|
141 | - { |
|
142 | - if (null === $type) { |
|
143 | - $view = $this->getViewFinder()->find([$uri], $engine); |
|
144 | - return $view->setBuilder( $this ); |
|
145 | - } |
|
128 | + /** |
|
129 | + * Get a view for a given URI, engine and type. |
|
130 | + * |
|
131 | + * @since 0.1.0 |
|
132 | + * |
|
133 | + * @param string $uri URI to get a view for. |
|
134 | + * @param Engine $engine Engine to use for the view. |
|
135 | + * @param mixed $type Type of view to get. |
|
136 | + * |
|
137 | + * @return View View that matches the given requirements. |
|
138 | + * @throws FailedToInstantiateView If the view could not be instantiated. |
|
139 | + */ |
|
140 | + public function getView(string $uri, Engine $engine, $type = null): View |
|
141 | + { |
|
142 | + if (null === $type) { |
|
143 | + $view = $this->getViewFinder()->find([$uri], $engine); |
|
144 | + return $view->setBuilder( $this ); |
|
145 | + } |
|
146 | 146 | |
147 | - return $this->resolveType($type, $uri, $engine); |
|
148 | - } |
|
147 | + return $this->resolveType($type, $uri, $engine); |
|
148 | + } |
|
149 | 149 | |
150 | - /** |
|
151 | - * Get the ViewFinder instance. |
|
152 | - * |
|
153 | - * @since 0.1.0 |
|
154 | - * |
|
155 | - * @return ViewFinder Instance of a BaseViewFinder. |
|
156 | - */ |
|
157 | - public function getViewFinder(): ViewFinder |
|
158 | - { |
|
159 | - return $this->viewFinder ?? $this->getFinder(static::VIEW_FINDER_KEY); |
|
160 | - } |
|
150 | + /** |
|
151 | + * Get the ViewFinder instance. |
|
152 | + * |
|
153 | + * @since 0.1.0 |
|
154 | + * |
|
155 | + * @return ViewFinder Instance of a BaseViewFinder. |
|
156 | + */ |
|
157 | + public function getViewFinder(): ViewFinder |
|
158 | + { |
|
159 | + return $this->viewFinder ?? $this->getFinder(static::VIEW_FINDER_KEY); |
|
160 | + } |
|
161 | 161 | |
162 | - /** |
|
163 | - * Get the EngineFinder instance. |
|
164 | - * |
|
165 | - * @since 0.1.0 |
|
166 | - * |
|
167 | - * @return EngineFinder Instance of a BaseEngineFinder. |
|
168 | - */ |
|
169 | - public function getEngineFinder(): EngineFinder |
|
170 | - { |
|
171 | - return $this->engineFinder ?? $this->getFinder(static::ENGINE_FINDER_KEY); |
|
172 | - } |
|
162 | + /** |
|
163 | + * Get the EngineFinder instance. |
|
164 | + * |
|
165 | + * @since 0.1.0 |
|
166 | + * |
|
167 | + * @return EngineFinder Instance of a BaseEngineFinder. |
|
168 | + */ |
|
169 | + public function getEngineFinder(): EngineFinder |
|
170 | + { |
|
171 | + return $this->engineFinder ?? $this->getFinder(static::ENGINE_FINDER_KEY); |
|
172 | + } |
|
173 | 173 | |
174 | - /** |
|
175 | - * Add a location to scan with the BaseViewFinder. |
|
176 | - * |
|
177 | - * @since 0.1.0 |
|
178 | - * |
|
179 | - * @param Location $location Location to scan with the BaseViewFinder. |
|
180 | - * |
|
181 | - * @return static |
|
182 | - */ |
|
183 | - public function addLocation(Location $location) |
|
184 | - { |
|
185 | - $this->locations->add($location); |
|
174 | + /** |
|
175 | + * Add a location to scan with the BaseViewFinder. |
|
176 | + * |
|
177 | + * @since 0.1.0 |
|
178 | + * |
|
179 | + * @param Location $location Location to scan with the BaseViewFinder. |
|
180 | + * |
|
181 | + * @return static |
|
182 | + */ |
|
183 | + public function addLocation(Location $location) |
|
184 | + { |
|
185 | + $this->locations->add($location); |
|
186 | 186 | |
187 | - return $this; |
|
188 | - } |
|
187 | + return $this; |
|
188 | + } |
|
189 | 189 | |
190 | - /** |
|
191 | - * Get the collection of locations registered with this ViewBuilder. |
|
192 | - * |
|
193 | - * @since 0.1.3 |
|
194 | - * |
|
195 | - * @return Locations Collection of locations. |
|
196 | - */ |
|
197 | - public function getLocations() |
|
198 | - { |
|
199 | - return $this->locations; |
|
200 | - } |
|
190 | + /** |
|
191 | + * Get the collection of locations registered with this ViewBuilder. |
|
192 | + * |
|
193 | + * @since 0.1.3 |
|
194 | + * |
|
195 | + * @return Locations Collection of locations. |
|
196 | + */ |
|
197 | + public function getLocations() |
|
198 | + { |
|
199 | + return $this->locations; |
|
200 | + } |
|
201 | 201 | |
202 | - /** |
|
203 | - * Scan Locations for an URI that matches the specified criteria. |
|
204 | - * |
|
205 | - * @since 0.1.0 |
|
206 | - * |
|
207 | - * @param array $criteria Criteria to match. |
|
208 | - * |
|
209 | - * @return string|false URI of the requested view, or false if not found. |
|
210 | - */ |
|
211 | - public function scanLocations(array $criteria) |
|
212 | - { |
|
213 | - $uris = $this->locations->map(function ($location) use ($criteria) { |
|
214 | - /** @var Location $location */ |
|
215 | - return $location->getURI($criteria); |
|
216 | - })->filter(function ($uri) { |
|
217 | - return false !== $uri; |
|
218 | - }); |
|
202 | + /** |
|
203 | + * Scan Locations for an URI that matches the specified criteria. |
|
204 | + * |
|
205 | + * @since 0.1.0 |
|
206 | + * |
|
207 | + * @param array $criteria Criteria to match. |
|
208 | + * |
|
209 | + * @return string|false URI of the requested view, or false if not found. |
|
210 | + */ |
|
211 | + public function scanLocations(array $criteria) |
|
212 | + { |
|
213 | + $uris = $this->locations->map(function ($location) use ($criteria) { |
|
214 | + /** @var Location $location */ |
|
215 | + return $location->getURI($criteria); |
|
216 | + })->filter(function ($uri) { |
|
217 | + return false !== $uri; |
|
218 | + }); |
|
219 | 219 | |
220 | - // Fall back for absolute paths on current filesystem. |
|
221 | - if ($uris->isEmpty()) { |
|
222 | - foreach ($criteria as $criterion) { |
|
223 | - if (file_exists($criterion)) { |
|
224 | - return $criterion; |
|
225 | - } |
|
226 | - } |
|
227 | - } |
|
220 | + // Fall back for absolute paths on current filesystem. |
|
221 | + if ($uris->isEmpty()) { |
|
222 | + foreach ($criteria as $criterion) { |
|
223 | + if (file_exists($criterion)) { |
|
224 | + return $criterion; |
|
225 | + } |
|
226 | + } |
|
227 | + } |
|
228 | 228 | |
229 | - return $uris->isEmpty() ? false : $uris->first(); |
|
230 | - } |
|
229 | + return $uris->isEmpty() ? false : $uris->first(); |
|
230 | + } |
|
231 | 231 | |
232 | - /** |
|
233 | - * Get a finder instance. |
|
234 | - * |
|
235 | - * @since 0.1.1 |
|
236 | - * |
|
237 | - * @param string $key Configuration key to use. |
|
238 | - * |
|
239 | - * @return ViewFinder|EngineFinder The requested finder instance. |
|
240 | - */ |
|
241 | - protected function getFinder($key) |
|
242 | - { |
|
243 | - $finderClass = $this->config->getKey($key, 'ClassName'); |
|
244 | - return new $finderClass($this->config->getSubConfig($key)); |
|
245 | - } |
|
232 | + /** |
|
233 | + * Get a finder instance. |
|
234 | + * |
|
235 | + * @since 0.1.1 |
|
236 | + * |
|
237 | + * @param string $key Configuration key to use. |
|
238 | + * |
|
239 | + * @return ViewFinder|EngineFinder The requested finder instance. |
|
240 | + */ |
|
241 | + protected function getFinder($key) |
|
242 | + { |
|
243 | + $finderClass = $this->config->getKey($key, 'ClassName'); |
|
244 | + return new $finderClass($this->config->getSubConfig($key)); |
|
245 | + } |
|
246 | 246 | |
247 | - /** |
|
248 | - * Resolve the view type. |
|
249 | - * |
|
250 | - * @since 0.1.0 |
|
251 | - * |
|
252 | - * @param mixed $type Type of view that was requested. |
|
253 | - * @param string $uri URI to get a view for. |
|
254 | - * @param Engine|null $engine Engine to use for the view. |
|
255 | - * |
|
256 | - * @return View Resolved View object. |
|
257 | - * @throws FailedToInstantiateView If the view type could not be resolved. |
|
258 | - */ |
|
259 | - protected function resolveType($type, string $uri, Engine $engine = null): View |
|
260 | - { |
|
261 | - $configKey = [static::VIEW_FINDER_KEY, 'Views', $type]; |
|
247 | + /** |
|
248 | + * Resolve the view type. |
|
249 | + * |
|
250 | + * @since 0.1.0 |
|
251 | + * |
|
252 | + * @param mixed $type Type of view that was requested. |
|
253 | + * @param string $uri URI to get a view for. |
|
254 | + * @param Engine|null $engine Engine to use for the view. |
|
255 | + * |
|
256 | + * @return View Resolved View object. |
|
257 | + * @throws FailedToInstantiateView If the view type could not be resolved. |
|
258 | + */ |
|
259 | + protected function resolveType($type, string $uri, Engine $engine = null): View |
|
260 | + { |
|
261 | + $configKey = [static::VIEW_FINDER_KEY, 'Views', $type]; |
|
262 | 262 | |
263 | - if (is_string($type) && $this->config->hasKey($configKey)) { |
|
264 | - $className = $this->config->getKey($configKey); |
|
265 | - $type = new $className($uri, $engine, $this); |
|
266 | - } |
|
263 | + if (is_string($type) && $this->config->hasKey($configKey)) { |
|
264 | + $className = $this->config->getKey($configKey); |
|
265 | + $type = new $className($uri, $engine, $this); |
|
266 | + } |
|
267 | 267 | |
268 | - if (is_string($type)) { |
|
269 | - $type = new $type($uri, $engine, $this); |
|
270 | - } |
|
268 | + if (is_string($type)) { |
|
269 | + $type = new $type($uri, $engine, $this); |
|
270 | + } |
|
271 | 271 | |
272 | - if (is_callable($type)) { |
|
273 | - $type = $type($uri, $engine, $this); |
|
274 | - } |
|
272 | + if (is_callable($type)) { |
|
273 | + $type = $type($uri, $engine, $this); |
|
274 | + } |
|
275 | 275 | |
276 | - if (! $type instanceof View) { |
|
277 | - throw new FailedToInstantiateView( |
|
278 | - sprintf( |
|
279 | - _('Could not instantiate view "%s".'), |
|
280 | - serialize($type) |
|
281 | - ) |
|
282 | - ); |
|
283 | - } |
|
276 | + if (! $type instanceof View) { |
|
277 | + throw new FailedToInstantiateView( |
|
278 | + sprintf( |
|
279 | + _('Could not instantiate view "%s".'), |
|
280 | + serialize($type) |
|
281 | + ) |
|
282 | + ); |
|
283 | + } |
|
284 | 284 | |
285 | - return $type; |
|
286 | - } |
|
285 | + return $type; |
|
286 | + } |
|
287 | 287 | |
288 | - /** |
|
289 | - * Get the configuration to use in the ViewBuilder. |
|
290 | - * |
|
291 | - * @since 0.2.0 |
|
292 | - * |
|
293 | - * @param ConfigInterface|array $config Config to merge with defaults. |
|
294 | - * |
|
295 | - * @return ConfigInterface Configuration passed in through the constructor. |
|
296 | - */ |
|
297 | - protected function getConfig($config = []): ConfigInterface |
|
298 | - { |
|
299 | - $defaults = ConfigFactory::create(dirname(__DIR__, 2) . '/config/defaults.php', $config); |
|
300 | - $config = $config |
|
301 | - ? ConfigFactory::createFromArray(array_merge_recursive($defaults->getArrayCopy(), $config->getArrayCopy())) |
|
302 | - : $defaults; |
|
288 | + /** |
|
289 | + * Get the configuration to use in the ViewBuilder. |
|
290 | + * |
|
291 | + * @since 0.2.0 |
|
292 | + * |
|
293 | + * @param ConfigInterface|array $config Config to merge with defaults. |
|
294 | + * |
|
295 | + * @return ConfigInterface Configuration passed in through the constructor. |
|
296 | + */ |
|
297 | + protected function getConfig($config = []): ConfigInterface |
|
298 | + { |
|
299 | + $defaults = ConfigFactory::create(dirname(__DIR__, 2) . '/config/defaults.php', $config); |
|
300 | + $config = $config |
|
301 | + ? ConfigFactory::createFromArray(array_merge_recursive($defaults->getArrayCopy(), $config->getArrayCopy())) |
|
302 | + : $defaults; |
|
303 | 303 | |
304 | - return $config->getSubConfig('BrightNucleus\View'); |
|
305 | - } |
|
304 | + return $config->getSubConfig('BrightNucleus\View'); |
|
305 | + } |
|
306 | 306 | } |
@@ -141,7 +141,7 @@ discard block |
||
141 | 141 | { |
142 | 142 | if (null === $type) { |
143 | 143 | $view = $this->getViewFinder()->find([$uri], $engine); |
144 | - return $view->setBuilder( $this ); |
|
144 | + return $view->setBuilder($this); |
|
145 | 145 | } |
146 | 146 | |
147 | 147 | return $this->resolveType($type, $uri, $engine); |
@@ -210,10 +210,10 @@ discard block |
||
210 | 210 | */ |
211 | 211 | public function scanLocations(array $criteria) |
212 | 212 | { |
213 | - $uris = $this->locations->map(function ($location) use ($criteria) { |
|
213 | + $uris = $this->locations->map(function($location) use ($criteria) { |
|
214 | 214 | /** @var Location $location */ |
215 | 215 | return $location->getURI($criteria); |
216 | - })->filter(function ($uri) { |
|
216 | + })->filter(function($uri) { |
|
217 | 217 | return false !== $uri; |
218 | 218 | }); |
219 | 219 | |
@@ -273,7 +273,7 @@ discard block |
||
273 | 273 | $type = $type($uri, $engine, $this); |
274 | 274 | } |
275 | 275 | |
276 | - if (! $type instanceof View) { |
|
276 | + if ( ! $type instanceof View) { |
|
277 | 277 | throw new FailedToInstantiateView( |
278 | 278 | sprintf( |
279 | 279 | _('Could not instantiate view "%s".'), |
@@ -296,7 +296,7 @@ discard block |
||
296 | 296 | */ |
297 | 297 | protected function getConfig($config = []): ConfigInterface |
298 | 298 | { |
299 | - $defaults = ConfigFactory::create(dirname(__DIR__, 2) . '/config/defaults.php', $config); |
|
299 | + $defaults = ConfigFactory::create(dirname(__DIR__, 2).'/config/defaults.php', $config); |
|
300 | 300 | $config = $config |
301 | 301 | ? ConfigFactory::createFromArray(array_merge_recursive($defaults->getArrayCopy(), $config->getArrayCopy())) |
302 | 302 | : $defaults; |
@@ -26,29 +26,29 @@ |
||
26 | 26 | interface ViewFinder extends Finder |
27 | 27 | { |
28 | 28 | |
29 | - // Constants to be used for the Config file sections. |
|
30 | - const CLASS_NAME_KEY = 'ClassName'; |
|
31 | - const VIEWS_KEY = 'Views'; |
|
32 | - const NULL_OBJECT = 'NullObject'; |
|
29 | + // Constants to be used for the Config file sections. |
|
30 | + const CLASS_NAME_KEY = 'ClassName'; |
|
31 | + const VIEWS_KEY = 'Views'; |
|
32 | + const NULL_OBJECT = 'NullObject'; |
|
33 | 33 | |
34 | - /** |
|
35 | - * Find a result based on a specific criteria. |
|
36 | - * |
|
37 | - * @since 0.1.0 |
|
38 | - * |
|
39 | - * @param array $criteria Criteria to search for. |
|
40 | - * @param Engine|null $engine Optional. Engine to use with the view. |
|
41 | - * |
|
42 | - * @return View View that was found. |
|
43 | - */ |
|
44 | - public function find(array $criteria, Engine $engine = null): View; |
|
34 | + /** |
|
35 | + * Find a result based on a specific criteria. |
|
36 | + * |
|
37 | + * @since 0.1.0 |
|
38 | + * |
|
39 | + * @param array $criteria Criteria to search for. |
|
40 | + * @param Engine|null $engine Optional. Engine to use with the view. |
|
41 | + * |
|
42 | + * @return View View that was found. |
|
43 | + */ |
|
44 | + public function find(array $criteria, Engine $engine = null): View; |
|
45 | 45 | |
46 | - /** |
|
47 | - * Get the NullObject. |
|
48 | - * |
|
49 | - * @since 0.1.1 |
|
50 | - * |
|
51 | - * @return NullView NullObject for the current Finder. |
|
52 | - */ |
|
53 | - public function getNullObject(): NullView; |
|
46 | + /** |
|
47 | + * Get the NullObject. |
|
48 | + * |
|
49 | + * @since 0.1.1 |
|
50 | + * |
|
51 | + * @return NullView NullObject for the current Finder. |
|
52 | + */ |
|
53 | + public function getNullObject(): NullView; |
|
54 | 54 | } |
@@ -27,56 +27,56 @@ |
||
27 | 27 | class BaseViewFinder extends AbstractFinder implements ViewFinder |
28 | 28 | { |
29 | 29 | |
30 | - /** |
|
31 | - * Find a result based on a specific criteria. |
|
32 | - * |
|
33 | - * @since 0.1.0 |
|
34 | - * |
|
35 | - * @param array $criteria Criteria to search for. |
|
36 | - * @param Engine|null $engine Optional. Engine to use with the view. |
|
37 | - * |
|
38 | - * @return View View that was found. |
|
39 | - * @throws FailedToInstantiateFindable If the Findable could not be instantiated. |
|
40 | - */ |
|
41 | - public function find(array $criteria, Engine $engine = null): View |
|
42 | - { |
|
43 | - $uri = $criteria[0]; |
|
30 | + /** |
|
31 | + * Find a result based on a specific criteria. |
|
32 | + * |
|
33 | + * @since 0.1.0 |
|
34 | + * |
|
35 | + * @param array $criteria Criteria to search for. |
|
36 | + * @param Engine|null $engine Optional. Engine to use with the view. |
|
37 | + * |
|
38 | + * @return View View that was found. |
|
39 | + * @throws FailedToInstantiateFindable If the Findable could not be instantiated. |
|
40 | + */ |
|
41 | + public function find(array $criteria, Engine $engine = null): View |
|
42 | + { |
|
43 | + $uri = $criteria[0]; |
|
44 | 44 | |
45 | - $views = $this->initializeFindables([$uri, $engine]); |
|
45 | + $views = $this->initializeFindables([$uri, $engine]); |
|
46 | 46 | |
47 | - foreach ($criteria as $entry) { |
|
48 | - foreach ($views as $viewObject) { |
|
49 | - if ($viewObject->canHandle($entry)) { |
|
50 | - return $viewObject; |
|
51 | - } |
|
52 | - } |
|
53 | - } |
|
47 | + foreach ($criteria as $entry) { |
|
48 | + foreach ($views as $viewObject) { |
|
49 | + if ($viewObject->canHandle($entry)) { |
|
50 | + return $viewObject; |
|
51 | + } |
|
52 | + } |
|
53 | + } |
|
54 | 54 | |
55 | - return $this->getNullObject(); |
|
56 | - } |
|
55 | + return $this->getNullObject(); |
|
56 | + } |
|
57 | 57 | |
58 | - /** |
|
59 | - * Get the config key for the Findables definitions. |
|
60 | - * |
|
61 | - * @since 0.1.0 |
|
62 | - * |
|
63 | - * @return string Config key use to define the Findables. |
|
64 | - */ |
|
65 | - protected function getFindablesConfigKey(): string |
|
66 | - { |
|
67 | - return 'Views'; |
|
68 | - } |
|
58 | + /** |
|
59 | + * Get the config key for the Findables definitions. |
|
60 | + * |
|
61 | + * @since 0.1.0 |
|
62 | + * |
|
63 | + * @return string Config key use to define the Findables. |
|
64 | + */ |
|
65 | + protected function getFindablesConfigKey(): string |
|
66 | + { |
|
67 | + return 'Views'; |
|
68 | + } |
|
69 | 69 | |
70 | - /** |
|
71 | - * Get the NullObject. |
|
72 | - * |
|
73 | - * @since 0.1.1 |
|
74 | - * |
|
75 | - * @return NullView NullObject for the current Finder. |
|
76 | - * @throws FailedToInstantiateFindable If the Findable could not be instantiated. |
|
77 | - */ |
|
78 | - public function getNullObject(): NullView |
|
79 | - { |
|
80 | - return parent::getNullObject(); |
|
81 | - } |
|
70 | + /** |
|
71 | + * Get the NullObject. |
|
72 | + * |
|
73 | + * @since 0.1.1 |
|
74 | + * |
|
75 | + * @return NullView NullObject for the current Finder. |
|
76 | + * @throws FailedToInstantiateFindable If the Findable could not be instantiated. |
|
77 | + */ |
|
78 | + public function getNullObject(): NullView |
|
79 | + { |
|
80 | + return parent::getNullObject(); |
|
81 | + } |
|
82 | 82 | } |
@@ -30,181 +30,181 @@ |
||
30 | 30 | abstract class AbstractView implements View |
31 | 31 | { |
32 | 32 | |
33 | - /** |
|
34 | - * URI of the view. |
|
35 | - * |
|
36 | - * The underscores are used to prevent accidental use of these properties from within the rendering closure. |
|
37 | - * |
|
38 | - * @since 0.1.0 |
|
39 | - * |
|
40 | - * @var string |
|
41 | - */ |
|
42 | - protected $_uri_; |
|
43 | - |
|
44 | - /** |
|
45 | - * Engine to use for the view. |
|
46 | - * |
|
47 | - * The underscores are used to prevent accidental use of these properties from within the rendering closure. |
|
48 | - * |
|
49 | - * @since 0.1.0 |
|
50 | - * |
|
51 | - * @var Engine |
|
52 | - */ |
|
53 | - protected $_engine_; |
|
54 | - |
|
55 | - /** |
|
56 | - * ViewBuilder instance. |
|
57 | - * |
|
58 | - * The underscores are used to prevent accidental use of these properties from within the rendering closure. |
|
59 | - * |
|
60 | - * @since 0.2.0 |
|
61 | - * |
|
62 | - * @var ViewBuilder |
|
63 | - */ |
|
64 | - protected $_builder_; |
|
65 | - |
|
66 | - /** |
|
67 | - * The context with which the view will be rendered. |
|
68 | - * |
|
69 | - * The underscores are used to prevent accidental use of these properties from within the rendering closure. |
|
70 | - * |
|
71 | - * @since 0.4.0 |
|
72 | - * |
|
73 | - * @var array |
|
74 | - */ |
|
75 | - protected $_context_ = []; |
|
76 | - |
|
77 | - /** |
|
78 | - * Instantiate an AbstractView object. |
|
79 | - * |
|
80 | - * @since 0.1.0 |
|
81 | - * |
|
82 | - * @param string $uri URI for the view. |
|
83 | - * @param Engine $engine Engine to use for the view. |
|
84 | - * @param ViewBuilder $viewBuilder View builder instance to use. |
|
85 | - */ |
|
86 | - public function __construct(string $uri, Engine $engine, ViewBuilder $viewBuilder = null) |
|
87 | - { |
|
88 | - $this->_uri_ = $uri; |
|
89 | - $this->_engine_ = $engine; |
|
90 | - $this->_builder_ = $viewBuilder ?? Views::getViewBuilder(); |
|
91 | - } |
|
92 | - |
|
93 | - /** |
|
94 | - * Render the view. |
|
95 | - * |
|
96 | - * @since 0.1.0 |
|
97 | - * |
|
98 | - * @param array $context Optional. The context in which to render the view. |
|
99 | - * @param bool $echo Optional. Whether to echo the output immediately. Defaults to false. |
|
100 | - * |
|
101 | - * @return string Rendered HTML. |
|
102 | - * @throws FailedToProcessConfigException If the Config could not be processed. |
|
103 | - */ |
|
104 | - public function render(array $context = [], bool $echo = false): string |
|
105 | - { |
|
106 | - $this->assimilateContext($context); |
|
107 | - |
|
108 | - $closure = Closure::bind( |
|
109 | - $this->_engine_->getRenderCallback($this->_uri_, $context), |
|
110 | - $this, |
|
111 | - static::class |
|
112 | - ); |
|
113 | - |
|
114 | - $output = $closure(); |
|
115 | - |
|
116 | - if ($echo) { |
|
117 | - echo $output; |
|
118 | - } |
|
119 | - |
|
120 | - return $output; |
|
121 | - } |
|
122 | - |
|
123 | - /** |
|
124 | - * Render a partial view (or section) for a given URI. |
|
125 | - * |
|
126 | - * @since 0.2.0 |
|
127 | - * |
|
128 | - * @param string $view View identifier to create a view for. |
|
129 | - * @param array $context Optional. The context in which to render the view. |
|
130 | - * @param string|null $type Type of view to create. |
|
131 | - * |
|
132 | - * @return string Rendered HTML content. |
|
133 | - * @throws FailedToProcessConfigException If the Config could not be processed. |
|
134 | - * @throws FailedToInstantiateView If the View could not be instantiated. |
|
135 | - */ |
|
136 | - public function section(string $view, array $context = null, $type = null): string |
|
137 | - { |
|
138 | - if (null === $context) { |
|
139 | - $context = $this->_context_; |
|
140 | - } |
|
141 | - |
|
142 | - $viewObject = $this->_builder_->create($view, $type); |
|
143 | - |
|
144 | - return $viewObject->render($context); |
|
145 | - } |
|
146 | - |
|
147 | - /** |
|
148 | - * Get the entire array of contextual data. |
|
149 | - * |
|
150 | - * @since 0.4.0 |
|
151 | - * |
|
152 | - * @return array Array of contextual data. |
|
153 | - */ |
|
154 | - public function getContext(): array |
|
155 | - { |
|
156 | - return $this->_context_; |
|
157 | - } |
|
158 | - |
|
159 | - /** |
|
160 | - * Associate a view builder with this view. |
|
161 | - * |
|
162 | - * @since 0.2.0 |
|
163 | - * |
|
164 | - * @param ViewBuilder $builder |
|
165 | - * |
|
166 | - * @return View |
|
167 | - */ |
|
168 | - public function setBuilder(ViewBuilder $builder): View |
|
169 | - { |
|
170 | - $this->_builder_ = $builder; |
|
171 | - |
|
172 | - return $this; |
|
173 | - } |
|
174 | - |
|
175 | - /** |
|
176 | - * Assimilate the context to make it available as properties. |
|
177 | - * |
|
178 | - * @since 0.2.0 |
|
179 | - * |
|
180 | - * @param array $context Context to assimilate. |
|
181 | - */ |
|
182 | - protected function assimilateContext(array $context = []) |
|
183 | - { |
|
184 | - $this->_context_ = $context; |
|
185 | - foreach ($context as $key => $value) { |
|
186 | - $this->$key = $value; |
|
187 | - } |
|
188 | - } |
|
189 | - |
|
190 | - /** |
|
191 | - * Turn invokable objects as properties into methods of the view. |
|
192 | - * |
|
193 | - * @param string $method Method that was called on the view. |
|
194 | - * @param array $arguments Array of arguments that were used. |
|
195 | - * @return mixed Return value of the invokable object. |
|
196 | - */ |
|
197 | - public function __call($method, $arguments) { |
|
198 | - if ( ! property_exists($this, $method) |
|
199 | - || ! is_callable($this->$method)) { |
|
200 | - trigger_error( |
|
201 | - "Call to undefined method {$method} on a view.", |
|
202 | - E_USER_ERROR |
|
203 | - ); |
|
204 | - } |
|
205 | - |
|
206 | - $callable = $this->$method; |
|
207 | - |
|
208 | - return $callable(...$arguments); |
|
209 | - } |
|
33 | + /** |
|
34 | + * URI of the view. |
|
35 | + * |
|
36 | + * The underscores are used to prevent accidental use of these properties from within the rendering closure. |
|
37 | + * |
|
38 | + * @since 0.1.0 |
|
39 | + * |
|
40 | + * @var string |
|
41 | + */ |
|
42 | + protected $_uri_; |
|
43 | + |
|
44 | + /** |
|
45 | + * Engine to use for the view. |
|
46 | + * |
|
47 | + * The underscores are used to prevent accidental use of these properties from within the rendering closure. |
|
48 | + * |
|
49 | + * @since 0.1.0 |
|
50 | + * |
|
51 | + * @var Engine |
|
52 | + */ |
|
53 | + protected $_engine_; |
|
54 | + |
|
55 | + /** |
|
56 | + * ViewBuilder instance. |
|
57 | + * |
|
58 | + * The underscores are used to prevent accidental use of these properties from within the rendering closure. |
|
59 | + * |
|
60 | + * @since 0.2.0 |
|
61 | + * |
|
62 | + * @var ViewBuilder |
|
63 | + */ |
|
64 | + protected $_builder_; |
|
65 | + |
|
66 | + /** |
|
67 | + * The context with which the view will be rendered. |
|
68 | + * |
|
69 | + * The underscores are used to prevent accidental use of these properties from within the rendering closure. |
|
70 | + * |
|
71 | + * @since 0.4.0 |
|
72 | + * |
|
73 | + * @var array |
|
74 | + */ |
|
75 | + protected $_context_ = []; |
|
76 | + |
|
77 | + /** |
|
78 | + * Instantiate an AbstractView object. |
|
79 | + * |
|
80 | + * @since 0.1.0 |
|
81 | + * |
|
82 | + * @param string $uri URI for the view. |
|
83 | + * @param Engine $engine Engine to use for the view. |
|
84 | + * @param ViewBuilder $viewBuilder View builder instance to use. |
|
85 | + */ |
|
86 | + public function __construct(string $uri, Engine $engine, ViewBuilder $viewBuilder = null) |
|
87 | + { |
|
88 | + $this->_uri_ = $uri; |
|
89 | + $this->_engine_ = $engine; |
|
90 | + $this->_builder_ = $viewBuilder ?? Views::getViewBuilder(); |
|
91 | + } |
|
92 | + |
|
93 | + /** |
|
94 | + * Render the view. |
|
95 | + * |
|
96 | + * @since 0.1.0 |
|
97 | + * |
|
98 | + * @param array $context Optional. The context in which to render the view. |
|
99 | + * @param bool $echo Optional. Whether to echo the output immediately. Defaults to false. |
|
100 | + * |
|
101 | + * @return string Rendered HTML. |
|
102 | + * @throws FailedToProcessConfigException If the Config could not be processed. |
|
103 | + */ |
|
104 | + public function render(array $context = [], bool $echo = false): string |
|
105 | + { |
|
106 | + $this->assimilateContext($context); |
|
107 | + |
|
108 | + $closure = Closure::bind( |
|
109 | + $this->_engine_->getRenderCallback($this->_uri_, $context), |
|
110 | + $this, |
|
111 | + static::class |
|
112 | + ); |
|
113 | + |
|
114 | + $output = $closure(); |
|
115 | + |
|
116 | + if ($echo) { |
|
117 | + echo $output; |
|
118 | + } |
|
119 | + |
|
120 | + return $output; |
|
121 | + } |
|
122 | + |
|
123 | + /** |
|
124 | + * Render a partial view (or section) for a given URI. |
|
125 | + * |
|
126 | + * @since 0.2.0 |
|
127 | + * |
|
128 | + * @param string $view View identifier to create a view for. |
|
129 | + * @param array $context Optional. The context in which to render the view. |
|
130 | + * @param string|null $type Type of view to create. |
|
131 | + * |
|
132 | + * @return string Rendered HTML content. |
|
133 | + * @throws FailedToProcessConfigException If the Config could not be processed. |
|
134 | + * @throws FailedToInstantiateView If the View could not be instantiated. |
|
135 | + */ |
|
136 | + public function section(string $view, array $context = null, $type = null): string |
|
137 | + { |
|
138 | + if (null === $context) { |
|
139 | + $context = $this->_context_; |
|
140 | + } |
|
141 | + |
|
142 | + $viewObject = $this->_builder_->create($view, $type); |
|
143 | + |
|
144 | + return $viewObject->render($context); |
|
145 | + } |
|
146 | + |
|
147 | + /** |
|
148 | + * Get the entire array of contextual data. |
|
149 | + * |
|
150 | + * @since 0.4.0 |
|
151 | + * |
|
152 | + * @return array Array of contextual data. |
|
153 | + */ |
|
154 | + public function getContext(): array |
|
155 | + { |
|
156 | + return $this->_context_; |
|
157 | + } |
|
158 | + |
|
159 | + /** |
|
160 | + * Associate a view builder with this view. |
|
161 | + * |
|
162 | + * @since 0.2.0 |
|
163 | + * |
|
164 | + * @param ViewBuilder $builder |
|
165 | + * |
|
166 | + * @return View |
|
167 | + */ |
|
168 | + public function setBuilder(ViewBuilder $builder): View |
|
169 | + { |
|
170 | + $this->_builder_ = $builder; |
|
171 | + |
|
172 | + return $this; |
|
173 | + } |
|
174 | + |
|
175 | + /** |
|
176 | + * Assimilate the context to make it available as properties. |
|
177 | + * |
|
178 | + * @since 0.2.0 |
|
179 | + * |
|
180 | + * @param array $context Context to assimilate. |
|
181 | + */ |
|
182 | + protected function assimilateContext(array $context = []) |
|
183 | + { |
|
184 | + $this->_context_ = $context; |
|
185 | + foreach ($context as $key => $value) { |
|
186 | + $this->$key = $value; |
|
187 | + } |
|
188 | + } |
|
189 | + |
|
190 | + /** |
|
191 | + * Turn invokable objects as properties into methods of the view. |
|
192 | + * |
|
193 | + * @param string $method Method that was called on the view. |
|
194 | + * @param array $arguments Array of arguments that were used. |
|
195 | + * @return mixed Return value of the invokable object. |
|
196 | + */ |
|
197 | + public function __call($method, $arguments) { |
|
198 | + if ( ! property_exists($this, $method) |
|
199 | + || ! is_callable($this->$method)) { |
|
200 | + trigger_error( |
|
201 | + "Call to undefined method {$method} on a view.", |
|
202 | + E_USER_ERROR |
|
203 | + ); |
|
204 | + } |
|
205 | + |
|
206 | + $callable = $this->$method; |
|
207 | + |
|
208 | + return $callable(...$arguments); |
|
209 | + } |
|
210 | 210 | } |
@@ -28,158 +28,158 @@ |
||
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()); |
|
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.1.3 |
|
156 | - * |
|
157 | - * @return string Path pattern to pass to the file finder. |
|
158 | - */ |
|
159 | - protected function getPathPattern(): string |
|
160 | - { |
|
161 | - return $this->path; |
|
162 | - } |
|
163 | - |
|
164 | - /** |
|
165 | - * Validate the extensions and return a collection. |
|
166 | - * |
|
167 | - * @since 0.1.1 |
|
168 | - * |
|
169 | - * @param Extensions|array|string|null $extensions Extensions to validate. |
|
170 | - * |
|
171 | - * @return Extensions Validated extensions collection. |
|
172 | - */ |
|
173 | - protected function validateExtensions($extensions): Extensions |
|
174 | - { |
|
175 | - if (empty($extensions)) { |
|
176 | - $extensions = new Extensions(['']); |
|
177 | - } |
|
178 | - |
|
179 | - if (! $extensions instanceof Extensions) { |
|
180 | - $extensions = new Extensions((array)$extensions); |
|
181 | - } |
|
182 | - |
|
183 | - return $extensions; |
|
184 | - } |
|
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()); |
|
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.1.3 |
|
156 | + * |
|
157 | + * @return string Path pattern to pass to the file finder. |
|
158 | + */ |
|
159 | + protected function getPathPattern(): string |
|
160 | + { |
|
161 | + return $this->path; |
|
162 | + } |
|
163 | + |
|
164 | + /** |
|
165 | + * Validate the extensions and return a collection. |
|
166 | + * |
|
167 | + * @since 0.1.1 |
|
168 | + * |
|
169 | + * @param Extensions|array|string|null $extensions Extensions to validate. |
|
170 | + * |
|
171 | + * @return Extensions Validated extensions collection. |
|
172 | + */ |
|
173 | + protected function validateExtensions($extensions): Extensions |
|
174 | + { |
|
175 | + if (empty($extensions)) { |
|
176 | + $extensions = new Extensions(['']); |
|
177 | + } |
|
178 | + |
|
179 | + if (! $extensions instanceof Extensions) { |
|
180 | + $extensions = new Extensions((array)$extensions); |
|
181 | + } |
|
182 | + |
|
183 | + return $extensions; |
|
184 | + } |
|
185 | 185 | } |
@@ -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 | /** |
@@ -176,8 +176,8 @@ discard block |
||
176 | 176 | $extensions = new Extensions(['']); |
177 | 177 | } |
178 | 178 | |
179 | - if (! $extensions instanceof Extensions) { |
|
180 | - $extensions = new Extensions((array)$extensions); |
|
179 | + if ( ! $extensions instanceof Extensions) { |
|
180 | + $extensions = new Extensions((array) $extensions); |
|
181 | 181 | } |
182 | 182 | |
183 | 183 | return $extensions; |
@@ -23,17 +23,17 @@ |
||
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 | |
31 | - /** |
|
32 | - * Get the NullObject. |
|
33 | - * |
|
34 | - * @since 0.1.1 |
|
35 | - * |
|
36 | - * @return NullEngine NullObject for the current Finder. |
|
37 | - */ |
|
38 | - public function getNullObject(): NullEngine; |
|
31 | + /** |
|
32 | + * Get the NullObject. |
|
33 | + * |
|
34 | + * @since 0.1.1 |
|
35 | + * |
|
36 | + * @return NullEngine NullObject for the current Finder. |
|
37 | + */ |
|
38 | + public function getNullObject(): NullEngine; |
|
39 | 39 | } |
@@ -25,53 +25,53 @@ |
||
25 | 25 | class BaseEngineFinder extends AbstractFinder implements EngineFinder |
26 | 26 | { |
27 | 27 | |
28 | - /** |
|
29 | - * Find a result based on a specific criteria. |
|
30 | - * |
|
31 | - * @since 0.1.0 |
|
32 | - * |
|
33 | - * @param array $criteria Criteria to search for. |
|
34 | - * |
|
35 | - * @return Engine Result of the search. |
|
36 | - * @throws FailedToInstantiateFindable If the Findable could not be instantiated. |
|
37 | - */ |
|
38 | - public function find(array $criteria): Engine |
|
39 | - { |
|
40 | - $this->findables = $this->initializeFindables(); |
|
28 | + /** |
|
29 | + * Find a result based on a specific criteria. |
|
30 | + * |
|
31 | + * @since 0.1.0 |
|
32 | + * |
|
33 | + * @param array $criteria Criteria to search for. |
|
34 | + * |
|
35 | + * @return Engine Result of the search. |
|
36 | + * @throws FailedToInstantiateFindable If the Findable could not be instantiated. |
|
37 | + */ |
|
38 | + public function find(array $criteria): Engine |
|
39 | + { |
|
40 | + $this->findables = $this->initializeFindables(); |
|
41 | 41 | |
42 | - foreach ($criteria as $entry) { |
|
43 | - foreach ($this->findables as $engine) { |
|
44 | - if ($engine->canHandle($entry)) { |
|
45 | - return $engine; |
|
46 | - } |
|
47 | - } |
|
48 | - } |
|
42 | + foreach ($criteria as $entry) { |
|
43 | + foreach ($this->findables as $engine) { |
|
44 | + if ($engine->canHandle($entry)) { |
|
45 | + return $engine; |
|
46 | + } |
|
47 | + } |
|
48 | + } |
|
49 | 49 | |
50 | - return $this->getNullObject(); |
|
51 | - } |
|
50 | + return $this->getNullObject(); |
|
51 | + } |
|
52 | 52 | |
53 | - /** |
|
54 | - * Get the config key for the Findables definitions. |
|
55 | - * |
|
56 | - * @since 0.1.0 |
|
57 | - * |
|
58 | - * @return string Config key use to define the Findables. |
|
59 | - */ |
|
60 | - protected function getFindablesConfigKey(): string |
|
61 | - { |
|
62 | - return 'Engines'; |
|
63 | - } |
|
53 | + /** |
|
54 | + * Get the config key for the Findables definitions. |
|
55 | + * |
|
56 | + * @since 0.1.0 |
|
57 | + * |
|
58 | + * @return string Config key use to define the Findables. |
|
59 | + */ |
|
60 | + protected function getFindablesConfigKey(): string |
|
61 | + { |
|
62 | + return 'Engines'; |
|
63 | + } |
|
64 | 64 | |
65 | - /** |
|
66 | - * Get the NullObject. |
|
67 | - * |
|
68 | - * @since 0.1.1 |
|
69 | - * |
|
70 | - * @return NullEngine NullObject for the current Finder. |
|
71 | - * @throws FailedToInstantiateFindable If the Findable could not be instantiated. |
|
72 | - */ |
|
73 | - public function getNullObject(): NullEngine |
|
74 | - { |
|
75 | - return parent::getNullObject(); |
|
76 | - } |
|
65 | + /** |
|
66 | + * Get the NullObject. |
|
67 | + * |
|
68 | + * @since 0.1.1 |
|
69 | + * |
|
70 | + * @return NullEngine NullObject for the current Finder. |
|
71 | + * @throws FailedToInstantiateFindable If the Findable could not be instantiated. |
|
72 | + */ |
|
73 | + public function getNullObject(): NullEngine |
|
74 | + { |
|
75 | + return parent::getNullObject(); |
|
76 | + } |
|
77 | 77 | } |
@@ -22,23 +22,23 @@ |
||
22 | 22 | interface Finder |
23 | 23 | { |
24 | 24 | |
25 | - /** |
|
26 | - * Find a result based on a specific criteria. |
|
27 | - * |
|
28 | - * @since 0.1.0 |
|
29 | - * |
|
30 | - * @param array $criteria Criteria to search for. |
|
31 | - * |
|
32 | - * @return mixed Result of the search. |
|
33 | - */ |
|
34 | - public function find(array $criteria); |
|
25 | + /** |
|
26 | + * Find a result based on a specific criteria. |
|
27 | + * |
|
28 | + * @since 0.1.0 |
|
29 | + * |
|
30 | + * @param array $criteria Criteria to search for. |
|
31 | + * |
|
32 | + * @return mixed Result of the search. |
|
33 | + */ |
|
34 | + public function find(array $criteria); |
|
35 | 35 | |
36 | - /** |
|
37 | - * Get the NullObject. |
|
38 | - * |
|
39 | - * @since 0.1.1 |
|
40 | - * |
|
41 | - * @return NullFindable NullObject for the current Finder. |
|
42 | - */ |
|
43 | - public function getNullObject(); |
|
36 | + /** |
|
37 | + * Get the NullObject. |
|
38 | + * |
|
39 | + * @since 0.1.1 |
|
40 | + * |
|
41 | + * @return NullFindable NullObject for the current Finder. |
|
42 | + */ |
|
43 | + public function getNullObject(); |
|
44 | 44 | } |