From ff3716e7253b29c4fd4e414d302f1e687d914bdb Mon Sep 17 00:00:00 2001 From: Johan Janssens Date: Sun, 30 Jun 2019 08:51:10 +0200 Subject: [PATCH 1/9] #146 - Implement snippet helper Helper defines two methods a define() method to create a snippet and an expand() method to render a snippet. A snipppet is expanded using heredoc syntax. Heredoc text behaves just like a double-quoted string, without the double quotes. This means that quotes in a heredoc do not need to be escaped. --- .../com_pages/template/helper/snippet.php | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 code/site/components/com_pages/template/helper/snippet.php diff --git a/code/site/components/com_pages/template/helper/snippet.php b/code/site/components/com_pages/template/helper/snippet.php new file mode 100644 index 000000000..45130739b --- /dev/null +++ b/code/site/components/com_pages/template/helper/snippet.php @@ -0,0 +1,37 @@ +__snippets[$name]) || $overwrite) { + $this->__snippets[$name] = $snippet; + } + } + + public function expand(string $name, array $variables = array()) + { + $result = false; + + if(isset($this->__snippets[$name])) + { + $snippet = $this->__snippets[$name]; + + //Use the stream buffer to evaluate the partial + $str = "getObject('filesystem.stream.factory')->createStream('koowa-buffer://temp', 'w+b'); + $buffer->truncate(0); + $buffer->write($str); + + extract($variables, EXTR_OVERWRITE); + + $result = include $buffer->getPath(); + } + + return $result; + } +} + From 99bc02fbe84bff7f27053b2b9d3af1a3f8370ba7 Mon Sep 17 00:00:00 2001 From: Johan Janssens Date: Sun, 30 Jun 2019 20:52:32 +0200 Subject: [PATCH 2/9] #146 - Fix typo --- code/site/components/com_pages/template/helper/snippet.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/code/site/components/com_pages/template/helper/snippet.php b/code/site/components/com_pages/template/helper/snippet.php index 45130739b..37d28a7fd 100644 --- a/code/site/components/com_pages/template/helper/snippet.php +++ b/code/site/components/com_pages/template/helper/snippet.php @@ -1,6 +1,6 @@ Date: Mon, 1 Jul 2019 04:12:16 +0200 Subject: [PATCH 3/9] #146 - Do not unpack params if we only have one method parameter --- code/site/components/com_pages/template/default.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/code/site/components/com_pages/template/default.php b/code/site/components/com_pages/template/default.php index 670ad5844..602623898 100644 --- a/code/site/components/com_pages/template/default.php +++ b/code/site/components/com_pages/template/default.php @@ -121,7 +121,13 @@ public function invokeHelper($identifier, ...$params) $params = array_merge($this->getParameters()->toArray(), $params); } - return $helper->$function(...$params); + if(is_numeric(key($params))) { + $result = $helper->$function(...$params); + } else { + $result = $helper->$function($params); + } + + return $result; } public function createHelper($helper, $config = array()) From d0e2b6df13bcd231cd4a7401d3a0d6a190af2c10 Mon Sep 17 00:00:00 2001 From: Johan Janssens Date: Mon, 1 Jul 2019 05:05:31 +0200 Subject: [PATCH 4/9] #146 - Cleanup whitespace before " and > --- code/site/components/com_pages/template/helper/snippet.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/code/site/components/com_pages/template/helper/snippet.php b/code/site/components/com_pages/template/helper/snippet.php index 37d28a7fd..fe345a29f 100644 --- a/code/site/components/com_pages/template/helper/snippet.php +++ b/code/site/components/com_pages/template/helper/snippet.php @@ -29,6 +29,9 @@ public function expand(string $name, array $variables = array()) extract($variables, EXTR_OVERWRITE); $result = include $buffer->getPath(); + + //Cleanup whitespace + $result = str_replace(array(' >', ' "'), array('>', '"'), $result); } return $result; From 0df1956a8b5b7f84ba6f6ba8e73997f6a2058b8e Mon Sep 17 00:00:00 2001 From: Johan Janssens Date: Mon, 1 Jul 2019 23:58:21 +0200 Subject: [PATCH 5/9] #146 - Do not pass params to the helper constructor --- code/site/components/com_pages/template/default.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/code/site/components/com_pages/template/default.php b/code/site/components/com_pages/template/default.php index 602623898..13b4e7aa6 100644 --- a/code/site/components/com_pages/template/default.php +++ b/code/site/components/com_pages/template/default.php @@ -108,8 +108,8 @@ public function invokeHelper($identifier, ...$params) if(!empty($parts)) { $identifier = implode('.', $parts).'.template.helper.'.$identifier; } - - $helper = $this->createHelper($identifier, $params); + + $helper = $this->createHelper($identifier); //Call the helper function if (!is_callable(array($helper, $function))) { From 9a1acd9451c18cc68a1ab6f17b346d594ef47201 Mon Sep 17 00:00:00 2001 From: Johan Janssens Date: Mon, 1 Jul 2019 23:59:07 +0200 Subject: [PATCH 6/9] #146 - Use php template engine to expand a snippet --- .../com_pages/template/helper/snippet.php | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/code/site/components/com_pages/template/helper/snippet.php b/code/site/components/com_pages/template/helper/snippet.php index fe345a29f..e073d659f 100644 --- a/code/site/components/com_pages/template/helper/snippet.php +++ b/code/site/components/com_pages/template/helper/snippet.php @@ -20,18 +20,15 @@ public function expand(string $name, array $variables = array()) $snippet = $this->__snippets[$name]; //Use the stream buffer to evaluate the partial - $str = "getObject('filesystem.stream.factory')->createStream('koowa-buffer://temp', 'w+b'); - $buffer->truncate(0); - $buffer->write($str); - - extract($variables, EXTR_OVERWRITE); - - $result = include $buffer->getPath(); + $result = $this->getObject('template.engine.factory') + ->createEngine('php') + ->loadString($str) + ->render($variables); //Cleanup whitespace - $result = str_replace(array(' >', ' "'), array('>', '"'), $result); + //$result = str_replace(array(' >', ' "'), array('>', '"'), $result); } return $result; From 33219b35af055f394a977998c46fe86ec6118139 Mon Sep 17 00:00:00 2001 From: Johan Janssens Date: Tue, 2 Jul 2019 00:26:39 +0200 Subject: [PATCH 7/9] #146 - Find single whitespace before " or before > in html tags and remove it --- .../components/com_pages/template/helper/snippet.php | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/code/site/components/com_pages/template/helper/snippet.php b/code/site/components/com_pages/template/helper/snippet.php index e073d659f..1747aaf01 100644 --- a/code/site/components/com_pages/template/helper/snippet.php +++ b/code/site/components/com_pages/template/helper/snippet.php @@ -19,7 +19,7 @@ public function expand(string $name, array $variables = array()) { $snippet = $this->__snippets[$name]; - //Use the stream buffer to evaluate the partial + //Use the php template engine to evaluate $str = "getObject('template.engine.factory') @@ -27,8 +27,12 @@ public function expand(string $name, array $variables = array()) ->loadString($str) ->render($variables); - //Cleanup whitespace - //$result = str_replace(array(' >', ' "'), array('>', '"'), $result); + //Find single whitespace before " or before > in html tags and remove it + preg_match_all('#<\s*\w.*?>#', $result, $tags); + + foreach($tags as $tag) { + $result = str_replace($tag, str_replace(array(' >', ' "'), array('>', '"'), $tag), $result); + } } return $result; From dbc115a96ff16bbce22ebbc5d68aecea109424a6 Mon Sep 17 00:00:00 2001 From: Johan Janssens Date: Tue, 2 Jul 2019 01:10:15 +0200 Subject: [PATCH 8/9] #146 - Add invoke method to allow defining and expanding in the same way --- .../com_pages/template/helper/snippet.php | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/code/site/components/com_pages/template/helper/snippet.php b/code/site/components/com_pages/template/helper/snippet.php index 1747aaf01..271510621 100644 --- a/code/site/components/com_pages/template/helper/snippet.php +++ b/code/site/components/com_pages/template/helper/snippet.php @@ -4,11 +4,28 @@ class ComPagesTemplateHelperSnippet extends ComPagesTemplateHelperAbstract { private $__snippets = array(); + public function __invoke($name, $snippet) + { + if(is_string($snippet)) { + $result = $this->define($name, $snippet); + } else { + $result = $this->expand($name, $snippet); + } + + return $result; + } + public function define(string $name, string $snippet, $overwrite = false) { - if(!isset($this->__snippets[$name]) || $overwrite) { + $result = false; + + if(!isset($this->__snippets[$name]) || $overwrite) + { $this->__snippets[$name] = $snippet; + $result = true; } + + return $result; } public function expand(string $name, array $variables = array()) From 39c92e489b21b10e3dd671595f7577996fe27b5a Mon Sep 17 00:00:00 2001 From: Johan Janssens Date: Tue, 2 Jul 2019 01:20:59 +0200 Subject: [PATCH 9/9] #146 - Throw RuntimeException if the snippet doesn't exist --- code/site/components/com_pages/template/helper/snippet.php | 1 + 1 file changed, 1 insertion(+) diff --git a/code/site/components/com_pages/template/helper/snippet.php b/code/site/components/com_pages/template/helper/snippet.php index 271510621..8effca8d4 100644 --- a/code/site/components/com_pages/template/helper/snippet.php +++ b/code/site/components/com_pages/template/helper/snippet.php @@ -51,6 +51,7 @@ public function expand(string $name, array $variables = array()) $result = str_replace($tag, str_replace(array(' >', ' "'), array('>', '"'), $tag), $result); } } + else throw new RuntimeException('Snippet: '.$name.' does not exist'); return $result; }