@@ -24,74 +24,74 @@ |
||
| 24 | 24 | interface View extends Findable |
| 25 | 25 | { |
| 26 | 26 | |
| 27 | - const MERGE = 'merge'; |
|
| 28 | - const REPLACE = 'replace'; |
|
| 29 | - const ADD_ONLY = 'add-only'; |
|
| 30 | - const REPLACE_ONLY = 'replace-only'; |
|
| 31 | - const MERGE_ONLY = 'merge-only'; |
|
| 27 | + const MERGE = 'merge'; |
|
| 28 | + const REPLACE = 'replace'; |
|
| 29 | + const ADD_ONLY = 'add-only'; |
|
| 30 | + const REPLACE_ONLY = 'replace-only'; |
|
| 31 | + const MERGE_ONLY = 'merge-only'; |
|
| 32 | 32 | |
| 33 | - /** |
|
| 34 | - * Check whether the Findable can handle an individual criterion. |
|
| 35 | - * |
|
| 36 | - * @since 0.1.0 |
|
| 37 | - * |
|
| 38 | - * @param mixed $criterion Criterion to check. |
|
| 39 | - * |
|
| 40 | - * @return bool Whether the Findable can handle the criterion. |
|
| 41 | - */ |
|
| 42 | - public function canHandle($criterion): bool; |
|
| 33 | + /** |
|
| 34 | + * Check whether the Findable can handle an individual criterion. |
|
| 35 | + * |
|
| 36 | + * @since 0.1.0 |
|
| 37 | + * |
|
| 38 | + * @param mixed $criterion Criterion to check. |
|
| 39 | + * |
|
| 40 | + * @return bool Whether the Findable can handle the criterion. |
|
| 41 | + */ |
|
| 42 | + public function canHandle($criterion): bool; |
|
| 43 | 43 | |
| 44 | - /** |
|
| 45 | - * Render the view. |
|
| 46 | - * |
|
| 47 | - * @since 0.1.0 |
|
| 48 | - * |
|
| 49 | - * @param array $context Optional. The context in which to render the view. |
|
| 50 | - * |
|
| 51 | - * @return string Rendered HTML. |
|
| 52 | - */ |
|
| 53 | - public function render(array $context = []): string; |
|
| 44 | + /** |
|
| 45 | + * Render the view. |
|
| 46 | + * |
|
| 47 | + * @since 0.1.0 |
|
| 48 | + * |
|
| 49 | + * @param array $context Optional. The context in which to render the view. |
|
| 50 | + * |
|
| 51 | + * @return string Rendered HTML. |
|
| 52 | + */ |
|
| 53 | + public function render(array $context = []): string; |
|
| 54 | 54 | |
| 55 | - /** |
|
| 56 | - * Render a partial view (or section) for a given URI. |
|
| 57 | - * |
|
| 58 | - * @since 0.2.0 |
|
| 59 | - * |
|
| 60 | - * @param string $view View identifier to create a view for. |
|
| 61 | - * @param array $context Optional. The context in which to render the view. |
|
| 62 | - * @param string|null $type Type of view to create. |
|
| 63 | - * |
|
| 64 | - * @return string Rendered HTML content. |
|
| 65 | - */ |
|
| 66 | - public function section(string $view, array $context = [], $type = null): string; |
|
| 55 | + /** |
|
| 56 | + * Render a partial view (or section) for a given URI. |
|
| 57 | + * |
|
| 58 | + * @since 0.2.0 |
|
| 59 | + * |
|
| 60 | + * @param string $view View identifier to create a view for. |
|
| 61 | + * @param array $context Optional. The context in which to render the view. |
|
| 62 | + * @param string|null $type Type of view to create. |
|
| 63 | + * |
|
| 64 | + * @return string Rendered HTML content. |
|
| 65 | + */ |
|
| 66 | + public function section(string $view, array $context = [], $type = null): string; |
|
| 67 | 67 | |
| 68 | - /** |
|
| 69 | - * Get the entire array of contextual data. |
|
| 70 | - * |
|
| 71 | - * @since 0.4.0 |
|
| 72 | - * |
|
| 73 | - * @return array Array of contextual data. |
|
| 74 | - */ |
|
| 75 | - public function getContext(): array; |
|
| 68 | + /** |
|
| 69 | + * Get the entire array of contextual data. |
|
| 70 | + * |
|
| 71 | + * @since 0.4.0 |
|
| 72 | + * |
|
| 73 | + * @return array Array of contextual data. |
|
| 74 | + */ |
|
| 75 | + public function getContext(): array; |
|
| 76 | 76 | |
| 77 | - /** |
|
| 78 | - * Add information to the context. |
|
| 79 | - * |
|
| 80 | - * @param string $key Context key to add. |
|
| 81 | - * @param mixed $value Value to add under the given key. |
|
| 82 | - * @param string $behavior Behavior to use for adapting the context. |
|
| 83 | - * @return View |
|
| 84 | - */ |
|
| 85 | - public function addToContext(string $key, $value, string $behavior): View; |
|
| 77 | + /** |
|
| 78 | + * Add information to the context. |
|
| 79 | + * |
|
| 80 | + * @param string $key Context key to add. |
|
| 81 | + * @param mixed $value Value to add under the given key. |
|
| 82 | + * @param string $behavior Behavior to use for adapting the context. |
|
| 83 | + * @return View |
|
| 84 | + */ |
|
| 85 | + public function addToContext(string $key, $value, string $behavior): View; |
|
| 86 | 86 | |
| 87 | - /** |
|
| 88 | - * Associate a view builder with this view. |
|
| 89 | - * |
|
| 90 | - * @since 0.2.0 |
|
| 91 | - * |
|
| 92 | - * @param ViewBuilder $builder |
|
| 93 | - * |
|
| 94 | - * @return View |
|
| 95 | - */ |
|
| 96 | - public function setBuilder(ViewBuilder $builder): View; |
|
| 87 | + /** |
|
| 88 | + * Associate a view builder with this view. |
|
| 89 | + * |
|
| 90 | + * @since 0.2.0 |
|
| 91 | + * |
|
| 92 | + * @param ViewBuilder $builder |
|
| 93 | + * |
|
| 94 | + * @return View |
|
| 95 | + */ |
|
| 96 | + public function setBuilder(ViewBuilder $builder): View; |
|
| 97 | 97 | } |
@@ -28,184 +28,184 @@ |
||
| 28 | 28 | class FilesystemLocation implements Location |
| 29 | 29 | { |
| 30 | 30 | |
| 31 | - /** |
|
| 32 | - * Path that this location points to. |
|
| 33 | - * |
|
| 34 | - * @since 0.1.0 |
|
| 35 | - * |
|
| 36 | - * @var string |
|
| 37 | - */ |
|
| 38 | - protected $path; |
|
| 39 | - |
|
| 40 | - /** |
|
| 41 | - * Extensions that this location can accept. |
|
| 42 | - * |
|
| 43 | - * @since 0.1.0 |
|
| 44 | - * |
|
| 45 | - * @var Extensions |
|
| 46 | - */ |
|
| 47 | - protected $extensions; |
|
| 48 | - |
|
| 49 | - /** |
|
| 50 | - * Instantiate a FilesystemLocation object. |
|
| 51 | - * |
|
| 52 | - * @since 0.1.0 |
|
| 53 | - * |
|
| 54 | - * @param string $path Path that this location points to. |
|
| 55 | - * @param Extensions|array|string|null $extensions Optional. Extensions that this location can accept. |
|
| 56 | - */ |
|
| 57 | - public function __construct(string $path, $extensions = null) |
|
| 58 | - { |
|
| 59 | - $this->path = $path; |
|
| 60 | - $this->extensions = $this->validateExtensions($extensions); |
|
| 61 | - } |
|
| 62 | - |
|
| 63 | - /** |
|
| 64 | - * Get the first URI that matches the given criteria. |
|
| 65 | - * |
|
| 66 | - * @since 0.1.0 |
|
| 67 | - * |
|
| 68 | - * @param array $criteria Criteria to match. |
|
| 69 | - * |
|
| 70 | - * @return string|false URI that matches the criteria or false if none found. |
|
| 71 | - */ |
|
| 72 | - public function getURI(array $criteria) |
|
| 73 | - { |
|
| 74 | - $uris = $this->getURIs($criteria); |
|
| 75 | - |
|
| 76 | - return $uris->count() > 0 |
|
| 77 | - ? $uris->first() |
|
| 78 | - : false; |
|
| 79 | - } |
|
| 80 | - |
|
| 81 | - /** |
|
| 82 | - * Get all URIs that match the given criteria. |
|
| 83 | - * |
|
| 84 | - * @since 0.1.1 |
|
| 85 | - * |
|
| 86 | - * @param array $criteria Criteria to match. |
|
| 87 | - * |
|
| 88 | - * @return URIs URIs that match the criteria or an empty collection if none found. |
|
| 89 | - */ |
|
| 90 | - public function getURIs(array $criteria): URIs |
|
| 91 | - { |
|
| 92 | - $uris = new URIs(); |
|
| 93 | - |
|
| 94 | - foreach ($this->extensions as $extension) { |
|
| 95 | - $finder = new Finder(); |
|
| 96 | - |
|
| 97 | - try { |
|
| 98 | - $finder->files() |
|
| 99 | - ->name($this->getNamePattern($criteria, $extension)) |
|
| 100 | - ->in($this->getPathPattern($this->getRelativePath($criteria))); |
|
| 101 | - foreach ($finder as $file) { |
|
| 102 | - /** @var SplFileInfo $file */ |
|
| 103 | - $uris->add($file->getPathname()); |
|
| 104 | - } |
|
| 105 | - } catch (Exception $exception) { |
|
| 106 | - // Fail silently; |
|
| 107 | - } |
|
| 108 | - } |
|
| 109 | - |
|
| 110 | - return $uris; |
|
| 111 | - } |
|
| 112 | - |
|
| 113 | - /** |
|
| 114 | - * Get the name pattern to pass to the file finder. |
|
| 115 | - * |
|
| 116 | - * @since 0.1.3 |
|
| 117 | - * |
|
| 118 | - * @param array $criteria Criteria to match. |
|
| 119 | - * @param string $extension Extension to match. |
|
| 120 | - * |
|
| 121 | - * @return string Name pattern to pass to the file finder. |
|
| 122 | - */ |
|
| 123 | - protected function getNamePattern(array $criteria, string $extension): string |
|
| 124 | - { |
|
| 125 | - $names = []; |
|
| 126 | - |
|
| 127 | - $names[] = array_map(function ($criterion) use ($extension) { |
|
| 128 | - $uriExtension = URIHelper::containsExtension($criterion); |
|
| 129 | - if (! empty($extension)) { |
|
| 130 | - $extension = ltrim($extension, '.'); |
|
| 131 | - |
|
| 132 | - if ($uriExtension === $extension) { |
|
| 133 | - $criterion = substr($criterion, 0, -strlen(".{$extension}")); |
|
| 134 | - } |
|
| 135 | - } else { |
|
| 136 | - $extension = URIHelper::containsExtension($criterion); |
|
| 137 | - if (!empty($extension)) { |
|
| 138 | - $criterion = substr($criterion, 0, -strlen(".{$extension}")); |
|
| 139 | - } |
|
| 140 | - } |
|
| 141 | - |
|
| 142 | - $criterion = preg_quote(URIHelper::getFilename($criterion), chr(1)); |
|
| 143 | - |
|
| 144 | - return (empty($extension) || URIHelper::hasExtension($criterion, $extension)) |
|
| 145 | - ? "^{$criterion}(?:\..*?)$" |
|
| 146 | - : "^{$criterion}\.{$extension}$"; |
|
| 147 | - }, $criteria)[0]; |
|
| 148 | - |
|
| 149 | - return chr(1) . implode('|', array_unique($names)) . chr(1); |
|
| 150 | - } |
|
| 151 | - |
|
| 152 | - /** |
|
| 153 | - * Get the path pattern to pass to the file finder. |
|
| 154 | - * |
|
| 155 | - * @since 0.4.7 |
|
| 156 | - * |
|
| 157 | - * @param string $relativePath Relative path that was included in the |
|
| 158 | - * criterion. |
|
| 159 | - * @return string Path pattern to pass to the file finder. |
|
| 160 | - */ |
|
| 161 | - protected function getPathPattern(string $relativePath): string |
|
| 162 | - { |
|
| 163 | - if (empty($relativePath)) { |
|
| 164 | - return $this->path; |
|
| 165 | - } |
|
| 166 | - |
|
| 167 | - return rtrim($this->path, '/') . '/' . ltrim($relativePath, '/'); |
|
| 168 | - } |
|
| 169 | - |
|
| 170 | - /** |
|
| 171 | - * Get the relative path that is included in the criterion. |
|
| 172 | - * |
|
| 173 | - * @since 0.4.7 |
|
| 174 | - * |
|
| 175 | - * @param array $criteria Criteria to extract the relative path from. |
|
| 176 | - * @return string Relative path included in the criterion. |
|
| 177 | - */ |
|
| 178 | - protected function getRelativePath($criteria): string |
|
| 179 | - { |
|
| 180 | - $criterion = current($criteria); |
|
| 181 | - $components = explode('/', $criterion); |
|
| 182 | - |
|
| 183 | - if (count($components) < 2) { |
|
| 184 | - return ''; |
|
| 185 | - } |
|
| 186 | - |
|
| 187 | - return implode('/', array_slice($components, 0, -1)); |
|
| 188 | - } |
|
| 189 | - |
|
| 190 | - /** |
|
| 191 | - * Validate the extensions and return a collection. |
|
| 192 | - * |
|
| 193 | - * @since 0.1.1 |
|
| 194 | - * |
|
| 195 | - * @param Extensions|array|string|null $extensions Extensions to validate. |
|
| 196 | - * |
|
| 197 | - * @return Extensions Validated extensions collection. |
|
| 198 | - */ |
|
| 199 | - protected function validateExtensions($extensions): Extensions |
|
| 200 | - { |
|
| 201 | - if (empty($extensions)) { |
|
| 202 | - $extensions = new Extensions(['']); |
|
| 203 | - } |
|
| 204 | - |
|
| 205 | - if (! $extensions instanceof Extensions) { |
|
| 206 | - $extensions = new Extensions((array)$extensions); |
|
| 207 | - } |
|
| 208 | - |
|
| 209 | - return $extensions; |
|
| 210 | - } |
|
| 31 | + /** |
|
| 32 | + * Path that this location points to. |
|
| 33 | + * |
|
| 34 | + * @since 0.1.0 |
|
| 35 | + * |
|
| 36 | + * @var string |
|
| 37 | + */ |
|
| 38 | + protected $path; |
|
| 39 | + |
|
| 40 | + /** |
|
| 41 | + * Extensions that this location can accept. |
|
| 42 | + * |
|
| 43 | + * @since 0.1.0 |
|
| 44 | + * |
|
| 45 | + * @var Extensions |
|
| 46 | + */ |
|
| 47 | + protected $extensions; |
|
| 48 | + |
|
| 49 | + /** |
|
| 50 | + * Instantiate a FilesystemLocation object. |
|
| 51 | + * |
|
| 52 | + * @since 0.1.0 |
|
| 53 | + * |
|
| 54 | + * @param string $path Path that this location points to. |
|
| 55 | + * @param Extensions|array|string|null $extensions Optional. Extensions that this location can accept. |
|
| 56 | + */ |
|
| 57 | + public function __construct(string $path, $extensions = null) |
|
| 58 | + { |
|
| 59 | + $this->path = $path; |
|
| 60 | + $this->extensions = $this->validateExtensions($extensions); |
|
| 61 | + } |
|
| 62 | + |
|
| 63 | + /** |
|
| 64 | + * Get the first URI that matches the given criteria. |
|
| 65 | + * |
|
| 66 | + * @since 0.1.0 |
|
| 67 | + * |
|
| 68 | + * @param array $criteria Criteria to match. |
|
| 69 | + * |
|
| 70 | + * @return string|false URI that matches the criteria or false if none found. |
|
| 71 | + */ |
|
| 72 | + public function getURI(array $criteria) |
|
| 73 | + { |
|
| 74 | + $uris = $this->getURIs($criteria); |
|
| 75 | + |
|
| 76 | + return $uris->count() > 0 |
|
| 77 | + ? $uris->first() |
|
| 78 | + : false; |
|
| 79 | + } |
|
| 80 | + |
|
| 81 | + /** |
|
| 82 | + * Get all URIs that match the given criteria. |
|
| 83 | + * |
|
| 84 | + * @since 0.1.1 |
|
| 85 | + * |
|
| 86 | + * @param array $criteria Criteria to match. |
|
| 87 | + * |
|
| 88 | + * @return URIs URIs that match the criteria or an empty collection if none found. |
|
| 89 | + */ |
|
| 90 | + public function getURIs(array $criteria): URIs |
|
| 91 | + { |
|
| 92 | + $uris = new URIs(); |
|
| 93 | + |
|
| 94 | + foreach ($this->extensions as $extension) { |
|
| 95 | + $finder = new Finder(); |
|
| 96 | + |
|
| 97 | + try { |
|
| 98 | + $finder->files() |
|
| 99 | + ->name($this->getNamePattern($criteria, $extension)) |
|
| 100 | + ->in($this->getPathPattern($this->getRelativePath($criteria))); |
|
| 101 | + foreach ($finder as $file) { |
|
| 102 | + /** @var SplFileInfo $file */ |
|
| 103 | + $uris->add($file->getPathname()); |
|
| 104 | + } |
|
| 105 | + } catch (Exception $exception) { |
|
| 106 | + // Fail silently; |
|
| 107 | + } |
|
| 108 | + } |
|
| 109 | + |
|
| 110 | + return $uris; |
|
| 111 | + } |
|
| 112 | + |
|
| 113 | + /** |
|
| 114 | + * Get the name pattern to pass to the file finder. |
|
| 115 | + * |
|
| 116 | + * @since 0.1.3 |
|
| 117 | + * |
|
| 118 | + * @param array $criteria Criteria to match. |
|
| 119 | + * @param string $extension Extension to match. |
|
| 120 | + * |
|
| 121 | + * @return string Name pattern to pass to the file finder. |
|
| 122 | + */ |
|
| 123 | + protected function getNamePattern(array $criteria, string $extension): string |
|
| 124 | + { |
|
| 125 | + $names = []; |
|
| 126 | + |
|
| 127 | + $names[] = array_map(function ($criterion) use ($extension) { |
|
| 128 | + $uriExtension = URIHelper::containsExtension($criterion); |
|
| 129 | + if (! empty($extension)) { |
|
| 130 | + $extension = ltrim($extension, '.'); |
|
| 131 | + |
|
| 132 | + if ($uriExtension === $extension) { |
|
| 133 | + $criterion = substr($criterion, 0, -strlen(".{$extension}")); |
|
| 134 | + } |
|
| 135 | + } else { |
|
| 136 | + $extension = URIHelper::containsExtension($criterion); |
|
| 137 | + if (!empty($extension)) { |
|
| 138 | + $criterion = substr($criterion, 0, -strlen(".{$extension}")); |
|
| 139 | + } |
|
| 140 | + } |
|
| 141 | + |
|
| 142 | + $criterion = preg_quote(URIHelper::getFilename($criterion), chr(1)); |
|
| 143 | + |
|
| 144 | + return (empty($extension) || URIHelper::hasExtension($criterion, $extension)) |
|
| 145 | + ? "^{$criterion}(?:\..*?)$" |
|
| 146 | + : "^{$criterion}\.{$extension}$"; |
|
| 147 | + }, $criteria)[0]; |
|
| 148 | + |
|
| 149 | + return chr(1) . implode('|', array_unique($names)) . chr(1); |
|
| 150 | + } |
|
| 151 | + |
|
| 152 | + /** |
|
| 153 | + * Get the path pattern to pass to the file finder. |
|
| 154 | + * |
|
| 155 | + * @since 0.4.7 |
|
| 156 | + * |
|
| 157 | + * @param string $relativePath Relative path that was included in the |
|
| 158 | + * criterion. |
|
| 159 | + * @return string Path pattern to pass to the file finder. |
|
| 160 | + */ |
|
| 161 | + protected function getPathPattern(string $relativePath): string |
|
| 162 | + { |
|
| 163 | + if (empty($relativePath)) { |
|
| 164 | + return $this->path; |
|
| 165 | + } |
|
| 166 | + |
|
| 167 | + return rtrim($this->path, '/') . '/' . ltrim($relativePath, '/'); |
|
| 168 | + } |
|
| 169 | + |
|
| 170 | + /** |
|
| 171 | + * Get the relative path that is included in the criterion. |
|
| 172 | + * |
|
| 173 | + * @since 0.4.7 |
|
| 174 | + * |
|
| 175 | + * @param array $criteria Criteria to extract the relative path from. |
|
| 176 | + * @return string Relative path included in the criterion. |
|
| 177 | + */ |
|
| 178 | + protected function getRelativePath($criteria): string |
|
| 179 | + { |
|
| 180 | + $criterion = current($criteria); |
|
| 181 | + $components = explode('/', $criterion); |
|
| 182 | + |
|
| 183 | + if (count($components) < 2) { |
|
| 184 | + return ''; |
|
| 185 | + } |
|
| 186 | + |
|
| 187 | + return implode('/', array_slice($components, 0, -1)); |
|
| 188 | + } |
|
| 189 | + |
|
| 190 | + /** |
|
| 191 | + * Validate the extensions and return a collection. |
|
| 192 | + * |
|
| 193 | + * @since 0.1.1 |
|
| 194 | + * |
|
| 195 | + * @param Extensions|array|string|null $extensions Extensions to validate. |
|
| 196 | + * |
|
| 197 | + * @return Extensions Validated extensions collection. |
|
| 198 | + */ |
|
| 199 | + protected function validateExtensions($extensions): Extensions |
|
| 200 | + { |
|
| 201 | + if (empty($extensions)) { |
|
| 202 | + $extensions = new Extensions(['']); |
|
| 203 | + } |
|
| 204 | + |
|
| 205 | + if (! $extensions instanceof Extensions) { |
|
| 206 | + $extensions = new Extensions((array)$extensions); |
|
| 207 | + } |
|
| 208 | + |
|
| 209 | + return $extensions; |
|
| 210 | + } |
|
| 211 | 211 | } |
@@ -124,9 +124,9 @@ discard block |
||
| 124 | 124 | { |
| 125 | 125 | $names = []; |
| 126 | 126 | |
| 127 | - $names[] = array_map(function ($criterion) use ($extension) { |
|
| 127 | + $names[] = array_map(function($criterion) use ($extension) { |
|
| 128 | 128 | $uriExtension = URIHelper::containsExtension($criterion); |
| 129 | - if (! empty($extension)) { |
|
| 129 | + if ( ! empty($extension)) { |
|
| 130 | 130 | $extension = ltrim($extension, '.'); |
| 131 | 131 | |
| 132 | 132 | if ($uriExtension === $extension) { |
@@ -134,7 +134,7 @@ discard block |
||
| 134 | 134 | } |
| 135 | 135 | } else { |
| 136 | 136 | $extension = URIHelper::containsExtension($criterion); |
| 137 | - if (!empty($extension)) { |
|
| 137 | + if ( ! empty($extension)) { |
|
| 138 | 138 | $criterion = substr($criterion, 0, -strlen(".{$extension}")); |
| 139 | 139 | } |
| 140 | 140 | } |
@@ -146,7 +146,7 @@ discard block |
||
| 146 | 146 | : "^{$criterion}\.{$extension}$"; |
| 147 | 147 | }, $criteria)[0]; |
| 148 | 148 | |
| 149 | - return chr(1) . implode('|', array_unique($names)) . chr(1); |
|
| 149 | + return chr(1).implode('|', array_unique($names)).chr(1); |
|
| 150 | 150 | } |
| 151 | 151 | |
| 152 | 152 | /** |
@@ -164,7 +164,7 @@ discard block |
||
| 164 | 164 | return $this->path; |
| 165 | 165 | } |
| 166 | 166 | |
| 167 | - return rtrim($this->path, '/') . '/' . ltrim($relativePath, '/'); |
|
| 167 | + return rtrim($this->path, '/').'/'.ltrim($relativePath, '/'); |
|
| 168 | 168 | } |
| 169 | 169 | |
| 170 | 170 | /** |
@@ -202,8 +202,8 @@ discard block |
||
| 202 | 202 | $extensions = new Extensions(['']); |
| 203 | 203 | } |
| 204 | 204 | |
| 205 | - if (! $extensions instanceof Extensions) { |
|
| 206 | - $extensions = new Extensions((array)$extensions); |
|
| 205 | + if ( ! $extensions instanceof Extensions) { |
|
| 206 | + $extensions = new Extensions((array) $extensions); |
|
| 207 | 207 | } |
| 208 | 208 | |
| 209 | 209 | return $extensions; |
@@ -24,39 +24,39 @@ |
||
| 24 | 24 | class Locations extends ArrayCollection |
| 25 | 25 | { |
| 26 | 26 | |
| 27 | - /** |
|
| 28 | - * Adds a location at the end of the collection if it does not already exist. |
|
| 29 | - * |
|
| 30 | - * @param mixed $location The location to add. |
|
| 31 | - * |
|
| 32 | - * @return bool Whether the location was added or not. |
|
| 33 | - */ |
|
| 34 | - public function add($location): bool |
|
| 35 | - { |
|
| 36 | - if ($this->hasLocation($location)) { |
|
| 37 | - return false; |
|
| 38 | - } |
|
| 27 | + /** |
|
| 28 | + * Adds a location at the end of the collection if it does not already exist. |
|
| 29 | + * |
|
| 30 | + * @param mixed $location The location to add. |
|
| 31 | + * |
|
| 32 | + * @return bool Whether the location was added or not. |
|
| 33 | + */ |
|
| 34 | + public function add($location): bool |
|
| 35 | + { |
|
| 36 | + if ($this->hasLocation($location)) { |
|
| 37 | + return false; |
|
| 38 | + } |
|
| 39 | 39 | |
| 40 | - parent::add($location); |
|
| 40 | + parent::add($location); |
|
| 41 | 41 | |
| 42 | - return true; |
|
| 43 | - } |
|
| 42 | + return true; |
|
| 43 | + } |
|
| 44 | 44 | |
| 45 | - /** |
|
| 46 | - * Check whether a given location is already registered. |
|
| 47 | - * |
|
| 48 | - * For two locations to be equal, both their path and their extensions must be the same. |
|
| 49 | - * |
|
| 50 | - * @since 0.1.1 |
|
| 51 | - * |
|
| 52 | - * @param Location $location Location to check the existence of. |
|
| 53 | - * |
|
| 54 | - * @return bool Whether the location is already registered or not. |
|
| 55 | - */ |
|
| 56 | - public function hasLocation(Location $location): bool |
|
| 57 | - { |
|
| 58 | - return $this->exists(function ($key, $element) use ($location) { |
|
| 59 | - return $location == $element; |
|
| 60 | - }); |
|
| 61 | - } |
|
| 45 | + /** |
|
| 46 | + * Check whether a given location is already registered. |
|
| 47 | + * |
|
| 48 | + * For two locations to be equal, both their path and their extensions must be the same. |
|
| 49 | + * |
|
| 50 | + * @since 0.1.1 |
|
| 51 | + * |
|
| 52 | + * @param Location $location Location to check the existence of. |
|
| 53 | + * |
|
| 54 | + * @return bool Whether the location is already registered or not. |
|
| 55 | + */ |
|
| 56 | + public function hasLocation(Location $location): bool |
|
| 57 | + { |
|
| 58 | + return $this->exists(function ($key, $element) use ($location) { |
|
| 59 | + return $location == $element; |
|
| 60 | + }); |
|
| 61 | + } |
|
| 62 | 62 | } |
@@ -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'); |
|
| 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'); |
|
| 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 | |
@@ -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'); |
|
| 330 | + $defaults = ConfigFactory::create(dirname(__DIR__, 2).'/config/defaults.php'); |
|
| 331 | 331 | $config = $config |
| 332 | 332 | ? ConfigFactory::createFromArray(array_merge_recursive($defaults->getArrayCopy(), $config->getArrayCopy())) |
| 333 | 333 | : $defaults; |
@@ -26,87 +26,87 @@ |
||
| 26 | 26 | class NullView implements View, NullFindable |
| 27 | 27 | { |
| 28 | 28 | |
| 29 | - /** |
|
| 30 | - * Check whether the Findable can handle an individual criterion. |
|
| 31 | - * |
|
| 32 | - * @since 0.1.0 |
|
| 33 | - * |
|
| 34 | - * @param mixed $criterion Criterion to check. |
|
| 35 | - * |
|
| 36 | - * @return bool Whether the Findable can handle the criterion. |
|
| 37 | - */ |
|
| 38 | - public function canHandle($criterion): bool |
|
| 39 | - { |
|
| 40 | - return true; |
|
| 41 | - } |
|
| 29 | + /** |
|
| 30 | + * Check whether the Findable can handle an individual criterion. |
|
| 31 | + * |
|
| 32 | + * @since 0.1.0 |
|
| 33 | + * |
|
| 34 | + * @param mixed $criterion Criterion to check. |
|
| 35 | + * |
|
| 36 | + * @return bool Whether the Findable can handle the criterion. |
|
| 37 | + */ |
|
| 38 | + public function canHandle($criterion): bool |
|
| 39 | + { |
|
| 40 | + return true; |
|
| 41 | + } |
|
| 42 | 42 | |
| 43 | - /** |
|
| 44 | - * Render the view. |
|
| 45 | - * |
|
| 46 | - * @since 0.1.0 |
|
| 47 | - * |
|
| 48 | - * @param array $context Optional. The context in which to render the view. |
|
| 49 | - * @param bool $echo Optional. Whether to echo the output immediately. Defaults to false. |
|
| 50 | - * |
|
| 51 | - * @return string Rendered HTML. |
|
| 52 | - */ |
|
| 53 | - public function render(array $context = [], bool $echo = false): string |
|
| 54 | - { |
|
| 55 | - return ''; |
|
| 56 | - } |
|
| 43 | + /** |
|
| 44 | + * Render the view. |
|
| 45 | + * |
|
| 46 | + * @since 0.1.0 |
|
| 47 | + * |
|
| 48 | + * @param array $context Optional. The context in which to render the view. |
|
| 49 | + * @param bool $echo Optional. Whether to echo the output immediately. Defaults to false. |
|
| 50 | + * |
|
| 51 | + * @return string Rendered HTML. |
|
| 52 | + */ |
|
| 53 | + public function render(array $context = [], bool $echo = false): string |
|
| 54 | + { |
|
| 55 | + return ''; |
|
| 56 | + } |
|
| 57 | 57 | |
| 58 | - /** |
|
| 59 | - * Render a partial view (or section) for a given URI. |
|
| 60 | - * |
|
| 61 | - * @since 0.2.0 |
|
| 62 | - * |
|
| 63 | - * @param string $view View identifier to create a view for. |
|
| 64 | - * @param array $context Optional. The context in which to render the view. |
|
| 65 | - * @param string|null $type Type of view to create. |
|
| 66 | - * |
|
| 67 | - * @return string Rendered HTML content. |
|
| 68 | - */ |
|
| 69 | - public function section(string $view, array $context = [], $type = null): string |
|
| 70 | - { |
|
| 71 | - return ''; |
|
| 72 | - } |
|
| 58 | + /** |
|
| 59 | + * Render a partial view (or section) for a given URI. |
|
| 60 | + * |
|
| 61 | + * @since 0.2.0 |
|
| 62 | + * |
|
| 63 | + * @param string $view View identifier to create a view for. |
|
| 64 | + * @param array $context Optional. The context in which to render the view. |
|
| 65 | + * @param string|null $type Type of view to create. |
|
| 66 | + * |
|
| 67 | + * @return string Rendered HTML content. |
|
| 68 | + */ |
|
| 69 | + public function section(string $view, array $context = [], $type = null): string |
|
| 70 | + { |
|
| 71 | + return ''; |
|
| 72 | + } |
|
| 73 | 73 | |
| 74 | - /** |
|
| 75 | - * Get the entire array of contextual data. |
|
| 76 | - * |
|
| 77 | - * @since 0.4.0 |
|
| 78 | - * |
|
| 79 | - * @return array Array of contextual data. |
|
| 80 | - */ |
|
| 81 | - public function getContext(): array |
|
| 82 | - { |
|
| 83 | - return []; |
|
| 84 | - } |
|
| 74 | + /** |
|
| 75 | + * Get the entire array of contextual data. |
|
| 76 | + * |
|
| 77 | + * @since 0.4.0 |
|
| 78 | + * |
|
| 79 | + * @return array Array of contextual data. |
|
| 80 | + */ |
|
| 81 | + public function getContext(): array |
|
| 82 | + { |
|
| 83 | + return []; |
|
| 84 | + } |
|
| 85 | 85 | |
| 86 | - /** |
|
| 87 | - * Add information to the context. |
|
| 88 | - * |
|
| 89 | - * @param string $key Context key to add. |
|
| 90 | - * @param mixed $value Value to add under the given key. |
|
| 91 | - * @param string $behavior Behavior to use for adapting the context. |
|
| 92 | - * @return View |
|
| 93 | - */ |
|
| 94 | - public function addToContext(string $key, $value, string $behavior): View |
|
| 95 | - { |
|
| 96 | - return $this; |
|
| 97 | - } |
|
| 86 | + /** |
|
| 87 | + * Add information to the context. |
|
| 88 | + * |
|
| 89 | + * @param string $key Context key to add. |
|
| 90 | + * @param mixed $value Value to add under the given key. |
|
| 91 | + * @param string $behavior Behavior to use for adapting the context. |
|
| 92 | + * @return View |
|
| 93 | + */ |
|
| 94 | + public function addToContext(string $key, $value, string $behavior): View |
|
| 95 | + { |
|
| 96 | + return $this; |
|
| 97 | + } |
|
| 98 | 98 | |
| 99 | - /** |
|
| 100 | - * Associate a view builder with this view. |
|
| 101 | - * |
|
| 102 | - * @since 0.2.0 |
|
| 103 | - * |
|
| 104 | - * @param ViewBuilder $builder |
|
| 105 | - * |
|
| 106 | - * @return View |
|
| 107 | - */ |
|
| 108 | - public function setBuilder(ViewBuilder $builder): View |
|
| 109 | - { |
|
| 110 | - return $this; |
|
| 111 | - } |
|
| 99 | + /** |
|
| 100 | + * Associate a view builder with this view. |
|
| 101 | + * |
|
| 102 | + * @since 0.2.0 |
|
| 103 | + * |
|
| 104 | + * @param ViewBuilder $builder |
|
| 105 | + * |
|
| 106 | + * @return View |
|
| 107 | + */ |
|
| 108 | + public function setBuilder(ViewBuilder $builder): View |
|
| 109 | + { |
|
| 110 | + return $this; |
|
| 111 | + } |
|
| 112 | 112 | } |
@@ -31,234 +31,234 @@ |
||
| 31 | 31 | abstract class AbstractView implements View |
| 32 | 32 | { |
| 33 | 33 | |
| 34 | - /** |
|
| 35 | - * URI of the view. |
|
| 36 | - * |
|
| 37 | - * The underscores are used to prevent accidental use of these properties from within the rendering closure. |
|
| 38 | - * |
|
| 39 | - * @since 0.1.0 |
|
| 40 | - * |
|
| 41 | - * @var string |
|
| 42 | - */ |
|
| 43 | - protected $_uri_; |
|
| 34 | + /** |
|
| 35 | + * URI of the view. |
|
| 36 | + * |
|
| 37 | + * The underscores are used to prevent accidental use of these properties from within the rendering closure. |
|
| 38 | + * |
|
| 39 | + * @since 0.1.0 |
|
| 40 | + * |
|
| 41 | + * @var string |
|
| 42 | + */ |
|
| 43 | + protected $_uri_; |
|
| 44 | 44 | |
| 45 | - /** |
|
| 46 | - * Engine to use for the view. |
|
| 47 | - * |
|
| 48 | - * The underscores are used to prevent accidental use of these properties from within the rendering closure. |
|
| 49 | - * |
|
| 50 | - * @since 0.1.0 |
|
| 51 | - * |
|
| 52 | - * @var Engine |
|
| 53 | - */ |
|
| 54 | - protected $_engine_; |
|
| 45 | + /** |
|
| 46 | + * Engine to use for the view. |
|
| 47 | + * |
|
| 48 | + * The underscores are used to prevent accidental use of these properties from within the rendering closure. |
|
| 49 | + * |
|
| 50 | + * @since 0.1.0 |
|
| 51 | + * |
|
| 52 | + * @var Engine |
|
| 53 | + */ |
|
| 54 | + protected $_engine_; |
|
| 55 | 55 | |
| 56 | - /** |
|
| 57 | - * ViewBuilder instance. |
|
| 58 | - * |
|
| 59 | - * The underscores are used to prevent accidental use of these properties from within the rendering closure. |
|
| 60 | - * |
|
| 61 | - * @since 0.2.0 |
|
| 62 | - * |
|
| 63 | - * @var ViewBuilder |
|
| 64 | - */ |
|
| 65 | - protected $_builder_; |
|
| 56 | + /** |
|
| 57 | + * ViewBuilder instance. |
|
| 58 | + * |
|
| 59 | + * The underscores are used to prevent accidental use of these properties from within the rendering closure. |
|
| 60 | + * |
|
| 61 | + * @since 0.2.0 |
|
| 62 | + * |
|
| 63 | + * @var ViewBuilder |
|
| 64 | + */ |
|
| 65 | + protected $_builder_; |
|
| 66 | 66 | |
| 67 | - /** |
|
| 68 | - * The context with which the view will be rendered. |
|
| 69 | - * |
|
| 70 | - * The underscores are used to prevent accidental use of these properties from within the rendering closure. |
|
| 71 | - * |
|
| 72 | - * @since 0.4.0 |
|
| 73 | - * |
|
| 74 | - * @var array |
|
| 75 | - */ |
|
| 76 | - protected $_context_; |
|
| 67 | + /** |
|
| 68 | + * The context with which the view will be rendered. |
|
| 69 | + * |
|
| 70 | + * The underscores are used to prevent accidental use of these properties from within the rendering closure. |
|
| 71 | + * |
|
| 72 | + * @since 0.4.0 |
|
| 73 | + * |
|
| 74 | + * @var array |
|
| 75 | + */ |
|
| 76 | + protected $_context_; |
|
| 77 | 77 | |
| 78 | - /** |
|
| 79 | - * Instantiate an AbstractView object. |
|
| 80 | - * |
|
| 81 | - * @since 0.1.0 |
|
| 82 | - * |
|
| 83 | - * @param string $uri URI for the view. |
|
| 84 | - * @param Engine $engine Engine to use for the view. |
|
| 85 | - * @param ViewBuilder $viewBuilder View builder instance to use. |
|
| 86 | - * @param array $context Initial context to use. |
|
| 87 | - */ |
|
| 88 | - public function __construct(string $uri, Engine $engine, ViewBuilder $viewBuilder = null, array $context = []) |
|
| 89 | - { |
|
| 90 | - $this->_uri_ = $uri; |
|
| 91 | - $this->_engine_ = $engine; |
|
| 92 | - $this->_builder_ = $viewBuilder ?? Views::getViewBuilder(); |
|
| 93 | - $this->_context_ = $context; |
|
| 94 | - } |
|
| 78 | + /** |
|
| 79 | + * Instantiate an AbstractView object. |
|
| 80 | + * |
|
| 81 | + * @since 0.1.0 |
|
| 82 | + * |
|
| 83 | + * @param string $uri URI for the view. |
|
| 84 | + * @param Engine $engine Engine to use for the view. |
|
| 85 | + * @param ViewBuilder $viewBuilder View builder instance to use. |
|
| 86 | + * @param array $context Initial context to use. |
|
| 87 | + */ |
|
| 88 | + public function __construct(string $uri, Engine $engine, ViewBuilder $viewBuilder = null, array $context = []) |
|
| 89 | + { |
|
| 90 | + $this->_uri_ = $uri; |
|
| 91 | + $this->_engine_ = $engine; |
|
| 92 | + $this->_builder_ = $viewBuilder ?? Views::getViewBuilder(); |
|
| 93 | + $this->_context_ = $context; |
|
| 94 | + } |
|
| 95 | 95 | |
| 96 | - /** |
|
| 97 | - * Render the view. |
|
| 98 | - * |
|
| 99 | - * @since 0.1.0 |
|
| 100 | - * |
|
| 101 | - * @param array $context Optional. The context in which to render the view. |
|
| 102 | - * @param bool $echo Optional. Whether to echo the output immediately. Defaults to false. |
|
| 103 | - * |
|
| 104 | - * @return string Rendered HTML. |
|
| 105 | - * @throws FailedToProcessConfigException If the Config could not be processed. |
|
| 106 | - */ |
|
| 107 | - public function render(array $context = [], bool $echo = false): string |
|
| 108 | - { |
|
| 109 | - $this->assimilateContext($context); |
|
| 96 | + /** |
|
| 97 | + * Render the view. |
|
| 98 | + * |
|
| 99 | + * @since 0.1.0 |
|
| 100 | + * |
|
| 101 | + * @param array $context Optional. The context in which to render the view. |
|
| 102 | + * @param bool $echo Optional. Whether to echo the output immediately. Defaults to false. |
|
| 103 | + * |
|
| 104 | + * @return string Rendered HTML. |
|
| 105 | + * @throws FailedToProcessConfigException If the Config could not be processed. |
|
| 106 | + */ |
|
| 107 | + public function render(array $context = [], bool $echo = false): string |
|
| 108 | + { |
|
| 109 | + $this->assimilateContext($context); |
|
| 110 | 110 | |
| 111 | - $closure = Closure::bind( |
|
| 112 | - $this->_engine_->getRenderCallback($this->_uri_, $context), |
|
| 113 | - $this, |
|
| 114 | - static::class |
|
| 115 | - ); |
|
| 111 | + $closure = Closure::bind( |
|
| 112 | + $this->_engine_->getRenderCallback($this->_uri_, $context), |
|
| 113 | + $this, |
|
| 114 | + static::class |
|
| 115 | + ); |
|
| 116 | 116 | |
| 117 | - $output = $closure(); |
|
| 117 | + $output = $closure(); |
|
| 118 | 118 | |
| 119 | - if ($echo) { |
|
| 120 | - echo $output; |
|
| 121 | - } |
|
| 119 | + if ($echo) { |
|
| 120 | + echo $output; |
|
| 121 | + } |
|
| 122 | 122 | |
| 123 | - return $output; |
|
| 124 | - } |
|
| 123 | + return $output; |
|
| 124 | + } |
|
| 125 | 125 | |
| 126 | - /** |
|
| 127 | - * Render a partial view (or section) for a given URI. |
|
| 128 | - * |
|
| 129 | - * @since 0.2.0 |
|
| 130 | - * |
|
| 131 | - * @param string $view View identifier to create a view for. |
|
| 132 | - * @param array $context Optional. The context in which to render the view. |
|
| 133 | - * @param string|null $type Type of view to create. |
|
| 134 | - * |
|
| 135 | - * @return string Rendered HTML content. |
|
| 136 | - * @throws FailedToProcessConfigException If the Config could not be processed. |
|
| 137 | - * @throws FailedToInstantiateView If the View could not be instantiated. |
|
| 138 | - */ |
|
| 139 | - public function section(string $view, array $context = null, $type = null): string |
|
| 140 | - { |
|
| 141 | - $context = (null === $context) |
|
| 142 | - ? $this->_context_ |
|
| 143 | - : array_merge($this->_context_, $context); |
|
| 126 | + /** |
|
| 127 | + * Render a partial view (or section) for a given URI. |
|
| 128 | + * |
|
| 129 | + * @since 0.2.0 |
|
| 130 | + * |
|
| 131 | + * @param string $view View identifier to create a view for. |
|
| 132 | + * @param array $context Optional. The context in which to render the view. |
|
| 133 | + * @param string|null $type Type of view to create. |
|
| 134 | + * |
|
| 135 | + * @return string Rendered HTML content. |
|
| 136 | + * @throws FailedToProcessConfigException If the Config could not be processed. |
|
| 137 | + * @throws FailedToInstantiateView If the View could not be instantiated. |
|
| 138 | + */ |
|
| 139 | + public function section(string $view, array $context = null, $type = null): string |
|
| 140 | + { |
|
| 141 | + $context = (null === $context) |
|
| 142 | + ? $this->_context_ |
|
| 143 | + : array_merge($this->_context_, $context); |
|
| 144 | 144 | |
| 145 | - $viewObject = $this->_builder_->create($view, $type); |
|
| 145 | + $viewObject = $this->_builder_->create($view, $type); |
|
| 146 | 146 | |
| 147 | - return $viewObject->render($context); |
|
| 148 | - } |
|
| 147 | + return $viewObject->render($context); |
|
| 148 | + } |
|
| 149 | 149 | |
| 150 | - /** |
|
| 151 | - * Get the entire array of contextual data. |
|
| 152 | - * |
|
| 153 | - * @since 0.4.0 |
|
| 154 | - * |
|
| 155 | - * @return array Array of contextual data. |
|
| 156 | - */ |
|
| 157 | - public function getContext(): array |
|
| 158 | - { |
|
| 159 | - return $this->_context_; |
|
| 160 | - } |
|
| 150 | + /** |
|
| 151 | + * Get the entire array of contextual data. |
|
| 152 | + * |
|
| 153 | + * @since 0.4.0 |
|
| 154 | + * |
|
| 155 | + * @return array Array of contextual data. |
|
| 156 | + */ |
|
| 157 | + public function getContext(): array |
|
| 158 | + { |
|
| 159 | + return $this->_context_; |
|
| 160 | + } |
|
| 161 | 161 | |
| 162 | - /** |
|
| 163 | - * Add information to the context. |
|
| 164 | - * |
|
| 165 | - * @param string $key Context key to add. |
|
| 166 | - * @param mixed $value Value to add under the given key. |
|
| 167 | - * @param string $behavior Behavior to use for adapting the context. |
|
| 168 | - * @return View |
|
| 169 | - */ |
|
| 170 | - public function addToContext(string $key, $value, string $behavior): View |
|
| 171 | - { |
|
| 172 | - switch ($behavior) { |
|
| 173 | - case View::REPLACE: |
|
| 174 | - $this->_context_[$key] = $value; |
|
| 175 | - return $this; |
|
| 176 | - case View::MERGE: |
|
| 177 | - if (array_key_exists($key, $this->_context_)) { |
|
| 178 | - $this->_context_ = array_merge_recursive($this->_context_, [$key => $value]); |
|
| 179 | - return $this; |
|
| 180 | - } |
|
| 181 | - $this->_context_[$key] = $value; |
|
| 182 | - return $this; |
|
| 183 | - case View::ADD_ONLY: |
|
| 184 | - if (array_key_exists($key, $this->_context_)) { |
|
| 185 | - return $this; |
|
| 186 | - } |
|
| 187 | - $this->_context_[$key] = $value; |
|
| 188 | - return $this; |
|
| 189 | - case View::REPLACE_ONLY: |
|
| 190 | - if (! array_key_exists($key, $this->_context_)) { |
|
| 191 | - return $this; |
|
| 192 | - } |
|
| 193 | - $this->_context_[$key] = $value; |
|
| 194 | - return $this; |
|
| 195 | - case View::MERGE_ONLY: |
|
| 196 | - if (! array_key_exists($key, $this->_context_)) { |
|
| 197 | - return $this; |
|
| 198 | - } |
|
| 199 | - $this->_context_ = array_merge_recursive($this->_context_, [$key => $value]); |
|
| 200 | - return $this; |
|
| 201 | - default: |
|
| 202 | - throw new InvalidContextAddingBehavior( |
|
| 203 | - sprintf( |
|
| 204 | - _('Invalid behavior "%s" for adding to the context of view "%s".'), |
|
| 205 | - $key, |
|
| 206 | - $this->_uri_ |
|
| 207 | - ) |
|
| 208 | - ); |
|
| 209 | - } |
|
| 210 | - } |
|
| 162 | + /** |
|
| 163 | + * Add information to the context. |
|
| 164 | + * |
|
| 165 | + * @param string $key Context key to add. |
|
| 166 | + * @param mixed $value Value to add under the given key. |
|
| 167 | + * @param string $behavior Behavior to use for adapting the context. |
|
| 168 | + * @return View |
|
| 169 | + */ |
|
| 170 | + public function addToContext(string $key, $value, string $behavior): View |
|
| 171 | + { |
|
| 172 | + switch ($behavior) { |
|
| 173 | + case View::REPLACE: |
|
| 174 | + $this->_context_[$key] = $value; |
|
| 175 | + return $this; |
|
| 176 | + case View::MERGE: |
|
| 177 | + if (array_key_exists($key, $this->_context_)) { |
|
| 178 | + $this->_context_ = array_merge_recursive($this->_context_, [$key => $value]); |
|
| 179 | + return $this; |
|
| 180 | + } |
|
| 181 | + $this->_context_[$key] = $value; |
|
| 182 | + return $this; |
|
| 183 | + case View::ADD_ONLY: |
|
| 184 | + if (array_key_exists($key, $this->_context_)) { |
|
| 185 | + return $this; |
|
| 186 | + } |
|
| 187 | + $this->_context_[$key] = $value; |
|
| 188 | + return $this; |
|
| 189 | + case View::REPLACE_ONLY: |
|
| 190 | + if (! array_key_exists($key, $this->_context_)) { |
|
| 191 | + return $this; |
|
| 192 | + } |
|
| 193 | + $this->_context_[$key] = $value; |
|
| 194 | + return $this; |
|
| 195 | + case View::MERGE_ONLY: |
|
| 196 | + if (! array_key_exists($key, $this->_context_)) { |
|
| 197 | + return $this; |
|
| 198 | + } |
|
| 199 | + $this->_context_ = array_merge_recursive($this->_context_, [$key => $value]); |
|
| 200 | + return $this; |
|
| 201 | + default: |
|
| 202 | + throw new InvalidContextAddingBehavior( |
|
| 203 | + sprintf( |
|
| 204 | + _('Invalid behavior "%s" for adding to the context of view "%s".'), |
|
| 205 | + $key, |
|
| 206 | + $this->_uri_ |
|
| 207 | + ) |
|
| 208 | + ); |
|
| 209 | + } |
|
| 210 | + } |
|
| 211 | 211 | |
| 212 | - /** |
|
| 213 | - * Associate a view builder with this view. |
|
| 214 | - * |
|
| 215 | - * @since 0.2.0 |
|
| 216 | - * |
|
| 217 | - * @param ViewBuilder $builder |
|
| 218 | - * |
|
| 219 | - * @return View |
|
| 220 | - */ |
|
| 221 | - public function setBuilder(ViewBuilder $builder): View |
|
| 222 | - { |
|
| 223 | - $this->_builder_ = $builder; |
|
| 212 | + /** |
|
| 213 | + * Associate a view builder with this view. |
|
| 214 | + * |
|
| 215 | + * @since 0.2.0 |
|
| 216 | + * |
|
| 217 | + * @param ViewBuilder $builder |
|
| 218 | + * |
|
| 219 | + * @return View |
|
| 220 | + */ |
|
| 221 | + public function setBuilder(ViewBuilder $builder): View |
|
| 222 | + { |
|
| 223 | + $this->_builder_ = $builder; |
|
| 224 | 224 | |
| 225 | - return $this; |
|
| 226 | - } |
|
| 225 | + return $this; |
|
| 226 | + } |
|
| 227 | 227 | |
| 228 | - /** |
|
| 229 | - * Assimilate the context to make it available as properties. |
|
| 230 | - * |
|
| 231 | - * @since 0.2.0 |
|
| 232 | - * |
|
| 233 | - * @param array $context Context to assimilate. |
|
| 234 | - */ |
|
| 235 | - protected function assimilateContext(array $context = []) |
|
| 236 | - { |
|
| 237 | - $this->_context_ = $context; |
|
| 238 | - foreach ($context as $key => $value) { |
|
| 239 | - $this->$key = $value; |
|
| 240 | - } |
|
| 241 | - } |
|
| 228 | + /** |
|
| 229 | + * Assimilate the context to make it available as properties. |
|
| 230 | + * |
|
| 231 | + * @since 0.2.0 |
|
| 232 | + * |
|
| 233 | + * @param array $context Context to assimilate. |
|
| 234 | + */ |
|
| 235 | + protected function assimilateContext(array $context = []) |
|
| 236 | + { |
|
| 237 | + $this->_context_ = $context; |
|
| 238 | + foreach ($context as $key => $value) { |
|
| 239 | + $this->$key = $value; |
|
| 240 | + } |
|
| 241 | + } |
|
| 242 | 242 | |
| 243 | - /** |
|
| 244 | - * Turn invokable objects as properties into methods of the view. |
|
| 245 | - * |
|
| 246 | - * @param string $method Method that was called on the view. |
|
| 247 | - * @param array $arguments Array of arguments that were used. |
|
| 248 | - * @return mixed Return value of the invokable object. |
|
| 249 | - */ |
|
| 250 | - public function __call($method, $arguments) |
|
| 251 | - { |
|
| 252 | - if (! property_exists($this, $method) |
|
| 253 | - || ! is_callable($this->$method)) { |
|
| 254 | - trigger_error( |
|
| 255 | - "Call to undefined method {$method} on a view.", |
|
| 256 | - E_USER_ERROR |
|
| 257 | - ); |
|
| 258 | - } |
|
| 243 | + /** |
|
| 244 | + * Turn invokable objects as properties into methods of the view. |
|
| 245 | + * |
|
| 246 | + * @param string $method Method that was called on the view. |
|
| 247 | + * @param array $arguments Array of arguments that were used. |
|
| 248 | + * @return mixed Return value of the invokable object. |
|
| 249 | + */ |
|
| 250 | + public function __call($method, $arguments) |
|
| 251 | + { |
|
| 252 | + if (! property_exists($this, $method) |
|
| 253 | + || ! is_callable($this->$method)) { |
|
| 254 | + trigger_error( |
|
| 255 | + "Call to undefined method {$method} on a view.", |
|
| 256 | + E_USER_ERROR |
|
| 257 | + ); |
|
| 258 | + } |
|
| 259 | 259 | |
| 260 | - $callable = $this->$method; |
|
| 260 | + $callable = $this->$method; |
|
| 261 | 261 | |
| 262 | - return $callable(...$arguments); |
|
| 263 | - } |
|
| 262 | + return $callable(...$arguments); |
|
| 263 | + } |
|
| 264 | 264 | } |
@@ -187,13 +187,13 @@ discard block |
||
| 187 | 187 | $this->_context_[$key] = $value; |
| 188 | 188 | return $this; |
| 189 | 189 | case View::REPLACE_ONLY: |
| 190 | - if (! array_key_exists($key, $this->_context_)) { |
|
| 190 | + if ( ! array_key_exists($key, $this->_context_)) { |
|
| 191 | 191 | return $this; |
| 192 | 192 | } |
| 193 | 193 | $this->_context_[$key] = $value; |
| 194 | 194 | return $this; |
| 195 | 195 | case View::MERGE_ONLY: |
| 196 | - if (! array_key_exists($key, $this->_context_)) { |
|
| 196 | + if ( ! array_key_exists($key, $this->_context_)) { |
|
| 197 | 197 | return $this; |
| 198 | 198 | } |
| 199 | 199 | $this->_context_ = array_merge_recursive($this->_context_, [$key => $value]); |
@@ -249,7 +249,7 @@ discard block |
||
| 249 | 249 | */ |
| 250 | 250 | public function __call($method, $arguments) |
| 251 | 251 | { |
| 252 | - if (! property_exists($this, $method) |
|
| 252 | + if ( ! property_exists($this, $method) |
|
| 253 | 253 | || ! is_callable($this->$method)) { |
| 254 | 254 | trigger_error( |
| 255 | 255 | "Call to undefined method {$method} on a view.", |
@@ -24,34 +24,34 @@ |
||
| 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): bool |
|
| 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): bool |
|
| 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 callable Rendering callback. |
|
| 50 | - */ |
|
| 51 | - public function getRenderCallback(string $uri, array $context = []): callable |
|
| 52 | - { |
|
| 53 | - return function () { |
|
| 54 | - return ''; |
|
| 55 | - }; |
|
| 56 | - } |
|
| 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 callable Rendering callback. |
|
| 50 | + */ |
|
| 51 | + public function getRenderCallback(string $uri, array $context = []): callable |
|
| 52 | + { |
|
| 53 | + return function () { |
|
| 54 | + return ''; |
|
| 55 | + }; |
|
| 56 | + } |
|
| 57 | 57 | } |
@@ -50,7 +50,7 @@ |
||
| 50 | 50 | */ |
| 51 | 51 | public function getRenderCallback(string $uri, array $context = []): callable |
| 52 | 52 | { |
| 53 | - return function () { |
|
| 53 | + return function() { |
|
| 54 | 54 | return ''; |
| 55 | 55 | }; |
| 56 | 56 | } |