@@ -124,7 +124,7 @@ discard block |
||
124 | 124 | * |
125 | 125 | * @param string $uri URI to get a view for. |
126 | 126 | * @param EngineInterface $engine Engine to use for the view. |
127 | - * @param mixed $type Type of view to get. |
|
127 | + * @param string|null $type Type of view to get. |
|
128 | 128 | * |
129 | 129 | * @return ViewInterface View that matches the given requirements. |
130 | 130 | */ |
@@ -188,9 +188,9 @@ discard block |
||
188 | 188 | * |
189 | 189 | * @since 0.1.0 |
190 | 190 | * |
191 | - * @param array $criteria Criteria to match. |
|
191 | + * @param string[] $criteria Criteria to match. |
|
192 | 192 | * |
193 | - * @return string|bool URI of the requested view, or false if not found. |
|
193 | + * @return string|false URI of the requested view, or false if not found. |
|
194 | 194 | */ |
195 | 195 | public function scanLocations(array $criteria) |
196 | 196 | { |
@@ -32,209 +32,209 @@ |
||
32 | 32 | class ViewBuilder |
33 | 33 | { |
34 | 34 | |
35 | - use ConfigTrait; |
|
36 | - |
|
37 | - const ENGINE_FINDER_KEY = 'EngineFinder'; |
|
38 | - const VIEW_FINDER_KEY = 'ViewFinder'; |
|
39 | - |
|
40 | - /** |
|
41 | - * ViewFinder instance. |
|
42 | - * |
|
43 | - * @since 0.1.0 |
|
44 | - * |
|
45 | - * @var ViewFinderInterface |
|
46 | - */ |
|
47 | - protected $viewFinder; |
|
48 | - |
|
49 | - /** |
|
50 | - * EngineFinder instance. |
|
51 | - * |
|
52 | - * @since 0.1.0 |
|
53 | - * |
|
54 | - * @var EngineFinderInterface |
|
55 | - */ |
|
56 | - protected $engineFinder; |
|
57 | - |
|
58 | - /** |
|
59 | - * Locations to scan for views. |
|
60 | - * |
|
61 | - * @since 0.1.0 |
|
62 | - * |
|
63 | - * @var LocationInterface[] |
|
64 | - */ |
|
65 | - protected $locations; |
|
66 | - |
|
67 | - /** |
|
68 | - * Instantiate a ViewBuilder object. |
|
69 | - * |
|
70 | - * @since 0.1.0 |
|
71 | - * |
|
72 | - * @param ConfigInterface $config Configuration settings. |
|
73 | - * @param ViewFinderInterface|null $viewFinder ViewFinder instance. |
|
74 | - * @param EngineFinderInterface|null $engineFinder EngineFinder instance. |
|
75 | - * |
|
76 | - * @throws FailedToProcessConfigException If the config could not be processed. |
|
77 | - */ |
|
78 | - public function __construct( |
|
79 | - ConfigInterface $config, |
|
80 | - ViewFinderInterface $viewFinder = null, |
|
81 | - EngineFinderInterface $engineFinder = null |
|
82 | - ) { |
|
83 | - $this->processConfig($config); |
|
84 | - $this->viewFinder = $viewFinder; |
|
85 | - $this->engineFinder = $engineFinder; |
|
86 | - } |
|
87 | - |
|
88 | - /** |
|
89 | - * Create a new view for a given URI. |
|
90 | - * |
|
91 | - * @since 0.1.0 |
|
92 | - * |
|
93 | - * @param string $view View identifier to create a view for. |
|
94 | - * @param string|null $type Type of view to create. |
|
95 | - * |
|
96 | - * @return ViewInterface Instance of the requested view. |
|
97 | - */ |
|
98 | - public function create($view, $type = null) |
|
99 | - { |
|
100 | - $uri = $this->scanLocations([$view]); |
|
101 | - $engine = $this->getEngine($uri); |
|
102 | - |
|
103 | - return $this->getView($uri, $engine, $type); |
|
104 | - } |
|
105 | - |
|
106 | - /** |
|
107 | - * Get an Engine that can deal with the given URI. |
|
108 | - * |
|
109 | - * @since 0.1.0 |
|
110 | - * |
|
111 | - * @param string|false $uri URI to get an engine for. |
|
112 | - * |
|
113 | - * @return EngineInterface Instance of an engine that can deal with the given URI. |
|
114 | - */ |
|
115 | - public function getEngine($uri) |
|
116 | - { |
|
117 | - return $this->getEngineFinder()->find([$uri]); |
|
118 | - } |
|
119 | - |
|
120 | - /** |
|
121 | - * Get a view for a given URI, engine and type. |
|
122 | - * |
|
123 | - * @since 0.1.0 |
|
124 | - * |
|
125 | - * @param string $uri URI to get a view for. |
|
126 | - * @param EngineInterface $engine Engine to use for the view. |
|
127 | - * @param mixed $type Type of view to get. |
|
128 | - * |
|
129 | - * @return ViewInterface View that matches the given requirements. |
|
130 | - */ |
|
131 | - public function getView($uri, EngineInterface $engine, $type = null) |
|
132 | - { |
|
133 | - if (null === $type) { |
|
134 | - return $this->getViewFinder()->find([$uri], $engine); |
|
135 | - } |
|
136 | - |
|
137 | - return $this->resolveType($type, $uri, $engine); |
|
138 | - } |
|
139 | - |
|
140 | - /** |
|
141 | - * Get the ViewFinder instance. |
|
142 | - * |
|
143 | - * @since 0.1.0 |
|
144 | - * |
|
145 | - * @return ViewFinderInterface Instance of a ViewFinder. |
|
146 | - */ |
|
147 | - public function getViewFinder() |
|
148 | - { |
|
149 | - if (null === $this->viewFinder) { |
|
150 | - $viewFinderClass = $this->config->getKey(ViewBuilder::VIEW_FINDER_KEY, 'ClassName'); |
|
151 | - $this->viewFinder = new $viewFinderClass($this->config->getSubConfig(ViewBuilder::VIEW_FINDER_KEY)); |
|
152 | - } |
|
153 | - |
|
154 | - return $this->viewFinder; |
|
155 | - } |
|
156 | - |
|
157 | - /** |
|
158 | - * Get the EngineFinder instance. |
|
159 | - * |
|
160 | - * @since 0.1.0 |
|
161 | - * |
|
162 | - * @return EngineFinderInterface Instance of a EngineFinder. |
|
163 | - */ |
|
164 | - public function getEngineFinder() |
|
165 | - { |
|
166 | - if (null === $this->engineFinder) { |
|
167 | - $engineFinderClass = $this->config->getKey(ViewBuilder::ENGINE_FINDER_KEY, 'ClassName'); |
|
168 | - $this->engineFinder = new $engineFinderClass($this->config->getSubConfig(ViewBuilder::ENGINE_FINDER_KEY)); |
|
169 | - } |
|
170 | - |
|
171 | - return $this->engineFinder; |
|
172 | - } |
|
173 | - |
|
174 | - /** |
|
175 | - * Add a location to scan with the ViewFinder. |
|
176 | - * |
|
177 | - * @since 0.1.0 |
|
178 | - * |
|
179 | - * @param LocationInterface $location Location to scan with the ViewFinder. |
|
180 | - */ |
|
181 | - public function addLocation(LocationInterface $location) |
|
182 | - { |
|
183 | - $this->locations[] = $location; |
|
184 | - } |
|
185 | - |
|
186 | - /** |
|
187 | - * Scan Locations for an URI that matches the specified criteria. |
|
188 | - * |
|
189 | - * @since 0.1.0 |
|
190 | - * |
|
191 | - * @param array $criteria Criteria to match. |
|
192 | - * |
|
193 | - * @return string|bool URI of the requested view, or false if not found. |
|
194 | - */ |
|
195 | - public function scanLocations(array $criteria) |
|
196 | - { |
|
197 | - /** @var LocationInterface $location */ |
|
198 | - foreach ($this->locations as $location) { |
|
199 | - if ($uri = $location->getURI($criteria)) { |
|
200 | - return $uri; |
|
201 | - } |
|
202 | - } |
|
203 | - |
|
204 | - return false; |
|
205 | - } |
|
206 | - |
|
207 | - /** |
|
208 | - * Resolve the view type. |
|
209 | - * |
|
210 | - * @since 0.1.0 |
|
211 | - * |
|
212 | - * @param mixed $type Type of view that was requested. |
|
213 | - * @param string $uri URI to get a view for. |
|
214 | - * @param EngineInterface $engine Engine to use for the view. |
|
215 | - * |
|
216 | - * @return ViewInterface Resolved View object. |
|
217 | - * @throws FailedToInstantiateViewException If the view type could not be resolved. |
|
218 | - */ |
|
219 | - protected function resolveType($type, $uri, EngineInterface $engine = null) |
|
220 | - { |
|
221 | - if (is_string($type) && $this->config->hasKey(ViewBuilder::VIEW_FINDER_KEY)) { |
|
222 | - $type = new $type($uri, $engine); |
|
223 | - } |
|
224 | - |
|
225 | - if (is_callable($type)) { |
|
226 | - $type = $type($uri, $engine); |
|
227 | - } |
|
228 | - |
|
229 | - if (! $type instanceof ViewInterface) { |
|
230 | - throw new FailedToInstantiateViewException( |
|
231 | - sprintf( |
|
232 | - _('Could not instantiate view "%s".'), |
|
233 | - serialize($type) |
|
234 | - ) |
|
235 | - ); |
|
236 | - } |
|
237 | - |
|
238 | - return $type; |
|
239 | - } |
|
35 | + use ConfigTrait; |
|
36 | + |
|
37 | + const ENGINE_FINDER_KEY = 'EngineFinder'; |
|
38 | + const VIEW_FINDER_KEY = 'ViewFinder'; |
|
39 | + |
|
40 | + /** |
|
41 | + * ViewFinder instance. |
|
42 | + * |
|
43 | + * @since 0.1.0 |
|
44 | + * |
|
45 | + * @var ViewFinderInterface |
|
46 | + */ |
|
47 | + protected $viewFinder; |
|
48 | + |
|
49 | + /** |
|
50 | + * EngineFinder instance. |
|
51 | + * |
|
52 | + * @since 0.1.0 |
|
53 | + * |
|
54 | + * @var EngineFinderInterface |
|
55 | + */ |
|
56 | + protected $engineFinder; |
|
57 | + |
|
58 | + /** |
|
59 | + * Locations to scan for views. |
|
60 | + * |
|
61 | + * @since 0.1.0 |
|
62 | + * |
|
63 | + * @var LocationInterface[] |
|
64 | + */ |
|
65 | + protected $locations; |
|
66 | + |
|
67 | + /** |
|
68 | + * Instantiate a ViewBuilder object. |
|
69 | + * |
|
70 | + * @since 0.1.0 |
|
71 | + * |
|
72 | + * @param ConfigInterface $config Configuration settings. |
|
73 | + * @param ViewFinderInterface|null $viewFinder ViewFinder instance. |
|
74 | + * @param EngineFinderInterface|null $engineFinder EngineFinder instance. |
|
75 | + * |
|
76 | + * @throws FailedToProcessConfigException If the config could not be processed. |
|
77 | + */ |
|
78 | + public function __construct( |
|
79 | + ConfigInterface $config, |
|
80 | + ViewFinderInterface $viewFinder = null, |
|
81 | + EngineFinderInterface $engineFinder = null |
|
82 | + ) { |
|
83 | + $this->processConfig($config); |
|
84 | + $this->viewFinder = $viewFinder; |
|
85 | + $this->engineFinder = $engineFinder; |
|
86 | + } |
|
87 | + |
|
88 | + /** |
|
89 | + * Create a new view for a given URI. |
|
90 | + * |
|
91 | + * @since 0.1.0 |
|
92 | + * |
|
93 | + * @param string $view View identifier to create a view for. |
|
94 | + * @param string|null $type Type of view to create. |
|
95 | + * |
|
96 | + * @return ViewInterface Instance of the requested view. |
|
97 | + */ |
|
98 | + public function create($view, $type = null) |
|
99 | + { |
|
100 | + $uri = $this->scanLocations([$view]); |
|
101 | + $engine = $this->getEngine($uri); |
|
102 | + |
|
103 | + return $this->getView($uri, $engine, $type); |
|
104 | + } |
|
105 | + |
|
106 | + /** |
|
107 | + * Get an Engine that can deal with the given URI. |
|
108 | + * |
|
109 | + * @since 0.1.0 |
|
110 | + * |
|
111 | + * @param string|false $uri URI to get an engine for. |
|
112 | + * |
|
113 | + * @return EngineInterface Instance of an engine that can deal with the given URI. |
|
114 | + */ |
|
115 | + public function getEngine($uri) |
|
116 | + { |
|
117 | + return $this->getEngineFinder()->find([$uri]); |
|
118 | + } |
|
119 | + |
|
120 | + /** |
|
121 | + * Get a view for a given URI, engine and type. |
|
122 | + * |
|
123 | + * @since 0.1.0 |
|
124 | + * |
|
125 | + * @param string $uri URI to get a view for. |
|
126 | + * @param EngineInterface $engine Engine to use for the view. |
|
127 | + * @param mixed $type Type of view to get. |
|
128 | + * |
|
129 | + * @return ViewInterface View that matches the given requirements. |
|
130 | + */ |
|
131 | + public function getView($uri, EngineInterface $engine, $type = null) |
|
132 | + { |
|
133 | + if (null === $type) { |
|
134 | + return $this->getViewFinder()->find([$uri], $engine); |
|
135 | + } |
|
136 | + |
|
137 | + return $this->resolveType($type, $uri, $engine); |
|
138 | + } |
|
139 | + |
|
140 | + /** |
|
141 | + * Get the ViewFinder instance. |
|
142 | + * |
|
143 | + * @since 0.1.0 |
|
144 | + * |
|
145 | + * @return ViewFinderInterface Instance of a ViewFinder. |
|
146 | + */ |
|
147 | + public function getViewFinder() |
|
148 | + { |
|
149 | + if (null === $this->viewFinder) { |
|
150 | + $viewFinderClass = $this->config->getKey(ViewBuilder::VIEW_FINDER_KEY, 'ClassName'); |
|
151 | + $this->viewFinder = new $viewFinderClass($this->config->getSubConfig(ViewBuilder::VIEW_FINDER_KEY)); |
|
152 | + } |
|
153 | + |
|
154 | + return $this->viewFinder; |
|
155 | + } |
|
156 | + |
|
157 | + /** |
|
158 | + * Get the EngineFinder instance. |
|
159 | + * |
|
160 | + * @since 0.1.0 |
|
161 | + * |
|
162 | + * @return EngineFinderInterface Instance of a EngineFinder. |
|
163 | + */ |
|
164 | + public function getEngineFinder() |
|
165 | + { |
|
166 | + if (null === $this->engineFinder) { |
|
167 | + $engineFinderClass = $this->config->getKey(ViewBuilder::ENGINE_FINDER_KEY, 'ClassName'); |
|
168 | + $this->engineFinder = new $engineFinderClass($this->config->getSubConfig(ViewBuilder::ENGINE_FINDER_KEY)); |
|
169 | + } |
|
170 | + |
|
171 | + return $this->engineFinder; |
|
172 | + } |
|
173 | + |
|
174 | + /** |
|
175 | + * Add a location to scan with the ViewFinder. |
|
176 | + * |
|
177 | + * @since 0.1.0 |
|
178 | + * |
|
179 | + * @param LocationInterface $location Location to scan with the ViewFinder. |
|
180 | + */ |
|
181 | + public function addLocation(LocationInterface $location) |
|
182 | + { |
|
183 | + $this->locations[] = $location; |
|
184 | + } |
|
185 | + |
|
186 | + /** |
|
187 | + * Scan Locations for an URI that matches the specified criteria. |
|
188 | + * |
|
189 | + * @since 0.1.0 |
|
190 | + * |
|
191 | + * @param array $criteria Criteria to match. |
|
192 | + * |
|
193 | + * @return string|bool URI of the requested view, or false if not found. |
|
194 | + */ |
|
195 | + public function scanLocations(array $criteria) |
|
196 | + { |
|
197 | + /** @var LocationInterface $location */ |
|
198 | + foreach ($this->locations as $location) { |
|
199 | + if ($uri = $location->getURI($criteria)) { |
|
200 | + return $uri; |
|
201 | + } |
|
202 | + } |
|
203 | + |
|
204 | + return false; |
|
205 | + } |
|
206 | + |
|
207 | + /** |
|
208 | + * Resolve the view type. |
|
209 | + * |
|
210 | + * @since 0.1.0 |
|
211 | + * |
|
212 | + * @param mixed $type Type of view that was requested. |
|
213 | + * @param string $uri URI to get a view for. |
|
214 | + * @param EngineInterface $engine Engine to use for the view. |
|
215 | + * |
|
216 | + * @return ViewInterface Resolved View object. |
|
217 | + * @throws FailedToInstantiateViewException If the view type could not be resolved. |
|
218 | + */ |
|
219 | + protected function resolveType($type, $uri, EngineInterface $engine = null) |
|
220 | + { |
|
221 | + if (is_string($type) && $this->config->hasKey(ViewBuilder::VIEW_FINDER_KEY)) { |
|
222 | + $type = new $type($uri, $engine); |
|
223 | + } |
|
224 | + |
|
225 | + if (is_callable($type)) { |
|
226 | + $type = $type($uri, $engine); |
|
227 | + } |
|
228 | + |
|
229 | + if (! $type instanceof ViewInterface) { |
|
230 | + throw new FailedToInstantiateViewException( |
|
231 | + sprintf( |
|
232 | + _('Could not instantiate view "%s".'), |
|
233 | + serialize($type) |
|
234 | + ) |
|
235 | + ); |
|
236 | + } |
|
237 | + |
|
238 | + return $type; |
|
239 | + } |
|
240 | 240 | } |
@@ -25,73 +25,73 @@ |
||
25 | 25 | class PHPEngine extends AbstractEngine |
26 | 26 | { |
27 | 27 | |
28 | - const PHP_EXTENSION = '.php'; |
|
28 | + const PHP_EXTENSION = '.php'; |
|
29 | 29 | |
30 | - /** |
|
31 | - * Check whether the Findable can handle an individual criterion. |
|
32 | - * |
|
33 | - * @since 0.1.0 |
|
34 | - * |
|
35 | - * @param mixed $criterion Criterion to check. |
|
36 | - * |
|
37 | - * @return bool Whether the Findable can handle the criterion. |
|
38 | - */ |
|
39 | - public function canHandle($criterion) |
|
40 | - { |
|
41 | - return $this->hasExtension($criterion, static::PHP_EXTENSION) |
|
42 | - && is_readable($criterion); |
|
43 | - } |
|
30 | + /** |
|
31 | + * Check whether the Findable can handle an individual criterion. |
|
32 | + * |
|
33 | + * @since 0.1.0 |
|
34 | + * |
|
35 | + * @param mixed $criterion Criterion to check. |
|
36 | + * |
|
37 | + * @return bool Whether the Findable can handle the criterion. |
|
38 | + */ |
|
39 | + public function canHandle($criterion) |
|
40 | + { |
|
41 | + return $this->hasExtension($criterion, static::PHP_EXTENSION) |
|
42 | + && is_readable($criterion); |
|
43 | + } |
|
44 | 44 | |
45 | - /** |
|
46 | - * Render a given URI. |
|
47 | - * |
|
48 | - * @since 0.1.0 |
|
49 | - * |
|
50 | - * @param string $uri URI to render. |
|
51 | - * @param array $context Context in which to render. |
|
52 | - * |
|
53 | - * @return string Rendered HTML. |
|
54 | - * @throws FailedToLoadViewException If the View URI is not accessible or readable. |
|
55 | - * @throws FailedToLoadViewException If the View URI could not be loaded. |
|
56 | - */ |
|
57 | - public function render($uri, array $context = []) |
|
58 | - { |
|
59 | - if (! is_readable($uri)) { |
|
60 | - throw new FailedToLoadViewException( |
|
61 | - sprintf( |
|
62 | - _('The View URI "%1$s" is not accessible or readable.'), |
|
63 | - $uri |
|
64 | - ) |
|
65 | - ); |
|
66 | - } |
|
45 | + /** |
|
46 | + * Render a given URI. |
|
47 | + * |
|
48 | + * @since 0.1.0 |
|
49 | + * |
|
50 | + * @param string $uri URI to render. |
|
51 | + * @param array $context Context in which to render. |
|
52 | + * |
|
53 | + * @return string Rendered HTML. |
|
54 | + * @throws FailedToLoadViewException If the View URI is not accessible or readable. |
|
55 | + * @throws FailedToLoadViewException If the View URI could not be loaded. |
|
56 | + */ |
|
57 | + public function render($uri, array $context = []) |
|
58 | + { |
|
59 | + if (! is_readable($uri)) { |
|
60 | + throw new FailedToLoadViewException( |
|
61 | + sprintf( |
|
62 | + _('The View URI "%1$s" is not accessible or readable.'), |
|
63 | + $uri |
|
64 | + ) |
|
65 | + ); |
|
66 | + } |
|
67 | 67 | |
68 | - extract($context, EXTR_SKIP); |
|
68 | + extract($context, EXTR_SKIP); |
|
69 | 69 | |
70 | - // Save current buffering level so we can backtrack in case of an error. |
|
71 | - // This is needed because the view itself might also add an unknown number of output buffering levels. |
|
72 | - $bufferLevel = ob_get_level(); |
|
73 | - ob_start(); |
|
70 | + // Save current buffering level so we can backtrack in case of an error. |
|
71 | + // This is needed because the view itself might also add an unknown number of output buffering levels. |
|
72 | + $bufferLevel = ob_get_level(); |
|
73 | + ob_start(); |
|
74 | 74 | |
75 | - try { |
|
76 | - include($uri); |
|
77 | - } catch (Exception $exception) { |
|
75 | + try { |
|
76 | + include($uri); |
|
77 | + } catch (Exception $exception) { |
|
78 | 78 | |
79 | - // Remove whatever levels were added up until now. |
|
80 | - while (ob_get_level() > $bufferLevel) { |
|
81 | - ob_end_clean(); |
|
82 | - } |
|
79 | + // Remove whatever levels were added up until now. |
|
80 | + while (ob_get_level() > $bufferLevel) { |
|
81 | + ob_end_clean(); |
|
82 | + } |
|
83 | 83 | |
84 | - throw new FailedToLoadViewException( |
|
85 | - sprintf( |
|
86 | - _('Could not load the View URI "%1$s". Reason: "%2$s".'), |
|
87 | - $uri, |
|
88 | - $exception->getMessage() |
|
89 | - ), |
|
90 | - $exception->getCode(), |
|
91 | - $exception |
|
92 | - ); |
|
93 | - } |
|
84 | + throw new FailedToLoadViewException( |
|
85 | + sprintf( |
|
86 | + _('Could not load the View URI "%1$s". Reason: "%2$s".'), |
|
87 | + $uri, |
|
88 | + $exception->getMessage() |
|
89 | + ), |
|
90 | + $exception->getCode(), |
|
91 | + $exception |
|
92 | + ); |
|
93 | + } |
|
94 | 94 | |
95 | - return ob_get_clean(); |
|
96 | - } |
|
95 | + return ob_get_clean(); |
|
96 | + } |
|
97 | 97 | } |
@@ -25,86 +25,86 @@ |
||
25 | 25 | class EngineFinder extends AbstractFinder |
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 mixed Result of the search. |
|
36 | - */ |
|
37 | - public function find(array $criteria) |
|
38 | - { |
|
39 | - $this->initializeEngines(); |
|
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 mixed Result of the search. |
|
36 | + */ |
|
37 | + public function find(array $criteria) |
|
38 | + { |
|
39 | + $this->initializeEngines(); |
|
40 | 40 | |
41 | - foreach ($criteria as $entry) { |
|
42 | - foreach ($this->findables as $engine) { |
|
43 | - if ($engine->canHandle($entry)) { |
|
44 | - return $engine; |
|
45 | - } |
|
46 | - } |
|
47 | - } |
|
41 | + foreach ($criteria as $entry) { |
|
42 | + foreach ($this->findables as $engine) { |
|
43 | + if ($engine->canHandle($entry)) { |
|
44 | + return $engine; |
|
45 | + } |
|
46 | + } |
|
47 | + } |
|
48 | 48 | |
49 | - return $this->nullObject; |
|
50 | - } |
|
49 | + return $this->nullObject; |
|
50 | + } |
|
51 | 51 | |
52 | - /** |
|
53 | - * Initialize the engines that can be iterated. |
|
54 | - * |
|
55 | - * @since 0.1.0 |
|
56 | - * |
|
57 | - */ |
|
58 | - protected function initializeEngines() |
|
59 | - { |
|
60 | - foreach ($this->findables as &$engine) { |
|
61 | - $engine = $this->initializeEngine($engine); |
|
62 | - } |
|
52 | + /** |
|
53 | + * Initialize the engines that can be iterated. |
|
54 | + * |
|
55 | + * @since 0.1.0 |
|
56 | + * |
|
57 | + */ |
|
58 | + protected function initializeEngines() |
|
59 | + { |
|
60 | + foreach ($this->findables as &$engine) { |
|
61 | + $engine = $this->initializeEngine($engine); |
|
62 | + } |
|
63 | 63 | |
64 | - $this->nullObject = $this->initializeEngine($this->nullObject); |
|
65 | - } |
|
64 | + $this->nullObject = $this->initializeEngine($this->nullObject); |
|
65 | + } |
|
66 | 66 | |
67 | - /** |
|
68 | - * Initialize a single engine by instantiating class name strings and calling closures. |
|
69 | - * |
|
70 | - * @since 0.1.0 |
|
71 | - * |
|
72 | - * @param mixed $engine Engine to instantiate. |
|
73 | - * |
|
74 | - * @return EngineInterface Instantiated engine. |
|
75 | - * @throws FailedToInstantiateEngineException If the engine could not be instantiated. |
|
76 | - */ |
|
77 | - protected function initializeEngine($engine) |
|
78 | - { |
|
79 | - if (is_string($engine)) { |
|
80 | - $engine = new $engine(); |
|
81 | - } |
|
67 | + /** |
|
68 | + * Initialize a single engine by instantiating class name strings and calling closures. |
|
69 | + * |
|
70 | + * @since 0.1.0 |
|
71 | + * |
|
72 | + * @param mixed $engine Engine to instantiate. |
|
73 | + * |
|
74 | + * @return EngineInterface Instantiated engine. |
|
75 | + * @throws FailedToInstantiateEngineException If the engine could not be instantiated. |
|
76 | + */ |
|
77 | + protected function initializeEngine($engine) |
|
78 | + { |
|
79 | + if (is_string($engine)) { |
|
80 | + $engine = new $engine(); |
|
81 | + } |
|
82 | 82 | |
83 | - if (is_callable($engine)) { |
|
84 | - $engine = $engine(); |
|
85 | - } |
|
83 | + if (is_callable($engine)) { |
|
84 | + $engine = $engine(); |
|
85 | + } |
|
86 | 86 | |
87 | - if (! $engine instanceof EngineInterface) { |
|
88 | - throw new FailedToInstantiateEngineException( |
|
89 | - sprintf( |
|
90 | - _('Could not instantiate engine "%s".'), |
|
91 | - serialize($engine) |
|
92 | - ) |
|
93 | - ); |
|
94 | - } |
|
87 | + if (! $engine instanceof EngineInterface) { |
|
88 | + throw new FailedToInstantiateEngineException( |
|
89 | + sprintf( |
|
90 | + _('Could not instantiate engine "%s".'), |
|
91 | + serialize($engine) |
|
92 | + ) |
|
93 | + ); |
|
94 | + } |
|
95 | 95 | |
96 | - return $engine; |
|
97 | - } |
|
96 | + return $engine; |
|
97 | + } |
|
98 | 98 | |
99 | - /** |
|
100 | - * Get the config key for the Findables definitions. |
|
101 | - * |
|
102 | - * @since 0.1.0 |
|
103 | - * |
|
104 | - * @return string Config key use to define the Findables. |
|
105 | - */ |
|
106 | - protected function getFindablesConfigKey() |
|
107 | - { |
|
108 | - return 'Engines'; |
|
109 | - } |
|
99 | + /** |
|
100 | + * Get the config key for the Findables definitions. |
|
101 | + * |
|
102 | + * @since 0.1.0 |
|
103 | + * |
|
104 | + * @return string Config key use to define the Findables. |
|
105 | + */ |
|
106 | + protected function getFindablesConfigKey() |
|
107 | + { |
|
108 | + return 'Engines'; |
|
109 | + } |
|
110 | 110 | } |
@@ -22,15 +22,15 @@ |
||
22 | 22 | interface EngineInterface |
23 | 23 | { |
24 | 24 | |
25 | - /** |
|
26 | - * Render a given URI. |
|
27 | - * |
|
28 | - * @since 0.1.0 |
|
29 | - * |
|
30 | - * @param string $uri URI to render. |
|
31 | - * @param array $context Context in which to render. |
|
32 | - * |
|
33 | - * @return string Rendered HTML. |
|
34 | - */ |
|
35 | - public function render($uri, array $context = []); |
|
25 | + /** |
|
26 | + * Render a given URI. |
|
27 | + * |
|
28 | + * @since 0.1.0 |
|
29 | + * |
|
30 | + * @param string $uri URI to render. |
|
31 | + * @param array $context Context in which to render. |
|
32 | + * |
|
33 | + * @return string Rendered HTML. |
|
34 | + */ |
|
35 | + public function render($uri, array $context = []); |
|
36 | 36 | } |
@@ -22,17 +22,17 @@ |
||
22 | 22 | class BaseView extends AbstractView |
23 | 23 | { |
24 | 24 | |
25 | - /** |
|
26 | - * Check whether the Findable can handle an individual criterion. |
|
27 | - * |
|
28 | - * @since 0.1.0 |
|
29 | - * |
|
30 | - * @param mixed $criterion Criterion to check. |
|
31 | - * |
|
32 | - * @return bool Whether the Findable can handle the criterion. |
|
33 | - */ |
|
34 | - public function canHandle($criterion) |
|
35 | - { |
|
36 | - return true; |
|
37 | - } |
|
25 | + /** |
|
26 | + * Check whether the Findable can handle an individual criterion. |
|
27 | + * |
|
28 | + * @since 0.1.0 |
|
29 | + * |
|
30 | + * @param mixed $criterion Criterion to check. |
|
31 | + * |
|
32 | + * @return bool Whether the Findable can handle the criterion. |
|
33 | + */ |
|
34 | + public function canHandle($criterion) |
|
35 | + { |
|
36 | + return true; |
|
37 | + } |
|
38 | 38 | } |
@@ -26,127 +26,127 @@ |
||
26 | 26 | class ViewFinder extends AbstractFinder |
27 | 27 | { |
28 | 28 | |
29 | - /** |
|
30 | - * Find a result based on a specific criteria. |
|
31 | - * |
|
32 | - * @since 0.1.0 |
|
33 | - * |
|
34 | - * @param array $criteria Criteria to search for. |
|
35 | - * @param EngineInterface $engine Optional. Engine to use with the view. |
|
36 | - * |
|
37 | - * @return ViewInterface View that was found. |
|
38 | - */ |
|
39 | - public function find(array $criteria, EngineInterface $engine = null) |
|
40 | - { |
|
41 | - $uri = $criteria[0]; |
|
42 | - |
|
43 | - $this->initializeViews($uri, $engine); |
|
44 | - |
|
45 | - foreach ($criteria as $entry) { |
|
46 | - foreach ($this->findables as $viewObject) { |
|
47 | - if ($viewObject->canHandle($entry)) { |
|
48 | - return $viewObject; |
|
49 | - } |
|
50 | - } |
|
51 | - } |
|
52 | - |
|
53 | - return $this->nullObject; |
|
54 | - } |
|
55 | - |
|
56 | - /** |
|
57 | - * Initialize the views that can be iterated. |
|
58 | - * |
|
59 | - * @since 0.1.0 |
|
60 | - * |
|
61 | - * @param string $uri URI to use for the view. |
|
62 | - * @param EngineInterface $engine Optional. Engine to use with the view. |
|
63 | - */ |
|
64 | - protected function initializeViews($uri, EngineInterface $engine = null) |
|
65 | - { |
|
66 | - foreach ($this->findables as &$view) { |
|
67 | - $view = $this->initializeView($view, $uri, $engine); |
|
68 | - } |
|
69 | - |
|
70 | - $this->nullObject = $this->initializeView($this->nullObject, $uri); |
|
71 | - } |
|
72 | - |
|
73 | - /** |
|
74 | - * Initialize a single view by instantiating class name strings and calling closures. |
|
75 | - * |
|
76 | - * @since 0.1.0 |
|
77 | - * |
|
78 | - * @param mixed $view View to instantiate. |
|
79 | - * @param string $uri URI to use for the view. |
|
80 | - * @param EngineInterface $engine Optional. Engine to use with the view. |
|
81 | - * |
|
82 | - * @return ViewInterface Instantiated view. |
|
83 | - * @throws FailedToInstantiateViewException If the view could not be instantiated. |
|
84 | - */ |
|
85 | - protected function initializeView($view, $uri, EngineInterface $engine = null) |
|
86 | - { |
|
87 | - if (is_string($view)) { |
|
88 | - $view = new $view($uri, $engine); |
|
89 | - } |
|
90 | - |
|
91 | - if (is_callable($view)) { |
|
92 | - $view = $view($uri, $engine); |
|
93 | - } |
|
94 | - |
|
95 | - if (! $view instanceof ViewInterface) { |
|
96 | - throw new FailedToInstantiateViewException( |
|
97 | - sprintf( |
|
98 | - _('Could not instantiate view "%s".'), |
|
99 | - serialize($view) |
|
100 | - ) |
|
101 | - ); |
|
102 | - } |
|
103 | - |
|
104 | - return $view; |
|
105 | - } |
|
106 | - |
|
107 | - /** |
|
108 | - * Instantiate a view by instantiating class name strings and calling closures. |
|
109 | - * |
|
110 | - * @since 0.1.0 |
|
111 | - * |
|
112 | - * @param mixed $view View to instantiate. |
|
113 | - * @param string $uri URI to use for the view. |
|
114 | - * @param EngineInterface $engine Optional. View to use with the view. |
|
115 | - * |
|
116 | - * @return ViewInterface Instantiated view. |
|
117 | - * @throws FailedToInstantiateViewException If the view could not be instantiated. |
|
118 | - */ |
|
119 | - protected function instantiateView($view, $uri, EngineInterface $engine = null) |
|
120 | - { |
|
121 | - if (is_string($view)) { |
|
122 | - $view = new $view($uri, $engine); |
|
123 | - } |
|
124 | - |
|
125 | - if (is_callable($view)) { |
|
126 | - $view = $view($uri, $engine); |
|
127 | - } |
|
128 | - |
|
129 | - if (! $view instanceof ViewInterface) { |
|
130 | - throw new FailedToInstantiateViewException( |
|
131 | - sprintf( |
|
132 | - _('Could not instantiate view "%s".'), |
|
133 | - serialize($view) |
|
134 | - ) |
|
135 | - ); |
|
136 | - } |
|
137 | - |
|
138 | - return $view; |
|
139 | - } |
|
140 | - |
|
141 | - /** |
|
142 | - * Get the config key for the Findables definitions. |
|
143 | - * |
|
144 | - * @since 0.1.0 |
|
145 | - * |
|
146 | - * @return string Config key use to define the Findables. |
|
147 | - */ |
|
148 | - protected function getFindablesConfigKey() |
|
149 | - { |
|
150 | - return 'Views'; |
|
151 | - } |
|
29 | + /** |
|
30 | + * Find a result based on a specific criteria. |
|
31 | + * |
|
32 | + * @since 0.1.0 |
|
33 | + * |
|
34 | + * @param array $criteria Criteria to search for. |
|
35 | + * @param EngineInterface $engine Optional. Engine to use with the view. |
|
36 | + * |
|
37 | + * @return ViewInterface View that was found. |
|
38 | + */ |
|
39 | + public function find(array $criteria, EngineInterface $engine = null) |
|
40 | + { |
|
41 | + $uri = $criteria[0]; |
|
42 | + |
|
43 | + $this->initializeViews($uri, $engine); |
|
44 | + |
|
45 | + foreach ($criteria as $entry) { |
|
46 | + foreach ($this->findables as $viewObject) { |
|
47 | + if ($viewObject->canHandle($entry)) { |
|
48 | + return $viewObject; |
|
49 | + } |
|
50 | + } |
|
51 | + } |
|
52 | + |
|
53 | + return $this->nullObject; |
|
54 | + } |
|
55 | + |
|
56 | + /** |
|
57 | + * Initialize the views that can be iterated. |
|
58 | + * |
|
59 | + * @since 0.1.0 |
|
60 | + * |
|
61 | + * @param string $uri URI to use for the view. |
|
62 | + * @param EngineInterface $engine Optional. Engine to use with the view. |
|
63 | + */ |
|
64 | + protected function initializeViews($uri, EngineInterface $engine = null) |
|
65 | + { |
|
66 | + foreach ($this->findables as &$view) { |
|
67 | + $view = $this->initializeView($view, $uri, $engine); |
|
68 | + } |
|
69 | + |
|
70 | + $this->nullObject = $this->initializeView($this->nullObject, $uri); |
|
71 | + } |
|
72 | + |
|
73 | + /** |
|
74 | + * Initialize a single view by instantiating class name strings and calling closures. |
|
75 | + * |
|
76 | + * @since 0.1.0 |
|
77 | + * |
|
78 | + * @param mixed $view View to instantiate. |
|
79 | + * @param string $uri URI to use for the view. |
|
80 | + * @param EngineInterface $engine Optional. Engine to use with the view. |
|
81 | + * |
|
82 | + * @return ViewInterface Instantiated view. |
|
83 | + * @throws FailedToInstantiateViewException If the view could not be instantiated. |
|
84 | + */ |
|
85 | + protected function initializeView($view, $uri, EngineInterface $engine = null) |
|
86 | + { |
|
87 | + if (is_string($view)) { |
|
88 | + $view = new $view($uri, $engine); |
|
89 | + } |
|
90 | + |
|
91 | + if (is_callable($view)) { |
|
92 | + $view = $view($uri, $engine); |
|
93 | + } |
|
94 | + |
|
95 | + if (! $view instanceof ViewInterface) { |
|
96 | + throw new FailedToInstantiateViewException( |
|
97 | + sprintf( |
|
98 | + _('Could not instantiate view "%s".'), |
|
99 | + serialize($view) |
|
100 | + ) |
|
101 | + ); |
|
102 | + } |
|
103 | + |
|
104 | + return $view; |
|
105 | + } |
|
106 | + |
|
107 | + /** |
|
108 | + * Instantiate a view by instantiating class name strings and calling closures. |
|
109 | + * |
|
110 | + * @since 0.1.0 |
|
111 | + * |
|
112 | + * @param mixed $view View to instantiate. |
|
113 | + * @param string $uri URI to use for the view. |
|
114 | + * @param EngineInterface $engine Optional. View to use with the view. |
|
115 | + * |
|
116 | + * @return ViewInterface Instantiated view. |
|
117 | + * @throws FailedToInstantiateViewException If the view could not be instantiated. |
|
118 | + */ |
|
119 | + protected function instantiateView($view, $uri, EngineInterface $engine = null) |
|
120 | + { |
|
121 | + if (is_string($view)) { |
|
122 | + $view = new $view($uri, $engine); |
|
123 | + } |
|
124 | + |
|
125 | + if (is_callable($view)) { |
|
126 | + $view = $view($uri, $engine); |
|
127 | + } |
|
128 | + |
|
129 | + if (! $view instanceof ViewInterface) { |
|
130 | + throw new FailedToInstantiateViewException( |
|
131 | + sprintf( |
|
132 | + _('Could not instantiate view "%s".'), |
|
133 | + serialize($view) |
|
134 | + ) |
|
135 | + ); |
|
136 | + } |
|
137 | + |
|
138 | + return $view; |
|
139 | + } |
|
140 | + |
|
141 | + /** |
|
142 | + * Get the config key for the Findables definitions. |
|
143 | + * |
|
144 | + * @since 0.1.0 |
|
145 | + * |
|
146 | + * @return string Config key use to define the Findables. |
|
147 | + */ |
|
148 | + protected function getFindablesConfigKey() |
|
149 | + { |
|
150 | + return 'Views'; |
|
151 | + } |
|
152 | 152 | } |
@@ -92,7 +92,7 @@ discard block |
||
92 | 92 | $view = $view($uri, $engine); |
93 | 93 | } |
94 | 94 | |
95 | - if (! $view instanceof ViewInterface) { |
|
95 | + if ( ! $view instanceof ViewInterface) { |
|
96 | 96 | throw new FailedToInstantiateViewException( |
97 | 97 | sprintf( |
98 | 98 | _('Could not instantiate view "%s".'), |
@@ -126,7 +126,7 @@ discard block |
||
126 | 126 | $view = $view($uri, $engine); |
127 | 127 | } |
128 | 128 | |
129 | - if (! $view instanceof ViewInterface) { |
|
129 | + if ( ! $view instanceof ViewInterface) { |
|
130 | 130 | throw new FailedToInstantiateViewException( |
131 | 131 | sprintf( |
132 | 132 | _('Could not instantiate view "%s".'), |
@@ -22,14 +22,14 @@ |
||
22 | 22 | interface Findable |
23 | 23 | { |
24 | 24 | |
25 | - /** |
|
26 | - * Check whether the Findable can handle an individual criterion. |
|
27 | - * |
|
28 | - * @since 0.1.0 |
|
29 | - * |
|
30 | - * @param mixed $criterion Criterion to check. |
|
31 | - * |
|
32 | - * @return bool Whether the Findable can handle the criterion. |
|
33 | - */ |
|
34 | - public function canHandle($criterion); |
|
25 | + /** |
|
26 | + * Check whether the Findable can handle an individual criterion. |
|
27 | + * |
|
28 | + * @since 0.1.0 |
|
29 | + * |
|
30 | + * @param mixed $criterion Criterion to check. |
|
31 | + * |
|
32 | + * @return bool Whether the Findable can handle the criterion. |
|
33 | + */ |
|
34 | + public function canHandle($criterion); |
|
35 | 35 | } |