1 : <?php
2 :
3 : define('DWOO_DIRECTORY', dirname(__FILE__) . DIRECTORY_SEPARATOR);
4 :
5 : /**
6 : * main dwoo class, allows communication between the compiler, template and data classes
7 : *
8 : * <pre>
9 : * requirements :
10 : * php 5.2.0 or above (might work below, it's a rough estimate)
11 : * SPL and PCRE extensions (for php versions prior to 5.3.0)
12 : * mbstring extension for some string manipulation plugins (especially if you intend to use UTF-8)
13 : * recommended :
14 : * hash extension (for Dwoo_Template_String - minor performance boost)
15 : *
16 : * project created :
17 : * 2008-01-05
18 : * </pre>
19 : *
20 : * This software is provided 'as-is', without any express or implied warranty.
21 : * In no event will the authors be held liable for any damages arising from the use of this software.
22 : *
23 : * @author Jordi Boggiano <j.boggiano@seld.be>
24 : * @copyright Copyright (c) 2008, Jordi Boggiano
25 : * @license http://dwoo.org/LICENSE Modified BSD License
26 : * @link http://dwoo.org/
27 : * @version 1.1.0
28 : * @date 2009-07-18
29 : * @package Dwoo
30 : */
31 : class Dwoo
32 : {
33 : /**
34 : * current version number
35 : *
36 : * @var string
37 : */
38 : const VERSION = '1.1.0';
39 :
40 : /**
41 : * unique number of this dwoo release
42 : *
43 : * this can be used by templates classes to check whether the compiled template
44 : * has been compiled before this release or not, so that old templates are
45 : * recompiled automatically when Dwoo is updated
46 : */
47 : const RELEASE_TAG = 16;
48 :
49 : /**#@+
50 : * constants that represents all plugin types
51 : *
52 : * these are bitwise-operation-safe values to allow multiple types
53 : * on a single plugin
54 : *
55 : * @var int
56 : */
57 : const CLASS_PLUGIN = 1;
58 : const FUNC_PLUGIN = 2;
59 : const NATIVE_PLUGIN = 4;
60 : const BLOCK_PLUGIN = 8;
61 : const COMPILABLE_PLUGIN = 16;
62 : const CUSTOM_PLUGIN = 32;
63 : const SMARTY_MODIFIER = 64;
64 : const SMARTY_BLOCK = 128;
65 : const SMARTY_FUNCTION = 256;
66 : const PROXY_PLUGIN = 512;
67 : const TEMPLATE_PLUGIN = 1024;
68 : /**#@-*/
69 :
70 : /**
71 : * character set of the template, used by string manipulation plugins
72 : *
73 : * it must be lowercase, but setCharset() will take care of that
74 : *
75 : * @see setCharset
76 : * @see getCharset
77 : * @var string
78 : */
79 : protected $charset = 'utf-8';
80 :
81 : /**
82 : * global variables that are accessible through $dwoo.* in the templates
83 : *
84 : * default values include:
85 : *
86 : * $dwoo.version - current version number
87 : * $dwoo.ad - a Powered by Dwoo link pointing to dwoo.org
88 : * $dwoo.now - the current time
89 : * $dwoo.template - the current template filename
90 : * $dwoo.charset - the character set used by the template
91 : *
92 : * on top of that, foreach and other plugins can store special values in there,
93 : * see their documentation for more details.
94 : *
95 : * @private
96 : * @var array
97 : */
98 : public $globals;
99 :
100 : /**
101 : * directory where the compiled templates are stored
102 : *
103 : * defaults to DWOO_COMPILEDIR (= dwoo_dir/compiled by default)
104 : *
105 : * @var string
106 : */
107 : protected $compileDir;
108 :
109 : /**
110 : * directory where the cached templates are stored
111 : *
112 : * defaults to DWOO_CACHEDIR (= dwoo_dir/cache by default)
113 : *
114 : * @var string
115 : */
116 : protected $cacheDir;
117 :
118 : /**
119 : * defines how long (in seconds) the cached files must remain valid
120 : *
121 : * can be overriden on a per-template basis
122 : *
123 : * -1 = never delete
124 : * 0 = disabled
125 : * >0 = duration in seconds
126 : *
127 : * @var int
128 : */
129 : protected $cacheTime = 0;
130 :
131 : /**
132 : * security policy object
133 : *
134 : * @var Dwoo_Security_Policy
135 : */
136 : protected $securityPolicy = null;
137 :
138 : /**
139 : * stores the custom plugins callbacks
140 : *
141 : * @see addPlugin
142 : * @see removePlugin
143 : * @var array
144 : */
145 : protected $plugins = array();
146 :
147 : /**
148 : * stores the filter callbacks
149 : *
150 : * @see addFilter
151 : * @see removeFilter
152 : * @var array
153 : */
154 : protected $filters = array();
155 :
156 : /**
157 : * stores the resource types and associated
158 : * classes / compiler classes
159 : *
160 : * @var array
161 : */
162 : protected $resources = array
163 : (
164 : 'file' => array
165 : (
166 : 'class' => 'Dwoo_Template_File',
167 : 'compiler' => null
168 : ),
169 : 'string' => array
170 : (
171 : 'class' => 'Dwoo_Template_String',
172 : 'compiler' => null
173 : )
174 : );
175 :
176 : /**
177 : * the dwoo loader object used to load plugins by this dwoo instance
178 : *
179 : * @var Dwoo_ILoader
180 : */
181 : protected $loader = null;
182 :
183 : /**
184 : * currently rendered template, set to null when not-rendering
185 : *
186 : * @var Dwoo_ITemplate
187 : */
188 : protected $template = null;
189 :
190 : /**
191 : * stores the instances of the class plugins during template runtime
192 : *
193 : * @var array
194 : */
195 : protected $runtimePlugins;
196 :
197 : /**
198 : * stores the data during template runtime
199 : *
200 : * @var array
201 : * @private
202 : */
203 : public $data;
204 :
205 : /**
206 : * stores the current scope during template runtime
207 : *
208 : * this should ideally not be accessed directly from outside template code
209 : *
210 : * @var mixed
211 : * @private
212 : */
213 : public $scope;
214 :
215 : /**
216 : * stores the scope tree during template runtime
217 : *
218 : * @var array
219 : */
220 : protected $scopeTree;
221 :
222 : /**
223 : * stores the block plugins stack during template runtime
224 : *
225 : * @var array
226 : */
227 : protected $stack;
228 :
229 : /**
230 : * stores the current block plugin at the top of the stack during template runtime
231 : *
232 : * @var Dwoo_Block_Plugin
233 : */
234 : protected $curBlock;
235 :
236 : /**
237 : * stores the output buffer during template runtime
238 : *
239 : * @var string
240 : */
241 : protected $buffer;
242 :
243 : /**
244 : * stores plugin proxy
245 : *
246 : * @var Dwoo_IPluginProxy
247 : */
248 : protected $pluginProxy;
249 :
250 : /**
251 : * constructor, sets the cache and compile dir to the default values if not provided
252 : *
253 : * @param string $compileDir path to the compiled directory, defaults to lib/compiled
254 : * @param string $cacheDir path to the cache directory, defaults to lib/cache
255 : */
256 : public function __construct($compileDir = null, $cacheDir = null)
257 : {
258 32 : if ($compileDir !== null) {
259 26 : $this->setCompileDir($compileDir);
260 26 : }
261 32 : if ($cacheDir !== null) {
262 26 : $this->setCacheDir($cacheDir);
263 26 : }
264 32 : $this->initGlobals();
265 32 : }
266 :
267 : /**
268 : * resets some runtime variables to allow a cloned object to be used to render sub-templates
269 : */
270 : public function __clone()
271 : {
272 5 : $this->template = null;
273 5 : unset($this->data);
274 5 : }
275 :
276 : /**
277 : * outputs the template instead of returning it, this is basically a shortcut for get(*, *, *, true)
278 : *
279 : * @see get
280 : * @param mixed $tpl template, can either be a Dwoo_ITemplate object (i.e. Dwoo_Template_File), a valid path to a template, or
281 : * a template as a string it is recommended to provide a Dwoo_ITemplate as it will probably make things faster,
282 : * especially if you render a template multiple times
283 : * @param mixed $data the data to use, can either be a Dwoo_IDataProvider object (i.e. Dwoo_Data) or an associative array. if you're
284 : * rendering the template from cache, it can be left null
285 : * @param Dwoo_ICompiler $compiler the compiler that must be used to compile the template, if left empty a default
286 : * Dwoo_Compiler will be used.
287 : * @return string nothing or the template output if $output is true
288 : */
289 : public function output($tpl, $data = array(), Dwoo_ICompiler $compiler = null)
290 : {
291 3 : return $this->get($tpl, $data, $compiler, true);
292 : }
293 :
294 : /**
295 : * returns the given template rendered using the provided data and optional compiler
296 : *
297 : * @param mixed $tpl template, can either be a Dwoo_ITemplate object (i.e. Dwoo_Template_File), a valid path to a template, or
298 : * a template as a string it is recommended to provide a Dwoo_ITemplate as it will probably make things faster,
299 : * especially if you render a template multiple times
300 : * @param mixed $data the data to use, can either be a Dwoo_IDataProvider object (i.e. Dwoo_Data) or an associative array. if you're
301 : * rendering the template from cache, it can be left null
302 : * @param Dwoo_ICompiler $compiler the compiler that must be used to compile the template, if left empty a default
303 : * Dwoo_Compiler will be used.
304 : * @param bool $output flag that defines whether the function returns the output of the template (false, default) or echoes it directly (true)
305 : * @return string nothing or the template output if $output is true
306 : */
307 : public function get($_tpl, $data = array(), $_compiler = null, $_output = false)
308 : {
309 : // a render call came from within a template, so we need a new dwoo instance in order to avoid breaking this one
310 221 : if ($this->template instanceof Dwoo_ITemplate) {
311 5 : $proxy = clone $this;
312 5 : return $proxy->get($_tpl, $data, $_compiler, $_output);
313 : }
314 :
315 : // auto-create template if required
316 221 : if ($_tpl instanceof Dwoo_ITemplate) {
317 : // valid, skip
318 221 : } elseif (is_string($_tpl) && file_exists($_tpl)) {
319 1 : $_tpl = new Dwoo_Template_File($_tpl);
320 1 : } else {
321 1 : throw new Dwoo_Exception('Dwoo->get/Dwoo->output\'s first argument must be a Dwoo_ITemplate (i.e. Dwoo_Template_File) or a valid path to a template file', E_USER_NOTICE);
322 : }
323 :
324 : // save the current template, enters render mode at the same time
325 : // if another rendering is requested it will be proxied to a new Dwoo instance
326 220 : $this->template = $_tpl;
327 :
328 : // load data
329 220 : if ($data instanceof Dwoo_IDataProvider) {
330 2 : $this->data = $data->getData();
331 220 : } elseif (is_array($data)) {
332 217 : $this->data = $data;
333 217 : } else {
334 1 : throw new Dwoo_Exception('Dwoo->get/Dwoo->output\'s data argument must be a Dwoo_IDataProvider object (i.e. Dwoo_Data) or an associative array', E_USER_NOTICE);
335 : }
336 :
337 219 : $this->globals['template'] = $_tpl->getName();
338 219 : $this->initRuntimeVars($_tpl);
339 :
340 : // try to get cached template
341 219 : $file = $_tpl->getCachedTemplate($this);
342 219 : $doCache = $file === true;
343 219 : $cacheLoaded = is_string($file);
344 :
345 219 : if ($cacheLoaded === true) {
346 : // cache is present, run it
347 5 : if ($_output === true) {
348 1 : include $file;
349 1 : $this->template = null;
350 1 : } else {
351 4 : ob_start();
352 4 : include $file;
353 4 : $this->template = null;
354 4 : return ob_get_clean();
355 : }
356 1 : } else {
357 : // no cache present
358 219 : if ($doCache === true) {
359 5 : $dynamicId = uniqid();
360 5 : }
361 :
362 : // render template
363 219 : $compiledTemplate = $_tpl->getCompiledTemplate($this, $_compiler);
364 208 : $out = include $compiledTemplate;
365 :
366 : // template returned false so it needs to be recompiled
367 208 : if ($out === false) {
368 0 : $_tpl->forceCompilation();
369 0 : $compiledTemplate = $_tpl->getCompiledTemplate($this, $_compiler);
370 0 : $out = include $compiledTemplate;
371 0 : }
372 :
373 208 : if ($doCache === true) {
374 5 : $out = preg_replace('/(<%|%>|<\?php|<\?|\?>)/', '<?php /*'.$dynamicId.'*/ echo \'$1\'; ?>', $out);
375 5 : if (!class_exists('Dwoo_plugin_dynamic', false)) {
376 0 : $this->getLoader()->loadPlugin('dynamic');
377 0 : }
378 5 : $out = Dwoo_Plugin_dynamic::unescape($out, $dynamicId, $compiledTemplate);
379 5 : }
380 :
381 : // process filters
382 208 : foreach ($this->filters as $filter) {
383 1 : if (is_array($filter) && $filter[0] instanceof Dwoo_Filter) {
384 1 : $out = call_user_func($filter, $out);
385 1 : } else {
386 0 : $out = call_user_func($filter, $this, $out);
387 : }
388 208 : }
389 :
390 208 : if ($doCache === true) {
391 : // building cache
392 5 : $file = $_tpl->cache($this, $out);
393 :
394 : // run it from the cache to be sure dynamics are rendered
395 5 : if ($_output === true) {
396 1 : include $file;
397 : // exit render mode
398 1 : $this->template = null;
399 1 : } else {
400 4 : ob_start();
401 4 : include $file;
402 : // exit render mode
403 4 : $this->template = null;
404 4 : return ob_get_clean();
405 : }
406 1 : } else {
407 : // no need to build cache
408 : // exit render mode
409 203 : $this->template = null;
410 : // output
411 203 : if ($_output === true) {
412 1 : echo $out;
413 1 : }
414 203 : return $out;
415 : }
416 : }
417 1 : }
418 :
419 : /**
420 : * re-initializes the globals array before each template run
421 : *
422 : * this method is only callede once when the Dwoo object is created
423 : */
424 : protected function initGlobals()
425 : {
426 32 : $this->globals = array
427 : (
428 32 : 'version' => self::VERSION,
429 32 : 'ad' => '<a href="http://dwoo.org/">Powered by Dwoo</a>',
430 32 : 'now' => $_SERVER['REQUEST_TIME'],
431 32 : 'charset' => $this->charset,
432 : );
433 32 : }
434 :
435 : /**
436 : * re-initializes the runtime variables before each template run
437 : *
438 : * override this method to inject data in the globals array if needed, this
439 : * method is called before each template execution
440 : *
441 : * @param Dwoo_ITemplate $tpl the template that is going to be rendered
442 : */
443 : protected function initRuntimeVars(Dwoo_ITemplate $tpl)
444 : {
445 219 : $this->runtimePlugins = array();
446 219 : $this->scope =& $this->data;
447 219 : $this->scopeTree = array();
448 219 : $this->stack = array();
449 219 : $this->curBlock = null;
450 219 : $this->buffer = '';
451 219 : }
452 :
453 : /*
454 : * --------- settings functions ---------
455 : */
456 :
457 : /**
458 : * adds a custom plugin that is not in one of the plugin directories
459 : *
460 : * @param string $name the plugin name to be used in the templates
461 : * @param callback $callback the plugin callback, either a function name,
462 : * a class name or an array containing an object
463 : * or class name and a method name
464 : * @param bool $compilable if set to true, the plugin is assumed to be compilable
465 : */
466 : public function addPlugin($name, $callback, $compilable = false)
467 : {
468 23 : $compilable = $compilable ? self::COMPILABLE_PLUGIN : 0;
469 23 : if (is_array($callback)) {
470 14 : if (is_subclass_of(is_object($callback[0]) ? get_class($callback[0]) : $callback[0], 'Dwoo_Block_Plugin')) {
471 1 : $this->plugins[$name] = array('type'=>self::BLOCK_PLUGIN | $compilable, 'callback'=>$callback, 'class'=>(is_object($callback[0]) ? get_class($callback[0]) : $callback[0]));
472 1 : } else {
473 13 : $this->plugins[$name] = array('type'=>self::CLASS_PLUGIN | $compilable, 'callback'=>$callback, 'class'=>(is_object($callback[0]) ? get_class($callback[0]) : $callback[0]), 'function'=>$callback[1]);
474 : }
475 23 : } elseif (class_exists($callback, false)) {
476 2 : if (is_subclass_of($callback, 'Dwoo_Block_Plugin')) {
477 1 : $this->plugins[$name] = array('type'=>self::BLOCK_PLUGIN | $compilable, 'callback'=>$callback, 'class'=>$callback);
478 1 : } else {
479 1 : $this->plugins[$name] = array('type'=>self::CLASS_PLUGIN | $compilable, 'callback'=>$callback, 'class'=>$callback, 'function'=>'process');
480 : }
481 9 : } elseif (function_exists($callback)) {
482 6 : $this->plugins[$name] = array('type'=>self::FUNC_PLUGIN | $compilable, 'callback'=>$callback);
483 6 : } else {
484 1 : throw new Dwoo_Exception('Callback could not be processed correctly, please check that the function/class you used exists');
485 : }
486 22 : }
487 :
488 : /**
489 : * removes a custom plugin
490 : *
491 : * @param string $name the plugin name
492 : */
493 : public function removePlugin($name)
494 : {
495 8 : if (isset($this->plugins[$name])) {
496 8 : unset($this->plugins[$name]);
497 8 : }
498 8 : }
499 :
500 : /**
501 : * adds a filter to this Dwoo instance, it will be used to filter the output of all the templates rendered by this instance
502 : *
503 : * @param mixed $callback a callback or a filter name if it is autoloaded from a plugin directory
504 : * @param bool $autoload if true, the first parameter must be a filter name from one of the plugin directories
505 : */
506 : public function addFilter($callback, $autoload = false)
507 : {
508 1 : if ($autoload) {
509 1 : $class = 'Dwoo_Filter_'.$callback;
510 :
511 1 : if (!class_exists($class, false) && !function_exists($class)) {
512 : try {
513 1 : $this->getLoader()->loadPlugin($callback);
514 1 : } catch (Dwoo_Exception $e) {
515 0 : if (strstr($callback, 'Dwoo_Filter_')) {
516 0 : throw new Dwoo_Exception('Wrong filter name : '.$callback.', the "Dwoo_Filter_" prefix should not be used, please only use "'.str_replace('Dwoo_Filter_', '', $callback).'"');
517 : } else {
518 0 : throw new Dwoo_Exception('Wrong filter name : '.$callback.', when using autoload the filter must be in one of your plugin dir as "name.php" containg a class or function named "Dwoo_Filter_name"');
519 : }
520 : }
521 1 : }
522 :
523 1 : if (class_exists($class, false)) {
524 1 : $callback = array(new $class($this), 'process');
525 1 : } elseif (function_exists($class)) {
526 0 : $callback = $class;
527 0 : } else {
528 0 : throw new Dwoo_Exception('Wrong filter name : '.$callback.', when using autoload the filter must be in one of your plugin dir as "name.php" containg a class or function named "Dwoo_Filter_name"');
529 : }
530 :
531 1 : $this->filters[] = $callback;
532 1 : } else {
533 0 : $this->filters[] = $callback;
534 : }
535 1 : }
536 :
537 : /**
538 : * removes a filter
539 : *
540 : * @param mixed $callback callback or filter name if it was autoloaded
541 : */
542 : public function removeFilter($callback)
543 : {
544 0 : if (($index = array_search('Dwoo_Filter_'.$callback, $this->filters, true)) !== false) {
545 0 : unset($this->filters[$index]);
546 0 : } elseif (($index = array_search($callback, $this->filters, true)) !== false) {
547 0 : unset($this->filters[$index]);
548 0 : } else {
549 0 : $class = 'Dwoo_Filter_' . $callback;
550 0 : foreach ($this->filters as $index=>$filter) {
551 0 : if (is_array($filter) && $filter[0] instanceof $class) {
552 0 : unset($this->filters[$index]);
553 0 : break;
554 : }
555 0 : }
556 : }
557 0 : }
558 :
559 : /**
560 : * adds a resource or overrides a default one
561 : *
562 : * @param string $name the resource name
563 : * @param string $class the resource class (which must implement Dwoo_ITemplate)
564 : * @param callback $compilerFactory the compiler factory callback, a function that must return a compiler instance used to compile this resource, if none is provided. by default it will produce a Dwoo_Compiler object
565 : */
566 : public function addResource($name, $class, $compilerFactory = null)
567 : {
568 1 : if (strlen($name) < 2) {
569 0 : throw new Dwoo_Exception('Resource names must be at least two-character long to avoid conflicts with Windows paths');
570 : }
571 :
572 1 : if (!class_exists($class)) {
573 0 : throw new Dwoo_Exception('Resource class does not exist');
574 : }
575 :
576 1 : $interfaces = class_implements($class);
577 1 : if (in_array('Dwoo_ITemplate', $interfaces) === false) {
578 0 : throw new Dwoo_Exception('Resource class must implement Dwoo_ITemplate');
579 : }
580 :
581 1 : $this->resources[$name] = array('class'=>$class, 'compiler'=>$compilerFactory);
582 1 : }
583 :
584 : /**
585 : * removes a custom resource
586 : *
587 : * @param string $name the resource name
588 : */
589 : public function removeResource($name)
590 : {
591 1 : unset($this->resources[$name]);
592 1 : if ($name==='file') {
593 1 : $this->resources['file'] = array('class'=>'Dwoo_Template_File', 'compiler'=>null);
594 1 : }
595 1 : }
596 :
597 : /*
598 : * --------- getters and setters ---------
599 : */
600 :
601 : /**
602 : * sets the loader object to use to load plugins
603 : *
604 : * @param Dwoo_ILoader $loader loader object
605 : */
606 : public function setLoader(Dwoo_ILoader $loader)
607 : {
608 3 : $this->loader = $loader;
609 3 : }
610 :
611 : /**
612 : * returns the current loader object or a default one if none is currently found
613 : *
614 : * @param Dwoo_ILoader
615 : */
616 : public function getLoader()
617 : {
618 69 : if ($this->loader === null) {
619 66 : $this->loader = new Dwoo_Loader($this->getCompileDir());
620 66 : }
621 :
622 69 : return $this->loader;
623 : }
624 :
625 : /**
626 : * returns the custom plugins loaded
627 : *
628 : * used by the Dwoo_ITemplate classes to pass the custom plugins to their Dwoo_ICompiler instance
629 : *
630 : * @return array
631 : */
632 : public function getCustomPlugins()
633 : {
634 219 : return $this->plugins;
635 : }
636 :
637 : /**
638 : * returns the cache directory with a trailing DIRECTORY_SEPARATOR
639 : *
640 : * @return string
641 : */
642 : public function getCacheDir()
643 : {
644 6 : if ($this->cacheDir === null) {
645 0 : $this->setCacheDir(dirname(__FILE__).DIRECTORY_SEPARATOR.'cache'.DIRECTORY_SEPARATOR);
646 0 : }
647 :
648 6 : return $this->cacheDir;
649 : }
650 :
651 : /**
652 : * sets the cache directory and automatically appends a DIRECTORY_SEPARATOR
653 : *
654 : * @param string $dir the cache directory
655 : */
656 : public function setCacheDir($dir)
657 : {
658 27 : $this->cacheDir = rtrim($dir, '/\\').DIRECTORY_SEPARATOR;
659 27 : if (is_writable($this->cacheDir) === false) {
660 0 : throw new Dwoo_Exception('The cache directory must be writable, chmod "'.$this->cacheDir.'" to make it writable');
661 : }
662 27 : }
663 :
664 : /**
665 : * returns the compile directory with a trailing DIRECTORY_SEPARATOR
666 : *
667 : * @return string
668 : */
669 : public function getCompileDir()
670 : {
671 221 : if ($this->compileDir === null) {
672 0 : $this->setCompileDir(dirname(__FILE__).DIRECTORY_SEPARATOR.'compiled'.DIRECTORY_SEPARATOR);
673 0 : }
674 :
675 221 : return $this->compileDir;
676 : }
677 :
678 : /**
679 : * sets the compile directory and automatically appends a DIRECTORY_SEPARATOR
680 : *
681 : * @param string $dir the compile directory
682 : */
683 : public function setCompileDir($dir)
684 : {
685 27 : $this->compileDir = rtrim($dir, '/\\').DIRECTORY_SEPARATOR;
686 27 : if (is_writable($this->compileDir) === false) {
687 0 : throw new Dwoo_Exception('The compile directory must be writable, chmod "'.$this->compileDir.'" to make it writable');
688 : }
689 27 : }
690 :
691 : /**
692 : * returns the default cache time that is used with templates that do not have a cache time set
693 : *
694 : * @return int the duration in seconds
695 : */
696 : public function getCacheTime()
697 : {
698 218 : return $this->cacheTime;
699 : }
700 :
701 : /**
702 : * sets the default cache time to use with templates that do not have a cache time set
703 : *
704 : * @param int $seconds the duration in seconds
705 : */
706 : public function setCacheTime($seconds)
707 : {
708 4 : $this->cacheTime = (int) $seconds;
709 4 : }
710 :
711 : /**
712 : * returns the character set used by the string manipulation plugins
713 : *
714 : * the charset is automatically lowercased
715 : *
716 : * @return string
717 : */
718 : public function getCharset()
719 : {
720 10 : return $this->charset;
721 : }
722 :
723 : /**
724 : * sets the character set used by the string manipulation plugins
725 : *
726 : * the charset will be automatically lowercased
727 : *
728 : * @param string $charset the character set
729 : */
730 : public function setCharset($charset)
731 : {
732 3 : $this->charset = strtolower((string) $charset);
733 3 : }
734 :
735 : /**
736 : * returns the current template being rendered, when applicable, or null
737 : *
738 : * @return Dwoo_ITemplate|null
739 : */
740 : public function getTemplate()
741 : {
742 16 : return $this->template;
743 : }
744 :
745 : /**
746 : * sets the current template being rendered
747 : *
748 : * @param Dwoo_ITemplate $tpl template object
749 : */
750 : public function setTemplate(Dwoo_ITemplate $tpl)
751 : {
752 0 : $this->template = $tpl;
753 0 : }
754 :
755 : /**
756 : * sets the default compiler factory function for the given resource name
757 : *
758 : * a compiler factory must return a Dwoo_ICompiler object pre-configured to fit your needs
759 : *
760 : * @param string $resourceName the resource name (i.e. file, string)
761 : * @param callback $compilerFactory the compiler factory callback
762 : */
763 : public function setDefaultCompilerFactory($resourceName, $compilerFactory)
764 : {
765 1 : $this->resources[$resourceName]['compiler'] = $compilerFactory;
766 1 : }
767 :
768 : /**
769 : * returns the default compiler factory function for the given resource name
770 : *
771 : * @param string $resourceName the resource name
772 : * @return callback the compiler factory callback
773 : */
774 : public function getDefaultCompilerFactory($resourceName)
775 : {
776 29 : return $this->resources[$resourceName]['compiler'];
777 : }
778 :
779 : /**
780 : * sets the security policy object to enforce some php security settings
781 : *
782 : * use this if untrusted persons can modify templates
783 : *
784 : * @param Dwoo_Security_Policy $policy the security policy object
785 : */
786 : public function setSecurityPolicy(Dwoo_Security_Policy $policy = null)
787 : {
788 1 : $this->securityPolicy = $policy;
789 1 : }
790 :
791 : /**
792 : * returns the current security policy object or null by default
793 : *
794 : * @return Dwoo_Security_Policy|null the security policy object if any
795 : */
796 : public function getSecurityPolicy()
797 : {
798 220 : return $this->securityPolicy;
799 : }
800 :
801 : /**
802 : * sets the object that must be used as a plugin proxy when plugin can't be found
803 : * by dwoo's loader
804 : *
805 : * @param Dwoo_IPluginProxy $pluginProxy the proxy object
806 : */
807 : public function setPluginProxy(Dwoo_IPluginProxy $pluginProxy) {
808 4 : $this->pluginProxy = $pluginProxy;
809 4 : }
810 :
811 : /**
812 : * returns the current plugin proxy object or null by default
813 : *
814 : * @param Dwoo_IPluginProxy|null the proxy object if any
815 : */
816 : public function getPluginProxy() {
817 4 : return $this->pluginProxy;
818 : }
819 :
820 : /*
821 : * --------- util functions ---------
822 : */
823 :
824 : /**
825 : * [util function] checks whether the given template is cached or not
826 : *
827 : * @param Dwoo_ITemplate $tpl the template object
828 : * @return bool
829 : */
830 : public function isCached(Dwoo_ITemplate $tpl)
831 : {
832 5 : return is_string($tpl->getCachedTemplate($this));
833 : }
834 :
835 : /**
836 : * [util function] clears the cached templates if they are older than the given time
837 : *
838 : * @param int $olderThan minimum time (in seconds) required for a cached template to be cleared
839 : * @return int the amount of templates cleared
840 : */
841 : public function clearCache($olderThan=-1)
842 : {
843 4 : $cacheDirs = new RecursiveDirectoryIterator($this->getCacheDir());
844 4 : $cache = new RecursiveIteratorIterator($cacheDirs);
845 4 : $expired = time() - $olderThan;
846 4 : $count = 0;
847 4 : foreach ($cache as $file) {
848 4 : if ($cache->isDot() || $cache->isDir() || substr($file, -5) !== '.html') {
849 4 : continue;
850 : }
851 4 : if ($cache->getCTime() < $expired) {
852 4 : $count += unlink((string) $file) ? 1 : 0;
853 4 : }
854 4 : }
855 4 : return $count;
856 : }
857 :
858 : /**
859 : * [util function] fetches a template object of the given resource
860 : *
861 : * @param string $resourceName the resource name (i.e. file, string)
862 : * @param string $resourceId the resource identifier (i.e. file path)
863 : * @param int $cacheTime the cache time setting for this resource
864 : * @param string $cacheId the unique cache identifier
865 : * @param string $compileId the unique compiler identifier
866 : * @return Dwoo_ITemplate
867 : */
868 : public function templateFactory($resourceName, $resourceId, $cacheTime = null, $cacheId = null, $compileId = null, Dwoo_ITemplate $parentTemplate = null)
869 : {
870 11 : if (isset($this->resources[$resourceName])) {
871 : // TODO could be changed to $this->resources[$resourceName]['class']::templateFactory(..) in 5.3 maybe
872 10 : return call_user_func(array($this->resources[$resourceName]['class'], 'templateFactory'), $this, $resourceId, $cacheTime, $cacheId, $compileId, $parentTemplate);
873 : } else {
874 1 : throw new Dwoo_Exception('Unknown resource type : '.$resourceName);
875 : }
876 : }
877 :
878 : /**
879 : * [util function] checks if the input is an array or arrayaccess object, optionally it can also check if it's empty
880 : *
881 : * @param mixed $value the variable to check
882 : * @param bool $checkIsEmpty if true, the function will also check if the array|arrayaccess is empty,
883 : * and return true only if it's not empty
884 : * @return int|bool true if it's an array|arrayaccess (or the item count if $checkIsEmpty is true) or false if it's not an array|arrayaccess (or 0 if $checkIsEmpty is true)
885 : */
886 : public function isArray($value, $checkIsEmpty=false)
887 : {
888 7 : if (is_array($value) === true || $value instanceof ArrayAccess) {
889 5 : if ($checkIsEmpty === false) {
890 4 : return true;
891 : } else {
892 5 : return $this->count($value);
893 : }
894 : }
895 3 : }
896 :
897 : /**
898 : * [util function] checks if the input is an array or a traversable object, optionally it can also check if it's empty
899 : *
900 : * @param mixed $value the variable to check
901 : * @param bool $checkIsEmpty if true, the function will also check if the array|traversable is empty,
902 : * and return true only if it's not empty
903 : * @return int|bool true if it's an array|traversable (or the item count if $checkIsEmpty is true) or false if it's not an array|traversable (or 0 if $checkIsEmpty is true)
904 : */
905 : public function isTraversable($value, $checkIsEmpty=false)
906 : {
907 27 : if (is_array($value) === true) {
908 23 : if ($checkIsEmpty === false) {
909 22 : return true;
910 : } else {
911 2 : return count($value) > 0;
912 : }
913 4 : } elseif ($value instanceof Traversable) {
914 2 : if ($checkIsEmpty === false) {
915 2 : return true;
916 : } else {
917 2 : return $this->count($value);
918 : }
919 : }
920 2 : return false;
921 : }
922 :
923 : /**
924 : * [util function] counts an array or arrayaccess/traversable object
925 : * @param mixed $value
926 : * @return int|bool the count for arrays and objects that implement countable, true for other objects that don't, and 0 for empty elements
927 : */
928 : public function count($value)
929 : {
930 11 : if (is_array($value) === true || $value instanceof Countable) {
931 9 : return count($value);
932 2 : } elseif ($value instanceof ArrayAccess) {
933 1 : if ($value->offsetExists(0)) {
934 1 : return true;
935 : }
936 2 : } elseif ($value instanceof Iterator) {
937 1 : $value->rewind();
938 1 : if ($value->valid()) {
939 1 : return true;
940 : }
941 1 : } elseif ($value instanceof Traversable) {
942 0 : foreach ($value as $dummy) {
943 0 : return true;
944 0 : }
945 0 : }
946 2 : return 0;
947 : }
948 :
949 : /**
950 : * [util function] triggers a dwoo error
951 : *
952 : * @param string $message the error message
953 : * @param int $level the error level, one of the PHP's E_* constants
954 : */
955 : public function triggerError($message, $level=E_USER_NOTICE)
956 : {
957 0 : if (!($tplIdentifier = $this->template->getResourceIdentifier())) {
958 0 : $tplIdentifier = $this->template->getResourceName();
959 0 : }
960 0 : trigger_error('Dwoo error (in '.$tplIdentifier.') : '.$message, $level);
961 0 : }
962 :
963 : /*
964 : * --------- runtime functions ---------
965 : */
966 :
967 : /**
968 : * [runtime function] adds a block to the block stack
969 : *
970 : * @param string $blockName the block name (without Dwoo_Plugin_ prefix)
971 : * @param array $args the arguments to be passed to the block's init() function
972 : * @return Dwoo_Block_Plugin the newly created block
973 : */
974 : public function addStack($blockName, array $args=array())
975 : {
976 4 : if (isset($this->plugins[$blockName])) {
977 2 : $class = $this->plugins[$blockName]['class'];
978 2 : } else {
979 2 : $class = 'Dwoo_Plugin_'.$blockName;
980 : }
981 :
982 4 : if ($this->curBlock !== null) {
983 1 : $this->curBlock->buffer(ob_get_contents());
984 1 : ob_clean();
985 1 : } else {
986 4 : $this->buffer .= ob_get_contents();
987 4 : ob_clean();
988 : }
989 :
990 4 : $block = new $class($this);
991 :
992 4 : $cnt = count($args);
993 4 : if ($cnt===0) {
994 0 : $block->init();
995 4 : } elseif ($cnt===1) {
996 0 : $block->init($args[0]);
997 4 : } elseif ($cnt===2) {
998 2 : $block->init($args[0], $args[1]);
999 4 : } elseif ($cnt===3) {
1000 0 : $block->init($args[0], $args[1], $args[2]);
1001 2 : } elseif ($cnt===4) {
1002 0 : $block->init($args[0], $args[1], $args[2], $args[3]);
1003 0 : } else {
1004 2 : call_user_func_array(array($block,'init'), $args);
1005 : }
1006 :
1007 4 : $this->stack[] = $this->curBlock = $block;
1008 4 : return $block;
1009 : }
1010 :
1011 : /**
1012 : * [runtime function] removes the plugin at the top of the block stack
1013 : *
1014 : * calls the block buffer() function, followed by a call to end()
1015 : * and finally a call to process()
1016 : */
1017 : public function delStack()
1018 : {
1019 4 : $args = func_get_args();
1020 :
1021 4 : $this->curBlock->buffer(ob_get_contents());
1022 4 : ob_clean();
1023 :
1024 4 : $cnt = count($args);
1025 4 : if ($cnt===0) {
1026 4 : $this->curBlock->end();
1027 4 : } elseif ($cnt===1) {
1028 0 : $this->curBlock->end($args[0]);
1029 0 : } elseif ($cnt===2) {
1030 0 : $this->curBlock->end($args[0], $args[1]);
1031 0 : } elseif ($cnt===3) {
1032 0 : $this->curBlock->end($args[0], $args[1], $args[2]);
1033 0 : } elseif ($cnt===4) {
1034 0 : $this->curBlock->end($args[0], $args[1], $args[2], $args[3]);
1035 0 : } else {
1036 0 : call_user_func_array(array($this->curBlock, 'end'), $args);
1037 : }
1038 :
1039 4 : $tmp = array_pop($this->stack);
1040 :
1041 4 : if (count($this->stack) > 0) {
1042 1 : $this->curBlock = end($this->stack);
1043 1 : $this->curBlock->buffer($tmp->process());
1044 1 : } else {
1045 4 : $this->curBlock = null;
1046 4 : echo $tmp->process();
1047 : }
1048 :
1049 4 : unset($tmp);
1050 4 : }
1051 :
1052 : /**
1053 : * [runtime function] returns the parent block of the given block
1054 : *
1055 : * @param Dwoo_Block_Plugin $block
1056 : * @return Dwoo_Block_Plugin or false if the given block isn't in the stack
1057 : */
1058 : public function getParentBlock(Dwoo_Block_Plugin $block)
1059 : {
1060 0 : $index = array_search($block, $this->stack, true);
1061 0 : if ($index !== false && $index > 0) {
1062 0 : return $this->stack[$index-1];
1063 : }
1064 0 : return false;
1065 : }
1066 :
1067 : /**
1068 : * [runtime function] finds the closest block of the given type, starting at the top of the stack
1069 : *
1070 : * @param string $type the type of plugin you want to find
1071 : * @return Dwoo_Block_Plugin or false if no plugin of such type is in the stack
1072 : */
1073 : public function findBlock($type)
1074 : {
1075 0 : if (isset($this->plugins[$type])) {
1076 0 : $type = $this->plugins[$type]['class'];
1077 0 : } else {
1078 0 : $type = 'Dwoo_Plugin_'.str_replace('Dwoo_Plugin_', '', $type);
1079 : }
1080 :
1081 0 : $keys = array_keys($this->stack);
1082 0 : while (($key = array_pop($keys)) !== false) {
1083 0 : if ($this->stack[$key] instanceof $type) {
1084 0 : return $this->stack[$key];
1085 : }
1086 0 : }
1087 0 : return false;
1088 : }
1089 :
1090 : /**
1091 : * [runtime function] returns a Dwoo_Plugin of the given class
1092 : *
1093 : * this is so a single instance of every class plugin is created at each template run,
1094 : * allowing class plugins to have "per-template-run" static variables
1095 : *
1096 : * @private
1097 : * @param string $class the class name
1098 : * @return mixed an object of the given class
1099 : */
1100 : public function getObjectPlugin($class)
1101 : {
1102 9 : if (isset($this->runtimePlugins[$class])) {
1103 3 : return $this->runtimePlugins[$class];
1104 : }
1105 9 : return $this->runtimePlugins[$class] = new $class($this);
1106 : }
1107 :
1108 : /**
1109 : * [runtime function] calls the process() method of the given class-plugin name
1110 : *
1111 : * @param string $plugName the class plugin name (without Dwoo_Plugin_ prefix)
1112 : * @param array $params an array of parameters to send to the process() method
1113 : * @return string the process() return value
1114 : */
1115 : public function classCall($plugName, array $params = array())
1116 : {
1117 6 : $class = 'Dwoo_Plugin_'.$plugName;
1118 :
1119 6 : $plugin = $this->getObjectPlugin($class);
1120 :
1121 6 : $cnt = count($params);
1122 6 : if ($cnt===0) {
1123 0 : return $plugin->process();
1124 6 : } elseif ($cnt===1) {
1125 0 : return $plugin->process($params[0]);
1126 6 : } elseif ($cnt===2) {
1127 4 : return $plugin->process($params[0], $params[1]);
1128 2 : } elseif ($cnt===3) {
1129 0 : return $plugin->process($params[0], $params[1], $params[2]);
1130 2 : } elseif ($cnt===4) {
1131 0 : return $plugin->process($params[0], $params[1], $params[2], $params[3]);
1132 : } else {
1133 2 : return call_user_func_array(array($plugin, 'process'), $params);
1134 : }
1135 : }
1136 :
1137 : /**
1138 : * [runtime function] calls a php function
1139 : *
1140 : * @param string $callback the function to call
1141 : * @param array $params an array of parameters to send to the function
1142 : * @return mixed the return value of the called function
1143 : */
1144 : public function arrayMap($callback, array $params)
1145 : {
1146 2 : if ($params[0] === $this) {
1147 1 : $addThis = true;
1148 1 : array_shift($params);
1149 1 : }
1150 2 : if ((is_array($params[0]) || ($params[0] instanceof Iterator && $params[0] instanceof ArrayAccess))) {
1151 2 : if (empty($params[0])) {
1152 0 : return $params[0];
1153 : }
1154 :
1155 : // array map
1156 2 : $out = array();
1157 2 : $cnt = count($params);
1158 :
1159 2 : if (isset($addThis)) {
1160 1 : array_unshift($params, $this);
1161 1 : $items = $params[1];
1162 1 : $keys = array_keys($items);
1163 :
1164 1 : if (is_string($callback) === false) {
1165 0 : while (($i = array_shift($keys)) !== null) {
1166 0 : $out[] = call_user_func_array($callback, array(1=>$items[$i]) + $params);
1167 0 : }
1168 1 : } elseif ($cnt===1) {
1169 0 : while (($i = array_shift($keys)) !== null) {
1170 0 : $out[] = $callback($this, $items[$i]);
1171 0 : }
1172 1 : } elseif ($cnt===2) {
1173 1 : while (($i = array_shift($keys)) !== null) {
1174 1 : $out[] = $callback($this, $items[$i], $params[2]);
1175 1 : }
1176 1 : } elseif ($cnt===3) {
1177 0 : while (($i = array_shift($keys)) !== null) {
1178 0 : $out[] = $callback($this, $items[$i], $params[2], $params[3]);
1179 0 : }
1180 0 : } else {
1181 0 : while (($i = array_shift($keys)) !== null) {
1182 0 : $out[] = call_user_func_array($callback, array(1=>$items[$i]) + $params);
1183 0 : }
1184 : }
1185 1 : } else {
1186 1 : $items = $params[0];
1187 1 : $keys = array_keys($items);
1188 :
1189 1 : if (is_string($callback) === false) {
1190 0 : while (($i = array_shift($keys)) !== null) {
1191 0 : $out[] = call_user_func_array($callback, array($items[$i]) + $params);
1192 0 : }
1193 1 : } elseif ($cnt===1) {
1194 1 : while (($i = array_shift($keys)) !== null) {
1195 1 : $out[] = $callback($items[$i]);
1196 1 : }
1197 1 : } elseif ($cnt===2) {
1198 0 : while (($i = array_shift($keys)) !== null) {
1199 0 : $out[] = $callback($items[$i], $params[1]);
1200 0 : }
1201 0 : } elseif ($cnt===3) {
1202 0 : while (($i = array_shift($keys)) !== null) {
1203 0 : $out[] = $callback($items[$i], $params[1], $params[2]);
1204 0 : }
1205 0 : } elseif ($cnt===4) {
1206 0 : while (($i = array_shift($keys)) !== null) {
1207 0 : $out[] = $callback($items[$i], $params[1], $params[2], $params[3]);
1208 0 : }
1209 0 : } else {
1210 0 : while (($i = array_shift($keys)) !== null) {
1211 0 : $out[] = call_user_func_array($callback, array($items[$i]) + $params);
1212 0 : }
1213 : }
1214 : }
1215 2 : return $out;
1216 : } else {
1217 0 : return $params[0];
1218 : }
1219 : }
1220 :
1221 : /**
1222 : * [runtime function] reads a variable into the given data array
1223 : *
1224 : * @param string $varstr the variable string, using dwoo variable syntax (i.e. "var.subvar[subsubvar]->property")
1225 : * @param mixed $data the data array or object to read from
1226 : * @param bool $safeRead if true, the function will check whether the index exists to prevent any notices from being output
1227 : * @return mixed
1228 : */
1229 : public function readVarInto($varstr, $data, $safeRead = false)
1230 : {
1231 4 : if ($data === null) {
1232 0 : return null;
1233 : }
1234 :
1235 4 : if (is_array($varstr) === false) {
1236 1 : preg_match_all('#(\[|->|\.)?((?:[^.[\]-]|-(?!>))+)\]?#i', $varstr, $m);
1237 1 : } else {
1238 4 : $m = $varstr;
1239 : }
1240 4 : unset($varstr);
1241 :
1242 4 : while (list($k, $sep) = each($m[1])) {
1243 4 : if ($sep === '.' || $sep === '[' || $sep === '') {
1244 3 : if ((is_array($data) || $data instanceof ArrayAccess) && ($safeRead === false || isset($data[$m[2][$k]]))) {
1245 3 : $data = $data[$m[2][$k]];
1246 3 : } else {
1247 0 : return null;
1248 : }
1249 3 : } else {
1250 3 : if (is_object($data) && ($safeRead === false || isset($data->$m[2][$k]) || is_callable(array($data, '__get')))) {
1251 3 : $data = $data->$m[2][$k];
1252 3 : } else {
1253 0 : return null;
1254 : }
1255 : }
1256 4 : }
1257 :
1258 4 : return $data;
1259 : }
1260 :
1261 : /**
1262 : * [runtime function] reads a variable into the parent scope
1263 : *
1264 : * @param int $parentLevels the amount of parent levels to go from the current scope
1265 : * @param string $varstr the variable string, using dwoo variable syntax (i.e. "var.subvar[subsubvar]->property")
1266 : * @return mixed
1267 : */
1268 : public function readParentVar($parentLevels, $varstr = null)
1269 : {
1270 2 : $tree = $this->scopeTree;
1271 2 : $cur = $this->data;
1272 :
1273 2 : while ($parentLevels--!==0) {
1274 2 : array_pop($tree);
1275 2 : }
1276 :
1277 2 : while (($i = array_shift($tree)) !== null) {
1278 0 : if (is_object($cur)) {
1279 0 : $cur = $cur->$i;
1280 0 : } else {
1281 0 : $cur = $cur[$i];
1282 : }
1283 0 : }
1284 :
1285 2 : if ($varstr!==null) {
1286 1 : return $this->readVarInto($varstr, $cur);
1287 : } else {
1288 1 : return $cur;
1289 : }
1290 : }
1291 :
1292 : /**
1293 : * [runtime function] reads a variable into the current scope
1294 : *
1295 : * @param string $varstr the variable string, using dwoo variable syntax (i.e. "var.subvar[subsubvar]->property")
1296 : * @return mixed
1297 : */
1298 : public function readVar($varstr)
1299 : {
1300 14 : if (is_array($varstr)===true) {
1301 0 : $m = $varstr;
1302 0 : unset($varstr);
1303 0 : } else {
1304 14 : if (strstr($varstr, '.') === false && strstr($varstr, '[') === false && strstr($varstr, '->') === false) {
1305 6 : if ($varstr === 'dwoo') {
1306 0 : return $this->globals;
1307 6 : } elseif ($varstr === '__' || $varstr === '_root' ) {
1308 5 : return $this->data;
1309 : $varstr = substr($varstr, 6);
1310 2 : } elseif ($varstr === '_' || $varstr === '_parent') {
1311 2 : $varstr = '.'.$varstr;
1312 2 : $tree = $this->scopeTree;
1313 2 : $cur = $this->data;
1314 2 : array_pop($tree);
1315 :
1316 2 : while (($i = array_shift($tree)) !== null) {
1317 0 : if (is_object($cur)) {
1318 0 : $cur = $cur->$i;
1319 0 : } else {
1320 0 : $cur = $cur[$i];
1321 : }
1322 0 : }
1323 :
1324 2 : return $cur;
1325 : }
1326 :
1327 1 : $cur = $this->scope;
1328 :
1329 1 : if (isset($cur[$varstr])) {
1330 1 : return $cur[$varstr];
1331 : } else {
1332 0 : return null;
1333 : }
1334 : }
1335 :
1336 9 : if (substr($varstr, 0, 1) === '.') {
1337 1 : $varstr = 'dwoo'.$varstr;
1338 1 : }
1339 :
1340 9 : preg_match_all('#(\[|->|\.)?((?:[^.[\]-]|-(?!>))+)\]?#i', $varstr, $m);
1341 : }
1342 :
1343 9 : $i = $m[2][0];
1344 9 : if ($i === 'dwoo') {
1345 3 : $cur = $this->globals;
1346 3 : array_shift($m[2]);
1347 3 : array_shift($m[1]);
1348 3 : switch ($m[2][0]) {
1349 :
1350 3 : case 'get':
1351 1 : $cur = $_GET;
1352 1 : break;
1353 2 : case 'post':
1354 0 : $cur = $_POST;
1355 0 : break;
1356 2 : case 'session':
1357 0 : $cur = $_SESSION;
1358 0 : break;
1359 2 : case 'cookies':
1360 2 : case 'cookie':
1361 0 : $cur = $_COOKIE;
1362 0 : break;
1363 2 : case 'server':
1364 0 : $cur = $_SERVER;
1365 0 : break;
1366 2 : case 'env':
1367 0 : $cur = $_ENV;
1368 0 : break;
1369 2 : case 'request':
1370 0 : $cur = $_REQUEST;
1371 0 : break;
1372 2 : case 'const':
1373 0 : array_shift($m[2]);
1374 0 : if (defined($m[2][0])) {
1375 0 : return constant($m[2][0]);
1376 : } else {
1377 0 : return null;
1378 : }
1379 :
1380 3 : }
1381 3 : if ($cur !== $this->globals) {
1382 1 : array_shift($m[2]);
1383 1 : array_shift($m[1]);
1384 1 : }
1385 9 : } elseif ($i === '__' || $i === '_root') {
1386 2 : $cur = $this->data;
1387 2 : array_shift($m[2]);
1388 2 : array_shift($m[1]);
1389 6 : } elseif ($i === '_' || $i === '_parent') {
1390 2 : $tree = $this->scopeTree;
1391 2 : $cur = $this->data;
1392 :
1393 2 : while (true) {
1394 2 : array_pop($tree);
1395 2 : array_shift($m[2]);
1396 2 : array_shift($m[1]);
1397 2 : if (current($m[2]) === '_' || current($m[2]) === '_parent') {
1398 0 : continue;
1399 : }
1400 :
1401 2 : while (($i = array_shift($tree)) !== null) {
1402 0 : if (is_object($cur)) {
1403 0 : $cur = $cur->$i;
1404 0 : } else {
1405 0 : $cur = $cur[$i];
1406 : }
1407 0 : }
1408 2 : break;
1409 : }
1410 2 : } else {
1411 6 : $cur = $this->scope;
1412 : }
1413 :
1414 9 : while (list($k, $sep) = each($m[1])) {
1415 9 : if ($sep === '.' || $sep === '[' || $sep === '') {
1416 9 : if ((is_array($cur) || $cur instanceof ArrayAccess) && isset($cur[$m[2][$k]])) {
1417 9 : $cur = $cur[$m[2][$k]];
1418 9 : } else {
1419 0 : return null;
1420 : }
1421 9 : } elseif ($sep === '->') {
1422 1 : if (is_object($cur)) {
1423 1 : $cur = $cur->$m[2][$k];
1424 1 : } else {
1425 0 : return null;
1426 : }
1427 1 : } else {
1428 0 : return null;
1429 : }
1430 9 : }
1431 :
1432 9 : return $cur;
1433 : }
1434 :
1435 : /**
1436 : * [runtime function] assign the value to the given variable
1437 : *
1438 : * @param mixed $value the value to assign
1439 : * @param string $scope the variable string, using dwoo variable syntax (i.e. "var.subvar[subsubvar]->property")
1440 : * @return bool true if assigned correctly or false if a problem occured while parsing the var string
1441 : */
1442 : public function assignInScope($value, $scope)
1443 : {
1444 13 : $tree =& $this->scopeTree;
1445 13 : $data =& $this->data;
1446 :
1447 13 : if (!is_string($scope)) {
1448 0 : return $this->triggerError('Assignments must be done into strings, ('.gettype($scope).') '.var_export($scope, true).' given', E_USER_ERROR);
1449 : }
1450 13 : if (strstr($scope, '.') === false && strstr($scope, '->') === false) {
1451 11 : $this->scope[$scope] = $value;
1452 11 : } else {
1453 : // TODO handle _root/_parent scopes ?
1454 2 : preg_match_all('#(\[|->|\.)?([^.[\]-]+)\]?#i', $scope, $m);
1455 :
1456 2 : $cur =& $this->scope;
1457 2 : $last = array(array_pop($m[1]), array_pop($m[2]));
1458 :
1459 2 : while (list($k, $sep) = each($m[1])) {
1460 2 : if ($sep === '.' || $sep === '[' || $sep === '') {
1461 2 : if (is_array($cur) === false) {
1462 1 : $cur = array();
1463 1 : }
1464 2 : $cur =& $cur[$m[2][$k]];
1465 2 : } elseif ($sep === '->') {
1466 1 : if (is_object($cur) === false) {
1467 0 : $cur = new stdClass;
1468 0 : }
1469 1 : $cur =& $cur->$m[2][$k];
1470 1 : } else {
1471 0 : return false;
1472 : }
1473 2 : }
1474 :
1475 2 : if ($last[0] === '.' || $last[0] === '[' || $last[0] === '') {
1476 1 : if (is_array($cur) === false) {
1477 0 : $cur = array();
1478 0 : }
1479 1 : $cur[$last[1]] = $value;
1480 2 : } elseif ($last[0] === '->') {
1481 2 : if (is_object($cur) === false) {
1482 1 : $cur = new stdClass;
1483 1 : }
1484 2 : $cur->$last[1] = $value;
1485 2 : } else {
1486 0 : return false;
1487 : }
1488 : }
1489 13 : }
1490 :
1491 : /**
1492 : * [runtime function] sets the scope to the given scope string or array
1493 : *
1494 : * @param mixed $scope a string i.e. "level1.level2" or an array i.e. array("level1", "level2")
1495 : * @param bool $absolute if true, the scope is set from the top level scope and not from the current scope
1496 : * @return array the current scope tree
1497 : */
1498 : public function setScope($scope, $absolute = false)
1499 : {
1500 11 : $old = $this->scopeTree;
1501 :
1502 11 : if (is_string($scope)===true) {
1503 4 : $scope = explode('.', $scope);
1504 4 : }
1505 :
1506 11 : if ($absolute===true) {
1507 11 : $this->scope =& $this->data;
1508 11 : $this->scopeTree = array();
1509 11 : }
1510 :
1511 11 : while (($bit = array_shift($scope)) !== null) {
1512 11 : if ($bit === '_' || $bit === '_parent') {
1513 0 : array_pop($this->scopeTree);
1514 0 : $this->scope =& $this->data;
1515 0 : $cnt = count($this->scopeTree);
1516 0 : for ($i=0;$i<$cnt;$i++)
1517 0 : $this->scope =& $this->scope[$this->scopeTree[$i]];
1518 11 : } elseif ($bit === '__' || $bit === '_root') {
1519 0 : $this->scope =& $this->data;
1520 0 : $this->scopeTree = array();
1521 11 : } elseif (isset($this->scope[$bit])) {
1522 11 : $this->scope =& $this->scope[$bit];
1523 11 : $this->scopeTree[] = $bit;
1524 11 : } else {
1525 0 : unset($this->scope);
1526 0 : $this->scope = null;
1527 : }
1528 11 : }
1529 :
1530 11 : return $old;
1531 : }
1532 :
1533 : /**
1534 : * [runtime function] returns the entire data array
1535 : *
1536 : * @return array
1537 : */
1538 : public function getData()
1539 : {
1540 1 : return $this->data;
1541 : }
1542 :
1543 : /**
1544 : * [runtime function] returns a reference to the current scope
1545 : *
1546 : * @return &mixed
1547 : */
1548 : public function &getScope()
1549 : {
1550 1 : return $this->scope;
1551 : }
1552 :
1553 : /**
1554 : * Redirects all calls to unexisting to plugin proxy.
1555 : *
1556 : * @param string Method name
1557 : * @param array List of arguments
1558 : * @return mixed
1559 : */
1560 : public function __call($method, $args) {
1561 0 : $proxy = $this->getPluginProxy();
1562 0 : if (!$proxy) {
1563 0 : throw new Dwoo_Exception('Call to undefined method '.__CLASS__.'::'.$method.'()');
1564 : }
1565 0 : return call_user_func_array($proxy->getCallback($method), $args);
1566 : }
1567 : }
|