@@ -34,277 +34,277 @@ |
||
| 34 | 34 | class ViewBuilder |
| 35 | 35 | { |
| 36 | 36 | |
| 37 | - use ConfigTrait; |
|
| 37 | + use ConfigTrait; |
|
| 38 | 38 | |
| 39 | - const ENGINE_FINDER_KEY = 'EngineFinder'; |
|
| 40 | - const VIEW_FINDER_KEY = 'ViewFinder'; |
|
| 39 | + const ENGINE_FINDER_KEY = 'EngineFinder'; |
|
| 40 | + const VIEW_FINDER_KEY = 'ViewFinder'; |
|
| 41 | 41 | |
| 42 | - /** |
|
| 43 | - * BaseViewFinder instance. |
|
| 44 | - * |
|
| 45 | - * @since 0.1.0 |
|
| 46 | - * |
|
| 47 | - * @var ViewFinder |
|
| 48 | - */ |
|
| 49 | - protected $viewFinder; |
|
| 42 | + /** |
|
| 43 | + * BaseViewFinder instance. |
|
| 44 | + * |
|
| 45 | + * @since 0.1.0 |
|
| 46 | + * |
|
| 47 | + * @var ViewFinder |
|
| 48 | + */ |
|
| 49 | + protected $viewFinder; |
|
| 50 | 50 | |
| 51 | - /** |
|
| 52 | - * BaseEngineFinder instance. |
|
| 53 | - * |
|
| 54 | - * @since 0.1.0 |
|
| 55 | - * |
|
| 56 | - * @var BaseEngineFinder |
|
| 57 | - */ |
|
| 58 | - protected $engineFinder; |
|
| 51 | + /** |
|
| 52 | + * BaseEngineFinder instance. |
|
| 53 | + * |
|
| 54 | + * @since 0.1.0 |
|
| 55 | + * |
|
| 56 | + * @var BaseEngineFinder |
|
| 57 | + */ |
|
| 58 | + protected $engineFinder; |
|
| 59 | 59 | |
| 60 | - /** |
|
| 61 | - * Locations to scan for views. |
|
| 62 | - * |
|
| 63 | - * @since 0.1.0 |
|
| 64 | - * |
|
| 65 | - * @var Locations |
|
| 66 | - */ |
|
| 67 | - protected $locations; |
|
| 60 | + /** |
|
| 61 | + * Locations to scan for views. |
|
| 62 | + * |
|
| 63 | + * @since 0.1.0 |
|
| 64 | + * |
|
| 65 | + * @var Locations |
|
| 66 | + */ |
|
| 67 | + protected $locations; |
|
| 68 | 68 | |
| 69 | - /** |
|
| 70 | - * Instantiate a ViewBuilder object. |
|
| 71 | - * |
|
| 72 | - * @since 0.1.0 |
|
| 73 | - * |
|
| 74 | - * @param ConfigInterface $config Optional. Configuration settings. |
|
| 75 | - * @param ViewFinder|null $viewFinder Optional. BaseViewFinder instance. |
|
| 76 | - * @param BaseEngineFinder|null $engineFinder Optional. BaseEngineFinder instance. |
|
| 77 | - * |
|
| 78 | - * @throws FailedToProcessConfigException If the config could not be processed. |
|
| 79 | - */ |
|
| 80 | - public function __construct( |
|
| 81 | - ConfigInterface $config = null, |
|
| 82 | - ViewFinder $viewFinder = null, |
|
| 83 | - BaseEngineFinder $engineFinder = null |
|
| 84 | - ) { |
|
| 85 | - $this->processConfig($this->getConfig($config)); |
|
| 86 | - $this->viewFinder = $viewFinder; |
|
| 87 | - $this->engineFinder = $engineFinder; |
|
| 88 | - $this->locations = new Locations(); |
|
| 89 | - } |
|
| 69 | + /** |
|
| 70 | + * Instantiate a ViewBuilder object. |
|
| 71 | + * |
|
| 72 | + * @since 0.1.0 |
|
| 73 | + * |
|
| 74 | + * @param ConfigInterface $config Optional. Configuration settings. |
|
| 75 | + * @param ViewFinder|null $viewFinder Optional. BaseViewFinder instance. |
|
| 76 | + * @param BaseEngineFinder|null $engineFinder Optional. BaseEngineFinder instance. |
|
| 77 | + * |
|
| 78 | + * @throws FailedToProcessConfigException If the config could not be processed. |
|
| 79 | + */ |
|
| 80 | + public function __construct( |
|
| 81 | + ConfigInterface $config = null, |
|
| 82 | + ViewFinder $viewFinder = null, |
|
| 83 | + BaseEngineFinder $engineFinder = null |
|
| 84 | + ) { |
|
| 85 | + $this->processConfig($this->getConfig($config)); |
|
| 86 | + $this->viewFinder = $viewFinder; |
|
| 87 | + $this->engineFinder = $engineFinder; |
|
| 88 | + $this->locations = new Locations(); |
|
| 89 | + } |
|
| 90 | 90 | |
| 91 | - /** |
|
| 92 | - * Create a new view for a given URI. |
|
| 93 | - * |
|
| 94 | - * @since 0.1.0 |
|
| 95 | - * |
|
| 96 | - * @param string $view View identifier to create a view for. |
|
| 97 | - * @param mixed $type Type of view to create. |
|
| 98 | - * |
|
| 99 | - * @return View Instance of the requested view. |
|
| 100 | - * @throws FailedToInstantiateView If the view could not be instantiated. |
|
| 101 | - */ |
|
| 102 | - public function create(string $view, $type = null): View |
|
| 103 | - { |
|
| 104 | - $uri = $this->scanLocations([$view]); |
|
| 105 | - $engine = $uri |
|
| 106 | - ? $this->getEngine($uri) |
|
| 107 | - : false; |
|
| 91 | + /** |
|
| 92 | + * Create a new view for a given URI. |
|
| 93 | + * |
|
| 94 | + * @since 0.1.0 |
|
| 95 | + * |
|
| 96 | + * @param string $view View identifier to create a view for. |
|
| 97 | + * @param mixed $type Type of view to create. |
|
| 98 | + * |
|
| 99 | + * @return View Instance of the requested view. |
|
| 100 | + * @throws FailedToInstantiateView If the view could not be instantiated. |
|
| 101 | + */ |
|
| 102 | + public function create(string $view, $type = null): View |
|
| 103 | + { |
|
| 104 | + $uri = $this->scanLocations([$view]); |
|
| 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 | - $view = (null === $type) |
|
| 143 | - ? $this->getViewFinder()->find([$uri], $engine) |
|
| 144 | - : $this->resolveType($type, $uri, $engine); |
|
| 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 | + $view = (null === $type) |
|
| 143 | + ? $this->getViewFinder()->find([$uri], $engine) |
|
| 144 | + : $this->resolveType($type, $uri, $engine); |
|
| 145 | 145 | |
| 146 | - return $view->setBuilder($this); |
|
| 147 | - } |
|
| 146 | + return $view->setBuilder($this); |
|
| 147 | + } |
|
| 148 | 148 | |
| 149 | - /** |
|
| 150 | - * Get the ViewFinder instance. |
|
| 151 | - * |
|
| 152 | - * @since 0.1.0 |
|
| 153 | - * |
|
| 154 | - * @return ViewFinder Instance of a BaseViewFinder. |
|
| 155 | - */ |
|
| 156 | - public function getViewFinder(): ViewFinder |
|
| 157 | - { |
|
| 158 | - return $this->getFinder($viewFinder, static::VIEW_FINDER_KEY); |
|
| 159 | - } |
|
| 149 | + /** |
|
| 150 | + * Get the ViewFinder instance. |
|
| 151 | + * |
|
| 152 | + * @since 0.1.0 |
|
| 153 | + * |
|
| 154 | + * @return ViewFinder Instance of a BaseViewFinder. |
|
| 155 | + */ |
|
| 156 | + public function getViewFinder(): ViewFinder |
|
| 157 | + { |
|
| 158 | + return $this->getFinder($viewFinder, static::VIEW_FINDER_KEY); |
|
| 159 | + } |
|
| 160 | 160 | |
| 161 | - /** |
|
| 162 | - * Get the EngineFinder instance. |
|
| 163 | - * |
|
| 164 | - * @since 0.1.0 |
|
| 165 | - * |
|
| 166 | - * @return EngineFinder Instance of a BaseEngineFinder. |
|
| 167 | - */ |
|
| 168 | - public function getEngineFinder(): EngineFinder |
|
| 169 | - { |
|
| 170 | - return $this->getFinder($this->engineFinder, static::ENGINE_FINDER_KEY); |
|
| 171 | - } |
|
| 161 | + /** |
|
| 162 | + * Get the EngineFinder instance. |
|
| 163 | + * |
|
| 164 | + * @since 0.1.0 |
|
| 165 | + * |
|
| 166 | + * @return EngineFinder Instance of a BaseEngineFinder. |
|
| 167 | + */ |
|
| 168 | + public function getEngineFinder(): EngineFinder |
|
| 169 | + { |
|
| 170 | + return $this->getFinder($this->engineFinder, static::ENGINE_FINDER_KEY); |
|
| 171 | + } |
|
| 172 | 172 | |
| 173 | - /** |
|
| 174 | - * Add a location to scan with the BaseViewFinder. |
|
| 175 | - * |
|
| 176 | - * @since 0.1.0 |
|
| 177 | - * |
|
| 178 | - * @param Location $location Location to scan with the BaseViewFinder. |
|
| 179 | - * |
|
| 180 | - * @return static |
|
| 181 | - */ |
|
| 182 | - public function addLocation(Location $location) |
|
| 183 | - { |
|
| 184 | - $this->locations->add($location); |
|
| 173 | + /** |
|
| 174 | + * Add a location to scan with the BaseViewFinder. |
|
| 175 | + * |
|
| 176 | + * @since 0.1.0 |
|
| 177 | + * |
|
| 178 | + * @param Location $location Location to scan with the BaseViewFinder. |
|
| 179 | + * |
|
| 180 | + * @return static |
|
| 181 | + */ |
|
| 182 | + public function addLocation(Location $location) |
|
| 183 | + { |
|
| 184 | + $this->locations->add($location); |
|
| 185 | 185 | |
| 186 | - return $this; |
|
| 187 | - } |
|
| 186 | + return $this; |
|
| 187 | + } |
|
| 188 | 188 | |
| 189 | - /** |
|
| 190 | - * Get the collection of locations registered with this ViewBuilder. |
|
| 191 | - * |
|
| 192 | - * @since 0.1.3 |
|
| 193 | - * |
|
| 194 | - * @return Locations Collection of locations. |
|
| 195 | - */ |
|
| 196 | - public function getLocations() |
|
| 197 | - { |
|
| 198 | - return $this->locations; |
|
| 199 | - } |
|
| 189 | + /** |
|
| 190 | + * Get the collection of locations registered with this ViewBuilder. |
|
| 191 | + * |
|
| 192 | + * @since 0.1.3 |
|
| 193 | + * |
|
| 194 | + * @return Locations Collection of locations. |
|
| 195 | + */ |
|
| 196 | + public function getLocations() |
|
| 197 | + { |
|
| 198 | + return $this->locations; |
|
| 199 | + } |
|
| 200 | 200 | |
| 201 | - /** |
|
| 202 | - * Scan Locations for an URI that matches the specified criteria. |
|
| 203 | - * |
|
| 204 | - * @since 0.1.0 |
|
| 205 | - * |
|
| 206 | - * @param array $criteria Criteria to match. |
|
| 207 | - * |
|
| 208 | - * @return string|false URI of the requested view, or false if not found. |
|
| 209 | - */ |
|
| 210 | - public function scanLocations(array $criteria) |
|
| 211 | - { |
|
| 212 | - $uris = $this->locations->map(function ($location) use ($criteria) { |
|
| 213 | - /** @var Location $location */ |
|
| 214 | - return $location->getURI($criteria); |
|
| 215 | - })->filter(function ($uri) { |
|
| 216 | - return false !== $uri; |
|
| 217 | - }); |
|
| 201 | + /** |
|
| 202 | + * Scan Locations for an URI that matches the specified criteria. |
|
| 203 | + * |
|
| 204 | + * @since 0.1.0 |
|
| 205 | + * |
|
| 206 | + * @param array $criteria Criteria to match. |
|
| 207 | + * |
|
| 208 | + * @return string|false URI of the requested view, or false if not found. |
|
| 209 | + */ |
|
| 210 | + public function scanLocations(array $criteria) |
|
| 211 | + { |
|
| 212 | + $uris = $this->locations->map(function ($location) use ($criteria) { |
|
| 213 | + /** @var Location $location */ |
|
| 214 | + return $location->getURI($criteria); |
|
| 215 | + })->filter(function ($uri) { |
|
| 216 | + return false !== $uri; |
|
| 217 | + }); |
|
| 218 | 218 | |
| 219 | - // Fall back for absolute paths on current filesystem. |
|
| 220 | - if ($uris->isEmpty()) { |
|
| 221 | - foreach ($criteria as $criterion) { |
|
| 222 | - if (file_exists($criterion)) { |
|
| 223 | - return $criterion; |
|
| 224 | - } |
|
| 225 | - } |
|
| 226 | - } |
|
| 219 | + // Fall back for absolute paths on current filesystem. |
|
| 220 | + if ($uris->isEmpty()) { |
|
| 221 | + foreach ($criteria as $criterion) { |
|
| 222 | + if (file_exists($criterion)) { |
|
| 223 | + return $criterion; |
|
| 224 | + } |
|
| 225 | + } |
|
| 226 | + } |
|
| 227 | 227 | |
| 228 | - return $uris->isEmpty() ? false : $uris->first(); |
|
| 229 | - } |
|
| 228 | + return $uris->isEmpty() ? false : $uris->first(); |
|
| 229 | + } |
|
| 230 | 230 | |
| 231 | - /** |
|
| 232 | - * Get a finder instance. |
|
| 233 | - * |
|
| 234 | - * @since 0.1.1 |
|
| 235 | - * |
|
| 236 | - * @param mixed $property Property to use. |
|
| 237 | - * @param string $key Configuration key to use. |
|
| 238 | - * |
|
| 239 | - * @return ViewFinder|EngineFinder The requested finder instance. |
|
| 240 | - */ |
|
| 241 | - protected function getFinder(&$property, $key) |
|
| 242 | - { |
|
| 243 | - if (null === $property) { |
|
| 244 | - $finderClass = $this->config->getKey($key, 'ClassName'); |
|
| 245 | - $property = new $finderClass($this->config->getSubConfig($key)); |
|
| 246 | - } |
|
| 231 | + /** |
|
| 232 | + * Get a finder instance. |
|
| 233 | + * |
|
| 234 | + * @since 0.1.1 |
|
| 235 | + * |
|
| 236 | + * @param mixed $property Property to use. |
|
| 237 | + * @param string $key Configuration key to use. |
|
| 238 | + * |
|
| 239 | + * @return ViewFinder|EngineFinder The requested finder instance. |
|
| 240 | + */ |
|
| 241 | + protected function getFinder(&$property, $key) |
|
| 242 | + { |
|
| 243 | + if (null === $property) { |
|
| 244 | + $finderClass = $this->config->getKey($key, 'ClassName'); |
|
| 245 | + $property = new $finderClass($this->config->getSubConfig($key)); |
|
| 246 | + } |
|
| 247 | 247 | |
| 248 | - return $property; |
|
| 249 | - } |
|
| 248 | + return $property; |
|
| 249 | + } |
|
| 250 | 250 | |
| 251 | - /** |
|
| 252 | - * Resolve the view type. |
|
| 253 | - * |
|
| 254 | - * @since 0.1.0 |
|
| 255 | - * |
|
| 256 | - * @param mixed $type Type of view that was requested. |
|
| 257 | - * @param string $uri URI to get a view for. |
|
| 258 | - * @param Engine|null $engine Engine to use for the view. |
|
| 259 | - * |
|
| 260 | - * @return View Resolved View object. |
|
| 261 | - * @throws FailedToInstantiateView If the view type could not be resolved. |
|
| 262 | - */ |
|
| 263 | - protected function resolveType($type, string $uri, Engine $engine = null): View |
|
| 264 | - { |
|
| 265 | - $configKey = [static::VIEW_FINDER_KEY, 'Views', $type]; |
|
| 251 | + /** |
|
| 252 | + * Resolve the view type. |
|
| 253 | + * |
|
| 254 | + * @since 0.1.0 |
|
| 255 | + * |
|
| 256 | + * @param mixed $type Type of view that was requested. |
|
| 257 | + * @param string $uri URI to get a view for. |
|
| 258 | + * @param Engine|null $engine Engine to use for the view. |
|
| 259 | + * |
|
| 260 | + * @return View Resolved View object. |
|
| 261 | + * @throws FailedToInstantiateView If the view type could not be resolved. |
|
| 262 | + */ |
|
| 263 | + protected function resolveType($type, string $uri, Engine $engine = null): View |
|
| 264 | + { |
|
| 265 | + $configKey = [static::VIEW_FINDER_KEY, 'Views', $type]; |
|
| 266 | 266 | |
| 267 | - if (is_string($type) && $this->config->hasKey($configKey)) { |
|
| 268 | - $className = $this->config->getKey($configKey); |
|
| 269 | - $type = new $className($uri, $engine); |
|
| 270 | - } |
|
| 267 | + if (is_string($type) && $this->config->hasKey($configKey)) { |
|
| 268 | + $className = $this->config->getKey($configKey); |
|
| 269 | + $type = new $className($uri, $engine); |
|
| 270 | + } |
|
| 271 | 271 | |
| 272 | - if (is_string($type)) { |
|
| 273 | - $type = new $type($uri, $engine); |
|
| 274 | - } |
|
| 272 | + if (is_string($type)) { |
|
| 273 | + $type = new $type($uri, $engine); |
|
| 274 | + } |
|
| 275 | 275 | |
| 276 | - if (is_callable($type)) { |
|
| 277 | - $type = $type($uri, $engine); |
|
| 278 | - } |
|
| 276 | + if (is_callable($type)) { |
|
| 277 | + $type = $type($uri, $engine); |
|
| 278 | + } |
|
| 279 | 279 | |
| 280 | - if (! $type instanceof View) { |
|
| 281 | - throw new FailedToInstantiateView( |
|
| 282 | - sprintf( |
|
| 283 | - _('Could not instantiate view "%s".'), |
|
| 284 | - serialize($type) |
|
| 285 | - ) |
|
| 286 | - ); |
|
| 287 | - } |
|
| 280 | + if (! $type instanceof View) { |
|
| 281 | + throw new FailedToInstantiateView( |
|
| 282 | + sprintf( |
|
| 283 | + _('Could not instantiate view "%s".'), |
|
| 284 | + serialize($type) |
|
| 285 | + ) |
|
| 286 | + ); |
|
| 287 | + } |
|
| 288 | 288 | |
| 289 | - return $type; |
|
| 290 | - } |
|
| 289 | + return $type; |
|
| 290 | + } |
|
| 291 | 291 | |
| 292 | - /** |
|
| 293 | - * Get the configuration to use in the ViewBuilder. |
|
| 294 | - * |
|
| 295 | - * @since 0.2.0 |
|
| 296 | - * |
|
| 297 | - * @param ConfigInterface|array $config Config to merge with defaults. |
|
| 298 | - * |
|
| 299 | - * @return ConfigInterface Configuration passed in through the constructor. |
|
| 300 | - */ |
|
| 301 | - protected function getConfig($config = []): ConfigInterface |
|
| 302 | - { |
|
| 303 | - $defaults = ConfigFactory::create(dirname(__DIR__, 2) . '/config/defaults.php', $config); |
|
| 304 | - $config = $config |
|
| 305 | - ? ConfigFactory::createFromArray(array_merge_recursive($defaults->getArrayCopy(), $config->getArrayCopy())) |
|
| 306 | - : $defaults; |
|
| 292 | + /** |
|
| 293 | + * Get the configuration to use in the ViewBuilder. |
|
| 294 | + * |
|
| 295 | + * @since 0.2.0 |
|
| 296 | + * |
|
| 297 | + * @param ConfigInterface|array $config Config to merge with defaults. |
|
| 298 | + * |
|
| 299 | + * @return ConfigInterface Configuration passed in through the constructor. |
|
| 300 | + */ |
|
| 301 | + protected function getConfig($config = []): ConfigInterface |
|
| 302 | + { |
|
| 303 | + $defaults = ConfigFactory::create(dirname(__DIR__, 2) . '/config/defaults.php', $config); |
|
| 304 | + $config = $config |
|
| 305 | + ? ConfigFactory::createFromArray(array_merge_recursive($defaults->getArrayCopy(), $config->getArrayCopy())) |
|
| 306 | + : $defaults; |
|
| 307 | 307 | |
| 308 | - return $config->getSubConfig('BrightNucleus\View'); |
|
| 309 | - } |
|
| 308 | + return $config->getSubConfig('BrightNucleus\View'); |
|
| 309 | + } |
|
| 310 | 310 | } |