From fd1705f45ed33943401fc39fe45a18d9f7b18acc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20=C4=8Ciha=C5=99?= Date: Mon, 2 Oct 2017 16:25:48 +0200 Subject: [PATCH] Simplify Template API MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Remove some features we do not seem to use: - helper functions (this never worked with Twig) - per class context, it now has to be passed to render - this avoids need of array_merge call on every render Signed-off-by: Michal Čihař --- libraries/classes/Template.php | 111 ++------------------------------ templates/test/set_helper.phtml | 1 - test/classes/TemplateTest.php | 54 +--------------- 3 files changed, 9 insertions(+), 157 deletions(-) delete mode 100644 templates/test/set_helper.phtml diff --git a/libraries/classes/Template.php b/libraries/classes/Template.php index 1b1c9a87d5..199e3ab6a0 100644 --- a/libraries/classes/Template.php +++ b/libraries/classes/Template.php @@ -31,16 +31,6 @@ class Template */ protected $name = null; - /** - * Data associated with the template - */ - protected $data; - - /** - * Helper functions for the template - */ - protected $helperFunctions; - /** * Twig environment */ @@ -52,16 +42,12 @@ 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, array $data = array(), array $helperFunctions = array()) + protected function __construct($name) { static $twig = null; $this->name = $name; - $this->data = $data; - $this->helperFunctions = $helperFunctions; if (is_null($twig)) { $loader = new Twig_Loader_Filesystem(static::BASE_PATH); @@ -90,106 +76,26 @@ class Template * 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, array $data = array(), array $helperFunctions = array()) + public static function get($name) { - return new Template($name, $data, $helperFunctions); - } - - /** - * Adds more entries to the data for this template - * - * @param array|string $data containing data array or data key - * @param string $value containing data value - * - * @return void - */ - public function set($data, $value = null) - { - if(is_array($data) && ! $value) { - $this->data = array_merge( - $this->data, - $data - ); - } else if (is_string($data)) { - $this->data[$data] = $value; - } - } - - /** - * Adds a function for use by the template - * - * @param string $funcName function name - * @param callable $funcDef function definition - * - * @return void - */ - public function setHelper($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 - * - * @return void - */ - public function removeHelper($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 - * - * @return mixed - */ - public function __call($funcName, array $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.' - ); - } + return new Template($name); } /** * Render template * * @param array $data Variables to be provided to the template - * @param array $helperFunctions Helper functions to be used by template * * @return string */ - public function render(array $data = array(), array $helperFunctions = array()) + public function render(array $data = array()) { $template = static::BASE_PATH . $this->name; if (file_exists($template . '.twig')) { - $this->set($data); try { $template = $this->twig->load($this->name . '.twig'); } catch (\RuntimeException $e) { @@ -209,17 +115,12 @@ class Template E_USER_WARNING ); } - return $template->render($this->data); + return $template->render($data); } $template = $template . '.phtml'; try { - $this->set($data); - $this->helperFunctions = array_merge( - $this->helperFunctions, - $helperFunctions - ); - extract($this->data); + extract($data); ob_start(); if (@file_exists($template)) { include $template; diff --git a/templates/test/set_helper.phtml b/templates/test/set_helper.phtml deleted file mode 100644 index d3dd0b6fbf..0000000000 --- a/templates/test/set_helper.phtml +++ /dev/null @@ -1 +0,0 @@ -hello($variable); \ No newline at end of file diff --git a/test/classes/TemplateTest.php b/test/classes/TemplateTest.php index de3e44b917..f5374cda7d 100644 --- a/test/classes/TemplateTest.php +++ b/test/classes/TemplateTest.php @@ -29,13 +29,12 @@ class TemplateTest extends PmaTestCase public function testSet($data) { $template = Template::get($data); - $template->set('variable1', 'value1'); - $template->set( + $result = $template->render( array( - 'variable2' => 'value2' + 'variable1' => 'value1', + 'variable2' => 'value2', ) ); - $result = $template->render(); $this->assertContains('value1', $result); $this->assertContains('value2', $result); } @@ -53,53 +52,6 @@ class TemplateTest extends PmaTestCase ]; } - /** - * Test for setHelper - * - * @return void - */ - public function testSetHelper() - { - $template = Template::get('test/set_helper'); - $template->setHelper('hello', function ($string) { - return 'hello ' . $string; - }); - $template->set(['variable' => 'world']); - $this->assertEquals('hello world', $template->render()); - - $this->setExpectedException('LogicException'); - $template->setHelper('hello', 'again'); - } - - /** - * Test for removeHelper - * - * @return void - */ - public function testRemoveHelper() - { - $template = Template::get('test/set_helper'); - $template->setHelper('hello', function ($string) { - return 'hello ' . $string; - }); - $template->set(['variable' => 'world']); - $template->removeHelper('hello'); - $this->setExpectedException('LogicException'); - $template->render(); - } - - /** - * Test for removeHelper - * - * @return void - */ - public function testRemoveHelperNotFound() - { - $template = Template::get('test/set_helper'); - $this->setExpectedException('LogicException'); - $template->removeHelper('not found'); - } - /** * Test for render *