@@ -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 | } |
@@ -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 | } |
@@ -22,38 +22,38 @@ |
||
| 22 | 22 | class URIHelper |
| 23 | 23 | { |
| 24 | 24 | |
| 25 | - /** |
|
| 26 | - * Check whether a given URI has a specific extension. |
|
| 27 | - * |
|
| 28 | - * @since 0.1.3 |
|
| 29 | - * |
|
| 30 | - * @param string $uri URI to check the extension of. |
|
| 31 | - * @param string $extension Extension to check for. |
|
| 32 | - * |
|
| 33 | - * @return bool |
|
| 34 | - */ |
|
| 35 | - public static function hasExtension($uri, $extension) |
|
| 36 | - { |
|
| 37 | - $uriLength = mb_strlen($uri); |
|
| 38 | - $extensionLength = mb_strlen($extension); |
|
| 39 | - if ($extensionLength > $uriLength) { |
|
| 40 | - return false; |
|
| 41 | - } |
|
| 25 | + /** |
|
| 26 | + * Check whether a given URI has a specific extension. |
|
| 27 | + * |
|
| 28 | + * @since 0.1.3 |
|
| 29 | + * |
|
| 30 | + * @param string $uri URI to check the extension of. |
|
| 31 | + * @param string $extension Extension to check for. |
|
| 32 | + * |
|
| 33 | + * @return bool |
|
| 34 | + */ |
|
| 35 | + public static function hasExtension($uri, $extension) |
|
| 36 | + { |
|
| 37 | + $uriLength = mb_strlen($uri); |
|
| 38 | + $extensionLength = mb_strlen($extension); |
|
| 39 | + if ($extensionLength > $uriLength) { |
|
| 40 | + return false; |
|
| 41 | + } |
|
| 42 | 42 | |
| 43 | - return substr_compare($uri, $extension, $uriLength - $extensionLength, $extensionLength) === 0; |
|
| 44 | - } |
|
| 43 | + return substr_compare($uri, $extension, $uriLength - $extensionLength, $extensionLength) === 0; |
|
| 44 | + } |
|
| 45 | 45 | |
| 46 | - /** |
|
| 47 | - * Get the filename for an URI. |
|
| 48 | - * |
|
| 49 | - * @since 0.1.3 |
|
| 50 | - * |
|
| 51 | - * @param string $uri URI to get the filename from. |
|
| 52 | - * |
|
| 53 | - * @return string Filename without path. |
|
| 54 | - */ |
|
| 55 | - public static function getFilename($uri) |
|
| 56 | - { |
|
| 57 | - return basename($uri); |
|
| 58 | - } |
|
| 46 | + /** |
|
| 47 | + * Get the filename for an URI. |
|
| 48 | + * |
|
| 49 | + * @since 0.1.3 |
|
| 50 | + * |
|
| 51 | + * @param string $uri URI to get the filename from. |
|
| 52 | + * |
|
| 53 | + * @return string Filename without path. |
|
| 54 | + */ |
|
| 55 | + public static function getFilename($uri) |
|
| 56 | + { |
|
| 57 | + return basename($uri); |
|
| 58 | + } |
|
| 59 | 59 | } |
@@ -51,7 +51,7 @@ |
||
| 51 | 51 | * @param string $uri URI to render. |
| 52 | 52 | * @param array $context Context in which to render. |
| 53 | 53 | * |
| 54 | - * @return callable Rendering callback. |
|
| 54 | + * @return \Closure Rendering callback. |
|
| 55 | 55 | * @throws FailedToLoadView If the View URI is not accessible or readable. |
| 56 | 56 | * @throws FailedToLoadView If the View URI could not be loaded. |
| 57 | 57 | */ |
@@ -26,76 +26,76 @@ |
||
| 26 | 26 | class PHPEngine extends AbstractEngine |
| 27 | 27 | { |
| 28 | 28 | |
| 29 | - const PHP_EXTENSION = '.php'; |
|
| 29 | + const PHP_EXTENSION = '.php'; |
|
| 30 | 30 | |
| 31 | - /** |
|
| 32 | - * Check whether the Findable can handle an individual criterion. |
|
| 33 | - * |
|
| 34 | - * @since 0.1.0 |
|
| 35 | - * |
|
| 36 | - * @param mixed $criterion Criterion to check. |
|
| 37 | - * |
|
| 38 | - * @return bool Whether the Findable can handle the criterion. |
|
| 39 | - */ |
|
| 40 | - public function canHandle($criterion) |
|
| 41 | - { |
|
| 42 | - return URIHelper::hasExtension($criterion, static::PHP_EXTENSION) |
|
| 43 | - && is_readable($criterion); |
|
| 44 | - } |
|
| 31 | + /** |
|
| 32 | + * Check whether the Findable can handle an individual criterion. |
|
| 33 | + * |
|
| 34 | + * @since 0.1.0 |
|
| 35 | + * |
|
| 36 | + * @param mixed $criterion Criterion to check. |
|
| 37 | + * |
|
| 38 | + * @return bool Whether the Findable can handle the criterion. |
|
| 39 | + */ |
|
| 40 | + public function canHandle($criterion) |
|
| 41 | + { |
|
| 42 | + return URIHelper::hasExtension($criterion, static::PHP_EXTENSION) |
|
| 43 | + && is_readable($criterion); |
|
| 44 | + } |
|
| 45 | 45 | |
| 46 | - /** |
|
| 47 | - * Get the rendering callback for a given URI. |
|
| 48 | - * |
|
| 49 | - * @since 0.1.0 |
|
| 50 | - * |
|
| 51 | - * @param string $uri URI to render. |
|
| 52 | - * @param array $context Context in which to render. |
|
| 53 | - * |
|
| 54 | - * @return callable Rendering callback. |
|
| 55 | - * @throws FailedToLoadView If the View URI is not accessible or readable. |
|
| 56 | - * @throws FailedToLoadView If the View URI could not be loaded. |
|
| 57 | - */ |
|
| 58 | - public function getRenderCallback($uri, array $context = []) |
|
| 59 | - { |
|
| 60 | - if ( ! is_readable($uri)) { |
|
| 61 | - throw new FailedToLoadView( |
|
| 62 | - sprintf( |
|
| 63 | - _('The View URI "%1$s" is not accessible or readable.'), |
|
| 64 | - $uri |
|
| 65 | - ) |
|
| 66 | - ); |
|
| 67 | - } |
|
| 46 | + /** |
|
| 47 | + * Get the rendering callback for a given URI. |
|
| 48 | + * |
|
| 49 | + * @since 0.1.0 |
|
| 50 | + * |
|
| 51 | + * @param string $uri URI to render. |
|
| 52 | + * @param array $context Context in which to render. |
|
| 53 | + * |
|
| 54 | + * @return callable Rendering callback. |
|
| 55 | + * @throws FailedToLoadView If the View URI is not accessible or readable. |
|
| 56 | + * @throws FailedToLoadView If the View URI could not be loaded. |
|
| 57 | + */ |
|
| 58 | + public function getRenderCallback($uri, array $context = []) |
|
| 59 | + { |
|
| 60 | + if ( ! is_readable($uri)) { |
|
| 61 | + throw new FailedToLoadView( |
|
| 62 | + sprintf( |
|
| 63 | + _('The View URI "%1$s" is not accessible or readable.'), |
|
| 64 | + $uri |
|
| 65 | + ) |
|
| 66 | + ); |
|
| 67 | + } |
|
| 68 | 68 | |
| 69 | - $closure = function () use ($uri, $context) { |
|
| 69 | + $closure = function () use ($uri, $context) { |
|
| 70 | 70 | |
| 71 | - // Save current buffering level so we can backtrack in case of an error. |
|
| 72 | - // This is needed because the view itself might also add an unknown number of output buffering levels. |
|
| 73 | - $bufferLevel = ob_get_level(); |
|
| 74 | - ob_start(); |
|
| 71 | + // Save current buffering level so we can backtrack in case of an error. |
|
| 72 | + // This is needed because the view itself might also add an unknown number of output buffering levels. |
|
| 73 | + $bufferLevel = ob_get_level(); |
|
| 74 | + ob_start(); |
|
| 75 | 75 | |
| 76 | - try { |
|
| 77 | - include($uri); |
|
| 78 | - } catch (Exception $exception) { |
|
| 76 | + try { |
|
| 77 | + include($uri); |
|
| 78 | + } catch (Exception $exception) { |
|
| 79 | 79 | |
| 80 | - // Remove whatever levels were added up until now. |
|
| 81 | - while (ob_get_level() > $bufferLevel) { |
|
| 82 | - ob_end_clean(); |
|
| 83 | - } |
|
| 80 | + // Remove whatever levels were added up until now. |
|
| 81 | + while (ob_get_level() > $bufferLevel) { |
|
| 82 | + ob_end_clean(); |
|
| 83 | + } |
|
| 84 | 84 | |
| 85 | - throw new FailedToLoadView( |
|
| 86 | - sprintf( |
|
| 87 | - _('Could not load the View URI "%1$s". Reason: "%2$s".'), |
|
| 88 | - $uri, |
|
| 89 | - $exception->getMessage() |
|
| 90 | - ), |
|
| 91 | - $exception->getCode(), |
|
| 92 | - $exception |
|
| 93 | - ); |
|
| 94 | - } |
|
| 85 | + throw new FailedToLoadView( |
|
| 86 | + sprintf( |
|
| 87 | + _('Could not load the View URI "%1$s". Reason: "%2$s".'), |
|
| 88 | + $uri, |
|
| 89 | + $exception->getMessage() |
|
| 90 | + ), |
|
| 91 | + $exception->getCode(), |
|
| 92 | + $exception |
|
| 93 | + ); |
|
| 94 | + } |
|
| 95 | 95 | |
| 96 | - return ob_get_clean(); |
|
| 97 | - }; |
|
| 96 | + return ob_get_clean(); |
|
| 97 | + }; |
|
| 98 | 98 | |
| 99 | - return $closure; |
|
| 100 | - } |
|
| 99 | + return $closure; |
|
| 100 | + } |
|
| 101 | 101 | } |
@@ -66,7 +66,7 @@ |
||
| 66 | 66 | ); |
| 67 | 67 | } |
| 68 | 68 | |
| 69 | - $closure = function () use ($uri, $context) { |
|
| 69 | + $closure = function() use ($uri, $context) { |
|
| 70 | 70 | |
| 71 | 71 | // Save current buffering level so we can backtrack in case of an error. |
| 72 | 72 | // This is needed because the view itself might also add an unknown number of output buffering levels. |
@@ -94,7 +94,7 @@ discard block |
||
| 94 | 94 | * @since 0.1.0 |
| 95 | 95 | * |
| 96 | 96 | * @param string $view View identifier to create a view for. |
| 97 | - * @param mixed $type Type of view to create. |
|
| 97 | + * @param string|null $type Type of view to create. |
|
| 98 | 98 | * |
| 99 | 99 | * @return View Instance of the requested view. |
| 100 | 100 | */ |
@@ -195,7 +195,7 @@ discard block |
||
| 195 | 195 | * |
| 196 | 196 | * @since 0.1.0 |
| 197 | 197 | * |
| 198 | - * @param array $criteria Criteria to match. |
|
| 198 | + * @param string[] $criteria Criteria to match. |
|
| 199 | 199 | * |
| 200 | 200 | * @return string|false URI of the requested view, or false if not found. |
| 201 | 201 | */ |
@@ -277,6 +277,7 @@ discard block |
||
| 277 | 277 | * |
| 278 | 278 | * @since 0.2.0 |
| 279 | 279 | * |
| 280 | + * @param ConfigInterface $config |
|
| 280 | 281 | * @return ConfigInterface Configuration passed in through the constructor. |
| 281 | 282 | */ |
| 282 | 283 | protected function getConfig($config = null) |
@@ -34,258 +34,258 @@ |
||
| 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 | - */ |
|
| 101 | - public function create($view, $type = null) |
|
| 102 | - { |
|
| 103 | - $uri = $this->scanLocations([$view]); |
|
| 104 | - $engine = $this->getEngine($uri); |
|
| 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 | + */ |
|
| 101 | + public function create($view, $type = null) |
|
| 102 | + { |
|
| 103 | + $uri = $this->scanLocations([$view]); |
|
| 104 | + $engine = $this->getEngine($uri); |
|
| 105 | 105 | |
| 106 | - return $uri |
|
| 107 | - ? $this->getView($uri, $engine, $type) |
|
| 108 | - : $this->getViewFinder()->getNullObject(); |
|
| 109 | - } |
|
| 106 | + return $uri |
|
| 107 | + ? $this->getView($uri, $engine, $type) |
|
| 108 | + : $this->getViewFinder()->getNullObject(); |
|
| 109 | + } |
|
| 110 | 110 | |
| 111 | - /** |
|
| 112 | - * Get an Engine that can deal with the given URI. |
|
| 113 | - * |
|
| 114 | - * @since 0.1.0 |
|
| 115 | - * |
|
| 116 | - * @param string|false $uri URI to get an engine for. |
|
| 117 | - * |
|
| 118 | - * @return Engine Instance of an engine that can deal with the given URI. |
|
| 119 | - */ |
|
| 120 | - public function getEngine($uri) |
|
| 121 | - { |
|
| 122 | - return $this->getEngineFinder()->find([$uri]); |
|
| 123 | - } |
|
| 111 | + /** |
|
| 112 | + * Get an Engine that can deal with the given URI. |
|
| 113 | + * |
|
| 114 | + * @since 0.1.0 |
|
| 115 | + * |
|
| 116 | + * @param string|false $uri URI to get an engine for. |
|
| 117 | + * |
|
| 118 | + * @return Engine Instance of an engine that can deal with the given URI. |
|
| 119 | + */ |
|
| 120 | + public function getEngine($uri) |
|
| 121 | + { |
|
| 122 | + return $this->getEngineFinder()->find([$uri]); |
|
| 123 | + } |
|
| 124 | 124 | |
| 125 | - /** |
|
| 126 | - * Get a view for a given URI, engine and type. |
|
| 127 | - * |
|
| 128 | - * @since 0.1.0 |
|
| 129 | - * |
|
| 130 | - * @param string $uri URI to get a view for. |
|
| 131 | - * @param Engine $engine Engine to use for the view. |
|
| 132 | - * @param mixed $type Type of view to get. |
|
| 133 | - * |
|
| 134 | - * @return View View that matches the given requirements. |
|
| 135 | - */ |
|
| 136 | - public function getView($uri, Engine $engine, $type = null) |
|
| 137 | - { |
|
| 138 | - $view = (null === $type) |
|
| 139 | - ? $this->getViewFinder()->find([$uri], $engine) |
|
| 140 | - : $this->resolveType($type, $uri, $engine); |
|
| 125 | + /** |
|
| 126 | + * Get a view for a given URI, engine and type. |
|
| 127 | + * |
|
| 128 | + * @since 0.1.0 |
|
| 129 | + * |
|
| 130 | + * @param string $uri URI to get a view for. |
|
| 131 | + * @param Engine $engine Engine to use for the view. |
|
| 132 | + * @param mixed $type Type of view to get. |
|
| 133 | + * |
|
| 134 | + * @return View View that matches the given requirements. |
|
| 135 | + */ |
|
| 136 | + public function getView($uri, Engine $engine, $type = null) |
|
| 137 | + { |
|
| 138 | + $view = (null === $type) |
|
| 139 | + ? $this->getViewFinder()->find([$uri], $engine) |
|
| 140 | + : $this->resolveType($type, $uri, $engine); |
|
| 141 | 141 | |
| 142 | - return $view->setBuilder($this); |
|
| 143 | - } |
|
| 142 | + return $view->setBuilder($this); |
|
| 143 | + } |
|
| 144 | 144 | |
| 145 | - /** |
|
| 146 | - * Get the BaseViewFinder instance. |
|
| 147 | - * |
|
| 148 | - * @since 0.1.0 |
|
| 149 | - * |
|
| 150 | - * @return ViewFinder Instance of a BaseViewFinder. |
|
| 151 | - */ |
|
| 152 | - public function getViewFinder() |
|
| 153 | - { |
|
| 154 | - return $this->getFinder($viewFinder, static::VIEW_FINDER_KEY); |
|
| 155 | - } |
|
| 145 | + /** |
|
| 146 | + * Get the BaseViewFinder instance. |
|
| 147 | + * |
|
| 148 | + * @since 0.1.0 |
|
| 149 | + * |
|
| 150 | + * @return ViewFinder Instance of a BaseViewFinder. |
|
| 151 | + */ |
|
| 152 | + public function getViewFinder() |
|
| 153 | + { |
|
| 154 | + return $this->getFinder($viewFinder, static::VIEW_FINDER_KEY); |
|
| 155 | + } |
|
| 156 | 156 | |
| 157 | - /** |
|
| 158 | - * Get the BaseEngineFinder instance. |
|
| 159 | - * |
|
| 160 | - * @since 0.1.0 |
|
| 161 | - * |
|
| 162 | - * @return BaseEngineFinder Instance of a BaseEngineFinder. |
|
| 163 | - */ |
|
| 164 | - public function getEngineFinder() |
|
| 165 | - { |
|
| 166 | - return $this->getFinder($this->engineFinder, static::ENGINE_FINDER_KEY); |
|
| 167 | - } |
|
| 157 | + /** |
|
| 158 | + * Get the BaseEngineFinder instance. |
|
| 159 | + * |
|
| 160 | + * @since 0.1.0 |
|
| 161 | + * |
|
| 162 | + * @return BaseEngineFinder Instance of a BaseEngineFinder. |
|
| 163 | + */ |
|
| 164 | + public function getEngineFinder() |
|
| 165 | + { |
|
| 166 | + return $this->getFinder($this->engineFinder, static::ENGINE_FINDER_KEY); |
|
| 167 | + } |
|
| 168 | 168 | |
| 169 | - /** |
|
| 170 | - * Add a location to scan with the BaseViewFinder. |
|
| 171 | - * |
|
| 172 | - * @since 0.1.0 |
|
| 173 | - * |
|
| 174 | - * @param Location $location Location to scan with the BaseViewFinder. |
|
| 175 | - */ |
|
| 176 | - public function addLocation(Location $location) |
|
| 177 | - { |
|
| 178 | - $this->locations->add($location); |
|
| 179 | - } |
|
| 169 | + /** |
|
| 170 | + * Add a location to scan with the BaseViewFinder. |
|
| 171 | + * |
|
| 172 | + * @since 0.1.0 |
|
| 173 | + * |
|
| 174 | + * @param Location $location Location to scan with the BaseViewFinder. |
|
| 175 | + */ |
|
| 176 | + public function addLocation(Location $location) |
|
| 177 | + { |
|
| 178 | + $this->locations->add($location); |
|
| 179 | + } |
|
| 180 | 180 | |
| 181 | - /** |
|
| 182 | - * Get the collection of locations registered with this ViewBuilder. |
|
| 183 | - * |
|
| 184 | - * @since 0.1.3 |
|
| 185 | - * |
|
| 186 | - * @return Locations Collection of locations. |
|
| 187 | - */ |
|
| 188 | - public function getLocations() |
|
| 189 | - { |
|
| 190 | - return $this->locations; |
|
| 191 | - } |
|
| 181 | + /** |
|
| 182 | + * Get the collection of locations registered with this ViewBuilder. |
|
| 183 | + * |
|
| 184 | + * @since 0.1.3 |
|
| 185 | + * |
|
| 186 | + * @return Locations Collection of locations. |
|
| 187 | + */ |
|
| 188 | + public function getLocations() |
|
| 189 | + { |
|
| 190 | + return $this->locations; |
|
| 191 | + } |
|
| 192 | 192 | |
| 193 | - /** |
|
| 194 | - * Scan Locations for an URI that matches the specified criteria. |
|
| 195 | - * |
|
| 196 | - * @since 0.1.0 |
|
| 197 | - * |
|
| 198 | - * @param array $criteria Criteria to match. |
|
| 199 | - * |
|
| 200 | - * @return string|false URI of the requested view, or false if not found. |
|
| 201 | - */ |
|
| 202 | - public function scanLocations(array $criteria) |
|
| 203 | - { |
|
| 204 | - $uris = $this->locations->map(function ($location) use ($criteria) { |
|
| 205 | - /** @var Location $location */ |
|
| 206 | - return $location->getURI($criteria); |
|
| 207 | - })->filter(function ($uri) { |
|
| 208 | - return false !== $uri; |
|
| 209 | - }); |
|
| 193 | + /** |
|
| 194 | + * Scan Locations for an URI that matches the specified criteria. |
|
| 195 | + * |
|
| 196 | + * @since 0.1.0 |
|
| 197 | + * |
|
| 198 | + * @param array $criteria Criteria to match. |
|
| 199 | + * |
|
| 200 | + * @return string|false URI of the requested view, or false if not found. |
|
| 201 | + */ |
|
| 202 | + public function scanLocations(array $criteria) |
|
| 203 | + { |
|
| 204 | + $uris = $this->locations->map(function ($location) use ($criteria) { |
|
| 205 | + /** @var Location $location */ |
|
| 206 | + return $location->getURI($criteria); |
|
| 207 | + })->filter(function ($uri) { |
|
| 208 | + return false !== $uri; |
|
| 209 | + }); |
|
| 210 | 210 | |
| 211 | - return $uris->count() > 0 ? $uris->first() : false; |
|
| 212 | - } |
|
| 211 | + return $uris->count() > 0 ? $uris->first() : false; |
|
| 212 | + } |
|
| 213 | 213 | |
| 214 | - /** |
|
| 215 | - * Get a finder instance. |
|
| 216 | - * |
|
| 217 | - * @since 0.1.1 |
|
| 218 | - * |
|
| 219 | - * @param mixed $property Property to use. |
|
| 220 | - * @param string $key Configuration key to use. |
|
| 221 | - * |
|
| 222 | - * @return Finder The requested finder instance. |
|
| 223 | - */ |
|
| 224 | - protected function getFinder(&$property, $key) |
|
| 225 | - { |
|
| 226 | - if (null === $property) { |
|
| 227 | - $finderClass = $this->config->getKey($key, 'ClassName'); |
|
| 228 | - $property = new $finderClass($this->config->getSubConfig($key)); |
|
| 229 | - } |
|
| 214 | + /** |
|
| 215 | + * Get a finder instance. |
|
| 216 | + * |
|
| 217 | + * @since 0.1.1 |
|
| 218 | + * |
|
| 219 | + * @param mixed $property Property to use. |
|
| 220 | + * @param string $key Configuration key to use. |
|
| 221 | + * |
|
| 222 | + * @return Finder The requested finder instance. |
|
| 223 | + */ |
|
| 224 | + protected function getFinder(&$property, $key) |
|
| 225 | + { |
|
| 226 | + if (null === $property) { |
|
| 227 | + $finderClass = $this->config->getKey($key, 'ClassName'); |
|
| 228 | + $property = new $finderClass($this->config->getSubConfig($key)); |
|
| 229 | + } |
|
| 230 | 230 | |
| 231 | - return $property; |
|
| 232 | - } |
|
| 231 | + return $property; |
|
| 232 | + } |
|
| 233 | 233 | |
| 234 | - /** |
|
| 235 | - * Resolve the view type. |
|
| 236 | - * |
|
| 237 | - * @since 0.1.0 |
|
| 238 | - * |
|
| 239 | - * @param mixed $type Type of view that was requested. |
|
| 240 | - * @param string $uri URI to get a view for. |
|
| 241 | - * @param Engine|null $engine Engine to use for the view. |
|
| 242 | - * |
|
| 243 | - * @return View Resolved View object. |
|
| 244 | - * @throws FailedToInstantiateView If the view type could not be resolved. |
|
| 245 | - */ |
|
| 246 | - protected function resolveType($type, $uri, Engine $engine = null) |
|
| 247 | - { |
|
| 248 | - $configKey = [static::VIEW_FINDER_KEY, 'Views', $type]; |
|
| 234 | + /** |
|
| 235 | + * Resolve the view type. |
|
| 236 | + * |
|
| 237 | + * @since 0.1.0 |
|
| 238 | + * |
|
| 239 | + * @param mixed $type Type of view that was requested. |
|
| 240 | + * @param string $uri URI to get a view for. |
|
| 241 | + * @param Engine|null $engine Engine to use for the view. |
|
| 242 | + * |
|
| 243 | + * @return View Resolved View object. |
|
| 244 | + * @throws FailedToInstantiateView If the view type could not be resolved. |
|
| 245 | + */ |
|
| 246 | + protected function resolveType($type, $uri, Engine $engine = null) |
|
| 247 | + { |
|
| 248 | + $configKey = [static::VIEW_FINDER_KEY, 'Views', $type]; |
|
| 249 | 249 | |
| 250 | - if (is_string($type) && $this->config->hasKey($configKey)) { |
|
| 251 | - $className = $this->config->getKey($configKey); |
|
| 252 | - $type = new $className($uri, $engine); |
|
| 253 | - } |
|
| 250 | + if (is_string($type) && $this->config->hasKey($configKey)) { |
|
| 251 | + $className = $this->config->getKey($configKey); |
|
| 252 | + $type = new $className($uri, $engine); |
|
| 253 | + } |
|
| 254 | 254 | |
| 255 | - if (is_string($type)) { |
|
| 256 | - $type = new $type($uri, $engine); |
|
| 257 | - } |
|
| 255 | + if (is_string($type)) { |
|
| 256 | + $type = new $type($uri, $engine); |
|
| 257 | + } |
|
| 258 | 258 | |
| 259 | - if (is_callable($type)) { |
|
| 260 | - $type = $type($uri, $engine); |
|
| 261 | - } |
|
| 259 | + if (is_callable($type)) { |
|
| 260 | + $type = $type($uri, $engine); |
|
| 261 | + } |
|
| 262 | 262 | |
| 263 | - if ( ! $type instanceof View) { |
|
| 264 | - throw new FailedToInstantiateView( |
|
| 265 | - sprintf( |
|
| 266 | - _('Could not instantiate view "%s".'), |
|
| 267 | - serialize($type) |
|
| 268 | - ) |
|
| 269 | - ); |
|
| 270 | - } |
|
| 263 | + if ( ! $type instanceof View) { |
|
| 264 | + throw new FailedToInstantiateView( |
|
| 265 | + sprintf( |
|
| 266 | + _('Could not instantiate view "%s".'), |
|
| 267 | + serialize($type) |
|
| 268 | + ) |
|
| 269 | + ); |
|
| 270 | + } |
|
| 271 | 271 | |
| 272 | - return $type; |
|
| 273 | - } |
|
| 272 | + return $type; |
|
| 273 | + } |
|
| 274 | 274 | |
| 275 | - /** |
|
| 276 | - * Get the configuration to use in the ViewBuilder. |
|
| 277 | - * |
|
| 278 | - * @since 0.2.0 |
|
| 279 | - * |
|
| 280 | - * @return ConfigInterface Configuration passed in through the constructor. |
|
| 281 | - */ |
|
| 282 | - protected function getConfig($config = null) |
|
| 283 | - { |
|
| 284 | - $defaults = ConfigFactory::create(__DIR__ . '/../../config/defaults.php', $config); |
|
| 285 | - $config = $config |
|
| 286 | - ? ConfigFactory::createFromArray(array_merge_recursive($defaults->getArrayCopy(), $config->getArrayCopy())) |
|
| 287 | - : $defaults; |
|
| 275 | + /** |
|
| 276 | + * Get the configuration to use in the ViewBuilder. |
|
| 277 | + * |
|
| 278 | + * @since 0.2.0 |
|
| 279 | + * |
|
| 280 | + * @return ConfigInterface Configuration passed in through the constructor. |
|
| 281 | + */ |
|
| 282 | + protected function getConfig($config = null) |
|
| 283 | + { |
|
| 284 | + $defaults = ConfigFactory::create(__DIR__ . '/../../config/defaults.php', $config); |
|
| 285 | + $config = $config |
|
| 286 | + ? ConfigFactory::createFromArray(array_merge_recursive($defaults->getArrayCopy(), $config->getArrayCopy())) |
|
| 287 | + : $defaults; |
|
| 288 | 288 | |
| 289 | - return $config->getSubConfig('BrightNucleus\View'); |
|
| 290 | - } |
|
| 289 | + return $config->getSubConfig('BrightNucleus\View'); |
|
| 290 | + } |
|
| 291 | 291 | } |
@@ -201,10 +201,10 @@ discard block |
||
| 201 | 201 | */ |
| 202 | 202 | public function scanLocations(array $criteria) |
| 203 | 203 | { |
| 204 | - $uris = $this->locations->map(function ($location) use ($criteria) { |
|
| 204 | + $uris = $this->locations->map(function($location) use ($criteria) { |
|
| 205 | 205 | /** @var Location $location */ |
| 206 | 206 | return $location->getURI($criteria); |
| 207 | - })->filter(function ($uri) { |
|
| 207 | + })->filter(function($uri) { |
|
| 208 | 208 | return false !== $uri; |
| 209 | 209 | }); |
| 210 | 210 | |
@@ -281,7 +281,7 @@ discard block |
||
| 281 | 281 | */ |
| 282 | 282 | protected function getConfig($config = null) |
| 283 | 283 | { |
| 284 | - $defaults = ConfigFactory::create(__DIR__ . '/../../config/defaults.php', $config); |
|
| 284 | + $defaults = ConfigFactory::create(__DIR__.'/../../config/defaults.php', $config); |
|
| 285 | 285 | $config = $config |
| 286 | 286 | ? ConfigFactory::createFromArray(array_merge_recursive($defaults->getArrayCopy(), $config->getArrayCopy())) |
| 287 | 287 | : $defaults; |
@@ -24,15 +24,15 @@ |
||
| 24 | 24 | interface Engine extends Findable |
| 25 | 25 | { |
| 26 | 26 | |
| 27 | - /** |
|
| 28 | - * Get the rendering callback for a given URI. |
|
| 29 | - * |
|
| 30 | - * @since 0.1.0 |
|
| 31 | - * |
|
| 32 | - * @param string $uri URI to render. |
|
| 33 | - * @param array $context Context in which to render. |
|
| 34 | - * |
|
| 35 | - * @return string Rendered HTML. |
|
| 36 | - */ |
|
| 37 | - public function getRenderCallback($uri, array $context = []); |
|
| 27 | + /** |
|
| 28 | + * Get the rendering callback for a given URI. |
|
| 29 | + * |
|
| 30 | + * @since 0.1.0 |
|
| 31 | + * |
|
| 32 | + * @param string $uri URI to render. |
|
| 33 | + * @param array $context Context in which to render. |
|
| 34 | + * |
|
| 35 | + * @return string Rendered HTML. |
|
| 36 | + */ |
|
| 37 | + public function getRenderCallback($uri, array $context = []); |
|
| 38 | 38 | } |
@@ -24,39 +24,39 @@ |
||
| 24 | 24 | class BaseEngineFinder extends AbstractFinder |
| 25 | 25 | { |
| 26 | 26 | |
| 27 | - /** |
|
| 28 | - * Find a result based on a specific criteria. |
|
| 29 | - * |
|
| 30 | - * @since 0.1.0 |
|
| 31 | - * |
|
| 32 | - * @param array $criteria Criteria to search for. |
|
| 33 | - * |
|
| 34 | - * @return Engine Result of the search. |
|
| 35 | - */ |
|
| 36 | - public function find(array $criteria) |
|
| 37 | - { |
|
| 38 | - $this->initializeFindables(); |
|
| 27 | + /** |
|
| 28 | + * Find a result based on a specific criteria. |
|
| 29 | + * |
|
| 30 | + * @since 0.1.0 |
|
| 31 | + * |
|
| 32 | + * @param array $criteria Criteria to search for. |
|
| 33 | + * |
|
| 34 | + * @return Engine Result of the search. |
|
| 35 | + */ |
|
| 36 | + public function find(array $criteria) |
|
| 37 | + { |
|
| 38 | + $this->initializeFindables(); |
|
| 39 | 39 | |
| 40 | - foreach ($criteria as $entry) { |
|
| 41 | - foreach ($this->findables as $engine) { |
|
| 42 | - if ($engine->canHandle($entry)) { |
|
| 43 | - return $engine; |
|
| 44 | - } |
|
| 45 | - } |
|
| 46 | - } |
|
| 40 | + foreach ($criteria as $entry) { |
|
| 41 | + foreach ($this->findables as $engine) { |
|
| 42 | + if ($engine->canHandle($entry)) { |
|
| 43 | + return $engine; |
|
| 44 | + } |
|
| 45 | + } |
|
| 46 | + } |
|
| 47 | 47 | |
| 48 | - return $this->getNullObject(); |
|
| 49 | - } |
|
| 48 | + return $this->getNullObject(); |
|
| 49 | + } |
|
| 50 | 50 | |
| 51 | - /** |
|
| 52 | - * Get the config key for the Findables definitions. |
|
| 53 | - * |
|
| 54 | - * @since 0.1.0 |
|
| 55 | - * |
|
| 56 | - * @return string Config key use to define the Findables. |
|
| 57 | - */ |
|
| 58 | - protected function getFindablesConfigKey() |
|
| 59 | - { |
|
| 60 | - return 'Engines'; |
|
| 61 | - } |
|
| 51 | + /** |
|
| 52 | + * Get the config key for the Findables definitions. |
|
| 53 | + * |
|
| 54 | + * @since 0.1.0 |
|
| 55 | + * |
|
| 56 | + * @return string Config key use to define the Findables. |
|
| 57 | + */ |
|
| 58 | + protected function getFindablesConfigKey() |
|
| 59 | + { |
|
| 60 | + return 'Engines'; |
|
| 61 | + } |
|
| 62 | 62 | } |
@@ -24,32 +24,32 @@ |
||
| 24 | 24 | class NullEngine implements Engine, NullFindable |
| 25 | 25 | { |
| 26 | 26 | |
| 27 | - /** |
|
| 28 | - * Check whether the Findable can handle an individual criterion. |
|
| 29 | - * |
|
| 30 | - * @since 0.1.0 |
|
| 31 | - * |
|
| 32 | - * @param mixed $criterion Criterion to check. |
|
| 33 | - * |
|
| 34 | - * @return bool Whether the Findable can handle the criterion. |
|
| 35 | - */ |
|
| 36 | - public function canHandle($criterion) |
|
| 37 | - { |
|
| 38 | - return true; |
|
| 39 | - } |
|
| 27 | + /** |
|
| 28 | + * Check whether the Findable can handle an individual criterion. |
|
| 29 | + * |
|
| 30 | + * @since 0.1.0 |
|
| 31 | + * |
|
| 32 | + * @param mixed $criterion Criterion to check. |
|
| 33 | + * |
|
| 34 | + * @return bool Whether the Findable can handle the criterion. |
|
| 35 | + */ |
|
| 36 | + public function canHandle($criterion) |
|
| 37 | + { |
|
| 38 | + return true; |
|
| 39 | + } |
|
| 40 | 40 | |
| 41 | - /** |
|
| 42 | - * Get the rendering callback for a given URI. |
|
| 43 | - * |
|
| 44 | - * @since 0.1.0 |
|
| 45 | - * |
|
| 46 | - * @param string $uri URI to render. |
|
| 47 | - * @param array $context Context in which to render. |
|
| 48 | - * |
|
| 49 | - * @return string Rendered HTML. |
|
| 50 | - */ |
|
| 51 | - public function getRenderCallback($uri, array $context = []) |
|
| 52 | - { |
|
| 53 | - return ''; |
|
| 54 | - } |
|
| 41 | + /** |
|
| 42 | + * Get the rendering callback for a given URI. |
|
| 43 | + * |
|
| 44 | + * @since 0.1.0 |
|
| 45 | + * |
|
| 46 | + * @param string $uri URI to render. |
|
| 47 | + * @param array $context Context in which to render. |
|
| 48 | + * |
|
| 49 | + * @return string Rendered HTML. |
|
| 50 | + */ |
|
| 51 | + public function getRenderCallback($uri, array $context = []) |
|
| 52 | + { |
|
| 53 | + return ''; |
|
| 54 | + } |
|
| 55 | 55 | } |
@@ -28,138 +28,138 @@ |
||
| 28 | 28 | abstract class AbstractView implements View |
| 29 | 29 | { |
| 30 | 30 | |
| 31 | - /** |
|
| 32 | - * URI of the view. |
|
| 33 | - * |
|
| 34 | - * @since 0.1.0 |
|
| 35 | - * |
|
| 36 | - * @var string |
|
| 37 | - */ |
|
| 38 | - protected $uri; |
|
| 39 | - |
|
| 40 | - /** |
|
| 41 | - * Engine to use for the view. |
|
| 42 | - * |
|
| 43 | - * @since 0.1.0 |
|
| 44 | - * |
|
| 45 | - * @var Engine |
|
| 46 | - */ |
|
| 47 | - protected $engine; |
|
| 48 | - |
|
| 49 | - /** |
|
| 50 | - * ViewBuilder instance. |
|
| 51 | - * |
|
| 52 | - * @since 0.2.0 |
|
| 53 | - * |
|
| 54 | - * @var ViewBuilder |
|
| 55 | - */ |
|
| 56 | - protected $builder; |
|
| 57 | - |
|
| 58 | - /** |
|
| 59 | - * Instantiate an AbstractView object. |
|
| 60 | - * |
|
| 61 | - * @since 0.1.0 |
|
| 62 | - * |
|
| 63 | - * @param string $uri URI for the view. |
|
| 64 | - * @param Engine $engine Engine to use for the view. |
|
| 65 | - */ |
|
| 66 | - public function __construct($uri, Engine $engine) |
|
| 67 | - { |
|
| 68 | - $this->uri = $uri; |
|
| 69 | - $this->engine = $engine; |
|
| 70 | - } |
|
| 71 | - |
|
| 72 | - /** |
|
| 73 | - * Render the view. |
|
| 74 | - * |
|
| 75 | - * @since 0.1.0 |
|
| 76 | - * |
|
| 77 | - * @param array $context Optional. The context in which to render the view. |
|
| 78 | - * @param bool $echo Optional. Whether to echo the output immediately. Defaults to false. |
|
| 79 | - * |
|
| 80 | - * @return string|void Rendered HTML or nothing, depending on $echo argument. |
|
| 81 | - */ |
|
| 82 | - public function render(array $context = [], $echo = false) |
|
| 83 | - { |
|
| 84 | - $this->initializeViewBuilder(); |
|
| 85 | - $this->assimilateContext($context); |
|
| 86 | - |
|
| 87 | - $closure = Closure::bind( |
|
| 88 | - $this->engine->getRenderCallback($this->uri, $context), |
|
| 89 | - $this, |
|
| 90 | - static::class |
|
| 91 | - ); |
|
| 92 | - |
|
| 93 | - if ( ! $echo) { |
|
| 94 | - return $closure(); |
|
| 95 | - } |
|
| 96 | - |
|
| 97 | - echo $closure(); |
|
| 98 | - } |
|
| 99 | - |
|
| 100 | - /** |
|
| 101 | - * Render a partial view for a given URI. |
|
| 102 | - * |
|
| 103 | - * @since 0.2.0 |
|
| 104 | - * |
|
| 105 | - * @param string $view View identifier to create a view for. |
|
| 106 | - * @param array $context Optional. The context in which to render the view. |
|
| 107 | - * @param string|null $type Type of view to create. |
|
| 108 | - * |
|
| 109 | - * @return string Rendered HTML content. |
|
| 110 | - */ |
|
| 111 | - public function renderPart($view, array $context = null, $type = null) |
|
| 112 | - { |
|
| 113 | - if (null === $context) { |
|
| 114 | - $context = $this->context; |
|
| 115 | - } |
|
| 116 | - |
|
| 117 | - $this->initializeViewBuilder(); |
|
| 118 | - $viewObject = $this->builder->create($view, $type); |
|
| 119 | - |
|
| 120 | - return $viewObject->render($context); |
|
| 121 | - } |
|
| 122 | - |
|
| 123 | - /** |
|
| 124 | - * Associate a view builder with this view. |
|
| 125 | - * |
|
| 126 | - * @since 0.2.0 |
|
| 127 | - * |
|
| 128 | - * @param ViewBuilder $builder |
|
| 129 | - * |
|
| 130 | - * @return static |
|
| 131 | - */ |
|
| 132 | - public function setBuilder(ViewBuilder $builder) |
|
| 133 | - { |
|
| 134 | - $this->builder = $builder; |
|
| 135 | - |
|
| 136 | - return $this; |
|
| 137 | - } |
|
| 138 | - |
|
| 139 | - /** |
|
| 140 | - * Initialize the view builder associated with the view. |
|
| 141 | - * |
|
| 142 | - * @since 0.2.0 |
|
| 143 | - */ |
|
| 144 | - protected function initializeViewBuilder() |
|
| 145 | - { |
|
| 146 | - if (null === $this->builder) { |
|
| 147 | - $this->builder = Views::getViewBuilder(); |
|
| 148 | - } |
|
| 149 | - } |
|
| 150 | - |
|
| 151 | - /** |
|
| 152 | - * Assimilate the context to make it available as properties. |
|
| 153 | - * |
|
| 154 | - * @since 0.2.0 |
|
| 155 | - * |
|
| 156 | - * @param array $context Context to assimilate. |
|
| 157 | - */ |
|
| 158 | - protected function assimilateContext(array $context = []) |
|
| 159 | - { |
|
| 160 | - $this->context = $context; |
|
| 161 | - foreach ($context as $key => $value) { |
|
| 162 | - $this->$key = $value; |
|
| 163 | - } |
|
| 164 | - } |
|
| 31 | + /** |
|
| 32 | + * URI of the view. |
|
| 33 | + * |
|
| 34 | + * @since 0.1.0 |
|
| 35 | + * |
|
| 36 | + * @var string |
|
| 37 | + */ |
|
| 38 | + protected $uri; |
|
| 39 | + |
|
| 40 | + /** |
|
| 41 | + * Engine to use for the view. |
|
| 42 | + * |
|
| 43 | + * @since 0.1.0 |
|
| 44 | + * |
|
| 45 | + * @var Engine |
|
| 46 | + */ |
|
| 47 | + protected $engine; |
|
| 48 | + |
|
| 49 | + /** |
|
| 50 | + * ViewBuilder instance. |
|
| 51 | + * |
|
| 52 | + * @since 0.2.0 |
|
| 53 | + * |
|
| 54 | + * @var ViewBuilder |
|
| 55 | + */ |
|
| 56 | + protected $builder; |
|
| 57 | + |
|
| 58 | + /** |
|
| 59 | + * Instantiate an AbstractView object. |
|
| 60 | + * |
|
| 61 | + * @since 0.1.0 |
|
| 62 | + * |
|
| 63 | + * @param string $uri URI for the view. |
|
| 64 | + * @param Engine $engine Engine to use for the view. |
|
| 65 | + */ |
|
| 66 | + public function __construct($uri, Engine $engine) |
|
| 67 | + { |
|
| 68 | + $this->uri = $uri; |
|
| 69 | + $this->engine = $engine; |
|
| 70 | + } |
|
| 71 | + |
|
| 72 | + /** |
|
| 73 | + * Render the view. |
|
| 74 | + * |
|
| 75 | + * @since 0.1.0 |
|
| 76 | + * |
|
| 77 | + * @param array $context Optional. The context in which to render the view. |
|
| 78 | + * @param bool $echo Optional. Whether to echo the output immediately. Defaults to false. |
|
| 79 | + * |
|
| 80 | + * @return string|void Rendered HTML or nothing, depending on $echo argument. |
|
| 81 | + */ |
|
| 82 | + public function render(array $context = [], $echo = false) |
|
| 83 | + { |
|
| 84 | + $this->initializeViewBuilder(); |
|
| 85 | + $this->assimilateContext($context); |
|
| 86 | + |
|
| 87 | + $closure = Closure::bind( |
|
| 88 | + $this->engine->getRenderCallback($this->uri, $context), |
|
| 89 | + $this, |
|
| 90 | + static::class |
|
| 91 | + ); |
|
| 92 | + |
|
| 93 | + if ( ! $echo) { |
|
| 94 | + return $closure(); |
|
| 95 | + } |
|
| 96 | + |
|
| 97 | + echo $closure(); |
|
| 98 | + } |
|
| 99 | + |
|
| 100 | + /** |
|
| 101 | + * Render a partial view for a given URI. |
|
| 102 | + * |
|
| 103 | + * @since 0.2.0 |
|
| 104 | + * |
|
| 105 | + * @param string $view View identifier to create a view for. |
|
| 106 | + * @param array $context Optional. The context in which to render the view. |
|
| 107 | + * @param string|null $type Type of view to create. |
|
| 108 | + * |
|
| 109 | + * @return string Rendered HTML content. |
|
| 110 | + */ |
|
| 111 | + public function renderPart($view, array $context = null, $type = null) |
|
| 112 | + { |
|
| 113 | + if (null === $context) { |
|
| 114 | + $context = $this->context; |
|
| 115 | + } |
|
| 116 | + |
|
| 117 | + $this->initializeViewBuilder(); |
|
| 118 | + $viewObject = $this->builder->create($view, $type); |
|
| 119 | + |
|
| 120 | + return $viewObject->render($context); |
|
| 121 | + } |
|
| 122 | + |
|
| 123 | + /** |
|
| 124 | + * Associate a view builder with this view. |
|
| 125 | + * |
|
| 126 | + * @since 0.2.0 |
|
| 127 | + * |
|
| 128 | + * @param ViewBuilder $builder |
|
| 129 | + * |
|
| 130 | + * @return static |
|
| 131 | + */ |
|
| 132 | + public function setBuilder(ViewBuilder $builder) |
|
| 133 | + { |
|
| 134 | + $this->builder = $builder; |
|
| 135 | + |
|
| 136 | + return $this; |
|
| 137 | + } |
|
| 138 | + |
|
| 139 | + /** |
|
| 140 | + * Initialize the view builder associated with the view. |
|
| 141 | + * |
|
| 142 | + * @since 0.2.0 |
|
| 143 | + */ |
|
| 144 | + protected function initializeViewBuilder() |
|
| 145 | + { |
|
| 146 | + if (null === $this->builder) { |
|
| 147 | + $this->builder = Views::getViewBuilder(); |
|
| 148 | + } |
|
| 149 | + } |
|
| 150 | + |
|
| 151 | + /** |
|
| 152 | + * Assimilate the context to make it available as properties. |
|
| 153 | + * |
|
| 154 | + * @since 0.2.0 |
|
| 155 | + * |
|
| 156 | + * @param array $context Context to assimilate. |
|
| 157 | + */ |
|
| 158 | + protected function assimilateContext(array $context = []) |
|
| 159 | + { |
|
| 160 | + $this->context = $context; |
|
| 161 | + foreach ($context as $key => $value) { |
|
| 162 | + $this->$key = $value; |
|
| 163 | + } |
|
| 164 | + } |
|
| 165 | 165 | } |