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