Source for file Dwoo.php

Documentation is available at Dwoo.php

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

Documentation generated on Wed, 24 Dec 2008 02:13:26 +0100 by phpDocumentor 1.4.0