@@ -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 | } |
@@ -1538,8 +1538,9 @@ |
||
| 1538 | 1538 | array_pop($this->scopeTree); |
| 1539 | 1539 | $this->scope =& $this->data; |
| 1540 | 1540 | $cnt = count($this->scopeTree); |
| 1541 | - for ($i=0;$i<$cnt;$i++) |
|
| 1542 | - $this->scope =& $this->scope[$this->scopeTree[$i]]; |
|
| 1541 | + for ($i=0;$i<$cnt;$i++) { |
|
| 1542 | + $this->scope =& $this->scope[$this->scopeTree[$i]]; |
|
| 1543 | + } |
|
| 1543 | 1544 | } elseif ($bit === '__' || $bit === '_root') { |
| 1544 | 1545 | $this->scope =& $this->data; |
| 1545 | 1546 | $this->scopeTree = array(); |
@@ -297,9 +297,10 @@ discard block |
||
| 297 | 297 | |
| 298 | 298 | public function unregister_prefilter($callback) |
| 299 | 299 | { |
| 300 | - foreach ($this->_filters['pre'] as $index => $processor) |
|
| 301 | - if ($processor->callback === $callback) { |
|
| 300 | + foreach ($this->_filters['pre'] as $index => $processor) { |
|
| 301 | + if ($processor->callback === $callback) { |
|
| 302 | 302 | $this->compiler->removePostProcessor($processor); |
| 303 | + } |
|
| 303 | 304 | unset($this->_filters['pre'][$index]); |
| 304 | 305 | } |
| 305 | 306 | } |
@@ -314,9 +315,10 @@ discard block |
||
| 314 | 315 | |
| 315 | 316 | public function unregister_postfilter($callback) |
| 316 | 317 | { |
| 317 | - foreach ($this->_filters['post'] as $index => $processor) |
|
| 318 | - if ($processor->callback === $callback) { |
|
| 318 | + foreach ($this->_filters['post'] as $index => $processor) { |
|
| 319 | + if ($processor->callback === $callback) { |
|
| 319 | 320 | $this->compiler->removePostProcessor($processor); |
| 321 | + } |
|
| 320 | 322 | unset($this->_filters['post'][$index]); |
| 321 | 323 | } |
| 322 | 324 | } |
@@ -331,9 +333,10 @@ discard block |
||
| 331 | 333 | |
| 332 | 334 | public function unregister_outputfilter($callback) |
| 333 | 335 | { |
| 334 | - foreach ($this->_filters['output'] as $index => $filter) |
|
| 335 | - if ($filter->callback === $callback) { |
|
| 336 | + foreach ($this->_filters['output'] as $index => $filter) { |
|
| 337 | + if ($filter->callback === $callback) { |
|
| 336 | 338 | $this->removeOutputFilter($filter); |
| 339 | + } |
|
| 337 | 340 | unset($this->_filters['output'][$index]); |
| 338 | 341 | } |
| 339 | 342 | } |
@@ -419,10 +422,11 @@ discard block |
||
| 419 | 422 | } |
| 420 | 423 | |
| 421 | 424 | $data = $this->dataProvider->getData(); |
| 422 | - if ($name === null) |
|
| 423 | - return $data; |
|
| 424 | - elseif (isset($data[$name])) |
|
| 425 | - return $data[$name]; |
|
| 425 | + if ($name === null) { |
|
| 426 | + return $data; |
|
| 427 | + } elseif (isset($data[$name])) { |
|
| 428 | + return $data[$name]; |
|
| 429 | + } |
|
| 426 | 430 | return null; |
| 427 | 431 | } |
| 428 | 432 | |
@@ -450,8 +454,9 @@ discard block |
||
| 450 | 454 | |
| 451 | 455 | protected function makeTemplate($file, $cacheId, $compileId) |
| 452 | 456 | { |
| 453 | - if ($compileId === null) |
|
| 454 | - $compileId = $this->compile_id; |
|
| 457 | + if ($compileId === null) { |
|
| 458 | + $compileId = $this->compile_id; |
|
| 459 | + } |
|
| 455 | 460 | |
| 456 | 461 | $hash = bin2hex(md5($file.$cacheId.$compileId, true)); |
| 457 | 462 | if (!isset(self::$tplCache[$hash])) { |
@@ -511,8 +516,7 @@ discard block |
||
| 511 | 516 | { |
| 512 | 517 | interface Smarty {} |
| 513 | 518 | class Dwoo_Smarty_Adapter extends Dwoo_Smarty__Adapter implements Smarty {} |
| 514 | -} |
|
| 515 | -else |
|
| 519 | +} else |
|
| 516 | 520 | { |
| 517 | 521 | class Dwoo_Smarty_Adapter extends Dwoo_Smarty__Adapter {} |
| 518 | 522 | } |
@@ -100,11 +100,12 @@ discard block |
||
| 100 | 100 | */ |
| 101 | 101 | public function allowPhpFunction($func) |
| 102 | 102 | { |
| 103 | - if (is_array($func)) |
|
| 104 | - foreach ($func as $fname) |
|
| 103 | + if (is_array($func)) { |
|
| 104 | + foreach ($func as $fname) |
|
| 105 | 105 | $this->allowedPhpFunctions[strtolower($fname)] = true; |
| 106 | - else |
|
| 107 | - $this->allowedPhpFunctions[strtolower($func)] = true; |
|
| 106 | + } else { |
|
| 107 | + $this->allowedPhpFunctions[strtolower($func)] = true; |
|
| 108 | + } |
|
| 108 | 109 | } |
| 109 | 110 | |
| 110 | 111 | /** |
@@ -114,11 +115,12 @@ discard block |
||
| 114 | 115 | */ |
| 115 | 116 | public function disallowPhpFunction($func) |
| 116 | 117 | { |
| 117 | - if (is_array($func)) |
|
| 118 | - foreach ($func as $fname) |
|
| 118 | + if (is_array($func)) { |
|
| 119 | + foreach ($func as $fname) |
|
| 119 | 120 | unset($this->allowedPhpFunctions[strtolower($fname)]); |
| 120 | - else |
|
| 121 | - unset($this->allowedPhpFunctions[strtolower($func)]); |
|
| 121 | + } else { |
|
| 122 | + unset($this->allowedPhpFunctions[strtolower($func)]); |
|
| 123 | + } |
|
| 122 | 124 | } |
| 123 | 125 | |
| 124 | 126 | /** |
@@ -142,11 +144,12 @@ discard block |
||
| 142 | 144 | */ |
| 143 | 145 | public function allowMethod($class, $method = null) |
| 144 | 146 | { |
| 145 | - if (is_array($class)) |
|
| 146 | - foreach ($class as $elem) |
|
| 147 | + if (is_array($class)) { |
|
| 148 | + foreach ($class as $elem) |
|
| 147 | 149 | $this->allowedMethods[strtolower($elem[0])][strtolower($elem[1])] = true; |
| 148 | - else |
|
| 149 | - $this->allowedMethods[strtolower($class)][strtolower($method)] = true; |
|
| 150 | + } else { |
|
| 151 | + $this->allowedMethods[strtolower($class)][strtolower($method)] = true; |
|
| 152 | + } |
|
| 150 | 153 | } |
| 151 | 154 | |
| 152 | 155 | /** |
@@ -157,11 +160,12 @@ discard block |
||
| 157 | 160 | */ |
| 158 | 161 | public function disallowMethod($class, $method = null) |
| 159 | 162 | { |
| 160 | - if (is_array($class)) |
|
| 161 | - foreach ($class as $elem) |
|
| 163 | + if (is_array($class)) { |
|
| 164 | + foreach ($class as $elem) |
|
| 162 | 165 | unset($this->allowedMethods[strtolower($elem[0])][strtolower($elem[1])]); |
| 163 | - else |
|
| 164 | - unset($this->allowedMethods[strtolower($class)][strtolower($method)]); |
|
| 166 | + } else { |
|
| 167 | + unset($this->allowedMethods[strtolower($class)][strtolower($method)]); |
|
| 168 | + } |
|
| 165 | 169 | } |
| 166 | 170 | |
| 167 | 171 | /** |
@@ -185,11 +189,12 @@ discard block |
||
| 185 | 189 | */ |
| 186 | 190 | public function allowDirectory($path) |
| 187 | 191 | { |
| 188 | - if (is_array($path)) |
|
| 189 | - foreach ($path as $dir) |
|
| 192 | + if (is_array($path)) { |
|
| 193 | + foreach ($path as $dir) |
|
| 190 | 194 | $this->allowedDirectories[realpath($dir)] = true; |
| 191 | - else |
|
| 192 | - $this->allowedDirectories[realpath($path)] = true; |
|
| 195 | + } else { |
|
| 196 | + $this->allowedDirectories[realpath($path)] = true; |
|
| 197 | + } |
|
| 193 | 198 | } |
| 194 | 199 | |
| 195 | 200 | /** |
@@ -199,11 +204,12 @@ discard block |
||
| 199 | 204 | */ |
| 200 | 205 | public function disallowDirectory($path) |
| 201 | 206 | { |
| 202 | - if (is_array($path)) |
|
| 203 | - foreach ($path as $dir) |
|
| 207 | + if (is_array($path)) { |
|
| 208 | + foreach ($path as $dir) |
|
| 204 | 209 | unset($this->allowedDirectories[realpath($dir)]); |
| 205 | - else |
|
| 206 | - unset($this->allowedDirectories[realpath($path)]); |
|
| 210 | + } else { |
|
| 211 | + unset($this->allowedDirectories[realpath($path)]); |
|
| 212 | + } |
|
| 207 | 213 | } |
| 208 | 214 | |
| 209 | 215 | /** |
@@ -713,9 +713,13 @@ discard block |
||
| 713 | 713 | $compiled = $this->addBlock('topLevelBlock', array(), 0); |
| 714 | 714 | $this->stack[0]['buffer'] = ''; |
| 715 | 715 | |
| 716 | - if ($this->debug) echo 'COMPILER INIT<br />'; |
|
| 716 | + if ($this->debug) { |
|
| 717 | + echo 'COMPILER INIT<br />'; |
|
| 718 | + } |
|
| 717 | 719 | |
| 718 | - if ($this->debug) echo 'PROCESSING PREPROCESSORS ('.count($this->processors['pre']).')<br>'; |
|
| 720 | + if ($this->debug) { |
|
| 721 | + echo 'PROCESSING PREPROCESSORS ('.count($this->processors['pre']).')<br>'; |
|
| 722 | + } |
|
| 719 | 723 | |
| 720 | 724 | // runs preprocessors |
| 721 | 725 | foreach ($this->processors['pre'] as $preProc) { |
@@ -731,7 +735,9 @@ discard block |
||
| 731 | 735 | unset($preProc); |
| 732 | 736 | |
| 733 | 737 | // show template source if debug |
| 734 | - if ($this->debug) echo '<pre>'.print_r(htmlentities($tpl), true).'</pre><hr />'; |
|
| 738 | + if ($this->debug) { |
|
| 739 | + echo '<pre>'.print_r(htmlentities($tpl), true).'</pre><hr />'; |
|
| 740 | + } |
|
| 735 | 741 | |
| 736 | 742 | // strips php tags if required by the security policy |
| 737 | 743 | if ($this->securityPolicy !== null) { |
@@ -820,7 +826,9 @@ discard block |
||
| 820 | 826 | |
| 821 | 827 | $compiled .= $this->removeBlock('topLevelBlock'); |
| 822 | 828 | |
| 823 | - if ($this->debug) echo 'PROCESSING POSTPROCESSORS<br>'; |
|
| 829 | + if ($this->debug) { |
|
| 830 | + echo 'PROCESSING POSTPROCESSORS<br>'; |
|
| 831 | + } |
|
| 824 | 832 | |
| 825 | 833 | foreach ($this->processors['post'] as $postProc) { |
| 826 | 834 | if (is_array($postProc) && isset($postProc['autoload'])) { |
@@ -834,7 +842,9 @@ discard block |
||
| 834 | 842 | } |
| 835 | 843 | unset($postProc); |
| 836 | 844 | |
| 837 | - if ($this->debug) echo 'COMPILATION COMPLETE : MEM USAGE : '.memory_get_usage().'<br>'; |
|
| 845 | + if ($this->debug) { |
|
| 846 | + echo 'COMPILATION COMPLETE : MEM USAGE : '.memory_get_usage().'<br>'; |
|
| 847 | + } |
|
| 838 | 848 | |
| 839 | 849 | $output = "<?php\n/* template head */\n"; |
| 840 | 850 | |
@@ -902,7 +912,9 @@ discard block |
||
| 902 | 912 | echo ($i+1).'. '.$line."\r\n"; |
| 903 | 913 | } |
| 904 | 914 | } |
| 905 | - if ($this->debug) echo '<hr></pre></pre>'; |
|
| 915 | + if ($this->debug) { |
|
| 916 | + echo '<hr></pre></pre>'; |
|
| 917 | + } |
|
| 906 | 918 | |
| 907 | 919 | $this->template = $this->dwoo = null; |
| 908 | 920 | $tpl = null; |
@@ -987,8 +999,9 @@ discard block |
||
| 987 | 999 | reset($this->scopeTree); |
| 988 | 1000 | $this->scope =& $this->data; |
| 989 | 1001 | $cnt = count($this->scopeTree); |
| 990 | - for ($i=0;$i<$cnt;$i++) |
|
| 991 | - $this->scope =& $this->scope[$this->scopeTree[$i]]; |
|
| 1002 | + for ($i=0;$i<$cnt;$i++) { |
|
| 1003 | + $this->scope =& $this->scope[$this->scopeTree[$i]]; |
|
| 1004 | + } |
|
| 992 | 1005 | } elseif ($bit === '_root' || $bit === '__') { |
| 993 | 1006 | $this->scope =& $this->data; |
| 994 | 1007 | $this->scopeTree = array(); |
@@ -1253,7 +1266,9 @@ discard block |
||
| 1253 | 1266 | if ($curBlock === 'root' && substr($in, $from, strlen($this->rd)) === $this->rd) { |
| 1254 | 1267 | // end template tag |
| 1255 | 1268 | $pointer += strlen($this->rd); |
| 1256 | - if ($this->debug) echo 'TEMPLATE PARSING ENDED<br />'; |
|
| 1269 | + if ($this->debug) { |
|
| 1270 | + echo 'TEMPLATE PARSING ENDED<br />'; |
|
| 1271 | + } |
|
| 1257 | 1272 | return false; |
| 1258 | 1273 | } |
| 1259 | 1274 | $from++; |
@@ -1272,7 +1287,9 @@ discard block |
||
| 1272 | 1287 | |
| 1273 | 1288 | $substr = substr($in, $from, $to-$from); |
| 1274 | 1289 | |
| 1275 | - if ($this->debug) echo '<br />PARSE CALL : PARSING "<b>'.htmlentities(substr($in, $from, min($to-$from, 50))).(($to-$from) > 50 ? '...':'').'</b>" @ '.$from.':'.$to.' in '.$curBlock.' : pointer='.$pointer.'<br/>'; |
|
| 1290 | + if ($this->debug) { |
|
| 1291 | + echo '<br />PARSE CALL : PARSING "<b>'.htmlentities(substr($in, $from, min($to-$from, 50))).(($to-$from) > 50 ? '...':'').'</b>" @ '.$from.':'.$to.' in '.$curBlock.' : pointer='.$pointer.'<br/>'; |
|
| 1292 | + } |
|
| 1276 | 1293 | $parsed = ""; |
| 1277 | 1294 | |
| 1278 | 1295 | if ($curBlock === 'root' && $first === '*') { |
@@ -1357,7 +1374,9 @@ discard block |
||
| 1357 | 1374 | $parsed = 'func'; |
| 1358 | 1375 | } elseif ($first === ';') { |
| 1359 | 1376 | // instruction end |
| 1360 | - if ($this->debug) echo 'END OF INSTRUCTION<br />'; |
|
| 1377 | + if ($this->debug) { |
|
| 1378 | + echo 'END OF INSTRUCTION<br />'; |
|
| 1379 | + } |
|
| 1361 | 1380 | if ($pointer !== null) { |
| 1362 | 1381 | $pointer++; |
| 1363 | 1382 | } |
@@ -1377,20 +1396,28 @@ discard block |
||
| 1377 | 1396 | if ($this->curBlock['type'] == 'else' || $this->curBlock['type'] == 'elseif') { |
| 1378 | 1397 | $pointer -= strlen($match[0]); |
| 1379 | 1398 | } |
| 1380 | - if ($this->debug) echo 'TOP BLOCK CLOSED<br />'; |
|
| 1399 | + if ($this->debug) { |
|
| 1400 | + echo 'TOP BLOCK CLOSED<br />'; |
|
| 1401 | + } |
|
| 1381 | 1402 | return $this->removeTopBlock(); |
| 1382 | 1403 | } else { |
| 1383 | - if ($this->debug) echo 'BLOCK OF TYPE '.$match[1].' CLOSED<br />'; |
|
| 1404 | + if ($this->debug) { |
|
| 1405 | + echo 'BLOCK OF TYPE '.$match[1].' CLOSED<br />'; |
|
| 1406 | + } |
|
| 1384 | 1407 | return $this->removeBlock($match[1]); |
| 1385 | 1408 | } |
| 1386 | 1409 | } elseif ($curBlock === 'root' && substr($substr, 0, strlen($this->rd)) === $this->rd) { |
| 1387 | 1410 | // end template tag |
| 1388 | - if ($this->debug) echo 'TAG PARSING ENDED<br />'; |
|
| 1411 | + if ($this->debug) { |
|
| 1412 | + echo 'TAG PARSING ENDED<br />'; |
|
| 1413 | + } |
|
| 1389 | 1414 | $pointer += strlen($this->rd); |
| 1390 | 1415 | return false; |
| 1391 | 1416 | } elseif (is_array($parsingParams) && preg_match('#^(([\'"]?)[a-z0-9_]+\2\s*='.($curBlock === 'array' ? '>?':'').')(?:\s+|[^=]).*#i', $substr, $match)) { |
| 1392 | 1417 | // named parameter |
| 1393 | - if ($this->debug) echo 'NAMED PARAM FOUND<br />'; |
|
| 1418 | + if ($this->debug) { |
|
| 1419 | + echo 'NAMED PARAM FOUND<br />'; |
|
| 1420 | + } |
|
| 1394 | 1421 | $len = strlen($match[1]); |
| 1395 | 1422 | while (substr($in, $from+$len, 1)===' ') { |
| 1396 | 1423 | $len++; |
@@ -1430,7 +1457,9 @@ discard block |
||
| 1430 | 1457 | // var parsed, check if any var-extension applies |
| 1431 | 1458 | if ($parsed==='var') { |
| 1432 | 1459 | if (preg_match('#^\s*([/%+*-])\s*([a-z0-9]|\$)#i', $substr, $match)) { |
| 1433 | - if($this->debug) echo 'PARSING POST-VAR EXPRESSION '.$substr.'<br />'; |
|
| 1460 | + if($this->debug) { |
|
| 1461 | + echo 'PARSING POST-VAR EXPRESSION '.$substr.'<br />'; |
|
| 1462 | + } |
|
| 1434 | 1463 | // parse expressions |
| 1435 | 1464 | $pointer += strlen($match[0]) - 1; |
| 1436 | 1465 | if (is_array($parsingParams)) { |
@@ -1460,7 +1489,9 @@ discard block |
||
| 1460 | 1489 | } |
| 1461 | 1490 | } |
| 1462 | 1491 | } else if ($curBlock === 'root' && preg_match('#^(\s*(?:[+/*%-.]=|=|\+\+|--)\s*)(.*)#s', $substr, $match)) { |
| 1463 | - if($this->debug) echo 'PARSING POST-VAR ASSIGNMENT '.$substr.'<br />'; |
|
| 1492 | + if($this->debug) { |
|
| 1493 | + echo 'PARSING POST-VAR ASSIGNMENT '.$substr.'<br />'; |
|
| 1494 | + } |
|
| 1464 | 1495 | // parse assignment |
| 1465 | 1496 | $value = $match[2]; |
| 1466 | 1497 | $operator = trim($match[1]); |
@@ -1502,7 +1533,9 @@ discard block |
||
| 1502 | 1533 | $out = Dwoo_Compiler::PHP_OPEN. $echo . $out . $operator . implode(' ', $value) . Dwoo_Compiler::PHP_CLOSE; |
| 1503 | 1534 | } else if ($curBlock === 'array' && is_array($parsingParams) && preg_match('#^(\s*=>?\s*)#', $substr, $match)) { |
| 1504 | 1535 | // parse namedparam with var as name (only for array) |
| 1505 | - if ($this->debug) echo 'VARIABLE NAMED PARAM (FOR ARRAY) FOUND<br />'; |
|
| 1536 | + if ($this->debug) { |
|
| 1537 | + echo 'VARIABLE NAMED PARAM (FOR ARRAY) FOUND<br />'; |
|
| 1538 | + } |
|
| 1506 | 1539 | $len = strlen($match[1]); |
| 1507 | 1540 | $var = $out[count($out)-1]; |
| 1508 | 1541 | $pointer += $len; |
@@ -1576,7 +1609,9 @@ discard block |
||
| 1576 | 1609 | $cmdstr = $match[1]; |
| 1577 | 1610 | } |
| 1578 | 1611 | |
| 1579 | - if ($this->debug) echo 'FUNC FOUND ('.$func.')<br />'; |
|
| 1612 | + if ($this->debug) { |
|
| 1613 | + echo 'FUNC FOUND ('.$func.')<br />'; |
|
| 1614 | + } |
|
| 1580 | 1615 | |
| 1581 | 1616 | $paramsep = ''; |
| 1582 | 1617 | |
@@ -1638,17 +1673,25 @@ discard block |
||
| 1638 | 1673 | } |
| 1639 | 1674 | |
| 1640 | 1675 | if ($func !== 'if' && $func !== 'elseif' && $paramstr[$ptr] === ')') { |
| 1641 | - if ($this->debug) echo 'PARAM PARSING ENDED, ")" FOUND, POINTER AT '.$ptr.'<br/>'; |
|
| 1676 | + if ($this->debug) { |
|
| 1677 | + echo 'PARAM PARSING ENDED, ")" FOUND, POINTER AT '.$ptr.'<br/>'; |
|
| 1678 | + } |
|
| 1642 | 1679 | break 2; |
| 1643 | 1680 | } elseif ($paramstr[$ptr] === ';') { |
| 1644 | 1681 | $ptr++; |
| 1645 | - if ($this->debug) echo 'PARAM PARSING ENDED, ";" FOUND, POINTER AT '.$ptr.'<br/>'; |
|
| 1682 | + if ($this->debug) { |
|
| 1683 | + echo 'PARAM PARSING ENDED, ";" FOUND, POINTER AT '.$ptr.'<br/>'; |
|
| 1684 | + } |
|
| 1646 | 1685 | break 2; |
| 1647 | 1686 | } elseif ($func !== 'if' && $func !== 'elseif' && $paramstr[$ptr] === '/') { |
| 1648 | - if ($this->debug) echo 'PARAM PARSING ENDED, "/" FOUND, POINTER AT '.$ptr.'<br/>'; |
|
| 1687 | + if ($this->debug) { |
|
| 1688 | + echo 'PARAM PARSING ENDED, "/" FOUND, POINTER AT '.$ptr.'<br/>'; |
|
| 1689 | + } |
|
| 1649 | 1690 | break 2; |
| 1650 | 1691 | } elseif (substr($paramstr, $ptr, strlen($this->rd)) === $this->rd) { |
| 1651 | - if ($this->debug) echo 'PARAM PARSING ENDED, RIGHT DELIMITER FOUND, POINTER AT '.$ptr.'<br/>'; |
|
| 1692 | + if ($this->debug) { |
|
| 1693 | + echo 'PARAM PARSING ENDED, RIGHT DELIMITER FOUND, POINTER AT '.$ptr.'<br/>'; |
|
| 1694 | + } |
|
| 1652 | 1695 | break 2; |
| 1653 | 1696 | } |
| 1654 | 1697 | |
@@ -1659,7 +1702,9 @@ discard block |
||
| 1659 | 1702 | } |
| 1660 | 1703 | } |
| 1661 | 1704 | |
| 1662 | - if ($this->debug) echo 'FUNC START PARAM PARSING WITH POINTER AT '.$ptr.'<br/>'; |
|
| 1705 | + if ($this->debug) { |
|
| 1706 | + echo 'FUNC START PARAM PARSING WITH POINTER AT '.$ptr.'<br/>'; |
|
| 1707 | + } |
|
| 1663 | 1708 | |
| 1664 | 1709 | if ($func === 'if' || $func === 'elseif' || $func === 'tif') { |
| 1665 | 1710 | $params = $this->parse($paramstr, $ptr, strlen($paramstr), $params, 'condition', $ptr); |
@@ -1669,7 +1714,9 @@ discard block |
||
| 1669 | 1714 | $params = $this->parse($paramstr, $ptr, strlen($paramstr), $params, 'function', $ptr); |
| 1670 | 1715 | } |
| 1671 | 1716 | |
| 1672 | - if ($this->debug) echo 'PARAM PARSED, POINTER AT '.$ptr.' ('.substr($paramstr, $ptr-1, 3).')<br/>'; |
|
| 1717 | + if ($this->debug) { |
|
| 1718 | + echo 'PARAM PARSED, POINTER AT '.$ptr.' ('.substr($paramstr, $ptr-1, 3).')<br/>'; |
|
| 1719 | + } |
|
| 1673 | 1720 | } |
| 1674 | 1721 | } |
| 1675 | 1722 | $paramstr = substr($paramstr, 0, $ptr); |
@@ -1693,7 +1740,9 @@ discard block |
||
| 1693 | 1740 | |
| 1694 | 1741 | if ($pointer !== null) { |
| 1695 | 1742 | $pointer += (isset($paramstr) ? strlen($paramstr) : 0) + (')' === $paramsep ? 2 : ($paramspos === false ? 0 : 1)) + strlen($func) + (isset($whitespace) ? $whitespace : 0); |
| 1696 | - if ($this->debug) echo 'FUNC ADDS '.((isset($paramstr) ? strlen($paramstr) : 0) + (')' === $paramsep ? 2 : ($paramspos === false ? 0 : 1)) + strlen($func)).' TO POINTER<br/>'; |
|
| 1743 | + if ($this->debug) { |
|
| 1744 | + echo 'FUNC ADDS '.((isset($paramstr) ? strlen($paramstr) : 0) + (')' === $paramsep ? 2 : ($paramspos === false ? 0 : 1)) + strlen($func)).' TO POINTER<br/>'; |
|
| 1745 | + } |
|
| 1697 | 1746 | } |
| 1698 | 1747 | |
| 1699 | 1748 | if ($curBlock === 'method' || $func === 'do' || strstr($func, '::') !== false) { |
@@ -1931,7 +1980,9 @@ discard block |
||
| 1931 | 1980 | $substr = substr($in, $from, $to-$from); |
| 1932 | 1981 | $first = $substr[0]; |
| 1933 | 1982 | |
| 1934 | - if ($this->debug) echo 'STRING FOUND (in '.htmlentities(substr($in, $from, min($to-$from, 50))).(($to-$from) > 50 ? '...':'').')<br />'; |
|
| 1983 | + if ($this->debug) { |
|
| 1984 | + echo 'STRING FOUND (in '.htmlentities(substr($in, $from, min($to-$from, 50))).(($to-$from) > 50 ? '...':'').')<br />'; |
|
| 1985 | + } |
|
| 1935 | 1986 | $strend = false; |
| 1936 | 1987 | $o = $from+1; |
| 1937 | 1988 | while ($strend === false) { |
@@ -1944,7 +1995,9 @@ discard block |
||
| 1944 | 1995 | $strend = false; |
| 1945 | 1996 | } |
| 1946 | 1997 | } |
| 1947 | - if ($this->debug) echo 'STRING DELIMITED: '.substr($in, $from, $strend+1-$from).'<br/>'; |
|
| 1998 | + if ($this->debug) { |
|
| 1999 | + echo 'STRING DELIMITED: '.substr($in, $from, $strend+1-$from).'<br/>'; |
|
| 2000 | + } |
|
| 1948 | 2001 | |
| 1949 | 2002 | $srcOutput = substr($in, $from, $strend+1-$from); |
| 1950 | 2003 | |
@@ -2150,11 +2203,15 @@ discard block |
||
| 2150 | 2203 | } |
| 2151 | 2204 | unset($uid, $current, $curTxt, $tree, $chars); |
| 2152 | 2205 | |
| 2153 | - if ($this->debug) echo 'RECURSIVE VAR REPLACEMENT : '.$key.'<br>'; |
|
| 2206 | + if ($this->debug) { |
|
| 2207 | + echo 'RECURSIVE VAR REPLACEMENT : '.$key.'<br>'; |
|
| 2208 | + } |
|
| 2154 | 2209 | |
| 2155 | 2210 | $key = $this->flattenVarTree($parsed); |
| 2156 | 2211 | |
| 2157 | - if ($this->debug) echo 'RECURSIVE VAR REPLACEMENT DONE : '.$key.'<br>'; |
|
| 2212 | + if ($this->debug) { |
|
| 2213 | + echo 'RECURSIVE VAR REPLACEMENT DONE : '.$key.'<br>'; |
|
| 2214 | + } |
|
| 2158 | 2215 | |
| 2159 | 2216 | $output = preg_replace('#(^""\.|""\.|\.""$|(\()""\.|\.""(\)))#', '$2$3', '$this->readVar("'.$key.'")'); |
| 2160 | 2217 | } else { |
@@ -2334,8 +2391,9 @@ discard block |
||
| 2334 | 2391 | $global = 'COOKIE'; |
| 2335 | 2392 | } |
| 2336 | 2393 | $key = '$_'.$global; |
| 2337 | - foreach (explode('.', ltrim($m[2], '.')) as $part) |
|
| 2338 | - $key .= '['.var_export($part, true).']'; |
|
| 2394 | + foreach (explode('.', ltrim($m[2], '.')) as $part) { |
|
| 2395 | + $key .= '['.var_export($part, true).']'; |
|
| 2396 | + } |
|
| 2339 | 2397 | if ($curBlock === 'root') { |
| 2340 | 2398 | $output = $key; |
| 2341 | 2399 | } else { |
@@ -2445,7 +2503,9 @@ discard block |
||
| 2445 | 2503 | } else { |
| 2446 | 2504 | $cnt = substr_count($key, '$'); |
| 2447 | 2505 | |
| 2448 | - if ($this->debug) echo 'PARSING SUBVARS IN : '.$key.'<br>'; |
|
| 2506 | + if ($this->debug) { |
|
| 2507 | + echo 'PARSING SUBVARS IN : '.$key.'<br>'; |
|
| 2508 | + } |
|
| 2449 | 2509 | if ($cnt > 0) { |
| 2450 | 2510 | while (--$cnt >= 0) { |
| 2451 | 2511 | if (isset($last)) { |
@@ -2467,7 +2527,9 @@ discard block |
||
| 2467 | 2527 | $last, |
| 2468 | 2528 | $len |
| 2469 | 2529 | ); |
| 2470 | - if ($this->debug) echo 'RECURSIVE VAR REPLACEMENT DONE : '.$key.'<br>'; |
|
| 2530 | + if ($this->debug) { |
|
| 2531 | + echo 'RECURSIVE VAR REPLACEMENT DONE : '.$key.'<br>'; |
|
| 2532 | + } |
|
| 2471 | 2533 | } |
| 2472 | 2534 | unset($last); |
| 2473 | 2535 | |
@@ -2548,15 +2610,21 @@ discard block |
||
| 2548 | 2610 | $substr = trim($substr); |
| 2549 | 2611 | |
| 2550 | 2612 | if (strtolower($substr) === 'false' || strtolower($substr) === 'no' || strtolower($substr) === 'off') { |
| 2551 | - if ($this->debug) echo 'BOOLEAN(FALSE) PARSED<br />'; |
|
| 2613 | + if ($this->debug) { |
|
| 2614 | + echo 'BOOLEAN(FALSE) PARSED<br />'; |
|
| 2615 | + } |
|
| 2552 | 2616 | $substr = 'false'; |
| 2553 | 2617 | $type = self::T_BOOL; |
| 2554 | 2618 | } elseif (strtolower($substr) === 'true' || strtolower($substr) === 'yes' || strtolower($substr) === 'on') { |
| 2555 | - if ($this->debug) echo 'BOOLEAN(TRUE) PARSED<br />'; |
|
| 2619 | + if ($this->debug) { |
|
| 2620 | + echo 'BOOLEAN(TRUE) PARSED<br />'; |
|
| 2621 | + } |
|
| 2556 | 2622 | $substr = 'true'; |
| 2557 | 2623 | $type = self::T_BOOL; |
| 2558 | 2624 | } elseif ($substr === 'null' || $substr === 'NULL') { |
| 2559 | - if ($this->debug) echo 'NULL PARSED<br />'; |
|
| 2625 | + if ($this->debug) { |
|
| 2626 | + echo 'NULL PARSED<br />'; |
|
| 2627 | + } |
|
| 2560 | 2628 | $substr = 'null'; |
| 2561 | 2629 | $type = self::T_NULL; |
| 2562 | 2630 | } elseif (is_numeric($substr)) { |
@@ -2565,19 +2633,27 @@ discard block |
||
| 2565 | 2633 | $substr = (int) $substr; |
| 2566 | 2634 | } |
| 2567 | 2635 | $type = self::T_NUMERIC; |
| 2568 | - if ($this->debug) echo 'NUMBER ('.$substr.') PARSED<br />'; |
|
| 2636 | + if ($this->debug) { |
|
| 2637 | + echo 'NUMBER ('.$substr.') PARSED<br />'; |
|
| 2638 | + } |
|
| 2569 | 2639 | } elseif (preg_match('{^-?(\d+|\d*(\.\d+))\s*([/*%+-]\s*-?(\d+|\d*(\.\d+)))+$}', $substr)) { |
| 2570 | - if ($this->debug) echo 'SIMPLE MATH PARSED<br />'; |
|
| 2640 | + if ($this->debug) { |
|
| 2641 | + echo 'SIMPLE MATH PARSED<br />'; |
|
| 2642 | + } |
|
| 2571 | 2643 | $type = self::T_MATH; |
| 2572 | 2644 | $substr = '('.$substr.')'; |
| 2573 | 2645 | } elseif ($curBlock === 'condition' && array_search($substr, $breakChars, true) !== false) { |
| 2574 | - if ($this->debug) echo 'BREAKCHAR ('.$substr.') PARSED<br />'; |
|
| 2646 | + if ($this->debug) { |
|
| 2647 | + echo 'BREAKCHAR ('.$substr.') PARSED<br />'; |
|
| 2648 | + } |
|
| 2575 | 2649 | $type = self::T_BREAKCHAR; |
| 2576 | 2650 | //$substr = '"'.$substr.'"'; |
| 2577 | 2651 | } else { |
| 2578 | 2652 | $substr = $this->replaceStringVars('\''.str_replace('\'', '\\\'', $substr).'\'', '\'', $curBlock); |
| 2579 | 2653 | $type = self::T_UNQUOTED_STRING; |
| 2580 | - if ($this->debug) echo 'BLABBER ('.$substr.') CASTED AS STRING<br />'; |
|
| 2654 | + if ($this->debug) { |
|
| 2655 | + echo 'BLABBER ('.$substr.') CASTED AS STRING<br />'; |
|
| 2656 | + } |
|
| 2581 | 2657 | } |
| 2582 | 2658 | |
| 2583 | 2659 | if (is_array($parsingParams)) { |
@@ -2603,7 +2679,9 @@ discard block |
||
| 2603 | 2679 | protected function replaceStringVars($string, $first, $curBlock='') |
| 2604 | 2680 | { |
| 2605 | 2681 | $pos = 0; |
| 2606 | - if ($this->debug) echo 'STRING VAR REPLACEMENT : '.$string.'<br>'; |
|
| 2682 | + if ($this->debug) { |
|
| 2683 | + echo 'STRING VAR REPLACEMENT : '.$string.'<br>'; |
|
| 2684 | + } |
|
| 2607 | 2685 | // replace vars |
| 2608 | 2686 | while (($pos = strpos($string, '$', $pos)) !== false) { |
| 2609 | 2687 | $prev = substr($string, $pos-1, 1); |
@@ -2622,7 +2700,9 @@ discard block |
||
| 2622 | 2700 | $string = substr_replace($string, $first.'.'.$var[1].'.'.$first, $pos, $len); |
| 2623 | 2701 | } |
| 2624 | 2702 | $pos += strlen($var[1]) + 2; |
| 2625 | - if ($this->debug) echo 'STRING VAR REPLACEMENT DONE : '.$string.'<br>'; |
|
| 2703 | + if ($this->debug) { |
|
| 2704 | + echo 'STRING VAR REPLACEMENT DONE : '.$string.'<br>'; |
|
| 2705 | + } |
|
| 2626 | 2706 | } |
| 2627 | 2707 | |
| 2628 | 2708 | // handle modifiers |
@@ -2646,7 +2726,9 @@ discard block |
||
| 2646 | 2726 | */ |
| 2647 | 2727 | protected function replaceModifiers(array $m, $curBlock = null, &$pointer = null) |
| 2648 | 2728 | { |
| 2649 | - if ($this->debug) echo 'PARSING MODIFIERS : '.$m[3].'<br />'; |
|
| 2729 | + if ($this->debug) { |
|
| 2730 | + echo 'PARSING MODIFIERS : '.$m[3].'<br />'; |
|
| 2731 | + } |
|
| 2650 | 2732 | |
| 2651 | 2733 | if ($pointer !== null) { |
| 2652 | 2734 | $pointer += strlen($m[3]); |
@@ -2668,7 +2750,9 @@ discard block |
||
| 2668 | 2750 | continue; |
| 2669 | 2751 | } |
| 2670 | 2752 | if ($cmdstrsrc[0] === ' ' || $cmdstrsrc[0] === ';' || substr($cmdstrsrc, 0, strlen($this->rd)) === $this->rd) { |
| 2671 | - if ($this->debug) echo 'MODIFIER PARSING ENDED, RIGHT DELIMITER or ";" FOUND<br/>'; |
|
| 2753 | + if ($this->debug) { |
|
| 2754 | + echo 'MODIFIER PARSING ENDED, RIGHT DELIMITER or ";" FOUND<br/>'; |
|
| 2755 | + } |
|
| 2672 | 2756 | $continue = false; |
| 2673 | 2757 | if ($pointer !== null) { |
| 2674 | 2758 | $pointer -= strlen($cmdstrsrc); |
@@ -2687,7 +2771,9 @@ discard block |
||
| 2687 | 2771 | if ($paramspos === false) { |
| 2688 | 2772 | $cmdstrsrc = substr($cmdstrsrc, strlen($func)); |
| 2689 | 2773 | $params = array(); |
| 2690 | - if ($this->debug) echo 'MODIFIER ('.$func.') CALLED WITH NO PARAMS<br/>'; |
|
| 2774 | + if ($this->debug) { |
|
| 2775 | + echo 'MODIFIER ('.$func.') CALLED WITH NO PARAMS<br/>'; |
|
| 2776 | + } |
|
| 2691 | 2777 | } else { |
| 2692 | 2778 | $paramstr = substr($cmdstr, $paramspos+1); |
| 2693 | 2779 | if (substr($paramstr, -1, 1) === $paramsep) { |
@@ -2697,18 +2783,28 @@ discard block |
||
| 2697 | 2783 | $ptr = 0; |
| 2698 | 2784 | $params = array(); |
| 2699 | 2785 | while ($ptr < strlen($paramstr)) { |
| 2700 | - if ($this->debug) echo 'MODIFIER ('.$func.') START PARAM PARSING WITH POINTER AT '.$ptr.'<br/>'; |
|
| 2701 | - if ($this->debug) echo $paramstr.'--'.$ptr.'--'.strlen($paramstr).'--modifier<br/>'; |
|
| 2786 | + if ($this->debug) { |
|
| 2787 | + echo 'MODIFIER ('.$func.') START PARAM PARSING WITH POINTER AT '.$ptr.'<br/>'; |
|
| 2788 | + } |
|
| 2789 | + if ($this->debug) { |
|
| 2790 | + echo $paramstr.'--'.$ptr.'--'.strlen($paramstr).'--modifier<br/>'; |
|
| 2791 | + } |
|
| 2702 | 2792 | $params = $this->parse($paramstr, $ptr, strlen($paramstr), $params, 'modifier', $ptr); |
| 2703 | - if ($this->debug) echo 'PARAM PARSED, POINTER AT '.$ptr.'<br/>'; |
|
| 2793 | + if ($this->debug) { |
|
| 2794 | + echo 'PARAM PARSED, POINTER AT '.$ptr.'<br/>'; |
|
| 2795 | + } |
|
| 2704 | 2796 | |
| 2705 | 2797 | if ($ptr >= strlen($paramstr)) { |
| 2706 | - if ($this->debug) echo 'PARAM PARSING ENDED, PARAM STRING CONSUMED<br/>'; |
|
| 2798 | + if ($this->debug) { |
|
| 2799 | + echo 'PARAM PARSING ENDED, PARAM STRING CONSUMED<br/>'; |
|
| 2800 | + } |
|
| 2707 | 2801 | break; |
| 2708 | 2802 | } |
| 2709 | 2803 | |
| 2710 | 2804 | if ($paramstr[$ptr] === ' ' || $paramstr[$ptr] === '|' || $paramstr[$ptr] === ';' || substr($paramstr, $ptr, strlen($this->rd)) === $this->rd) { |
| 2711 | - if ($this->debug) echo 'PARAM PARSING ENDED, " ", "|", RIGHT DELIMITER or ";" FOUND, POINTER AT '.$ptr.'<br/>'; |
|
| 2805 | + if ($this->debug) { |
|
| 2806 | + echo 'PARAM PARSING ENDED, " ", "|", RIGHT DELIMITER or ";" FOUND, POINTER AT '.$ptr.'<br/>'; |
|
| 2807 | + } |
|
| 2712 | 2808 | if ($paramstr[$ptr] !== '|') { |
| 2713 | 2809 | $continue = false; |
| 2714 | 2810 | if ($pointer !== null) { |
@@ -2769,8 +2865,9 @@ discard block |
||
| 2769 | 2865 | } |
| 2770 | 2866 | } elseif ($pluginType & Dwoo_Core::PROXY_PLUGIN) { |
| 2771 | 2867 | $params = $this->mapParams($params, $this->getDwoo()->getPluginProxy()->getCallback($func), $state); |
| 2772 | - foreach ($params as &$p) |
|
| 2773 | - $p = $p[0]; |
|
| 2868 | + foreach ($params as &$p) { |
|
| 2869 | + $p = $p[0]; |
|
| 2870 | + } |
|
| 2774 | 2871 | $output = call_user_func(array($this->dwoo->getPluginProxy(), 'getCode'), $func, $params); |
| 2775 | 2872 | } elseif ($pluginType & Dwoo_Core::SMARTY_MODIFIER) { |
| 2776 | 2873 | $params = $this->mapParams($params, null, $state); |
@@ -2812,8 +2909,9 @@ discard block |
||
| 2812 | 2909 | |
| 2813 | 2910 | $params = $this->mapParams($params, $callback, $state); |
| 2814 | 2911 | |
| 2815 | - foreach ($params as &$p) |
|
| 2816 | - $p = $p[0]; |
|
| 2912 | + foreach ($params as &$p) { |
|
| 2913 | + $p = $p[0]; |
|
| 2914 | + } |
|
| 2817 | 2915 | |
| 2818 | 2916 | if ($pluginType & Dwoo_Core::FUNC_PLUGIN) { |
| 2819 | 2917 | if ($pluginType & Dwoo_Core::COMPILABLE_PLUGIN) { |
@@ -2904,8 +3002,9 @@ discard block |
||
| 2904 | 3002 | foreach ($params as $k=>$p) { |
| 2905 | 3003 | if (is_array($p)) { |
| 2906 | 3004 | $out2 = 'array('; |
| 2907 | - foreach ($p as $k2=>$v) |
|
| 2908 | - $out2 .= var_export($k2, true).' => '.(is_array($v) ? 'array('.self::implode_r($v, true).')' : $v).', '; |
|
| 3005 | + foreach ($p as $k2=>$v) { |
|
| 3006 | + $out2 .= var_export($k2, true).' => '.(is_array($v) ? 'array('.self::implode_r($v, true).')' : $v).', '; |
|
| 3007 | + } |
|
| 2909 | 3008 | $p = rtrim($out2, ', ').')'; |
| 2910 | 3009 | } |
| 2911 | 3010 | if ($recursiveCall) { |
@@ -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 |
@@ -1,4 +1,6 @@ |
||
| 1 | -<?php if (!defined('BASEPATH')) exit('No direct script access allowed'); |
|
| 1 | +<?php if (!defined('BASEPATH')) { |
|
| 2 | + exit('No direct script access allowed'); |
|
| 3 | +} |
|
| 2 | 4 | |
| 3 | 5 | require "dwoo/dwooAutoload.php"; |
| 4 | 6 | |
@@ -1,4 +1,6 @@ |
||
| 1 | -<?php if (!defined('BASEPATH')) exit('No direct script access allowed'); |
|
| 1 | +<?php if (!defined('BASEPATH')) { |
|
| 2 | + exit('No direct script access allowed'); |
|
| 3 | +} |
|
| 2 | 4 | |
| 3 | 5 | // The name of the directory where templates are located. |
| 4 | 6 | $config['template_dir'] = dirname(FCPATH) . '/../application/views/'; |
@@ -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 |
@@ -248,8 +248,7 @@ discard block |
||
| 248 | 248 | // |
| 249 | 249 | elseif (is_subclass_of($engine, 'Dwoo') || 'Dwoo' === $engine) { |
| 250 | 250 | $this->_engine = new $engine(); |
| 251 | - } |
|
| 252 | - else { |
|
| 251 | + } else { |
|
| 253 | 252 | throw new Dwoo_Exception("Custom engine must be a subclass of Dwoo"); |
| 254 | 253 | } |
| 255 | 254 | } |
@@ -277,11 +276,9 @@ discard block |
||
| 277 | 276 | { |
| 278 | 277 | if ($data instanceof Dwoo_IDataProvider) { |
| 279 | 278 | $this->_dataProvider = $data; |
| 280 | - } |
|
| 281 | - elseif (is_subclass_of($data, 'Dwoo_Data') || 'Dwoo_Data' == $data) { |
|
| 279 | + } elseif (is_subclass_of($data, 'Dwoo_Data') || 'Dwoo_Data' == $data) { |
|
| 282 | 280 | $this->_dataProvider = new $data(); |
| 283 | - } |
|
| 284 | - else { |
|
| 281 | + } else { |
|
| 285 | 282 | throw new Dwoo_Exception("Custom data provider must be a subclass of Dwoo_Data or instance of Dwoo_IDataProvider"); |
| 286 | 283 | } |
| 287 | 284 | } |
@@ -320,8 +317,7 @@ discard block |
||
| 320 | 317 | // if param given as a string |
| 321 | 318 | elseif (is_subclass_of($compiler, 'Dwoo_Compiler') || 'Dwoo_Compiler' == $compiler) { |
| 322 | 319 | $this->_compiler = new $compiler; |
| 323 | - } |
|
| 324 | - else { |
|
| 320 | + } else { |
|
| 325 | 321 | throw new Dwoo_Exception("Custom compiler must be a subclass of Dwoo_Compiler or instance of Dwoo_ICompiler"); |
| 326 | 322 | } |
| 327 | 323 | } |
@@ -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 | } |