1 | <?php |
---|
2 | # -- BEGIN LICENSE BLOCK --------------------------------------- |
---|
3 | # |
---|
4 | # This file is part of Dotclear 2. |
---|
5 | # |
---|
6 | # Copyright (c) 2003-2011 Olivier Meunier & Association Dotclear |
---|
7 | # Licensed under the GPL version 2.0 license. |
---|
8 | # See LICENSE file or |
---|
9 | # http://www.gnu.org/licenses/old-licenses/gpl-2.0.html |
---|
10 | # |
---|
11 | # -- END LICENSE BLOCK ----------------------------------------- |
---|
12 | if (!defined('DC_RC_PATH')) { return; } |
---|
13 | |
---|
14 | /** |
---|
15 | * @ingroup DC_CORE |
---|
16 | * @brief Template extension for public context |
---|
17 | * |
---|
18 | * This extends template environment with tools required in context. |
---|
19 | * Admin context and public context should extend this class. |
---|
20 | */ |
---|
21 | class dcContext extends Twig_Extension |
---|
22 | { |
---|
23 | protected $core; |
---|
24 | protected $globals = array(); |
---|
25 | protected $protected_globals = array(); |
---|
26 | protected $memory = array(); |
---|
27 | |
---|
28 | public function __construct($core) |
---|
29 | { |
---|
30 | $this->core = $core; |
---|
31 | |
---|
32 | # Globals editable via context |
---|
33 | $this->globals = array(); |
---|
34 | |
---|
35 | # Globals not editable via context |
---|
36 | $this->protected_globals = array( |
---|
37 | 'messages' => array( |
---|
38 | 'static' => array(), |
---|
39 | 'lists' => array(), |
---|
40 | 'alert' => '', |
---|
41 | 'errors' => array() |
---|
42 | ), |
---|
43 | |
---|
44 | 'version' => DC_VERSION, |
---|
45 | 'vendor_name' => DC_VENDOR_NAME, |
---|
46 | |
---|
47 | 'safe_mode' => isset($_SESSION['sess_safe_mode']) && $_SESSION['sess_safe_mode'], |
---|
48 | 'debug_mode' => DC_DEBUG |
---|
49 | ); |
---|
50 | } |
---|
51 | |
---|
52 | /** |
---|
53 | * Prevent call crash from template on method that return this class |
---|
54 | */ |
---|
55 | public function __toString() |
---|
56 | { |
---|
57 | return ''; |
---|
58 | } |
---|
59 | |
---|
60 | /** |
---|
61 | * Test a global variable |
---|
62 | * |
---|
63 | * @param string $name Name of the variable to test |
---|
64 | * @return boolean |
---|
65 | */ |
---|
66 | public function __isset($name) |
---|
67 | { |
---|
68 | return isset($this->globals[$name]); |
---|
69 | } |
---|
70 | |
---|
71 | /** |
---|
72 | * Add a global variable |
---|
73 | * |
---|
74 | * @param string $name Name of the variable |
---|
75 | * @param mixed $value Value of the variable |
---|
76 | */ |
---|
77 | public function __set($name,$value) |
---|
78 | { |
---|
79 | /* |
---|
80 | # Overload protect |
---|
81 | if ($value === null && isset($this->globals[$name])) { |
---|
82 | unset($this->globals[$name]); |
---|
83 | } |
---|
84 | elseif (!isset($this->globals[$name])) { |
---|
85 | throw new Exception('Modification of overloaded globals has no effect'); |
---|
86 | } |
---|
87 | //*/ |
---|
88 | $this->globals[$name] = $value; |
---|
89 | } |
---|
90 | |
---|
91 | /** |
---|
92 | * Get a global variable |
---|
93 | * |
---|
94 | * @param string $name Name of the variable |
---|
95 | * @return mixed Value of the variable or null |
---|
96 | */ |
---|
97 | public function __get($name) |
---|
98 | { |
---|
99 | return isset($this->globals[$name]) ? $this->globals[$name] : null; |
---|
100 | } |
---|
101 | |
---|
102 | /** |
---|
103 | * Returns a list of filters to add to the existing list. |
---|
104 | * |
---|
105 | * @return array An array of filters |
---|
106 | */ |
---|
107 | public function getFilters() |
---|
108 | { |
---|
109 | return array( |
---|
110 | 'trans' => new Twig_Filter_Function("__", array('is_safe' => array('html'))) |
---|
111 | ); |
---|
112 | } |
---|
113 | |
---|
114 | /** |
---|
115 | * Returns a list of functions to add to the existing list. |
---|
116 | * |
---|
117 | * @return array An array of functions |
---|
118 | */ |
---|
119 | public function getFunctions() |
---|
120 | { |
---|
121 | return array( |
---|
122 | '__' => new Twig_Function_Function("__", array('is_safe' => array('html'))), |
---|
123 | 'debug_info' => new Twig_Function_Method($this, 'getDebugInfo', array('is_safe' => array('html'))), |
---|
124 | 'memorize' => new Twig_Function_Method($this, 'setMemory', array('is_safe' => array('html'))), |
---|
125 | 'memorized' => new Twig_Function_Method($this, 'getMemory', array('is_safe' => array('html'))) |
---|
126 | ); |
---|
127 | } |
---|
128 | |
---|
129 | /** |
---|
130 | * Returns a list of global variables to add to the existing list. |
---|
131 | * |
---|
132 | * This merges overloaded variables with defined variables. |
---|
133 | * |
---|
134 | * @return array An array of global variables |
---|
135 | */ |
---|
136 | public function getGlobals() |
---|
137 | { |
---|
138 | # Keep protected globals safe |
---|
139 | return array_merge($this->globals,$this->protected_globals); |
---|
140 | } |
---|
141 | |
---|
142 | /** |
---|
143 | * Returns the name of the extension. |
---|
144 | * |
---|
145 | * @return string The extension name |
---|
146 | */ |
---|
147 | public function getName() |
---|
148 | { |
---|
149 | return 'dcContext'; |
---|
150 | } |
---|
151 | |
---|
152 | |
---|
153 | /** |
---|
154 | * Add an informational message |
---|
155 | * |
---|
156 | * @param string $message A message |
---|
157 | * @return object self |
---|
158 | */ |
---|
159 | public function setSafeMode($safe_mode) |
---|
160 | { |
---|
161 | $this->protected_globals['safe_mode'] = (boolean) $safe_mode; |
---|
162 | return $this; |
---|
163 | } |
---|
164 | |
---|
165 | /** |
---|
166 | * Add an informational message |
---|
167 | * |
---|
168 | * @param string $message A message |
---|
169 | * @return object self |
---|
170 | */ |
---|
171 | public function addMessageStatic($message) |
---|
172 | { |
---|
173 | $this->protected_globals['messages']['static'][] = $message; |
---|
174 | return $this; |
---|
175 | } |
---|
176 | |
---|
177 | /** |
---|
178 | * Add a list of informational messages |
---|
179 | * |
---|
180 | * @param string $message A title |
---|
181 | * @param array $message A list of messages |
---|
182 | * @return object self |
---|
183 | */ |
---|
184 | public function addMessagesList($title,$messages) |
---|
185 | { |
---|
186 | $this->protected_globals['messages']['lists'][$title] = $messages; |
---|
187 | return $this; |
---|
188 | } |
---|
189 | |
---|
190 | /** |
---|
191 | * Set an important message |
---|
192 | * |
---|
193 | * @param string $message A message |
---|
194 | * @return object self |
---|
195 | */ |
---|
196 | public function setAlert($message) |
---|
197 | { |
---|
198 | $this->protected_globals['messages']['alert'] = $message; |
---|
199 | return $this; |
---|
200 | } |
---|
201 | |
---|
202 | /** |
---|
203 | * Add an error message |
---|
204 | * |
---|
205 | * @param string Error message |
---|
206 | * @return object self |
---|
207 | */ |
---|
208 | public function addError($error) |
---|
209 | { |
---|
210 | $this->protected_globals['messages']['errors'][] = $error; |
---|
211 | return $this; |
---|
212 | } |
---|
213 | |
---|
214 | /** |
---|
215 | * Check if there is an error message |
---|
216 | * |
---|
217 | * @return boolean |
---|
218 | */ |
---|
219 | public function hasError() |
---|
220 | { |
---|
221 | return !empty($this->protected_globals['messages']['errors']); |
---|
222 | } |
---|
223 | |
---|
224 | /** |
---|
225 | * Get an array of debug/dev infos |
---|
226 | */ |
---|
227 | public function getDebugInfo() |
---|
228 | { |
---|
229 | if (!DC_DEBUG) { |
---|
230 | return array(); |
---|
231 | } |
---|
232 | |
---|
233 | $di = array( |
---|
234 | 'global_vars' => implode(', ',array_keys($GLOBALS)), |
---|
235 | 'memory' => array( |
---|
236 | 'usage' => memory_get_usage(), |
---|
237 | 'size' => files::size(memory_get_usage()) |
---|
238 | ), |
---|
239 | 'xdebug' => array() |
---|
240 | ); |
---|
241 | |
---|
242 | if (function_exists('xdebug_get_profiler_filename')) { |
---|
243 | |
---|
244 | $url = http::getSelfURI(); |
---|
245 | $url .= strpos($url,'?') === false ? '?' : '&'; |
---|
246 | $url .= 'XDEBUG_PROFILE'; |
---|
247 | |
---|
248 | $di['xdebug'] = array( |
---|
249 | 'elapse_time' => xdebug_time_index(), |
---|
250 | 'profiler_file' => xdebug_get_profiler_filename(), |
---|
251 | 'profiler_url' => $url |
---|
252 | ); |
---|
253 | |
---|
254 | /* xdebug configuration: |
---|
255 | zend_extension = /.../xdebug.so |
---|
256 | xdebug.auto_trace = On |
---|
257 | xdebug.trace_format = 0 |
---|
258 | xdebug.trace_options = 1 |
---|
259 | xdebug.show_mem_delta = On |
---|
260 | xdebug.profiler_enable = 0 |
---|
261 | xdebug.profiler_enable_trigger = 1 |
---|
262 | xdebug.profiler_output_dir = /tmp |
---|
263 | xdebug.profiler_append = 0 |
---|
264 | xdebug.profiler_output_name = timestamp |
---|
265 | */ |
---|
266 | } |
---|
267 | |
---|
268 | return $di; |
---|
269 | } |
---|
270 | |
---|
271 | /** |
---|
272 | * Add a value in a namespace memory |
---|
273 | * |
---|
274 | * This help keep variable when recalling Twig macros |
---|
275 | * |
---|
276 | * @param string $ns A namespace |
---|
277 | * @param string $str A value to memorize in this namespace |
---|
278 | */ |
---|
279 | public function setMemory($ns,$str) |
---|
280 | { |
---|
281 | if (!array_key_exists($ns,$this->memory) || !in_array($str,$this->memory[$ns])) { |
---|
282 | $this->memory[$ns][] = $str; |
---|
283 | } |
---|
284 | } |
---|
285 | |
---|
286 | /** |
---|
287 | * Check if a value is previously memorized in a namespace |
---|
288 | * |
---|
289 | * @param string $ns A namespace |
---|
290 | * @param string $str A value to search in this namespace |
---|
291 | * @return array True if exists |
---|
292 | */ |
---|
293 | public function getMemory($ns,$str) |
---|
294 | { |
---|
295 | return array_key_exists($ns,$this->memory) && in_array($str,$this->memory[$ns]); |
---|
296 | } |
---|
297 | } |
---|
298 | ?> |
---|