@@ -293,7 +293,7 @@ |
||
| 293 | 293 | * @param Core $core the dwoo instance that requests it |
| 294 | 294 | * @param string $output the template output |
| 295 | 295 | * |
| 296 | - * @return mixed full path of the cached file or false upon failure |
|
| 296 | + * @return false|string full path of the cached file or false upon failure |
|
| 297 | 297 | */ |
| 298 | 298 | public function cache(Core $core, $output) |
| 299 | 299 | { |
@@ -29,509 +29,509 @@ |
||
| 29 | 29 | */ |
| 30 | 30 | class String implements ITemplate |
| 31 | 31 | { |
| 32 | - /** |
|
| 33 | - * Template name. |
|
| 34 | - * |
|
| 35 | - * @var string |
|
| 36 | - */ |
|
| 37 | - protected $name; |
|
| 38 | - |
|
| 39 | - /** |
|
| 40 | - * Template compilation id. |
|
| 41 | - * |
|
| 42 | - * @var string |
|
| 43 | - */ |
|
| 44 | - protected $compileId; |
|
| 45 | - |
|
| 46 | - /** |
|
| 47 | - * Template cache id, if not provided in the constructor, it is set to |
|
| 48 | - * the md4 hash of the request_uri. it is however highly recommended to |
|
| 49 | - * provide one that will fit your needs. |
|
| 50 | - * in all cases, the compilation id is prepended to the cache id to separate |
|
| 51 | - * templates with similar cache ids from one another |
|
| 52 | - * |
|
| 53 | - * @var string |
|
| 54 | - */ |
|
| 55 | - protected $cacheId; |
|
| 56 | - |
|
| 57 | - /** |
|
| 58 | - * Validity duration of the generated cache file (in seconds). |
|
| 59 | - * set to -1 for infinite cache, 0 to disable and null to inherit the Dwoo instance's cache time |
|
| 60 | - * |
|
| 61 | - * @var int |
|
| 62 | - */ |
|
| 63 | - protected $cacheTime; |
|
| 64 | - |
|
| 65 | - /** |
|
| 66 | - * Boolean flag that defines whether the compilation should be enforced (once) or |
|
| 67 | - * not use this if you have issues with the compiled templates not being updated |
|
| 68 | - * but if you do need this it's most likely that you should file a bug report. |
|
| 69 | - * |
|
| 70 | - * @var bool |
|
| 71 | - */ |
|
| 72 | - protected $compilationEnforced; |
|
| 73 | - |
|
| 74 | - /** |
|
| 75 | - * Caches the results of the file checks to save some time when the same |
|
| 76 | - * templates is rendered several times. |
|
| 77 | - * |
|
| 78 | - * @var array |
|
| 79 | - */ |
|
| 80 | - protected static $cache = array( |
|
| 81 | - 'cached' => array(), |
|
| 82 | - 'compiled' => array() |
|
| 83 | - ); |
|
| 84 | - |
|
| 85 | - /** |
|
| 86 | - * Holds the compiler that built this template. |
|
| 87 | - * |
|
| 88 | - * @var ICompiler |
|
| 89 | - */ |
|
| 90 | - protected $compiler; |
|
| 91 | - |
|
| 92 | - /** |
|
| 93 | - * Chmod value for all files written (cached or compiled ones). |
|
| 94 | - * set to null if you don't want any chmod operation to happen |
|
| 95 | - * |
|
| 96 | - * @var int |
|
| 97 | - */ |
|
| 98 | - protected $chmod = 0777; |
|
| 99 | - |
|
| 100 | - /** |
|
| 101 | - * Containing template string. |
|
| 102 | - * |
|
| 103 | - * @var string |
|
| 104 | - */ |
|
| 105 | - protected $template; |
|
| 106 | - |
|
| 107 | - /** |
|
| 108 | - * Creates a template from a string. |
|
| 109 | - * |
|
| 110 | - * @param string $templateString the template to use |
|
| 111 | - * @param int $cacheTime duration of the cache validity for this template, |
|
| 112 | - * if null it defaults to the Dwoo instance that will |
|
| 113 | - * render this template, set to -1 for infinite cache or 0 to disable |
|
| 114 | - * @param string $cacheId the unique cache identifier of this page or anything else that |
|
| 115 | - * makes this template's content unique, if null it defaults |
|
| 116 | - * to the current url |
|
| 117 | - * @param string $compileId the unique compiled identifier, which is used to distinguish this |
|
| 118 | - * template from others, if null it defaults to the md4 hash of the template |
|
| 119 | - */ |
|
| 120 | - public function __construct($templateString, $cacheTime = null, $cacheId = null, $compileId = null) |
|
| 121 | - { |
|
| 122 | - $this->template = $templateString; |
|
| 123 | - $this->name = hash('md4', $templateString); |
|
| 124 | - $this->cacheTime = $cacheTime; |
|
| 125 | - |
|
| 126 | - if ($compileId !== null) { |
|
| 127 | - $this->compileId = str_replace('../', '__', strtr($compileId, '\\%?=!:;' . PATH_SEPARATOR, '/-------')); |
|
| 128 | - } |
|
| 129 | - |
|
| 130 | - if ($cacheId !== null) { |
|
| 131 | - $this->cacheId = str_replace('../', '__', strtr($cacheId, '\\%?=!:;' . PATH_SEPARATOR, '/-------')); |
|
| 132 | - } |
|
| 133 | - } |
|
| 134 | - |
|
| 135 | - /** |
|
| 136 | - * Returns the cache duration for this template. |
|
| 137 | - * defaults to null if it was not provided |
|
| 138 | - * |
|
| 139 | - * @return int|null |
|
| 140 | - */ |
|
| 141 | - public function getCacheTime() |
|
| 142 | - { |
|
| 143 | - return $this->cacheTime; |
|
| 144 | - } |
|
| 145 | - |
|
| 146 | - /** |
|
| 147 | - * Sets the cache duration for this template. |
|
| 148 | - * can be used to set it after the object is created if you did not provide |
|
| 149 | - * it in the constructor |
|
| 150 | - * |
|
| 151 | - * @param int $seconds duration of the cache validity for this template, if |
|
| 152 | - * null it defaults to the Dwoo instance's cache time. 0 = disable and |
|
| 153 | - * -1 = infinite cache |
|
| 154 | - */ |
|
| 155 | - public function setCacheTime($seconds = null) |
|
| 156 | - { |
|
| 157 | - $this->cacheTime = $seconds; |
|
| 158 | - } |
|
| 159 | - |
|
| 160 | - /** |
|
| 161 | - * Returns the chmod value for all files written (cached or compiled ones). |
|
| 162 | - * defaults to 0777 |
|
| 163 | - * |
|
| 164 | - * @return int|null |
|
| 165 | - */ |
|
| 166 | - public function getChmod() |
|
| 167 | - { |
|
| 168 | - return $this->chmod; |
|
| 169 | - } |
|
| 170 | - |
|
| 171 | - /** |
|
| 172 | - * Set the chmod value for all files written (cached or compiled ones). |
|
| 173 | - * set to null if you don't want to do any chmod() operation |
|
| 174 | - * |
|
| 175 | - * @param int $mask new bitmask to use for all files |
|
| 176 | - */ |
|
| 177 | - public function setChmod($mask = null) |
|
| 178 | - { |
|
| 179 | - $this->chmod = $mask; |
|
| 180 | - } |
|
| 181 | - |
|
| 182 | - /** |
|
| 183 | - * Returns the template name. |
|
| 184 | - * |
|
| 185 | - * @return string |
|
| 186 | - */ |
|
| 187 | - public function getName() |
|
| 188 | - { |
|
| 189 | - return $this->name; |
|
| 190 | - } |
|
| 191 | - |
|
| 192 | - /** |
|
| 193 | - * Returns the resource name for this template class. |
|
| 194 | - * |
|
| 195 | - * @return string |
|
| 196 | - */ |
|
| 197 | - public function getResourceName() |
|
| 198 | - { |
|
| 199 | - return 'string'; |
|
| 200 | - } |
|
| 201 | - |
|
| 202 | - /** |
|
| 203 | - * Returns the resource identifier for this template, false here as strings don't have identifiers. |
|
| 204 | - * |
|
| 205 | - * @return false |
|
| 206 | - */ |
|
| 207 | - public function getResourceIdentifier() |
|
| 208 | - { |
|
| 209 | - return false; |
|
| 210 | - } |
|
| 211 | - |
|
| 212 | - /** |
|
| 213 | - * Returns the template source of this template. |
|
| 214 | - * |
|
| 215 | - * @return string |
|
| 216 | - */ |
|
| 217 | - public function getSource() |
|
| 218 | - { |
|
| 219 | - return $this->template; |
|
| 220 | - } |
|
| 221 | - |
|
| 222 | - /** |
|
| 223 | - * Returns an unique value identifying the current version of this template, |
|
| 224 | - * in this case it's the md4 hash of the content. |
|
| 225 | - * |
|
| 226 | - * @return string |
|
| 227 | - */ |
|
| 228 | - public function getUid() |
|
| 229 | - { |
|
| 230 | - return $this->name; |
|
| 231 | - } |
|
| 232 | - |
|
| 233 | - /** |
|
| 234 | - * Returns the compiler used by this template, if it was just compiled, or null. |
|
| 235 | - * |
|
| 236 | - * @return ICompiler |
|
| 237 | - */ |
|
| 238 | - public function getCompiler() |
|
| 239 | - { |
|
| 240 | - return $this->compiler; |
|
| 241 | - } |
|
| 242 | - |
|
| 243 | - /** |
|
| 244 | - * Marks this template as compile-forced, which means it will be recompiled even if it |
|
| 245 | - * was already saved and wasn't modified since the last compilation. do not use this in production, |
|
| 246 | - * it's only meant to be used in development (and the development of dwoo particularly). |
|
| 247 | - */ |
|
| 248 | - public function forceCompilation() |
|
| 249 | - { |
|
| 250 | - $this->compilationEnforced = true; |
|
| 251 | - } |
|
| 252 | - |
|
| 253 | - /** |
|
| 254 | - * Returns the cached template output file name, true if it's cache-able but not cached |
|
| 255 | - * or false if it's not cached. |
|
| 256 | - * |
|
| 257 | - * @param Core $core the dwoo instance that requests it |
|
| 258 | - * |
|
| 259 | - * @return string|bool |
|
| 260 | - */ |
|
| 261 | - public function getCachedTemplate(Core $core) |
|
| 262 | - { |
|
| 263 | - if ($this->cacheTime !== null) { |
|
| 264 | - $cacheLength = $this->cacheTime; |
|
| 265 | - } else { |
|
| 266 | - $cacheLength = $core->getCacheTime(); |
|
| 267 | - } |
|
| 268 | - |
|
| 269 | - // file is not cacheable |
|
| 270 | - if ($cacheLength == 0) { |
|
| 271 | - return false; |
|
| 272 | - } |
|
| 273 | - |
|
| 274 | - $cachedFile = $this->getCacheFilename($core); |
|
| 275 | - |
|
| 276 | - if (isset(self::$cache['cached'][$this->cacheId]) === true && file_exists($cachedFile)) { |
|
| 277 | - // already checked, return cache file |
|
| 278 | - return $cachedFile; |
|
| 279 | - } elseif ($this->compilationEnforced !== true && file_exists($cachedFile) && ($cacheLength === - 1 || filemtime($cachedFile) > ($_SERVER['REQUEST_TIME'] - $cacheLength)) && $this->isValidCompiledFile($this->getCompiledFilename($core))) { |
|
| 280 | - // cache is still valid and can be loaded |
|
| 281 | - self::$cache['cached'][$this->cacheId] = true; |
|
| 282 | - |
|
| 283 | - return $cachedFile; |
|
| 284 | - } else { |
|
| 285 | - // file is cacheable |
|
| 286 | - return true; |
|
| 287 | - } |
|
| 288 | - } |
|
| 289 | - |
|
| 290 | - /** |
|
| 291 | - * Caches the provided output into the cache file. |
|
| 292 | - * |
|
| 293 | - * @param Core $core the dwoo instance that requests it |
|
| 294 | - * @param string $output the template output |
|
| 295 | - * |
|
| 296 | - * @return mixed full path of the cached file or false upon failure |
|
| 297 | - */ |
|
| 298 | - public function cache(Core $core, $output) |
|
| 299 | - { |
|
| 300 | - $cacheDir = $core->getCacheDir(); |
|
| 301 | - $cachedFile = $this->getCacheFilename($core); |
|
| 302 | - |
|
| 303 | - // the code below is courtesy of Rasmus Schultz, |
|
| 304 | - // thanks for his help on avoiding concurency issues |
|
| 305 | - $temp = tempnam($cacheDir, 'temp'); |
|
| 306 | - if (!($file = @fopen($temp, 'wb'))) { |
|
| 307 | - $temp = $cacheDir . uniqid('temp'); |
|
| 308 | - if (!($file = @fopen($temp, 'wb'))) { |
|
| 309 | - trigger_error('Error writing temporary file \'' . $temp . '\'', E_USER_WARNING); |
|
| 310 | - |
|
| 311 | - return false; |
|
| 312 | - } |
|
| 313 | - } |
|
| 314 | - |
|
| 315 | - fwrite($file, $output); |
|
| 316 | - fclose($file); |
|
| 317 | - |
|
| 318 | - $this->makeDirectory(dirname($cachedFile), $cacheDir); |
|
| 319 | - if (!@rename($temp, $cachedFile)) { |
|
| 320 | - @unlink($cachedFile); |
|
| 321 | - @rename($temp, $cachedFile); |
|
| 322 | - } |
|
| 323 | - |
|
| 324 | - if ($this->chmod !== null) { |
|
| 325 | - chmod($cachedFile, $this->chmod); |
|
| 326 | - } |
|
| 327 | - |
|
| 328 | - self::$cache['cached'][$this->cacheId] = true; |
|
| 329 | - |
|
| 330 | - return $cachedFile; |
|
| 331 | - } |
|
| 332 | - |
|
| 333 | - /** |
|
| 334 | - * Clears the cached template if it's older than the given time. |
|
| 335 | - * |
|
| 336 | - * @param Core $core the dwoo instance that was used to cache that template |
|
| 337 | - * @param int $olderThan minimum time (in seconds) required for the cache to be cleared |
|
| 338 | - * |
|
| 339 | - * @return bool true if the cache was not present or if it was deleted, false if it remains there |
|
| 340 | - */ |
|
| 341 | - public function clearCache(Core $core, $olderThan = - 1) |
|
| 342 | - { |
|
| 343 | - $cachedFile = $this->getCacheFilename($core); |
|
| 344 | - |
|
| 345 | - return !file_exists($cachedFile) || (filectime($cachedFile) < (time() - $olderThan) && unlink($cachedFile)); |
|
| 346 | - } |
|
| 347 | - |
|
| 348 | - /** |
|
| 349 | - * Returns the compiled template file name. |
|
| 350 | - * |
|
| 351 | - * @param Core $core the dwoo instance that requests it |
|
| 352 | - * @param ICompiler $compiler the compiler that must be used |
|
| 353 | - * |
|
| 354 | - * @return string |
|
| 355 | - */ |
|
| 356 | - public function getCompiledTemplate(Core $core, ICompiler $compiler = null) |
|
| 357 | - { |
|
| 358 | - $compiledFile = $this->getCompiledFilename($core); |
|
| 359 | - |
|
| 360 | - if ($this->compilationEnforced !== true && isset(self::$cache['compiled'][$this->compileId]) === true) { |
|
| 361 | - // already checked, return compiled file |
|
| 362 | - } elseif ($this->compilationEnforced !== true && $this->isValidCompiledFile($compiledFile)) { |
|
| 363 | - // template is compiled |
|
| 364 | - self::$cache['compiled'][$this->compileId] = true; |
|
| 365 | - } else { |
|
| 366 | - // compiles the template |
|
| 367 | - $this->compilationEnforced = false; |
|
| 368 | - |
|
| 369 | - if ($compiler === null) { |
|
| 370 | - $compiler = $core->getDefaultCompilerFactory($this->getResourceName()); |
|
| 371 | - |
|
| 372 | - if ($compiler === null || $compiler === array('Dwoo\Compiler', 'compilerFactory')) { |
|
| 373 | - $compiler = Compiler::compilerFactory(); |
|
| 374 | - } else { |
|
| 375 | - $compiler = call_user_func($compiler); |
|
| 376 | - } |
|
| 377 | - } |
|
| 378 | - |
|
| 379 | - $this->compiler = $compiler; |
|
| 380 | - |
|
| 381 | - $compiler->setCustomPlugins($core->getCustomPlugins()); |
|
| 382 | - $compiler->setSecurityPolicy($core->getSecurityPolicy()); |
|
| 383 | - $this->makeDirectory(dirname($compiledFile), $core->getCompileDir()); |
|
| 384 | - file_put_contents($compiledFile, $compiler->compile($core, $this)); |
|
| 385 | - if ($this->chmod !== null) { |
|
| 386 | - chmod($compiledFile, $this->chmod); |
|
| 387 | - } |
|
| 388 | - |
|
| 389 | - if (extension_loaded('Zend OPcache')) { |
|
| 390 | - opcache_invalidate($compiledFile); |
|
| 391 | - } elseif (extension_loaded('apc') && ini_get('apc.enabled')) { |
|
| 392 | - apc_delete_file($compiledFile); |
|
| 393 | - } |
|
| 394 | - |
|
| 395 | - self::$cache['compiled'][$this->compileId] = true; |
|
| 396 | - } |
|
| 397 | - |
|
| 398 | - return $compiledFile; |
|
| 399 | - } |
|
| 400 | - |
|
| 401 | - /** |
|
| 402 | - * Checks if compiled file is valid (it exists). |
|
| 403 | - * |
|
| 404 | - * @param string $file |
|
| 405 | - * |
|
| 406 | - * @return bool True cache file existance |
|
| 407 | - */ |
|
| 408 | - protected function isValidCompiledFile($file) |
|
| 409 | - { |
|
| 410 | - return file_exists($file); |
|
| 411 | - } |
|
| 412 | - |
|
| 413 | - /** |
|
| 414 | - * Returns a new template string object with the resource id being the template source code. |
|
| 415 | - * |
|
| 416 | - * @param Core $core the dwoo instance requiring it |
|
| 417 | - * @param mixed $resourceId the filename (relative to this template's dir) of the template to include |
|
| 418 | - * @param int $cacheTime duration of the cache validity for this template, if null it defaults to the |
|
| 419 | - * Dwoo instance that will render this template if null it defaults to the Dwoo |
|
| 420 | - * instance that will render this template |
|
| 421 | - * @param string $cacheId the unique cache identifier of this page or anything else that makes this |
|
| 422 | - * template's content unique, if null it defaults to the current url makes this |
|
| 423 | - * template's content unique, if null it defaults to the current url |
|
| 424 | - * @param string $compileId the unique compiled identifier, which is used to distinguish this template from |
|
| 425 | - * others, if null it defaults to the filename+bits of the path template from |
|
| 426 | - * others, if null it defaults to the filename+bits of the path |
|
| 427 | - * @param ITemplate $parentTemplate the template that is requesting a new template object (through an include, |
|
| 428 | - * extends or any other plugin) an include, extends or any other plugin) |
|
| 429 | - * |
|
| 430 | - * @return $this |
|
| 431 | - */ |
|
| 432 | - public static function templateFactory(Core $core, $resourceId, $cacheTime = null, $cacheId = null, $compileId = null, ITemplate $parentTemplate = null) |
|
| 433 | - { |
|
| 434 | - return new self($resourceId, $cacheTime, $cacheId, $compileId); |
|
| 435 | - } |
|
| 436 | - |
|
| 437 | - /** |
|
| 438 | - * Returns the full compiled file name and assigns a default value to it if |
|
| 439 | - * required. |
|
| 440 | - * |
|
| 441 | - * @param Core $core the dwoo instance that requests the file name |
|
| 442 | - * |
|
| 443 | - * @return string the full path to the compiled file |
|
| 444 | - */ |
|
| 445 | - protected function getCompiledFilename(Core $core) |
|
| 446 | - { |
|
| 447 | - // no compile id was provided, set default |
|
| 448 | - if ($this->compileId === null) { |
|
| 449 | - $this->compileId = $this->name; |
|
| 450 | - } |
|
| 451 | - |
|
| 452 | - return $core->getCompileDir() . $this->compileId . '.d' . Core::RELEASE_TAG . '.php'; |
|
| 453 | - } |
|
| 454 | - |
|
| 455 | - /** |
|
| 456 | - * Returns the full cached file name and assigns a default value to it if |
|
| 457 | - * required. |
|
| 458 | - * |
|
| 459 | - * @param Core $core the dwoo instance that requests the file name |
|
| 460 | - * |
|
| 461 | - * @return string the full path to the cached file |
|
| 462 | - */ |
|
| 463 | - protected function getCacheFilename(Core $core) |
|
| 464 | - { |
|
| 465 | - // no cache id provided, use request_uri as default |
|
| 466 | - if ($this->cacheId === null) { |
|
| 467 | - if (isset($_SERVER['REQUEST_URI']) === true) { |
|
| 468 | - $cacheId = $_SERVER['REQUEST_URI']; |
|
| 469 | - } elseif (isset($_SERVER['SCRIPT_FILENAME']) && isset($_SERVER['argv'])) { |
|
| 470 | - $cacheId = $_SERVER['SCRIPT_FILENAME'] . '-' . implode('-', $_SERVER['argv']); |
|
| 471 | - } else { |
|
| 472 | - $cacheId = ''; |
|
| 473 | - } |
|
| 474 | - // force compiled id generation |
|
| 475 | - $this->getCompiledFilename($core); |
|
| 476 | - |
|
| 477 | - $this->cacheId = str_replace('../', '__', $this->compileId . strtr($cacheId, '\\%?=!:;' . PATH_SEPARATOR, '/-------')); |
|
| 478 | - } |
|
| 479 | - |
|
| 480 | - return $core->getCacheDir() . $this->cacheId . '.html'; |
|
| 481 | - } |
|
| 482 | - |
|
| 483 | - /** |
|
| 484 | - * Returns some php code that will check if this template has been modified or not. |
|
| 485 | - * if the function returns null, the template will be instanciated and then the Uid checked |
|
| 486 | - * |
|
| 487 | - * @return string |
|
| 488 | - */ |
|
| 489 | - public function getIsModifiedCode() |
|
| 490 | - { |
|
| 491 | - return null; |
|
| 492 | - } |
|
| 493 | - |
|
| 494 | - /** |
|
| 495 | - * Ensures the given path exists. |
|
| 496 | - * |
|
| 497 | - * @param string $path any path |
|
| 498 | - * @param string $baseDir the base directory where the directory is created |
|
| 499 | - * ($path must still contain the full path, $baseDir |
|
| 500 | - * is only used for unix permissions) |
|
| 501 | - * |
|
| 502 | - * @throws Exception |
|
| 503 | - */ |
|
| 504 | - protected function makeDirectory($path, $baseDir = null) |
|
| 505 | - { |
|
| 506 | - if (is_dir($path) === true) { |
|
| 507 | - return; |
|
| 508 | - } |
|
| 509 | - |
|
| 510 | - if ($this->chmod === null) { |
|
| 511 | - $chmod = 0777; |
|
| 512 | - } else { |
|
| 513 | - $chmod = $this->chmod; |
|
| 514 | - } |
|
| 515 | - |
|
| 516 | - $retries = 3; |
|
| 517 | - while ($retries --) { |
|
| 518 | - @mkdir($path, $chmod, true); |
|
| 519 | - if (is_dir($path)) { |
|
| 520 | - break; |
|
| 521 | - } |
|
| 522 | - usleep(20); |
|
| 523 | - } |
|
| 524 | - |
|
| 525 | - // enforce the correct mode for all directories created |
|
| 526 | - if (strpos(PHP_OS, 'WIN') !== 0 && $baseDir !== null) { |
|
| 527 | - $path = strtr(str_replace($baseDir, '', $path), '\\', '/'); |
|
| 528 | - $folders = explode('/', trim($path, '/')); |
|
| 529 | - foreach ($folders as $folder) { |
|
| 530 | - $baseDir .= $folder . DIRECTORY_SEPARATOR; |
|
| 531 | - if (!chmod($baseDir, $chmod)) { |
|
| 532 | - throw new Exception('Unable to chmod ' . "$baseDir to $chmod: " . print_r(error_get_last(), true)); |
|
| 533 | - } |
|
| 534 | - } |
|
| 535 | - } |
|
| 536 | - } |
|
| 32 | + /** |
|
| 33 | + * Template name. |
|
| 34 | + * |
|
| 35 | + * @var string |
|
| 36 | + */ |
|
| 37 | + protected $name; |
|
| 38 | + |
|
| 39 | + /** |
|
| 40 | + * Template compilation id. |
|
| 41 | + * |
|
| 42 | + * @var string |
|
| 43 | + */ |
|
| 44 | + protected $compileId; |
|
| 45 | + |
|
| 46 | + /** |
|
| 47 | + * Template cache id, if not provided in the constructor, it is set to |
|
| 48 | + * the md4 hash of the request_uri. it is however highly recommended to |
|
| 49 | + * provide one that will fit your needs. |
|
| 50 | + * in all cases, the compilation id is prepended to the cache id to separate |
|
| 51 | + * templates with similar cache ids from one another |
|
| 52 | + * |
|
| 53 | + * @var string |
|
| 54 | + */ |
|
| 55 | + protected $cacheId; |
|
| 56 | + |
|
| 57 | + /** |
|
| 58 | + * Validity duration of the generated cache file (in seconds). |
|
| 59 | + * set to -1 for infinite cache, 0 to disable and null to inherit the Dwoo instance's cache time |
|
| 60 | + * |
|
| 61 | + * @var int |
|
| 62 | + */ |
|
| 63 | + protected $cacheTime; |
|
| 64 | + |
|
| 65 | + /** |
|
| 66 | + * Boolean flag that defines whether the compilation should be enforced (once) or |
|
| 67 | + * not use this if you have issues with the compiled templates not being updated |
|
| 68 | + * but if you do need this it's most likely that you should file a bug report. |
|
| 69 | + * |
|
| 70 | + * @var bool |
|
| 71 | + */ |
|
| 72 | + protected $compilationEnforced; |
|
| 73 | + |
|
| 74 | + /** |
|
| 75 | + * Caches the results of the file checks to save some time when the same |
|
| 76 | + * templates is rendered several times. |
|
| 77 | + * |
|
| 78 | + * @var array |
|
| 79 | + */ |
|
| 80 | + protected static $cache = array( |
|
| 81 | + 'cached' => array(), |
|
| 82 | + 'compiled' => array() |
|
| 83 | + ); |
|
| 84 | + |
|
| 85 | + /** |
|
| 86 | + * Holds the compiler that built this template. |
|
| 87 | + * |
|
| 88 | + * @var ICompiler |
|
| 89 | + */ |
|
| 90 | + protected $compiler; |
|
| 91 | + |
|
| 92 | + /** |
|
| 93 | + * Chmod value for all files written (cached or compiled ones). |
|
| 94 | + * set to null if you don't want any chmod operation to happen |
|
| 95 | + * |
|
| 96 | + * @var int |
|
| 97 | + */ |
|
| 98 | + protected $chmod = 0777; |
|
| 99 | + |
|
| 100 | + /** |
|
| 101 | + * Containing template string. |
|
| 102 | + * |
|
| 103 | + * @var string |
|
| 104 | + */ |
|
| 105 | + protected $template; |
|
| 106 | + |
|
| 107 | + /** |
|
| 108 | + * Creates a template from a string. |
|
| 109 | + * |
|
| 110 | + * @param string $templateString the template to use |
|
| 111 | + * @param int $cacheTime duration of the cache validity for this template, |
|
| 112 | + * if null it defaults to the Dwoo instance that will |
|
| 113 | + * render this template, set to -1 for infinite cache or 0 to disable |
|
| 114 | + * @param string $cacheId the unique cache identifier of this page or anything else that |
|
| 115 | + * makes this template's content unique, if null it defaults |
|
| 116 | + * to the current url |
|
| 117 | + * @param string $compileId the unique compiled identifier, which is used to distinguish this |
|
| 118 | + * template from others, if null it defaults to the md4 hash of the template |
|
| 119 | + */ |
|
| 120 | + public function __construct($templateString, $cacheTime = null, $cacheId = null, $compileId = null) |
|
| 121 | + { |
|
| 122 | + $this->template = $templateString; |
|
| 123 | + $this->name = hash('md4', $templateString); |
|
| 124 | + $this->cacheTime = $cacheTime; |
|
| 125 | + |
|
| 126 | + if ($compileId !== null) { |
|
| 127 | + $this->compileId = str_replace('../', '__', strtr($compileId, '\\%?=!:;' . PATH_SEPARATOR, '/-------')); |
|
| 128 | + } |
|
| 129 | + |
|
| 130 | + if ($cacheId !== null) { |
|
| 131 | + $this->cacheId = str_replace('../', '__', strtr($cacheId, '\\%?=!:;' . PATH_SEPARATOR, '/-------')); |
|
| 132 | + } |
|
| 133 | + } |
|
| 134 | + |
|
| 135 | + /** |
|
| 136 | + * Returns the cache duration for this template. |
|
| 137 | + * defaults to null if it was not provided |
|
| 138 | + * |
|
| 139 | + * @return int|null |
|
| 140 | + */ |
|
| 141 | + public function getCacheTime() |
|
| 142 | + { |
|
| 143 | + return $this->cacheTime; |
|
| 144 | + } |
|
| 145 | + |
|
| 146 | + /** |
|
| 147 | + * Sets the cache duration for this template. |
|
| 148 | + * can be used to set it after the object is created if you did not provide |
|
| 149 | + * it in the constructor |
|
| 150 | + * |
|
| 151 | + * @param int $seconds duration of the cache validity for this template, if |
|
| 152 | + * null it defaults to the Dwoo instance's cache time. 0 = disable and |
|
| 153 | + * -1 = infinite cache |
|
| 154 | + */ |
|
| 155 | + public function setCacheTime($seconds = null) |
|
| 156 | + { |
|
| 157 | + $this->cacheTime = $seconds; |
|
| 158 | + } |
|
| 159 | + |
|
| 160 | + /** |
|
| 161 | + * Returns the chmod value for all files written (cached or compiled ones). |
|
| 162 | + * defaults to 0777 |
|
| 163 | + * |
|
| 164 | + * @return int|null |
|
| 165 | + */ |
|
| 166 | + public function getChmod() |
|
| 167 | + { |
|
| 168 | + return $this->chmod; |
|
| 169 | + } |
|
| 170 | + |
|
| 171 | + /** |
|
| 172 | + * Set the chmod value for all files written (cached or compiled ones). |
|
| 173 | + * set to null if you don't want to do any chmod() operation |
|
| 174 | + * |
|
| 175 | + * @param int $mask new bitmask to use for all files |
|
| 176 | + */ |
|
| 177 | + public function setChmod($mask = null) |
|
| 178 | + { |
|
| 179 | + $this->chmod = $mask; |
|
| 180 | + } |
|
| 181 | + |
|
| 182 | + /** |
|
| 183 | + * Returns the template name. |
|
| 184 | + * |
|
| 185 | + * @return string |
|
| 186 | + */ |
|
| 187 | + public function getName() |
|
| 188 | + { |
|
| 189 | + return $this->name; |
|
| 190 | + } |
|
| 191 | + |
|
| 192 | + /** |
|
| 193 | + * Returns the resource name for this template class. |
|
| 194 | + * |
|
| 195 | + * @return string |
|
| 196 | + */ |
|
| 197 | + public function getResourceName() |
|
| 198 | + { |
|
| 199 | + return 'string'; |
|
| 200 | + } |
|
| 201 | + |
|
| 202 | + /** |
|
| 203 | + * Returns the resource identifier for this template, false here as strings don't have identifiers. |
|
| 204 | + * |
|
| 205 | + * @return false |
|
| 206 | + */ |
|
| 207 | + public function getResourceIdentifier() |
|
| 208 | + { |
|
| 209 | + return false; |
|
| 210 | + } |
|
| 211 | + |
|
| 212 | + /** |
|
| 213 | + * Returns the template source of this template. |
|
| 214 | + * |
|
| 215 | + * @return string |
|
| 216 | + */ |
|
| 217 | + public function getSource() |
|
| 218 | + { |
|
| 219 | + return $this->template; |
|
| 220 | + } |
|
| 221 | + |
|
| 222 | + /** |
|
| 223 | + * Returns an unique value identifying the current version of this template, |
|
| 224 | + * in this case it's the md4 hash of the content. |
|
| 225 | + * |
|
| 226 | + * @return string |
|
| 227 | + */ |
|
| 228 | + public function getUid() |
|
| 229 | + { |
|
| 230 | + return $this->name; |
|
| 231 | + } |
|
| 232 | + |
|
| 233 | + /** |
|
| 234 | + * Returns the compiler used by this template, if it was just compiled, or null. |
|
| 235 | + * |
|
| 236 | + * @return ICompiler |
|
| 237 | + */ |
|
| 238 | + public function getCompiler() |
|
| 239 | + { |
|
| 240 | + return $this->compiler; |
|
| 241 | + } |
|
| 242 | + |
|
| 243 | + /** |
|
| 244 | + * Marks this template as compile-forced, which means it will be recompiled even if it |
|
| 245 | + * was already saved and wasn't modified since the last compilation. do not use this in production, |
|
| 246 | + * it's only meant to be used in development (and the development of dwoo particularly). |
|
| 247 | + */ |
|
| 248 | + public function forceCompilation() |
|
| 249 | + { |
|
| 250 | + $this->compilationEnforced = true; |
|
| 251 | + } |
|
| 252 | + |
|
| 253 | + /** |
|
| 254 | + * Returns the cached template output file name, true if it's cache-able but not cached |
|
| 255 | + * or false if it's not cached. |
|
| 256 | + * |
|
| 257 | + * @param Core $core the dwoo instance that requests it |
|
| 258 | + * |
|
| 259 | + * @return string|bool |
|
| 260 | + */ |
|
| 261 | + public function getCachedTemplate(Core $core) |
|
| 262 | + { |
|
| 263 | + if ($this->cacheTime !== null) { |
|
| 264 | + $cacheLength = $this->cacheTime; |
|
| 265 | + } else { |
|
| 266 | + $cacheLength = $core->getCacheTime(); |
|
| 267 | + } |
|
| 268 | + |
|
| 269 | + // file is not cacheable |
|
| 270 | + if ($cacheLength == 0) { |
|
| 271 | + return false; |
|
| 272 | + } |
|
| 273 | + |
|
| 274 | + $cachedFile = $this->getCacheFilename($core); |
|
| 275 | + |
|
| 276 | + if (isset(self::$cache['cached'][$this->cacheId]) === true && file_exists($cachedFile)) { |
|
| 277 | + // already checked, return cache file |
|
| 278 | + return $cachedFile; |
|
| 279 | + } elseif ($this->compilationEnforced !== true && file_exists($cachedFile) && ($cacheLength === - 1 || filemtime($cachedFile) > ($_SERVER['REQUEST_TIME'] - $cacheLength)) && $this->isValidCompiledFile($this->getCompiledFilename($core))) { |
|
| 280 | + // cache is still valid and can be loaded |
|
| 281 | + self::$cache['cached'][$this->cacheId] = true; |
|
| 282 | + |
|
| 283 | + return $cachedFile; |
|
| 284 | + } else { |
|
| 285 | + // file is cacheable |
|
| 286 | + return true; |
|
| 287 | + } |
|
| 288 | + } |
|
| 289 | + |
|
| 290 | + /** |
|
| 291 | + * Caches the provided output into the cache file. |
|
| 292 | + * |
|
| 293 | + * @param Core $core the dwoo instance that requests it |
|
| 294 | + * @param string $output the template output |
|
| 295 | + * |
|
| 296 | + * @return mixed full path of the cached file or false upon failure |
|
| 297 | + */ |
|
| 298 | + public function cache(Core $core, $output) |
|
| 299 | + { |
|
| 300 | + $cacheDir = $core->getCacheDir(); |
|
| 301 | + $cachedFile = $this->getCacheFilename($core); |
|
| 302 | + |
|
| 303 | + // the code below is courtesy of Rasmus Schultz, |
|
| 304 | + // thanks for his help on avoiding concurency issues |
|
| 305 | + $temp = tempnam($cacheDir, 'temp'); |
|
| 306 | + if (!($file = @fopen($temp, 'wb'))) { |
|
| 307 | + $temp = $cacheDir . uniqid('temp'); |
|
| 308 | + if (!($file = @fopen($temp, 'wb'))) { |
|
| 309 | + trigger_error('Error writing temporary file \'' . $temp . '\'', E_USER_WARNING); |
|
| 310 | + |
|
| 311 | + return false; |
|
| 312 | + } |
|
| 313 | + } |
|
| 314 | + |
|
| 315 | + fwrite($file, $output); |
|
| 316 | + fclose($file); |
|
| 317 | + |
|
| 318 | + $this->makeDirectory(dirname($cachedFile), $cacheDir); |
|
| 319 | + if (!@rename($temp, $cachedFile)) { |
|
| 320 | + @unlink($cachedFile); |
|
| 321 | + @rename($temp, $cachedFile); |
|
| 322 | + } |
|
| 323 | + |
|
| 324 | + if ($this->chmod !== null) { |
|
| 325 | + chmod($cachedFile, $this->chmod); |
|
| 326 | + } |
|
| 327 | + |
|
| 328 | + self::$cache['cached'][$this->cacheId] = true; |
|
| 329 | + |
|
| 330 | + return $cachedFile; |
|
| 331 | + } |
|
| 332 | + |
|
| 333 | + /** |
|
| 334 | + * Clears the cached template if it's older than the given time. |
|
| 335 | + * |
|
| 336 | + * @param Core $core the dwoo instance that was used to cache that template |
|
| 337 | + * @param int $olderThan minimum time (in seconds) required for the cache to be cleared |
|
| 338 | + * |
|
| 339 | + * @return bool true if the cache was not present or if it was deleted, false if it remains there |
|
| 340 | + */ |
|
| 341 | + public function clearCache(Core $core, $olderThan = - 1) |
|
| 342 | + { |
|
| 343 | + $cachedFile = $this->getCacheFilename($core); |
|
| 344 | + |
|
| 345 | + return !file_exists($cachedFile) || (filectime($cachedFile) < (time() - $olderThan) && unlink($cachedFile)); |
|
| 346 | + } |
|
| 347 | + |
|
| 348 | + /** |
|
| 349 | + * Returns the compiled template file name. |
|
| 350 | + * |
|
| 351 | + * @param Core $core the dwoo instance that requests it |
|
| 352 | + * @param ICompiler $compiler the compiler that must be used |
|
| 353 | + * |
|
| 354 | + * @return string |
|
| 355 | + */ |
|
| 356 | + public function getCompiledTemplate(Core $core, ICompiler $compiler = null) |
|
| 357 | + { |
|
| 358 | + $compiledFile = $this->getCompiledFilename($core); |
|
| 359 | + |
|
| 360 | + if ($this->compilationEnforced !== true && isset(self::$cache['compiled'][$this->compileId]) === true) { |
|
| 361 | + // already checked, return compiled file |
|
| 362 | + } elseif ($this->compilationEnforced !== true && $this->isValidCompiledFile($compiledFile)) { |
|
| 363 | + // template is compiled |
|
| 364 | + self::$cache['compiled'][$this->compileId] = true; |
|
| 365 | + } else { |
|
| 366 | + // compiles the template |
|
| 367 | + $this->compilationEnforced = false; |
|
| 368 | + |
|
| 369 | + if ($compiler === null) { |
|
| 370 | + $compiler = $core->getDefaultCompilerFactory($this->getResourceName()); |
|
| 371 | + |
|
| 372 | + if ($compiler === null || $compiler === array('Dwoo\Compiler', 'compilerFactory')) { |
|
| 373 | + $compiler = Compiler::compilerFactory(); |
|
| 374 | + } else { |
|
| 375 | + $compiler = call_user_func($compiler); |
|
| 376 | + } |
|
| 377 | + } |
|
| 378 | + |
|
| 379 | + $this->compiler = $compiler; |
|
| 380 | + |
|
| 381 | + $compiler->setCustomPlugins($core->getCustomPlugins()); |
|
| 382 | + $compiler->setSecurityPolicy($core->getSecurityPolicy()); |
|
| 383 | + $this->makeDirectory(dirname($compiledFile), $core->getCompileDir()); |
|
| 384 | + file_put_contents($compiledFile, $compiler->compile($core, $this)); |
|
| 385 | + if ($this->chmod !== null) { |
|
| 386 | + chmod($compiledFile, $this->chmod); |
|
| 387 | + } |
|
| 388 | + |
|
| 389 | + if (extension_loaded('Zend OPcache')) { |
|
| 390 | + opcache_invalidate($compiledFile); |
|
| 391 | + } elseif (extension_loaded('apc') && ini_get('apc.enabled')) { |
|
| 392 | + apc_delete_file($compiledFile); |
|
| 393 | + } |
|
| 394 | + |
|
| 395 | + self::$cache['compiled'][$this->compileId] = true; |
|
| 396 | + } |
|
| 397 | + |
|
| 398 | + return $compiledFile; |
|
| 399 | + } |
|
| 400 | + |
|
| 401 | + /** |
|
| 402 | + * Checks if compiled file is valid (it exists). |
|
| 403 | + * |
|
| 404 | + * @param string $file |
|
| 405 | + * |
|
| 406 | + * @return bool True cache file existance |
|
| 407 | + */ |
|
| 408 | + protected function isValidCompiledFile($file) |
|
| 409 | + { |
|
| 410 | + return file_exists($file); |
|
| 411 | + } |
|
| 412 | + |
|
| 413 | + /** |
|
| 414 | + * Returns a new template string object with the resource id being the template source code. |
|
| 415 | + * |
|
| 416 | + * @param Core $core the dwoo instance requiring it |
|
| 417 | + * @param mixed $resourceId the filename (relative to this template's dir) of the template to include |
|
| 418 | + * @param int $cacheTime duration of the cache validity for this template, if null it defaults to the |
|
| 419 | + * Dwoo instance that will render this template if null it defaults to the Dwoo |
|
| 420 | + * instance that will render this template |
|
| 421 | + * @param string $cacheId the unique cache identifier of this page or anything else that makes this |
|
| 422 | + * template's content unique, if null it defaults to the current url makes this |
|
| 423 | + * template's content unique, if null it defaults to the current url |
|
| 424 | + * @param string $compileId the unique compiled identifier, which is used to distinguish this template from |
|
| 425 | + * others, if null it defaults to the filename+bits of the path template from |
|
| 426 | + * others, if null it defaults to the filename+bits of the path |
|
| 427 | + * @param ITemplate $parentTemplate the template that is requesting a new template object (through an include, |
|
| 428 | + * extends or any other plugin) an include, extends or any other plugin) |
|
| 429 | + * |
|
| 430 | + * @return $this |
|
| 431 | + */ |
|
| 432 | + public static function templateFactory(Core $core, $resourceId, $cacheTime = null, $cacheId = null, $compileId = null, ITemplate $parentTemplate = null) |
|
| 433 | + { |
|
| 434 | + return new self($resourceId, $cacheTime, $cacheId, $compileId); |
|
| 435 | + } |
|
| 436 | + |
|
| 437 | + /** |
|
| 438 | + * Returns the full compiled file name and assigns a default value to it if |
|
| 439 | + * required. |
|
| 440 | + * |
|
| 441 | + * @param Core $core the dwoo instance that requests the file name |
|
| 442 | + * |
|
| 443 | + * @return string the full path to the compiled file |
|
| 444 | + */ |
|
| 445 | + protected function getCompiledFilename(Core $core) |
|
| 446 | + { |
|
| 447 | + // no compile id was provided, set default |
|
| 448 | + if ($this->compileId === null) { |
|
| 449 | + $this->compileId = $this->name; |
|
| 450 | + } |
|
| 451 | + |
|
| 452 | + return $core->getCompileDir() . $this->compileId . '.d' . Core::RELEASE_TAG . '.php'; |
|
| 453 | + } |
|
| 454 | + |
|
| 455 | + /** |
|
| 456 | + * Returns the full cached file name and assigns a default value to it if |
|
| 457 | + * required. |
|
| 458 | + * |
|
| 459 | + * @param Core $core the dwoo instance that requests the file name |
|
| 460 | + * |
|
| 461 | + * @return string the full path to the cached file |
|
| 462 | + */ |
|
| 463 | + protected function getCacheFilename(Core $core) |
|
| 464 | + { |
|
| 465 | + // no cache id provided, use request_uri as default |
|
| 466 | + if ($this->cacheId === null) { |
|
| 467 | + if (isset($_SERVER['REQUEST_URI']) === true) { |
|
| 468 | + $cacheId = $_SERVER['REQUEST_URI']; |
|
| 469 | + } elseif (isset($_SERVER['SCRIPT_FILENAME']) && isset($_SERVER['argv'])) { |
|
| 470 | + $cacheId = $_SERVER['SCRIPT_FILENAME'] . '-' . implode('-', $_SERVER['argv']); |
|
| 471 | + } else { |
|
| 472 | + $cacheId = ''; |
|
| 473 | + } |
|
| 474 | + // force compiled id generation |
|
| 475 | + $this->getCompiledFilename($core); |
|
| 476 | + |
|
| 477 | + $this->cacheId = str_replace('../', '__', $this->compileId . strtr($cacheId, '\\%?=!:;' . PATH_SEPARATOR, '/-------')); |
|
| 478 | + } |
|
| 479 | + |
|
| 480 | + return $core->getCacheDir() . $this->cacheId . '.html'; |
|
| 481 | + } |
|
| 482 | + |
|
| 483 | + /** |
|
| 484 | + * Returns some php code that will check if this template has been modified or not. |
|
| 485 | + * if the function returns null, the template will be instanciated and then the Uid checked |
|
| 486 | + * |
|
| 487 | + * @return string |
|
| 488 | + */ |
|
| 489 | + public function getIsModifiedCode() |
|
| 490 | + { |
|
| 491 | + return null; |
|
| 492 | + } |
|
| 493 | + |
|
| 494 | + /** |
|
| 495 | + * Ensures the given path exists. |
|
| 496 | + * |
|
| 497 | + * @param string $path any path |
|
| 498 | + * @param string $baseDir the base directory where the directory is created |
|
| 499 | + * ($path must still contain the full path, $baseDir |
|
| 500 | + * is only used for unix permissions) |
|
| 501 | + * |
|
| 502 | + * @throws Exception |
|
| 503 | + */ |
|
| 504 | + protected function makeDirectory($path, $baseDir = null) |
|
| 505 | + { |
|
| 506 | + if (is_dir($path) === true) { |
|
| 507 | + return; |
|
| 508 | + } |
|
| 509 | + |
|
| 510 | + if ($this->chmod === null) { |
|
| 511 | + $chmod = 0777; |
|
| 512 | + } else { |
|
| 513 | + $chmod = $this->chmod; |
|
| 514 | + } |
|
| 515 | + |
|
| 516 | + $retries = 3; |
|
| 517 | + while ($retries --) { |
|
| 518 | + @mkdir($path, $chmod, true); |
|
| 519 | + if (is_dir($path)) { |
|
| 520 | + break; |
|
| 521 | + } |
|
| 522 | + usleep(20); |
|
| 523 | + } |
|
| 524 | + |
|
| 525 | + // enforce the correct mode for all directories created |
|
| 526 | + if (strpos(PHP_OS, 'WIN') !== 0 && $baseDir !== null) { |
|
| 527 | + $path = strtr(str_replace($baseDir, '', $path), '\\', '/'); |
|
| 528 | + $folders = explode('/', trim($path, '/')); |
|
| 529 | + foreach ($folders as $folder) { |
|
| 530 | + $baseDir .= $folder . DIRECTORY_SEPARATOR; |
|
| 531 | + if (!chmod($baseDir, $chmod)) { |
|
| 532 | + throw new Exception('Unable to chmod ' . "$baseDir to $chmod: " . print_r(error_get_last(), true)); |
|
| 533 | + } |
|
| 534 | + } |
|
| 535 | + } |
|
| 536 | + } |
|
| 537 | 537 | } |
@@ -124,11 +124,11 @@ discard block |
||
| 124 | 124 | $this->cacheTime = $cacheTime; |
| 125 | 125 | |
| 126 | 126 | if ($compileId !== null) { |
| 127 | - $this->compileId = str_replace('../', '__', strtr($compileId, '\\%?=!:;' . PATH_SEPARATOR, '/-------')); |
|
| 127 | + $this->compileId = str_replace('../', '__', strtr($compileId, '\\%?=!:;'.PATH_SEPARATOR, '/-------')); |
|
| 128 | 128 | } |
| 129 | 129 | |
| 130 | 130 | if ($cacheId !== null) { |
| 131 | - $this->cacheId = str_replace('../', '__', strtr($cacheId, '\\%?=!:;' . PATH_SEPARATOR, '/-------')); |
|
| 131 | + $this->cacheId = str_replace('../', '__', strtr($cacheId, '\\%?=!:;'.PATH_SEPARATOR, '/-------')); |
|
| 132 | 132 | } |
| 133 | 133 | } |
| 134 | 134 | |
@@ -276,7 +276,7 @@ discard block |
||
| 276 | 276 | if (isset(self::$cache['cached'][$this->cacheId]) === true && file_exists($cachedFile)) { |
| 277 | 277 | // already checked, return cache file |
| 278 | 278 | return $cachedFile; |
| 279 | - } elseif ($this->compilationEnforced !== true && file_exists($cachedFile) && ($cacheLength === - 1 || filemtime($cachedFile) > ($_SERVER['REQUEST_TIME'] - $cacheLength)) && $this->isValidCompiledFile($this->getCompiledFilename($core))) { |
|
| 279 | + } elseif ($this->compilationEnforced !== true && file_exists($cachedFile) && ($cacheLength === - 1 || filemtime($cachedFile) > ($_SERVER['REQUEST_TIME']-$cacheLength)) && $this->isValidCompiledFile($this->getCompiledFilename($core))) { |
|
| 280 | 280 | // cache is still valid and can be loaded |
| 281 | 281 | self::$cache['cached'][$this->cacheId] = true; |
| 282 | 282 | |
@@ -304,9 +304,9 @@ discard block |
||
| 304 | 304 | // thanks for his help on avoiding concurency issues |
| 305 | 305 | $temp = tempnam($cacheDir, 'temp'); |
| 306 | 306 | if (!($file = @fopen($temp, 'wb'))) { |
| 307 | - $temp = $cacheDir . uniqid('temp'); |
|
| 307 | + $temp = $cacheDir.uniqid('temp'); |
|
| 308 | 308 | if (!($file = @fopen($temp, 'wb'))) { |
| 309 | - trigger_error('Error writing temporary file \'' . $temp . '\'', E_USER_WARNING); |
|
| 309 | + trigger_error('Error writing temporary file \''.$temp.'\'', E_USER_WARNING); |
|
| 310 | 310 | |
| 311 | 311 | return false; |
| 312 | 312 | } |
@@ -342,7 +342,7 @@ discard block |
||
| 342 | 342 | { |
| 343 | 343 | $cachedFile = $this->getCacheFilename($core); |
| 344 | 344 | |
| 345 | - return !file_exists($cachedFile) || (filectime($cachedFile) < (time() - $olderThan) && unlink($cachedFile)); |
|
| 345 | + return !file_exists($cachedFile) || (filectime($cachedFile) < (time()-$olderThan) && unlink($cachedFile)); |
|
| 346 | 346 | } |
| 347 | 347 | |
| 348 | 348 | /** |
@@ -449,7 +449,7 @@ discard block |
||
| 449 | 449 | $this->compileId = $this->name; |
| 450 | 450 | } |
| 451 | 451 | |
| 452 | - return $core->getCompileDir() . $this->compileId . '.d' . Core::RELEASE_TAG . '.php'; |
|
| 452 | + return $core->getCompileDir().$this->compileId.'.d'.Core::RELEASE_TAG.'.php'; |
|
| 453 | 453 | } |
| 454 | 454 | |
| 455 | 455 | /** |
@@ -467,17 +467,17 @@ discard block |
||
| 467 | 467 | if (isset($_SERVER['REQUEST_URI']) === true) { |
| 468 | 468 | $cacheId = $_SERVER['REQUEST_URI']; |
| 469 | 469 | } elseif (isset($_SERVER['SCRIPT_FILENAME']) && isset($_SERVER['argv'])) { |
| 470 | - $cacheId = $_SERVER['SCRIPT_FILENAME'] . '-' . implode('-', $_SERVER['argv']); |
|
| 470 | + $cacheId = $_SERVER['SCRIPT_FILENAME'].'-'.implode('-', $_SERVER['argv']); |
|
| 471 | 471 | } else { |
| 472 | 472 | $cacheId = ''; |
| 473 | 473 | } |
| 474 | 474 | // force compiled id generation |
| 475 | 475 | $this->getCompiledFilename($core); |
| 476 | 476 | |
| 477 | - $this->cacheId = str_replace('../', '__', $this->compileId . strtr($cacheId, '\\%?=!:;' . PATH_SEPARATOR, '/-------')); |
|
| 477 | + $this->cacheId = str_replace('../', '__', $this->compileId.strtr($cacheId, '\\%?=!:;'.PATH_SEPARATOR, '/-------')); |
|
| 478 | 478 | } |
| 479 | 479 | |
| 480 | - return $core->getCacheDir() . $this->cacheId . '.html'; |
|
| 480 | + return $core->getCacheDir().$this->cacheId.'.html'; |
|
| 481 | 481 | } |
| 482 | 482 | |
| 483 | 483 | /** |
@@ -514,7 +514,7 @@ discard block |
||
| 514 | 514 | } |
| 515 | 515 | |
| 516 | 516 | $retries = 3; |
| 517 | - while ($retries --) { |
|
| 517 | + while ($retries--) { |
|
| 518 | 518 | @mkdir($path, $chmod, true); |
| 519 | 519 | if (is_dir($path)) { |
| 520 | 520 | break; |
@@ -527,9 +527,9 @@ discard block |
||
| 527 | 527 | $path = strtr(str_replace($baseDir, '', $path), '\\', '/'); |
| 528 | 528 | $folders = explode('/', trim($path, '/')); |
| 529 | 529 | foreach ($folders as $folder) { |
| 530 | - $baseDir .= $folder . DIRECTORY_SEPARATOR; |
|
| 530 | + $baseDir .= $folder.DIRECTORY_SEPARATOR; |
|
| 531 | 531 | if (!chmod($baseDir, $chmod)) { |
| 532 | - throw new Exception('Unable to chmod ' . "$baseDir to $chmod: " . print_r(error_get_last(), true)); |
|
| 532 | + throw new Exception('Unable to chmod '."$baseDir to $chmod: ".print_r(error_get_last(), true)); |
|
| 533 | 533 | } |
| 534 | 534 | } |
| 535 | 535 | } |
@@ -23,23 +23,23 @@ |
||
| 23 | 23 | */ |
| 24 | 24 | class Adapter extends Processor |
| 25 | 25 | { |
| 26 | - public $callback; |
|
| 26 | + public $callback; |
|
| 27 | 27 | |
| 28 | - /** |
|
| 29 | - * @param string $input |
|
| 30 | - * |
|
| 31 | - * @return mixed |
|
| 32 | - */ |
|
| 33 | - public function process($input) |
|
| 34 | - { |
|
| 35 | - return call_user_func($this->callback, $input); |
|
| 36 | - } |
|
| 28 | + /** |
|
| 29 | + * @param string $input |
|
| 30 | + * |
|
| 31 | + * @return mixed |
|
| 32 | + */ |
|
| 33 | + public function process($input) |
|
| 34 | + { |
|
| 35 | + return call_user_func($this->callback, $input); |
|
| 36 | + } |
|
| 37 | 37 | |
| 38 | - /** |
|
| 39 | - * @param $callback |
|
| 40 | - */ |
|
| 41 | - public function registerCallback($callback) |
|
| 42 | - { |
|
| 43 | - $this->callback = $callback; |
|
| 44 | - } |
|
| 38 | + /** |
|
| 39 | + * @param $callback |
|
| 40 | + */ |
|
| 41 | + public function registerCallback($callback) |
|
| 42 | + { |
|
| 43 | + $this->callback = $callback; |
|
| 44 | + } |
|
| 45 | 45 | } |
| 46 | 46 | \ No newline at end of file |
@@ -26,35 +26,35 @@ |
||
| 26 | 26 | */ |
| 27 | 27 | class Exception extends DwooException |
| 28 | 28 | { |
| 29 | - protected $compiler; |
|
| 30 | - protected $template; |
|
| 29 | + protected $compiler; |
|
| 30 | + protected $template; |
|
| 31 | 31 | |
| 32 | - /** |
|
| 33 | - * Exception constructor. |
|
| 34 | - * |
|
| 35 | - * @param DwooCompiler $compiler |
|
| 36 | - * @param int $message |
|
| 37 | - */ |
|
| 38 | - public function __construct(DwooCompiler $compiler, $message) |
|
| 39 | - { |
|
| 40 | - $this->compiler = $compiler; |
|
| 41 | - $this->template = $compiler->getDwoo()->getTemplate(); |
|
| 42 | - parent::__construct('Compilation error at line ' . $compiler->getLine() . ' in "' . $this->template->getResourceName() . ':' . $this->template->getResourceIdentifier() . '" : ' . $message); |
|
| 43 | - } |
|
| 32 | + /** |
|
| 33 | + * Exception constructor. |
|
| 34 | + * |
|
| 35 | + * @param DwooCompiler $compiler |
|
| 36 | + * @param int $message |
|
| 37 | + */ |
|
| 38 | + public function __construct(DwooCompiler $compiler, $message) |
|
| 39 | + { |
|
| 40 | + $this->compiler = $compiler; |
|
| 41 | + $this->template = $compiler->getDwoo()->getTemplate(); |
|
| 42 | + parent::__construct('Compilation error at line ' . $compiler->getLine() . ' in "' . $this->template->getResourceName() . ':' . $this->template->getResourceIdentifier() . '" : ' . $message); |
|
| 43 | + } |
|
| 44 | 44 | |
| 45 | - /** |
|
| 46 | - * @return DwooCompiler |
|
| 47 | - */ |
|
| 48 | - public function getCompiler() |
|
| 49 | - { |
|
| 50 | - return $this->compiler; |
|
| 51 | - } |
|
| 45 | + /** |
|
| 46 | + * @return DwooCompiler |
|
| 47 | + */ |
|
| 48 | + public function getCompiler() |
|
| 49 | + { |
|
| 50 | + return $this->compiler; |
|
| 51 | + } |
|
| 52 | 52 | |
| 53 | - /** |
|
| 54 | - * @return \Dwoo\ITemplate|null |
|
| 55 | - */ |
|
| 56 | - public function getTemplate() |
|
| 57 | - { |
|
| 58 | - return $this->template; |
|
| 59 | - } |
|
| 53 | + /** |
|
| 54 | + * @return \Dwoo\ITemplate|null |
|
| 55 | + */ |
|
| 56 | + public function getTemplate() |
|
| 57 | + { |
|
| 58 | + return $this->template; |
|
| 59 | + } |
|
| 60 | 60 | } |
@@ -39,7 +39,7 @@ |
||
| 39 | 39 | { |
| 40 | 40 | $this->compiler = $compiler; |
| 41 | 41 | $this->template = $compiler->getDwoo()->getTemplate(); |
| 42 | - parent::__construct('Compilation error at line ' . $compiler->getLine() . ' in "' . $this->template->getResourceName() . ':' . $this->template->getResourceIdentifier() . '" : ' . $message); |
|
| 42 | + parent::__construct('Compilation error at line '.$compiler->getLine().' in "'.$this->template->getResourceName().':'.$this->template->getResourceIdentifier().'" : '.$message); |
|
| 43 | 43 | } |
| 44 | 44 | |
| 45 | 45 | /** |
@@ -23,52 +23,52 @@ |
||
| 23 | 23 | */ |
| 24 | 24 | interface IPluginProxy |
| 25 | 25 | { |
| 26 | - /** |
|
| 27 | - * Returns true or false to say whether the given plugin is handled by this proxy or not. |
|
| 28 | - * |
|
| 29 | - * @param string $name the plugin name |
|
| 30 | - * |
|
| 31 | - * @return bool true if the plugin is known and usable, otherwise false |
|
| 32 | - */ |
|
| 33 | - public function handles($name); |
|
| 26 | + /** |
|
| 27 | + * Returns true or false to say whether the given plugin is handled by this proxy or not. |
|
| 28 | + * |
|
| 29 | + * @param string $name the plugin name |
|
| 30 | + * |
|
| 31 | + * @return bool true if the plugin is known and usable, otherwise false |
|
| 32 | + */ |
|
| 33 | + public function handles($name); |
|
| 34 | 34 | |
| 35 | - /** |
|
| 36 | - * Returns the code (as a string) to call the plugin |
|
| 37 | - * (this will be executed at runtime inside the Dwoo class). |
|
| 38 | - * |
|
| 39 | - * @param string $name the plugin name |
|
| 40 | - * @param array $params a parameter array, array key "*" is the rest array |
|
| 41 | - * |
|
| 42 | - * @return string |
|
| 43 | - */ |
|
| 44 | - public function getCode($name, $params); |
|
| 35 | + /** |
|
| 36 | + * Returns the code (as a string) to call the plugin |
|
| 37 | + * (this will be executed at runtime inside the Dwoo class). |
|
| 38 | + * |
|
| 39 | + * @param string $name the plugin name |
|
| 40 | + * @param array $params a parameter array, array key "*" is the rest array |
|
| 41 | + * |
|
| 42 | + * @return string |
|
| 43 | + */ |
|
| 44 | + public function getCode($name, $params); |
|
| 45 | 45 | |
| 46 | - /** |
|
| 47 | - * Returns a callback to the plugin, this is used with the reflection API to |
|
| 48 | - * find out about the plugin's parameter names etc. |
|
| 49 | - * should you need a rest array without the possibility to edit the |
|
| 50 | - * plugin's code, you can provide a callback to some |
|
| 51 | - * other function with the correct parameter signature, i.e. : |
|
| 52 | - * <code> |
|
| 53 | - * return array($this, "callbackHelper"); |
|
| 54 | - * // and callbackHelper would be as such: |
|
| 55 | - * public function callbackHelper(array $rest=array()){} |
|
| 56 | - * </code> |
|
| 57 | - * |
|
| 58 | - * @param string $name the plugin name |
|
| 59 | - * |
|
| 60 | - * @return callback |
|
| 61 | - */ |
|
| 62 | - public function getCallback($name); |
|
| 46 | + /** |
|
| 47 | + * Returns a callback to the plugin, this is used with the reflection API to |
|
| 48 | + * find out about the plugin's parameter names etc. |
|
| 49 | + * should you need a rest array without the possibility to edit the |
|
| 50 | + * plugin's code, you can provide a callback to some |
|
| 51 | + * other function with the correct parameter signature, i.e. : |
|
| 52 | + * <code> |
|
| 53 | + * return array($this, "callbackHelper"); |
|
| 54 | + * // and callbackHelper would be as such: |
|
| 55 | + * public function callbackHelper(array $rest=array()){} |
|
| 56 | + * </code> |
|
| 57 | + * |
|
| 58 | + * @param string $name the plugin name |
|
| 59 | + * |
|
| 60 | + * @return callback |
|
| 61 | + */ |
|
| 62 | + public function getCallback($name); |
|
| 63 | 63 | |
| 64 | - /** |
|
| 65 | - * Returns some code that will check if the plugin is loaded and if not load it |
|
| 66 | - * this is optional, if your plugins are autoloaded or whatever, just return an |
|
| 67 | - * empty string. |
|
| 68 | - * |
|
| 69 | - * @param string $name the plugin name |
|
| 70 | - * |
|
| 71 | - * @return string |
|
| 72 | - */ |
|
| 73 | - public function getLoader($name); |
|
| 64 | + /** |
|
| 65 | + * Returns some code that will check if the plugin is loaded and if not load it |
|
| 66 | + * this is optional, if your plugins are autoloaded or whatever, just return an |
|
| 67 | + * empty string. |
|
| 68 | + * |
|
| 69 | + * @param string $name the plugin name |
|
| 70 | + * |
|
| 71 | + * @return string |
|
| 72 | + */ |
|
| 73 | + public function getLoader($name); |
|
| 74 | 74 | } |
@@ -23,137 +23,137 @@ |
||
| 23 | 23 | */ |
| 24 | 24 | interface ITemplate |
| 25 | 25 | { |
| 26 | - /** |
|
| 27 | - * Returns the cache duration for this template. |
|
| 28 | - * defaults to null if it was not provided |
|
| 29 | - * |
|
| 30 | - * @return int|null |
|
| 31 | - */ |
|
| 32 | - public function getCacheTime(); |
|
| 26 | + /** |
|
| 27 | + * Returns the cache duration for this template. |
|
| 28 | + * defaults to null if it was not provided |
|
| 29 | + * |
|
| 30 | + * @return int|null |
|
| 31 | + */ |
|
| 32 | + public function getCacheTime(); |
|
| 33 | 33 | |
| 34 | - /** |
|
| 35 | - * Sets the cache duration for this template. |
|
| 36 | - * can be used to set it after the object is created if you did not provide |
|
| 37 | - * it in the constructor |
|
| 38 | - * |
|
| 39 | - * @param int $seconds duration of the cache validity for this template, if |
|
| 40 | - * null it defaults to the Dwoo instance's cache time. 0 = disable and |
|
| 41 | - * -1 = infinite cache |
|
| 42 | - */ |
|
| 43 | - public function setCacheTime($seconds = null); |
|
| 34 | + /** |
|
| 35 | + * Sets the cache duration for this template. |
|
| 36 | + * can be used to set it after the object is created if you did not provide |
|
| 37 | + * it in the constructor |
|
| 38 | + * |
|
| 39 | + * @param int $seconds duration of the cache validity for this template, if |
|
| 40 | + * null it defaults to the Dwoo instance's cache time. 0 = disable and |
|
| 41 | + * -1 = infinite cache |
|
| 42 | + */ |
|
| 43 | + public function setCacheTime($seconds = null); |
|
| 44 | 44 | |
| 45 | - /** |
|
| 46 | - * Returns the cached template output file name, true if it's cache-able but not cached |
|
| 47 | - * or false if it's not cached. |
|
| 48 | - * |
|
| 49 | - * @param Core $dwoo the dwoo instance that requests it |
|
| 50 | - * |
|
| 51 | - * @return string|bool |
|
| 52 | - */ |
|
| 53 | - public function getCachedTemplate(Core $dwoo); |
|
| 45 | + /** |
|
| 46 | + * Returns the cached template output file name, true if it's cache-able but not cached |
|
| 47 | + * or false if it's not cached. |
|
| 48 | + * |
|
| 49 | + * @param Core $dwoo the dwoo instance that requests it |
|
| 50 | + * |
|
| 51 | + * @return string|bool |
|
| 52 | + */ |
|
| 53 | + public function getCachedTemplate(Core $dwoo); |
|
| 54 | 54 | |
| 55 | - /** |
|
| 56 | - * Caches the provided output into the cache file. |
|
| 57 | - * |
|
| 58 | - * @param Core $dwoo the dwoo instance that requests it |
|
| 59 | - * @param string $output the template output |
|
| 60 | - * |
|
| 61 | - * @return mixed full path of the cached file or false upon failure |
|
| 62 | - */ |
|
| 63 | - public function cache(Core $dwoo, $output); |
|
| 55 | + /** |
|
| 56 | + * Caches the provided output into the cache file. |
|
| 57 | + * |
|
| 58 | + * @param Core $dwoo the dwoo instance that requests it |
|
| 59 | + * @param string $output the template output |
|
| 60 | + * |
|
| 61 | + * @return mixed full path of the cached file or false upon failure |
|
| 62 | + */ |
|
| 63 | + public function cache(Core $dwoo, $output); |
|
| 64 | 64 | |
| 65 | - /** |
|
| 66 | - * Clears the cached template if it's older than the given time. |
|
| 67 | - * |
|
| 68 | - * @param Core $dwoo the dwoo instance that was used to cache that template |
|
| 69 | - * @param int $olderThan minimum time (in seconds) required for the cache to be cleared |
|
| 70 | - * |
|
| 71 | - * @return bool true if the cache was not present or if it was deleted, false if it remains there |
|
| 72 | - */ |
|
| 73 | - public function clearCache(Core $dwoo, $olderThan = - 1); |
|
| 65 | + /** |
|
| 66 | + * Clears the cached template if it's older than the given time. |
|
| 67 | + * |
|
| 68 | + * @param Core $dwoo the dwoo instance that was used to cache that template |
|
| 69 | + * @param int $olderThan minimum time (in seconds) required for the cache to be cleared |
|
| 70 | + * |
|
| 71 | + * @return bool true if the cache was not present or if it was deleted, false if it remains there |
|
| 72 | + */ |
|
| 73 | + public function clearCache(Core $dwoo, $olderThan = - 1); |
|
| 74 | 74 | |
| 75 | - /** |
|
| 76 | - * Returns the compiled template file name. |
|
| 77 | - * |
|
| 78 | - * @param Core $dwoo the dwoo instance that requests it |
|
| 79 | - * @param ICompiler $compiler the compiler that must be used |
|
| 80 | - * |
|
| 81 | - * @return string |
|
| 82 | - */ |
|
| 83 | - public function getCompiledTemplate(Core $dwoo, ICompiler $compiler = null); |
|
| 75 | + /** |
|
| 76 | + * Returns the compiled template file name. |
|
| 77 | + * |
|
| 78 | + * @param Core $dwoo the dwoo instance that requests it |
|
| 79 | + * @param ICompiler $compiler the compiler that must be used |
|
| 80 | + * |
|
| 81 | + * @return string |
|
| 82 | + */ |
|
| 83 | + public function getCompiledTemplate(Core $dwoo, ICompiler $compiler = null); |
|
| 84 | 84 | |
| 85 | - /** |
|
| 86 | - * Returns the template name. |
|
| 87 | - * |
|
| 88 | - * @return string |
|
| 89 | - */ |
|
| 90 | - public function getName(); |
|
| 85 | + /** |
|
| 86 | + * Returns the template name. |
|
| 87 | + * |
|
| 88 | + * @return string |
|
| 89 | + */ |
|
| 90 | + public function getName(); |
|
| 91 | 91 | |
| 92 | - /** |
|
| 93 | - * Returns the resource name for this template class. |
|
| 94 | - * |
|
| 95 | - * @return string |
|
| 96 | - */ |
|
| 97 | - public function getResourceName(); |
|
| 92 | + /** |
|
| 93 | + * Returns the resource name for this template class. |
|
| 94 | + * |
|
| 95 | + * @return string |
|
| 96 | + */ |
|
| 97 | + public function getResourceName(); |
|
| 98 | 98 | |
| 99 | - /** |
|
| 100 | - * Returns the resource identifier for this template or false if it has no identifier. |
|
| 101 | - * |
|
| 102 | - * @return string|false |
|
| 103 | - */ |
|
| 104 | - public function getResourceIdentifier(); |
|
| 99 | + /** |
|
| 100 | + * Returns the resource identifier for this template or false if it has no identifier. |
|
| 101 | + * |
|
| 102 | + * @return string|false |
|
| 103 | + */ |
|
| 104 | + public function getResourceIdentifier(); |
|
| 105 | 105 | |
| 106 | - /** |
|
| 107 | - * Returns the template source of this template. |
|
| 108 | - * |
|
| 109 | - * @return string |
|
| 110 | - */ |
|
| 111 | - public function getSource(); |
|
| 106 | + /** |
|
| 107 | + * Returns the template source of this template. |
|
| 108 | + * |
|
| 109 | + * @return string |
|
| 110 | + */ |
|
| 111 | + public function getSource(); |
|
| 112 | 112 | |
| 113 | - /** |
|
| 114 | - * Returns an unique string identifying the current version of this template, |
|
| 115 | - * for example a timestamp of the last modified date or a hash of the template source. |
|
| 116 | - * |
|
| 117 | - * @return string |
|
| 118 | - */ |
|
| 119 | - public function getUid(); |
|
| 113 | + /** |
|
| 114 | + * Returns an unique string identifying the current version of this template, |
|
| 115 | + * for example a timestamp of the last modified date or a hash of the template source. |
|
| 116 | + * |
|
| 117 | + * @return string |
|
| 118 | + */ |
|
| 119 | + public function getUid(); |
|
| 120 | 120 | |
| 121 | - /** |
|
| 122 | - * Returns the compiler used by this template, if it was just compiled, or null. |
|
| 123 | - * |
|
| 124 | - * @return ICompiler |
|
| 125 | - */ |
|
| 126 | - public function getCompiler(); |
|
| 121 | + /** |
|
| 122 | + * Returns the compiler used by this template, if it was just compiled, or null. |
|
| 123 | + * |
|
| 124 | + * @return ICompiler |
|
| 125 | + */ |
|
| 126 | + public function getCompiler(); |
|
| 127 | 127 | |
| 128 | - /** |
|
| 129 | - * Returns some php code that will check if this template has been modified or not. |
|
| 130 | - * if the function returns null, the template will be instanciated and then the Uid checked |
|
| 131 | - * |
|
| 132 | - * @return string |
|
| 133 | - */ |
|
| 134 | - public function getIsModifiedCode(); |
|
| 128 | + /** |
|
| 129 | + * Returns some php code that will check if this template has been modified or not. |
|
| 130 | + * if the function returns null, the template will be instanciated and then the Uid checked |
|
| 131 | + * |
|
| 132 | + * @return string |
|
| 133 | + */ |
|
| 134 | + public function getIsModifiedCode(); |
|
| 135 | 135 | |
| 136 | - /** |
|
| 137 | - * Returns a new template object from the given resource identifier, null if no include is |
|
| 138 | - * possible (resource not found), or false if include is not permitted by this resource type. |
|
| 139 | - * this method should also check if $dwoo->getSecurityPolicy() is null or not and do the |
|
| 140 | - * necessary permission checks if required, if the security policy prevents the template |
|
| 141 | - * generation it should throw a new Security\Exception with a relevant message |
|
| 142 | - * |
|
| 143 | - * @param mixed $resourceId the resource identifier |
|
| 144 | - * @param int $cacheTime duration of the cache validity for this template, if null it defaults to the |
|
| 145 | - * Dwoo instance that will render this template if null it defaults to the Dwoo |
|
| 146 | - * instance that will render this template |
|
| 147 | - * @param string $cacheId the unique cache identifier of this page or anything else that makes this |
|
| 148 | - * template's content unique, if null it defaults to the current url makes this |
|
| 149 | - * template's content unique, if null it defaults to the current url |
|
| 150 | - * @param string $compileId the unique compiled identifier, which is used to distinguish this template from |
|
| 151 | - * others, if null it defaults to the filename+bits of the path template from |
|
| 152 | - * others, if null it defaults to the filename+bits of the path |
|
| 153 | - * @param ITemplate $parentTemplate the template that is requesting a new template object (through an include, |
|
| 154 | - * extends or any other plugin) an include, extends or any other plugin) |
|
| 155 | - * |
|
| 156 | - * @return ITemplate|null|false |
|
| 157 | - */ |
|
| 158 | - public static function templateFactory(Core $dwoo, $resourceId, $cacheTime = null, $cacheId = null, $compileId = null, ITemplate $parentTemplate = null); |
|
| 136 | + /** |
|
| 137 | + * Returns a new template object from the given resource identifier, null if no include is |
|
| 138 | + * possible (resource not found), or false if include is not permitted by this resource type. |
|
| 139 | + * this method should also check if $dwoo->getSecurityPolicy() is null or not and do the |
|
| 140 | + * necessary permission checks if required, if the security policy prevents the template |
|
| 141 | + * generation it should throw a new Security\Exception with a relevant message |
|
| 142 | + * |
|
| 143 | + * @param mixed $resourceId the resource identifier |
|
| 144 | + * @param int $cacheTime duration of the cache validity for this template, if null it defaults to the |
|
| 145 | + * Dwoo instance that will render this template if null it defaults to the Dwoo |
|
| 146 | + * instance that will render this template |
|
| 147 | + * @param string $cacheId the unique cache identifier of this page or anything else that makes this |
|
| 148 | + * template's content unique, if null it defaults to the current url makes this |
|
| 149 | + * template's content unique, if null it defaults to the current url |
|
| 150 | + * @param string $compileId the unique compiled identifier, which is used to distinguish this template from |
|
| 151 | + * others, if null it defaults to the filename+bits of the path template from |
|
| 152 | + * others, if null it defaults to the filename+bits of the path |
|
| 153 | + * @param ITemplate $parentTemplate the template that is requesting a new template object (through an include, |
|
| 154 | + * extends or any other plugin) an include, extends or any other plugin) |
|
| 155 | + * |
|
| 156 | + * @return ITemplate|null|false |
|
| 157 | + */ |
|
| 158 | + public static function templateFactory(Core $dwoo, $resourceId, $cacheTime = null, $cacheId = null, $compileId = null, ITemplate $parentTemplate = null); |
|
| 159 | 159 | } |
@@ -23,30 +23,30 @@ |
||
| 23 | 23 | */ |
| 24 | 24 | abstract class Filter |
| 25 | 25 | { |
| 26 | - /** |
|
| 27 | - * The dwoo instance that runs this filter. |
|
| 28 | - * |
|
| 29 | - * @var Core |
|
| 30 | - */ |
|
| 31 | - protected $dwoo; |
|
| 26 | + /** |
|
| 27 | + * The dwoo instance that runs this filter. |
|
| 28 | + * |
|
| 29 | + * @var Core |
|
| 30 | + */ |
|
| 31 | + protected $dwoo; |
|
| 32 | 32 | |
| 33 | - /** |
|
| 34 | - * Constructor, if you override it, call parent::__construct($dwoo); or assign |
|
| 35 | - * the dwoo instance yourself if you need it. |
|
| 36 | - * |
|
| 37 | - * @param Core $dwoo the dwoo instance that runs this plugin |
|
| 38 | - */ |
|
| 39 | - public function __construct(Core $dwoo) |
|
| 40 | - { |
|
| 41 | - $this->dwoo = $dwoo; |
|
| 42 | - } |
|
| 33 | + /** |
|
| 34 | + * Constructor, if you override it, call parent::__construct($dwoo); or assign |
|
| 35 | + * the dwoo instance yourself if you need it. |
|
| 36 | + * |
|
| 37 | + * @param Core $dwoo the dwoo instance that runs this plugin |
|
| 38 | + */ |
|
| 39 | + public function __construct(Core $dwoo) |
|
| 40 | + { |
|
| 41 | + $this->dwoo = $dwoo; |
|
| 42 | + } |
|
| 43 | 43 | |
| 44 | - /** |
|
| 45 | - * Processes the input and returns it filtered. |
|
| 46 | - * |
|
| 47 | - * @param string $input the template to process |
|
| 48 | - * |
|
| 49 | - * @return string |
|
| 50 | - */ |
|
| 51 | - abstract public function process($input); |
|
| 44 | + /** |
|
| 45 | + * Processes the input and returns it filtered. |
|
| 46 | + * |
|
| 47 | + * @param string $input the template to process |
|
| 48 | + * |
|
| 49 | + * @return string |
|
| 50 | + */ |
|
| 51 | + abstract public function process($input); |
|
| 52 | 52 | } |
@@ -23,15 +23,15 @@ |
||
| 23 | 23 | */ |
| 24 | 24 | interface ILoader |
| 25 | 25 | { |
| 26 | - /** |
|
| 27 | - * Loads a plugin file. |
|
| 28 | - * the second parameter is used to avoid permanent rehashing when using php functions, |
|
| 29 | - * however this means that if you have add a plugin that overrides a php function you have |
|
| 30 | - * to delete the classpath.cache file(s) by hand to force a rehash of the plugins |
|
| 31 | - * |
|
| 32 | - * @param string $class the plugin name, without the `Plugin` prefix |
|
| 33 | - * @param bool $forceRehash if true, the class path caches will be rebuilt if the plugin is not found, in case it |
|
| 34 | - * has just been added, defaults to true |
|
| 35 | - */ |
|
| 36 | - public function loadPlugin($class, $forceRehash = true); |
|
| 26 | + /** |
|
| 27 | + * Loads a plugin file. |
|
| 28 | + * the second parameter is used to avoid permanent rehashing when using php functions, |
|
| 29 | + * however this means that if you have add a plugin that overrides a php function you have |
|
| 30 | + * to delete the classpath.cache file(s) by hand to force a rehash of the plugins |
|
| 31 | + * |
|
| 32 | + * @param string $class the plugin name, without the `Plugin` prefix |
|
| 33 | + * @param bool $forceRehash if true, the class path caches will be rebuilt if the plugin is not found, in case it |
|
| 34 | + * has just been added, defaults to true |
|
| 35 | + */ |
|
| 36 | + public function loadPlugin($class, $forceRehash = true); |
|
| 37 | 37 | } |
@@ -26,6 +26,6 @@ |
||
| 26 | 26 | */ |
| 27 | 27 | interface ICompilable |
| 28 | 28 | { |
| 29 | - // this replaces the process function |
|
| 30 | - //public static function compile(Compiler $compiler, $arg, $arg, ...); |
|
| 29 | + // this replaces the process function |
|
| 30 | + //public static function compile(Compiler $compiler, $arg, $arg, ...); |
|
| 31 | 31 | } |
@@ -42,118 +42,118 @@ |
||
| 42 | 42 | */ |
| 43 | 43 | class DwooView extends View |
| 44 | 44 | { |
| 45 | - protected $_sv_template_dir; |
|
| 46 | - protected $_sv_layout_dir; |
|
| 47 | - protected $_sv_compile_dir; |
|
| 48 | - protected $_sv_cache_dir; |
|
| 49 | - protected $_sv_compile_id; |
|
| 50 | - |
|
| 51 | - protected $_dwoo; |
|
| 52 | - |
|
| 53 | - public $sv_processedTpl; |
|
| 45 | + protected $_sv_template_dir; |
|
| 46 | + protected $_sv_layout_dir; |
|
| 47 | + protected $_sv_compile_dir; |
|
| 48 | + protected $_sv_cache_dir; |
|
| 49 | + protected $_sv_compile_id; |
|
| 50 | + |
|
| 51 | + protected $_dwoo; |
|
| 52 | + |
|
| 53 | + public $sv_processedTpl; |
|
| 54 | 54 | |
| 55 | - public function __construct(&$controller) |
|
| 56 | - { |
|
| 57 | - parent::__construct($controller); |
|
| 58 | - |
|
| 59 | - $this->ext = '.tpl'; |
|
| 60 | - |
|
| 61 | - $this->_sv_template_dir = array( |
|
| 62 | - VIEWS.$this->viewPath.DS.$this->subDir, |
|
| 63 | - VIEWS.$this->viewPath, |
|
| 64 | - VIEWS, |
|
| 65 | - ); |
|
| 66 | - |
|
| 67 | - $this->_sv_layout_dir = array( |
|
| 68 | - LAYOUTS.$this->subDir, |
|
| 69 | - VIEWS, |
|
| 70 | - ); |
|
| 71 | - |
|
| 72 | - $this->_sv_compile_dir = TMP.'dwoo'.DS.'compile'; |
|
| 73 | - $this->_sv_cache_dir = TMP.'dwoo'.DS.'cache'; |
|
| 74 | - |
|
| 75 | - $this->_dwoo = new Dwoo_Core($this->_sv_compile_dir, $this->_sv_cache_dir); |
|
| 76 | - |
|
| 77 | - $this->_sv_compile_id = $controller->name; |
|
| 78 | - |
|
| 79 | - $this->_dwoo->sv_this = $this; |
|
| 80 | - $this->_dwoo->setSecurityPolicy(); |
|
| 81 | - |
|
| 82 | - return; |
|
| 83 | - } |
|
| 84 | - |
|
| 85 | - /** |
|
| 86 | - * changes the template directory. |
|
| 87 | - */ |
|
| 88 | - public function setTemplateDir($path = VIEW) |
|
| 89 | - { |
|
| 90 | - $old = $this->_sv_template_dir; |
|
| 91 | - $this->_sv_template_dir = $path; |
|
| 92 | - |
|
| 93 | - return $old; |
|
| 94 | - } |
|
| 95 | - |
|
| 96 | - public function getTemplateDir() |
|
| 97 | - { |
|
| 98 | - return $this->_sv_template_dir; |
|
| 99 | - } |
|
| 100 | - |
|
| 101 | - public function _render($___viewFn, $___data_for_view, $___play_safe = true, $loadHelpers = true) |
|
| 102 | - { |
|
| 103 | - // let's determine if this is a layout call or a template call |
|
| 104 | - // and change the template dir accordingly |
|
| 105 | - $layout = false; |
|
| 106 | - if (isset($___data_for_view['content_for_layout'])) { |
|
| 107 | - $this->_sv_template_dir = $this->_sv_layout_dir; |
|
| 108 | - $layout = true; |
|
| 109 | - } |
|
| 110 | - |
|
| 111 | - $tpl = new Dwoo_Template_File($___viewFn); |
|
| 112 | - $data = $___data_for_view; |
|
| 113 | - |
|
| 114 | - $data['view'] = $this; |
|
| 115 | - |
|
| 116 | - if ($this->helpers != false && $loadHelpers === true) { |
|
| 117 | - $loadedHelpers = array(); |
|
| 118 | - $loadedHelpers = $this->_loadHelpers($loadedHelpers, $this->helpers); |
|
| 119 | - |
|
| 120 | - foreach (array_keys($loadedHelpers) as $helper) { |
|
| 121 | - $camelBackedHelper = strtolower(substr($helper, 0, 1)).substr($helper, 1); |
|
| 122 | - |
|
| 123 | - ${$camelBackedHelper} = $loadedHelpers[$helper]; |
|
| 124 | - |
|
| 125 | - if (is_array(${$camelBackedHelper}->helpers) && !empty(${$camelBackedHelper}->helpers)) { |
|
| 126 | - $subHelpers = ${$camelBackedHelper}->helpers; |
|
| 127 | - foreach ($subHelpers as $subHelper) { |
|
| 128 | - ${$camelBackedHelper}->{$subHelper} = $loadedHelpers[$subHelper]; |
|
| 129 | - } |
|
| 130 | - } |
|
| 131 | - |
|
| 132 | - if (isset($this->passedArgs)) { |
|
| 133 | - ${$camelBackedHelper}->passedArgs = $this->passedArgs; |
|
| 134 | - } |
|
| 135 | - |
|
| 136 | - $this->loaded[$camelBackedHelper] = ${$camelBackedHelper}; |
|
| 137 | - |
|
| 138 | - $data[$camelBackedHelper] = ${$camelBackedHelper}; |
|
| 139 | - } |
|
| 140 | - } |
|
| 141 | - |
|
| 142 | - if ($this->helpers != false && $loadHelpers === true) { |
|
| 143 | - foreach ($loadedHelpers as $helper) { |
|
| 144 | - if (is_object($helper)) { |
|
| 145 | - if (is_subclass_of($helper, 'Helper') || is_subclass_of($helper, 'helper')) { |
|
| 146 | - $helper->beforeRender(); |
|
| 147 | - } |
|
| 148 | - } |
|
| 149 | - } |
|
| 150 | - } |
|
| 151 | - |
|
| 152 | - return $this->_dwoo->get($tpl, $data); |
|
| 153 | - } |
|
| 154 | - |
|
| 155 | - public function get() |
|
| 156 | - { |
|
| 157 | - return $this->_dwoo; |
|
| 158 | - } |
|
| 55 | + public function __construct(&$controller) |
|
| 56 | + { |
|
| 57 | + parent::__construct($controller); |
|
| 58 | + |
|
| 59 | + $this->ext = '.tpl'; |
|
| 60 | + |
|
| 61 | + $this->_sv_template_dir = array( |
|
| 62 | + VIEWS.$this->viewPath.DS.$this->subDir, |
|
| 63 | + VIEWS.$this->viewPath, |
|
| 64 | + VIEWS, |
|
| 65 | + ); |
|
| 66 | + |
|
| 67 | + $this->_sv_layout_dir = array( |
|
| 68 | + LAYOUTS.$this->subDir, |
|
| 69 | + VIEWS, |
|
| 70 | + ); |
|
| 71 | + |
|
| 72 | + $this->_sv_compile_dir = TMP.'dwoo'.DS.'compile'; |
|
| 73 | + $this->_sv_cache_dir = TMP.'dwoo'.DS.'cache'; |
|
| 74 | + |
|
| 75 | + $this->_dwoo = new Dwoo_Core($this->_sv_compile_dir, $this->_sv_cache_dir); |
|
| 76 | + |
|
| 77 | + $this->_sv_compile_id = $controller->name; |
|
| 78 | + |
|
| 79 | + $this->_dwoo->sv_this = $this; |
|
| 80 | + $this->_dwoo->setSecurityPolicy(); |
|
| 81 | + |
|
| 82 | + return; |
|
| 83 | + } |
|
| 84 | + |
|
| 85 | + /** |
|
| 86 | + * changes the template directory. |
|
| 87 | + */ |
|
| 88 | + public function setTemplateDir($path = VIEW) |
|
| 89 | + { |
|
| 90 | + $old = $this->_sv_template_dir; |
|
| 91 | + $this->_sv_template_dir = $path; |
|
| 92 | + |
|
| 93 | + return $old; |
|
| 94 | + } |
|
| 95 | + |
|
| 96 | + public function getTemplateDir() |
|
| 97 | + { |
|
| 98 | + return $this->_sv_template_dir; |
|
| 99 | + } |
|
| 100 | + |
|
| 101 | + public function _render($___viewFn, $___data_for_view, $___play_safe = true, $loadHelpers = true) |
|
| 102 | + { |
|
| 103 | + // let's determine if this is a layout call or a template call |
|
| 104 | + // and change the template dir accordingly |
|
| 105 | + $layout = false; |
|
| 106 | + if (isset($___data_for_view['content_for_layout'])) { |
|
| 107 | + $this->_sv_template_dir = $this->_sv_layout_dir; |
|
| 108 | + $layout = true; |
|
| 109 | + } |
|
| 110 | + |
|
| 111 | + $tpl = new Dwoo_Template_File($___viewFn); |
|
| 112 | + $data = $___data_for_view; |
|
| 113 | + |
|
| 114 | + $data['view'] = $this; |
|
| 115 | + |
|
| 116 | + if ($this->helpers != false && $loadHelpers === true) { |
|
| 117 | + $loadedHelpers = array(); |
|
| 118 | + $loadedHelpers = $this->_loadHelpers($loadedHelpers, $this->helpers); |
|
| 119 | + |
|
| 120 | + foreach (array_keys($loadedHelpers) as $helper) { |
|
| 121 | + $camelBackedHelper = strtolower(substr($helper, 0, 1)).substr($helper, 1); |
|
| 122 | + |
|
| 123 | + ${$camelBackedHelper} = $loadedHelpers[$helper]; |
|
| 124 | + |
|
| 125 | + if (is_array(${$camelBackedHelper}->helpers) && !empty(${$camelBackedHelper}->helpers)) { |
|
| 126 | + $subHelpers = ${$camelBackedHelper}->helpers; |
|
| 127 | + foreach ($subHelpers as $subHelper) { |
|
| 128 | + ${$camelBackedHelper}->{$subHelper} = $loadedHelpers[$subHelper]; |
|
| 129 | + } |
|
| 130 | + } |
|
| 131 | + |
|
| 132 | + if (isset($this->passedArgs)) { |
|
| 133 | + ${$camelBackedHelper}->passedArgs = $this->passedArgs; |
|
| 134 | + } |
|
| 135 | + |
|
| 136 | + $this->loaded[$camelBackedHelper] = ${$camelBackedHelper}; |
|
| 137 | + |
|
| 138 | + $data[$camelBackedHelper] = ${$camelBackedHelper}; |
|
| 139 | + } |
|
| 140 | + } |
|
| 141 | + |
|
| 142 | + if ($this->helpers != false && $loadHelpers === true) { |
|
| 143 | + foreach ($loadedHelpers as $helper) { |
|
| 144 | + if (is_object($helper)) { |
|
| 145 | + if (is_subclass_of($helper, 'Helper') || is_subclass_of($helper, 'helper')) { |
|
| 146 | + $helper->beforeRender(); |
|
| 147 | + } |
|
| 148 | + } |
|
| 149 | + } |
|
| 150 | + } |
|
| 151 | + |
|
| 152 | + return $this->_dwoo->get($tpl, $data); |
|
| 153 | + } |
|
| 154 | + |
|
| 155 | + public function get() |
|
| 156 | + { |
|
| 157 | + return $this->_dwoo; |
|
| 158 | + } |
|
| 159 | 159 | } |