| @@ 85-105 (lines=21) @@ | ||
| 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. |
|
| @@ 119-139 (lines=21) @@ | ||
| 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. |
|
| @@ 219-239 (lines=21) @@ | ||
| 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 | } |
|
| 241 | ||