Lock page when changes are done in the SQL editor
[phpmyadmin.git] / libraries / Template.php
blobb7d5ac0a0c25ca9ff62b54299b0c10a733181300
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * hold PMA\libraries\Template class
6 * @package PMA\libraries
7 */
8 namespace PMA\libraries;
10 /**
11 * Class Template
13 * Handle front end templating
15 * @package PMA\libraries
17 class Template
19 /**
20 * Name of the template
22 protected $name = null;
24 /**
25 * Data associated with the template
27 protected $data;
29 /**
30 * Helper functions for the template
32 protected $helperFunctions;
34 const BASE_PATH = 'templates/';
36 /**
37 * Template constructor
39 * @param string $name Template name
40 * @param array $data Variables to be provided to the template
41 * @param array $helperFunctions Helper functions to be used by template
43 protected function __construct($name, $data = array(), $helperFunctions = array())
45 $this->name = $name;
46 $this->data = $data;
47 $this->helperFunctions = $helperFunctions;
50 /**
51 * Template getter
53 * @param string $name Template name
54 * @param array $data Variables to be provided to the template
55 * @param array $helperFunctions Helper functions to be used by template
57 * @return Template
59 public static function get($name, $data = array(), $helperFunctions = array())
61 return new Template($name, $data, $helperFunctions);
64 /**
65 * Adds more entries to the data for this template
67 * @param array|string $data containing data array or data key
68 * @param string $value containing data value
70 public function set($data, $value = null)
72 if(is_array($data) && ! $value) {
73 $this->data = array_merge(
74 $this->data,
75 $data
77 } else if (is_string($data)) {
78 $this->data[$data] = $value;
82 /**
83 * Adds a function for use by the template
85 * @param string $funcName function name
86 * @param callable $funcDef function definition
88 public function setHelper($funcName, $funcDef)
90 if (! isset($this->helperFunctions[$funcName])) {
91 $this->helperFunctions[$funcName] = $funcDef;
92 } else {
93 throw new \LogicException(
94 'The function "' . $funcName . '" is already associated with the template.'
99 /**
100 * Removes a function
102 * @param string $funcName function name
104 public function removeHelper($funcName)
106 if (isset($this->helperFunctions[$funcName])) {
107 unset($this->helperFunctions[$funcName]);
108 } else {
109 throw new \LogicException(
110 'The function "' . $funcName . '" is not associated with the template.'
116 * Magic call to locally inaccessible but associated helper functions
118 * @param string $funcName function name
119 * @param array $arguments function arguments
121 public function __call($funcName, $arguments)
123 if (isset($this->helperFunctions[$funcName])) {
124 return call_user_func_array($this->helperFunctions[$funcName], $arguments);
125 } else {
126 throw new \LogicException(
127 'The function "' . $funcName . '" is not associated with the template.'
133 * Render template
135 * @param array $data Variables to be provided to the template
136 * @param array $helperFunctions Helper functions to be used by template
138 * @return string
140 public function render($data = array(), $helperFunctions = array())
142 $template = static::BASE_PATH . $this->name . '.phtml';
143 try {
144 $this->set($data);
145 $this->helperFunctions = array_merge(
146 $this->helperFunctions,
147 $helperFunctions
149 extract($this->data);
150 ob_start();
151 if (@file_exists($template)) {
152 include $template;
153 } else {
154 throw new \LogicException(
155 'The template "' . $template . '" not found.'
158 $content = ob_get_clean();
160 return $content;
161 } catch (\LogicException $e) {
162 ob_end_clean();
163 throw new \LogicException($e->getMessage());