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