From 704b02306be1a8cec46930772f16ac2ec5dfc20e Mon Sep 17 00:00:00 2001 From: Atul Pratap Singh Date: Fri, 15 Jan 2016 09:08:44 +0530 Subject: [PATCH 1/5] Provide for passing data incrementally and passing functions into templates: Issue #11857 Signed-off-by: Atul Pratap Singh --- libraries/Template.php | 91 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 89 insertions(+), 2 deletions(-) diff --git a/libraries/Template.php b/libraries/Template.php index 8a1a3aa83e..9588e4814f 100644 --- a/libraries/Template.php +++ b/libraries/Template.php @@ -16,9 +16,21 @@ namespace PMA\libraries; */ class Template { - + /** + * Name of the template + */ protected $name = null; + /** + * Data associated with the template + */ + protected $data; + + /** + * Helper functions for the template + */ + protected $helperFunctions; + const BASE_PATH = 'templates/'; /** @@ -29,6 +41,8 @@ class Template protected function __construct($name) { $this->name = $name; + $this->data = array(); + $this->helperFunctions = array(); } /** @@ -57,6 +71,78 @@ class Template return preg_replace($regexp, "$1$2", $content); } + /** + * Sets data to be used by this template + * + * @param array $data containing data entries + */ + public function setData($data = array()) + { + $this->data = $data; + } + + /** + * Adds more entries to the data for this template + * + * @param array $data containing data entries + */ + public function addData($data = array()) + { + foreach ($data as $key => $value) { + $this->data[$key] = $value; + } + } + + /** + * Adds a function for use by the template + * + * @param string $funcName function name + * @param callable $funcDef function definition + */ + public function addFunction($funcName, $funcDef) + { + if (! isset($this->helperFunctions[$funcName])) { + $this->helperFunctions[$funcName] = $funcDef; + } else { + throw new \LogicException( + 'The function "' . $funcName . '" is already associated with the template.' + ); + } + } + + /** + * Removes a function + * + * @param string $funcName function name + */ + public function removeFunction($funcName) + { + if (isset($this->helperFunctions[$funcName])) { + unset($this->helperFunctions[$funcName]); + } else { + throw new \LogicException( + 'The function "' . $funcName . '" is not associated with the template.' + ); + } + } + + /** + * Magic call to locally inaccessible but associated helper functions + * + * @param string $funcName function name + * @param array $arguments function arguments + */ + public function __call($funcName, $arguments) + { + if (isset($this->helperFunctions[$funcName])) { + return call_user_func_array($this->helperFunctions[$funcName], $arguments); + } else { + throw new \LogicException( + 'The function "' . $funcName . '" is not associated with the template.' + ); + } + } + /** * Render template * @@ -69,7 +155,8 @@ class Template { $template = static::BASE_PATH . $this->name . '.phtml'; try { - extract($data); + $this->addData($data); + extract($this->data); ob_start(); if (file_exists($template)) { include $template; From b725916252cd6877232841cf34d6765d95edab6c Mon Sep 17 00:00:00 2001 From: Atul Pratap Singh Date: Fri, 15 Jan 2016 09:38:48 +0530 Subject: [PATCH 2/5] Add tests for template functions: setData, addData, addFunction Signed-off-by: Atul Pratap Singh --- templates/test/add_data.phtml | 3 ++ templates/test/add_function.phtml | 2 ++ test/classes/TemplateTest.php | 58 +++++++++++++++++++++++++++++++ 3 files changed, 63 insertions(+) create mode 100644 templates/test/add_data.phtml create mode 100644 templates/test/add_function.phtml diff --git a/templates/test/add_data.phtml b/templates/test/add_data.phtml new file mode 100644 index 0000000000..eee5ebe053 --- /dev/null +++ b/templates/test/add_data.phtml @@ -0,0 +1,3 @@ +setData( + array( + 'variable' => 'value' + ) + ); + $this->assertEquals('value', $template->render()); + } + + /** + * Test for addData + * + * @return void + */ + public function testAddData() + { + $template = PMA\libraries\Template::get('test/addData'); + $template->addData( + array( + 'variable1' => 'value1' + ) + ); + $template->addData( + array( + 'variable2' => 'value2' + ) + ); + $result = $template->render(); + $this->assertContains('value1', $result); + $this->assertContains('value2', $result); + } + + /** + * Test for addFunction + * + * @return void + */ + public function testAddFunction() + { + $template = PMA\libraries\Template::get('test/add_function'); + $template->addFunction('hello', function ($string) { + return 'hello ' . $string; + }); + $template->addData( + array( + 'variable' => 'world' + ) + ); + $this->assertEquals('hello world', $template->render()); + } + /** * Test for render * From 8bdcd7d3c0778f9c9e3a38f84c710337e0189ccf Mon Sep 17 00:00:00 2001 From: Atul Pratap Singh Date: Sat, 16 Jan 2016 17:00:32 +0530 Subject: [PATCH 3/5] Fix template test case Signed-off-by: Atul Pratap Singh --- templates/test/add_function.phtml | 2 +- test/classes/TemplateTest.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/templates/test/add_function.phtml b/templates/test/add_function.phtml index 5b8fe908c0..8491e437b8 100644 --- a/templates/test/add_function.phtml +++ b/templates/test/add_function.phtml @@ -1,2 +1,2 @@ hello($variable); diff --git a/test/classes/TemplateTest.php b/test/classes/TemplateTest.php index acf310ba68..a9345e9051 100644 --- a/test/classes/TemplateTest.php +++ b/test/classes/TemplateTest.php @@ -38,7 +38,7 @@ class TemplateTest extends PMATestCase */ public function testAddData() { - $template = PMA\libraries\Template::get('test/addData'); + $template = PMA\libraries\Template::get('test/add_data'); $template->addData( array( 'variable1' => 'value1' From 4e35951b702e68d823fefe2a27ce70d7530181b2 Mon Sep 17 00:00:00 2001 From: Atul Pratap Singh Date: Sun, 17 Jan 2016 22:15:29 +0530 Subject: [PATCH 4/5] Provide for optional parameters while instantiating and rendering template Signed-off-by: Atul Pratap Singh --- libraries/Template.php | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/libraries/Template.php b/libraries/Template.php index 9588e4814f..bb0801726b 100644 --- a/libraries/Template.php +++ b/libraries/Template.php @@ -37,24 +37,28 @@ class Template * Template constructor * * @param string $name Template name + * @param array $data Variables to be provided to the template + * @param array $helperFunctions Helper functions to be used by template */ - protected function __construct($name) + protected function __construct($name, $data = array(), $helperFunctions = array()) { $this->name = $name; - $this->data = array(); - $this->helperFunctions = array(); + $this->data = $data; + $this->helperFunctions = $helperFunctions; } /** * Template getter * * @param string $name Template name + * @param array $data Variables to be provided to the template + * @param array $helperFunctions Helper functions to be used by template * * @return Template */ - public static function get($name) + public static function get($name, $data = array(), $helperFunctions = array()) { - return new Template($name); + return new Template($name, $data, $helperFunctions); } /** @@ -146,17 +150,22 @@ class Template /** * Render template * - * @param array $data Variables to provides for template + * @param array $data Variables to be provided to the template * @param bool $trim Trim content + * @param array $helperFunctions Helper functions to be used by template * * @return string */ - public function render($data = array(), $trim = true) + public function render($data = array(), $trim = true, $helperFunctions = array()) { $template = static::BASE_PATH . $this->name . '.phtml'; try { $this->addData($data); extract($this->data); + $this->helperFunctions = array_merge( + $this->helperFunctions, + $helperFunctions + ); ob_start(); if (file_exists($template)) { include $template; From e1a8fc758a98fad13ae88b60ccaa6ae44df6f6ce Mon Sep 17 00:00:00 2001 From: Atul Pratap Singh Date: Mon, 18 Jan 2016 11:25:41 +0530 Subject: [PATCH 5/5] Change some function names Signed-off-by: Atul Pratap Singh --- libraries/Template.php | 30 +++++++-------- .../{add_function.phtml => set_helper.phtml} | 0 test/classes/TemplateTest.php | 38 +++++-------------- 3 files changed, 22 insertions(+), 46 deletions(-) rename templates/test/{add_function.phtml => set_helper.phtml} (100%) diff --git a/libraries/Template.php b/libraries/Template.php index bb0801726b..b2a759f3de 100644 --- a/libraries/Template.php +++ b/libraries/Template.php @@ -75,25 +75,21 @@ class Template return preg_replace($regexp, "$1$2", $content); } - /** - * Sets data to be used by this template - * - * @param array $data containing data entries - */ - public function setData($data = array()) - { - $this->data = $data; - } - /** * Adds more entries to the data for this template * - * @param array $data containing data entries + * @param array|string $data containing data array or data key + * @param string $value containing data value */ - public function addData($data = array()) + public function set($data, $value = null) { - foreach ($data as $key => $value) { - $this->data[$key] = $value; + if(is_array($data) && ! $value) { + $this->data = array_merge( + $this->data, + $data + ); + } else if (is_string($data)) { + $this->data[$data] = $value; } } @@ -103,7 +99,7 @@ class Template * @param string $funcName function name * @param callable $funcDef function definition */ - public function addFunction($funcName, $funcDef) + public function setHelper($funcName, $funcDef) { if (! isset($this->helperFunctions[$funcName])) { $this->helperFunctions[$funcName] = $funcDef; @@ -119,7 +115,7 @@ class Template * * @param string $funcName function name */ - public function removeFunction($funcName) + public function removeHelper($funcName) { if (isset($this->helperFunctions[$funcName])) { unset($this->helperFunctions[$funcName]); @@ -160,7 +156,7 @@ class Template { $template = static::BASE_PATH . $this->name . '.phtml'; try { - $this->addData($data); + $this->set($data); extract($this->data); $this->helperFunctions = array_merge( $this->helperFunctions, diff --git a/templates/test/add_function.phtml b/templates/test/set_helper.phtml similarity index 100% rename from templates/test/add_function.phtml rename to templates/test/set_helper.phtml diff --git a/test/classes/TemplateTest.php b/test/classes/TemplateTest.php index a9345e9051..8d9a19aa67 100644 --- a/test/classes/TemplateTest.php +++ b/test/classes/TemplateTest.php @@ -16,35 +16,15 @@ require_once 'test/PMATestCase.php'; class TemplateTest extends PMATestCase { /** - * Test for setData + * Test for set function * * @return void */ - public function testSetData() - { - $template = PMA\libraries\Template::get('test/echo'); - $template->setData( - array( - 'variable' => 'value' - ) - ); - $this->assertEquals('value', $template->render()); - } - - /** - * Test for addData - * - * @return void - */ - public function testAddData() + public function testSet() { $template = PMA\libraries\Template::get('test/add_data'); - $template->addData( - array( - 'variable1' => 'value1' - ) - ); - $template->addData( + $template->set('variable1', 'value1'); + $template->set( array( 'variable2' => 'value2' ) @@ -55,17 +35,17 @@ class TemplateTest extends PMATestCase } /** - * Test for addFunction + * Test for setHelper * * @return void */ - public function testAddFunction() + public function testSetHelper() { - $template = PMA\libraries\Template::get('test/add_function'); - $template->addFunction('hello', function ($string) { + $template = PMA\libraries\Template::get('test/set_helper'); + $template->setHelper('hello', function ($string) { return 'hello ' . $string; }); - $template->addData( + $template->set( array( 'variable' => 'world' )