Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/phpmyadmin/phpmyadmin.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--libraries/classes/Template.php54
-rw-r--r--test/classes/TemplateTest.php32
2 files changed, 53 insertions, 33 deletions
diff --git a/libraries/classes/Template.php b/libraries/classes/Template.php
index e27dcca62b..f09b96274d 100644
--- a/libraries/classes/Template.php
+++ b/libraries/classes/Template.php
@@ -38,29 +38,26 @@ use Twig\Loader\FilesystemLoader;
class Template
{
/**
- * Name of the template
+ * Twig environment
+ * @var Environment
*/
- protected $name = null;
+ protected static $twig;
/**
- * Twig environment
+ * @var string
*/
- static protected $twig;
-
public const BASE_PATH = 'templates/';
/**
* Template constructor
- *
- * @param string $name Template name
*/
- protected function __construct($name)
+ public function __construct()
{
- $this->name = $name;
-
+ /* @var \PhpMyAdmin\Config $config */
+ $config = $GLOBALS['PMA_Config'];
if (is_null($this::$twig)) {
$loader = new FilesystemLoader(static::BASE_PATH);
- $cache_dir = $GLOBALS['PMA_Config']->getTempDir('twig');
+ $cache_dir = $config->getTempDir('twig');
/* Twig expects false when cache is not configured */
if (is_null($cache_dir)) {
$cache_dir = false;
@@ -93,30 +90,31 @@ class Template
/**
* Template getter
*
- * @param string $name Template name
+ * @param string $templateName Template path name
*
- * @return Template
+ * @return \Twig_TemplateWrapper
*/
- public static function get($name)
+ public static function get(string $templateName): \Twig_TemplateWrapper
{
- return new Template($name);
+ $template = new Template();
+ return $template->load($templateName);
}
/**
- * Render template
+ * Loads a template.
*
- * @param array $data Variables to be provided to the template
+ * @param string $templateName Template path name
*
- * @return string
+ * @return \Twig_TemplateWrapper
*/
- public function render(array $data = [])
+ public function load(string $templateName): \Twig_TemplateWrapper
{
try {
- $template = $this::$twig->load($this->name . '.twig');
+ $template = $this::$twig->load($templateName . '.twig');
} catch (\RuntimeException $e) {
/* Retry with disabled cache */
$this::$twig->setCache(false);
- $template = $this::$twig->load($this->name . '.twig');
+ $template = $this::$twig->load($templateName . '.twig');
/*
* The trigger error is intentionally after second load
* to avoid triggering error when disabling cache does not
@@ -130,6 +128,18 @@ class Template
E_USER_WARNING
);
}
- return $template->render($data);
+
+ return $template;
+ }
+
+ /**
+ * @param string $template Template path name
+ * @param array $data Associative array of template variables
+ *
+ * @return string
+ */
+ public function render(string $template, array $data = []): string
+ {
+ return $this->load($template)->render($data);
}
}
diff --git a/test/classes/TemplateTest.php b/test/classes/TemplateTest.php
index f63402ff78..cfabd9d67a 100644
--- a/test/classes/TemplateTest.php
+++ b/test/classes/TemplateTest.php
@@ -21,6 +21,19 @@ use Twig\Error\LoaderError;
class TemplateTest extends PmaTestCase
{
/**
+ * @var Template
+ */
+ protected $template;
+
+ /**
+ * Sets up the fixture.
+ */
+ protected function setUp()
+ {
+ $this->template = new Template();
+ }
+
+ /**
* Test for set function
*
* @param string $data Template name
@@ -31,13 +44,10 @@ class TemplateTest extends PmaTestCase
*/
public function testSet($data)
{
- $template = Template::get($data);
- $result = $template->render(
- [
- 'variable1' => 'value1',
- 'variable2' => 'value2',
- ]
- );
+ $result = $this->template->render($data, [
+ 'variable1' => 'value1',
+ 'variable2' => 'value2',
+ ]);
$this->assertContains('value1', $result);
$this->assertContains('value2', $result);
}
@@ -69,7 +79,7 @@ class TemplateTest extends PmaTestCase
{
$this->assertEquals(
$value,
- Template::get($templateFile)->render([$key => $value])
+ $this->template->render($templateFile, [$key => $value])
);
}
@@ -93,7 +103,7 @@ class TemplateTest extends PmaTestCase
public function testRenderTemplateNotFound()
{
$this->expectException(LoaderError::class);
- Template::get('template not found')->render();
+ $this->template->render('template not found');
}
/**
@@ -110,7 +120,7 @@ class TemplateTest extends PmaTestCase
{
$this->assertEquals(
$expectedResult,
- Template::get($templateFile)->render()
+ $this->template->render($templateFile)
);
}
@@ -141,7 +151,7 @@ class TemplateTest extends PmaTestCase
{
$this->assertEquals(
$expectedResult,
- Template::get($templateFile)->render($renderParams)
+ $this->template->render($templateFile, $renderParams)
);
}