@@ -33,228 +33,228 @@ |
||
| 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 | - * ViewFinder instance. |
|
| 43 | - * |
|
| 44 | - * @since 0.1.0 |
|
| 45 | - * |
|
| 46 | - * @var ViewFinderInterface |
|
| 47 | - */ |
|
| 48 | - protected $viewFinder; |
|
| 41 | + /** |
|
| 42 | + * ViewFinder instance. |
|
| 43 | + * |
|
| 44 | + * @since 0.1.0 |
|
| 45 | + * |
|
| 46 | + * @var ViewFinderInterface |
|
| 47 | + */ |
|
| 48 | + protected $viewFinder; |
|
| 49 | 49 | |
| 50 | - /** |
|
| 51 | - * EngineFinder instance. |
|
| 52 | - * |
|
| 53 | - * @since 0.1.0 |
|
| 54 | - * |
|
| 55 | - * @var EngineFinderInterface |
|
| 56 | - */ |
|
| 57 | - protected $engineFinder; |
|
| 50 | + /** |
|
| 51 | + * EngineFinder instance. |
|
| 52 | + * |
|
| 53 | + * @since 0.1.0 |
|
| 54 | + * |
|
| 55 | + * @var EngineFinderInterface |
|
| 56 | + */ |
|
| 57 | + protected $engineFinder; |
|
| 58 | 58 | |
| 59 | - /** |
|
| 60 | - * Locations to scan for views. |
|
| 61 | - * |
|
| 62 | - * @since 0.1.0 |
|
| 63 | - * |
|
| 64 | - * @var LocationInterface[] |
|
| 65 | - */ |
|
| 66 | - protected $locations; |
|
| 59 | + /** |
|
| 60 | + * Locations to scan for views. |
|
| 61 | + * |
|
| 62 | + * @since 0.1.0 |
|
| 63 | + * |
|
| 64 | + * @var LocationInterface[] |
|
| 65 | + */ |
|
| 66 | + protected $locations; |
|
| 67 | 67 | |
| 68 | - /** |
|
| 69 | - * Instantiate a ViewBuilder object. |
|
| 70 | - * |
|
| 71 | - * @since 0.1.0 |
|
| 72 | - * |
|
| 73 | - * @param ConfigInterface $config Configuration settings. |
|
| 74 | - * @param ViewFinderInterface|null $viewFinder ViewFinder instance. |
|
| 75 | - * @param EngineFinderInterface|null $engineFinder EngineFinder instance. |
|
| 76 | - * |
|
| 77 | - * @throws FailedToProcessConfigException If the config could not be processed. |
|
| 78 | - */ |
|
| 79 | - public function __construct( |
|
| 80 | - ConfigInterface $config, |
|
| 81 | - ViewFinderInterface $viewFinder = null, |
|
| 82 | - EngineFinderInterface $engineFinder = null |
|
| 83 | - ) { |
|
| 84 | - $this->processConfig($config); |
|
| 85 | - $this->viewFinder = $viewFinder; |
|
| 86 | - $this->engineFinder = $engineFinder; |
|
| 87 | - } |
|
| 68 | + /** |
|
| 69 | + * Instantiate a ViewBuilder object. |
|
| 70 | + * |
|
| 71 | + * @since 0.1.0 |
|
| 72 | + * |
|
| 73 | + * @param ConfigInterface $config Configuration settings. |
|
| 74 | + * @param ViewFinderInterface|null $viewFinder ViewFinder instance. |
|
| 75 | + * @param EngineFinderInterface|null $engineFinder EngineFinder instance. |
|
| 76 | + * |
|
| 77 | + * @throws FailedToProcessConfigException If the config could not be processed. |
|
| 78 | + */ |
|
| 79 | + public function __construct( |
|
| 80 | + ConfigInterface $config, |
|
| 81 | + ViewFinderInterface $viewFinder = null, |
|
| 82 | + EngineFinderInterface $engineFinder = null |
|
| 83 | + ) { |
|
| 84 | + $this->processConfig($config); |
|
| 85 | + $this->viewFinder = $viewFinder; |
|
| 86 | + $this->engineFinder = $engineFinder; |
|
| 87 | + } |
|
| 88 | 88 | |
| 89 | - /** |
|
| 90 | - * Create a new view for a given URI. |
|
| 91 | - * |
|
| 92 | - * @since 0.1.0 |
|
| 93 | - * |
|
| 94 | - * @param string $view View identifier to create a view for. |
|
| 95 | - * @param mixed $type Type of view to create. |
|
| 96 | - * |
|
| 97 | - * @return ViewInterface Instance of the requested view. |
|
| 98 | - */ |
|
| 99 | - public function create($view, $type = null) |
|
| 100 | - { |
|
| 101 | - $uri = $this->scanLocations([$view]); |
|
| 102 | - $engine = $this->getEngine($uri); |
|
| 89 | + /** |
|
| 90 | + * Create a new view for a given URI. |
|
| 91 | + * |
|
| 92 | + * @since 0.1.0 |
|
| 93 | + * |
|
| 94 | + * @param string $view View identifier to create a view for. |
|
| 95 | + * @param mixed $type Type of view to create. |
|
| 96 | + * |
|
| 97 | + * @return ViewInterface Instance of the requested view. |
|
| 98 | + */ |
|
| 99 | + public function create($view, $type = null) |
|
| 100 | + { |
|
| 101 | + $uri = $this->scanLocations([$view]); |
|
| 102 | + $engine = $this->getEngine($uri); |
|
| 103 | 103 | |
| 104 | - return $uri |
|
| 105 | - ? $this->getView($uri, $engine, $type) |
|
| 106 | - : $this->getViewFinder()->getNullObject(); |
|
| 107 | - } |
|
| 104 | + return $uri |
|
| 105 | + ? $this->getView($uri, $engine, $type) |
|
| 106 | + : $this->getViewFinder()->getNullObject(); |
|
| 107 | + } |
|
| 108 | 108 | |
| 109 | - /** |
|
| 110 | - * Get an Engine that can deal with the given URI. |
|
| 111 | - * |
|
| 112 | - * @since 0.1.0 |
|
| 113 | - * |
|
| 114 | - * @param string|false $uri URI to get an engine for. |
|
| 115 | - * |
|
| 116 | - * @return EngineInterface Instance of an engine that can deal with the given URI. |
|
| 117 | - */ |
|
| 118 | - public function getEngine($uri) |
|
| 119 | - { |
|
| 120 | - return $this->getEngineFinder()->find([$uri]); |
|
| 121 | - } |
|
| 109 | + /** |
|
| 110 | + * Get an Engine that can deal with the given URI. |
|
| 111 | + * |
|
| 112 | + * @since 0.1.0 |
|
| 113 | + * |
|
| 114 | + * @param string|false $uri URI to get an engine for. |
|
| 115 | + * |
|
| 116 | + * @return EngineInterface Instance of an engine that can deal with the given URI. |
|
| 117 | + */ |
|
| 118 | + public function getEngine($uri) |
|
| 119 | + { |
|
| 120 | + return $this->getEngineFinder()->find([$uri]); |
|
| 121 | + } |
|
| 122 | 122 | |
| 123 | - /** |
|
| 124 | - * Get a view for a given URI, engine and type. |
|
| 125 | - * |
|
| 126 | - * @since 0.1.0 |
|
| 127 | - * |
|
| 128 | - * @param string $uri URI to get a view for. |
|
| 129 | - * @param EngineInterface $engine Engine to use for the view. |
|
| 130 | - * @param mixed $type Type of view to get. |
|
| 131 | - * |
|
| 132 | - * @return ViewInterface View that matches the given requirements. |
|
| 133 | - */ |
|
| 134 | - public function getView($uri, EngineInterface $engine, $type = null) |
|
| 135 | - { |
|
| 136 | - if (null === $type) { |
|
| 137 | - return $this->getViewFinder()->find([$uri], $engine); |
|
| 138 | - } |
|
| 123 | + /** |
|
| 124 | + * Get a view for a given URI, engine and type. |
|
| 125 | + * |
|
| 126 | + * @since 0.1.0 |
|
| 127 | + * |
|
| 128 | + * @param string $uri URI to get a view for. |
|
| 129 | + * @param EngineInterface $engine Engine to use for the view. |
|
| 130 | + * @param mixed $type Type of view to get. |
|
| 131 | + * |
|
| 132 | + * @return ViewInterface View that matches the given requirements. |
|
| 133 | + */ |
|
| 134 | + public function getView($uri, EngineInterface $engine, $type = null) |
|
| 135 | + { |
|
| 136 | + if (null === $type) { |
|
| 137 | + return $this->getViewFinder()->find([$uri], $engine); |
|
| 138 | + } |
|
| 139 | 139 | |
| 140 | - return $this->resolveType($type, $uri, $engine); |
|
| 141 | - } |
|
| 140 | + return $this->resolveType($type, $uri, $engine); |
|
| 141 | + } |
|
| 142 | 142 | |
| 143 | - /** |
|
| 144 | - * Get the ViewFinder instance. |
|
| 145 | - * |
|
| 146 | - * @since 0.1.0 |
|
| 147 | - * |
|
| 148 | - * @return ViewFinderInterface Instance of a ViewFinder. |
|
| 149 | - */ |
|
| 150 | - public function getViewFinder() |
|
| 151 | - { |
|
| 152 | - return $this->getFinder($this->viewFinder, ViewBuilder::VIEW_FINDER_KEY); |
|
| 153 | - } |
|
| 143 | + /** |
|
| 144 | + * Get the ViewFinder instance. |
|
| 145 | + * |
|
| 146 | + * @since 0.1.0 |
|
| 147 | + * |
|
| 148 | + * @return ViewFinderInterface Instance of a ViewFinder. |
|
| 149 | + */ |
|
| 150 | + public function getViewFinder() |
|
| 151 | + { |
|
| 152 | + return $this->getFinder($this->viewFinder, ViewBuilder::VIEW_FINDER_KEY); |
|
| 153 | + } |
|
| 154 | 154 | |
| 155 | - /** |
|
| 156 | - * Get the EngineFinder instance. |
|
| 157 | - * |
|
| 158 | - * @since 0.1.0 |
|
| 159 | - * |
|
| 160 | - * @return EngineFinderInterface Instance of a EngineFinder. |
|
| 161 | - */ |
|
| 162 | - public function getEngineFinder() |
|
| 163 | - { |
|
| 164 | - return $this->getFinder($this->engineFinder, ViewBuilder::ENGINE_FINDER_KEY); |
|
| 165 | - } |
|
| 155 | + /** |
|
| 156 | + * Get the EngineFinder instance. |
|
| 157 | + * |
|
| 158 | + * @since 0.1.0 |
|
| 159 | + * |
|
| 160 | + * @return EngineFinderInterface Instance of a EngineFinder. |
|
| 161 | + */ |
|
| 162 | + public function getEngineFinder() |
|
| 163 | + { |
|
| 164 | + return $this->getFinder($this->engineFinder, ViewBuilder::ENGINE_FINDER_KEY); |
|
| 165 | + } |
|
| 166 | 166 | |
| 167 | - /** |
|
| 168 | - * Add a location to scan with the ViewFinder. |
|
| 169 | - * |
|
| 170 | - * @since 0.1.0 |
|
| 171 | - * |
|
| 172 | - * @param LocationInterface $location Location to scan with the ViewFinder. |
|
| 173 | - */ |
|
| 174 | - public function addLocation(LocationInterface $location) |
|
| 175 | - { |
|
| 176 | - $this->locations[] = $location; |
|
| 177 | - } |
|
| 167 | + /** |
|
| 168 | + * Add a location to scan with the ViewFinder. |
|
| 169 | + * |
|
| 170 | + * @since 0.1.0 |
|
| 171 | + * |
|
| 172 | + * @param LocationInterface $location Location to scan with the ViewFinder. |
|
| 173 | + */ |
|
| 174 | + public function addLocation(LocationInterface $location) |
|
| 175 | + { |
|
| 176 | + $this->locations[] = $location; |
|
| 177 | + } |
|
| 178 | 178 | |
| 179 | - /** |
|
| 180 | - * Scan Locations for an URI that matches the specified criteria. |
|
| 181 | - * |
|
| 182 | - * @since 0.1.0 |
|
| 183 | - * |
|
| 184 | - * @param array $criteria Criteria to match. |
|
| 185 | - * |
|
| 186 | - * @return string|false URI of the requested view, or false if not found. |
|
| 187 | - */ |
|
| 188 | - public function scanLocations(array $criteria) |
|
| 189 | - { |
|
| 190 | - /** @var LocationInterface $location */ |
|
| 191 | - foreach ($this->locations as $location) { |
|
| 192 | - if ($uri = $location->getURI($criteria)) { |
|
| 193 | - return $uri; |
|
| 194 | - } |
|
| 195 | - } |
|
| 179 | + /** |
|
| 180 | + * Scan Locations for an URI that matches the specified criteria. |
|
| 181 | + * |
|
| 182 | + * @since 0.1.0 |
|
| 183 | + * |
|
| 184 | + * @param array $criteria Criteria to match. |
|
| 185 | + * |
|
| 186 | + * @return string|false URI of the requested view, or false if not found. |
|
| 187 | + */ |
|
| 188 | + public function scanLocations(array $criteria) |
|
| 189 | + { |
|
| 190 | + /** @var LocationInterface $location */ |
|
| 191 | + foreach ($this->locations as $location) { |
|
| 192 | + if ($uri = $location->getURI($criteria)) { |
|
| 193 | + return $uri; |
|
| 194 | + } |
|
| 195 | + } |
|
| 196 | 196 | |
| 197 | - return false; |
|
| 198 | - } |
|
| 197 | + return false; |
|
| 198 | + } |
|
| 199 | 199 | |
| 200 | - /** |
|
| 201 | - * Get a finder instance. |
|
| 202 | - * |
|
| 203 | - * @since 0.1.1 |
|
| 204 | - * |
|
| 205 | - * @param mixed $property Property to use. |
|
| 206 | - * @param string $key Configuration key to use. |
|
| 207 | - * |
|
| 208 | - * @return FinderInterface The requested finder instance. |
|
| 209 | - */ |
|
| 210 | - protected function getFinder(&$property, $key) |
|
| 211 | - { |
|
| 212 | - if (null === $property) { |
|
| 213 | - $engineFinderClass = $this->config->getKey($key, 'ClassName'); |
|
| 214 | - $property = new $engineFinderClass($this->config->getSubConfig($key)); |
|
| 215 | - } |
|
| 200 | + /** |
|
| 201 | + * Get a finder instance. |
|
| 202 | + * |
|
| 203 | + * @since 0.1.1 |
|
| 204 | + * |
|
| 205 | + * @param mixed $property Property to use. |
|
| 206 | + * @param string $key Configuration key to use. |
|
| 207 | + * |
|
| 208 | + * @return FinderInterface The requested finder instance. |
|
| 209 | + */ |
|
| 210 | + protected function getFinder(&$property, $key) |
|
| 211 | + { |
|
| 212 | + if (null === $property) { |
|
| 213 | + $engineFinderClass = $this->config->getKey($key, 'ClassName'); |
|
| 214 | + $property = new $engineFinderClass($this->config->getSubConfig($key)); |
|
| 215 | + } |
|
| 216 | 216 | |
| 217 | - return $property; |
|
| 218 | - } |
|
| 217 | + return $property; |
|
| 218 | + } |
|
| 219 | 219 | |
| 220 | - /** |
|
| 221 | - * Resolve the view type. |
|
| 222 | - * |
|
| 223 | - * @since 0.1.0 |
|
| 224 | - * |
|
| 225 | - * @param mixed $type Type of view that was requested. |
|
| 226 | - * @param string $uri URI to get a view for. |
|
| 227 | - * @param EngineInterface|null $engine Engine to use for the view. |
|
| 228 | - * |
|
| 229 | - * @return ViewInterface Resolved View object. |
|
| 230 | - * @throws FailedToInstantiateViewException If the view type could not be resolved. |
|
| 231 | - */ |
|
| 232 | - protected function resolveType($type, $uri, EngineInterface $engine = null) |
|
| 233 | - { |
|
| 234 | - $configKey = [static::VIEW_FINDER_KEY, 'Views', $type]; |
|
| 220 | + /** |
|
| 221 | + * Resolve the view type. |
|
| 222 | + * |
|
| 223 | + * @since 0.1.0 |
|
| 224 | + * |
|
| 225 | + * @param mixed $type Type of view that was requested. |
|
| 226 | + * @param string $uri URI to get a view for. |
|
| 227 | + * @param EngineInterface|null $engine Engine to use for the view. |
|
| 228 | + * |
|
| 229 | + * @return ViewInterface Resolved View object. |
|
| 230 | + * @throws FailedToInstantiateViewException If the view type could not be resolved. |
|
| 231 | + */ |
|
| 232 | + protected function resolveType($type, $uri, EngineInterface $engine = null) |
|
| 233 | + { |
|
| 234 | + $configKey = [static::VIEW_FINDER_KEY, 'Views', $type]; |
|
| 235 | 235 | |
| 236 | - if (is_string($type) && $this->config->hasKey($configKey)) { |
|
| 237 | - $className = $this->config->getKey($configKey); |
|
| 238 | - $type = new $className($uri, $engine); |
|
| 239 | - } |
|
| 236 | + if (is_string($type) && $this->config->hasKey($configKey)) { |
|
| 237 | + $className = $this->config->getKey($configKey); |
|
| 238 | + $type = new $className($uri, $engine); |
|
| 239 | + } |
|
| 240 | 240 | |
| 241 | - if (is_string($type)) { |
|
| 242 | - $type = new $type($uri, $engine); |
|
| 243 | - } |
|
| 241 | + if (is_string($type)) { |
|
| 242 | + $type = new $type($uri, $engine); |
|
| 243 | + } |
|
| 244 | 244 | |
| 245 | - if (is_callable($type)) { |
|
| 246 | - $type = $type($uri, $engine); |
|
| 247 | - } |
|
| 245 | + if (is_callable($type)) { |
|
| 246 | + $type = $type($uri, $engine); |
|
| 247 | + } |
|
| 248 | 248 | |
| 249 | - if (! $type instanceof ViewInterface) { |
|
| 250 | - throw new FailedToInstantiateViewException( |
|
| 251 | - sprintf( |
|
| 252 | - _('Could not instantiate view "%s".'), |
|
| 253 | - serialize($type) |
|
| 254 | - ) |
|
| 255 | - ); |
|
| 256 | - } |
|
| 249 | + if (! $type instanceof ViewInterface) { |
|
| 250 | + throw new FailedToInstantiateViewException( |
|
| 251 | + sprintf( |
|
| 252 | + _('Could not instantiate view "%s".'), |
|
| 253 | + serialize($type) |
|
| 254 | + ) |
|
| 255 | + ); |
|
| 256 | + } |
|
| 257 | 257 | |
| 258 | - return $type; |
|
| 259 | - } |
|
| 258 | + return $type; |
|
| 259 | + } |
|
| 260 | 260 | } |