From 10c4ad53e54cef37858723d3e88174b5eeb30fbf Mon Sep 17 00:00:00 2001 From: Teppo Koivula Date: Sat, 11 Apr 2020 21:46:57 +0300 Subject: [PATCH 1/2] Throw an exception if missing partial is requested --- CHANGELOG.md | 3 +++ Wireframe.info.json | 2 +- Wireframe.module.php | 9 ++++---- lib/Partials.php | 55 +++++++++++++++++++++++++++++++++++++++++++- 4 files changed, 63 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 879fe5c..84997ad 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Changed +- Partials class now detects if a missing partial is being requested and throws an exception. + ## [0.10.2] - 2020-03-13 ### Fixed diff --git a/Wireframe.info.json b/Wireframe.info.json index b00d80f..482e518 100644 --- a/Wireframe.info.json +++ b/Wireframe.info.json @@ -1,7 +1,7 @@ { "title": "Wireframe", "summary": "Wireframe is an output framework for ProcessWire CMS/CMF.", - "version": "0.10.2", + "version": "0.11.0", "author": "Teppo Koivula", "href": "https://wireframe-framework.com", "requires": [ diff --git a/Wireframe.module.php b/Wireframe.module.php index 1eefcb3..ce50eea 100644 --- a/Wireframe.module.php +++ b/Wireframe.module.php @@ -14,7 +14,7 @@ * @method static string|Page|NullPage page($source, $args = []) Static getter (factory) method for Pages. * @method static string|null partial(string $partial_name, array $args = []) Static getter (factory) method for Partials. * - * @version 0.10.2 + * @version 0.11.0 * @author Teppo Koivula * @license Mozilla Public License v2.0 https://mozilla.org/MPL/2.0/ */ @@ -510,7 +510,7 @@ public function ___initView(): \Wireframe\View { $view->setExt($ext); $view->setPage($this->page); $view->setData($data); - $view->setPartials($this->findPartials($paths->partials . "*")); + $view->setPartials($this->findPartials($paths->partials)); $view->setPlaceholders(new \Wireframe\ViewPlaceholders($view)); $view->setRenderer($this->renderer); $this->view = $view; @@ -953,11 +953,11 @@ protected function findPartials(string $path, string $ext = null): \Wireframe\Pa $cache_key = 'files:' . $path . ':' . $ext; $files = $this->cache[$cache_key] ?? []; if (empty($files)) { - foreach (\glob($path) as $file) { + foreach (\glob($path . '*') as $file) { $name = \basename($file); if (\strpos($name, ".") === 0) continue; if (\is_dir($file)) { - $files[$name] = $this->findPartials("{$file}/*", $ext); + $files[$name] = $this->findPartials("{$file}/", $ext); } else { $file_data = []; $ext_pos = \strrpos($name, '.'); @@ -979,6 +979,7 @@ protected function findPartials(string $path, string $ext = null): \Wireframe\Pa if (\is_array($files)) { $temp_files = $files; $files = new \Wireframe\Partials(); + $files->setPath($path); foreach ($temp_files as $key => $file) { $files->{$key} = \is_object($file) ? $file : new \Wireframe\Partial($file); } diff --git a/lib/Partials.php b/lib/Partials.php index d531e58..e0872cf 100644 --- a/lib/Partials.php +++ b/lib/Partials.php @@ -7,12 +7,19 @@ * * This class holds Partial objects and provides method access to rendering them with optional arguments. * - * @version 0.1.0 + * @version 0.2.0 * @author Teppo Koivula * @license Mozilla Public License v2.0 https://mozilla.org/MPL/2.0/ */ class Partials extends \ProcessWire\WireArray { + /** + * Path to the root directory for partial files + * + * @var string + */ + private $path; + /** * Gateway for rendering partials with arguments * @@ -28,4 +35,50 @@ public function __call($method, $arguments) { return parent::__call($method, $arguments); } + /** + * Enables derefencing of WireArray elements in object notation. + * + * Note that unlike regular WireArray, we're going to prioritize local data items. + * + * @param int|string $property + * @return Partial|Partials|null Partial or Partials object or null if no matching item found + * + */ + public function __get($property) { + $value = null; + if ((\is_string($property) || \is_int($property)) && isset($this->data[$property])) { + $value = $this->data[$property]; + } else { + $value = parent::__get($property); + } + if (\is_null($value)) { + $ext = $this->wire('config')->templateExtension; + return new Partial([ + $ext => $this->path . $property . '.' . $ext, + ]); + } + return $value; + } + + /** + * Get a new/blank Partial object + * + * @return Partial + * + */ + public function makeBlankItem() { + return new Partial([]); + } + + /** + * Set path + * + * @param string $path + * @return Partials Self-reference + */ + public function setPath(string $path): Partials { + $this->path = $path; + return $this; + } + } From a4a58c3fce64e037e7e983ea660b6e3c288bd218 Mon Sep 17 00:00:00 2001 From: Teppo Koivula Date: Wed, 13 May 2020 11:34:14 +0300 Subject: [PATCH 2/2] Update CHANGELOG --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 84997ad..4fe90f2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +## [0.11.0] - 2020-05-13 + ### Changed - Partials class now detects if a missing partial is being requested and throws an exception.