Source for file String.php
Documentation is available at String.php
* represents a Dwoo template contained in a string
* This software is provided 'as-is', without any express or implied warranty.
* In no event will the authors be held liable for any damages arising from the use of this software.
* @author Jordi Boggiano <j.boggiano@seld.be>
* @copyright Copyright (c) 2008, Jordi Boggiano
* @license http://dwoo.org/LICENSE Modified BSD License
* template compilation id
* template cache id, if not provided in the constructor, it is set to
* the md4 hash of the request_uri. it is however highly recommended to
* provide one that will fit your needs.
* in all cases, the compilation id is prepended to the cache id to separate
* templates with similar cache ids from one another
* validity duration of the generated cache file (in seconds)
* set to -1 for infinite cache, 0 to disable and null to inherit the Dwoo instance's cache time
* boolean flag that defines whether the compilation should be enforced (once) or
* not use this if you have issues with the compiled templates not being updated
* but if you do need this it's most likely that you should file a bug report
* caches the results of the file checks to save some time when the same
* templates is rendered several times
protected static $cache =
array('cached'=>
array(), 'compiled'=>
array());
* holds the compiler that built this template
* chmod value for all files written (cached or compiled ones)
* set to null if you don't want any chmod operation to happen
* creates a template from a string
* @param string $templateString the template to use
* @param int $cacheTime duration of the cache validity for this template,
* if null it defaults to the Dwoo instance that will
* render this template, set to -1 for infinite cache or 0 to disable
* @param string $cacheId the unique cache identifier of this page or anything else that
* makes this template's content unique, if null it defaults
* @param string $compileId the unique compiled identifier, which is used to distinguish this
* template from others, if null it defaults to the md4 hash of the template
public function __construct($templateString, $cacheTime =
null, $cacheId =
null, $compileId =
null)
$this->template =
$templateString;
$this->name =
hash('md4', $templateString);
$this->name =
md5($templateString);
if ($compileId !==
null) {
* returns the cache duration for this template
* defaults to null if it was not provided
* sets the cache duration for this template
* can be used to set it after the object is created if you did not provide
* @param int $seconds duration of the cache validity for this template, if
* null it defaults to the Dwoo instance's cache time. 0 = disable and
* returns the chmod value for all files written (cached or compiled ones)
* set the chmod value for all files written (cached or compiled ones)
* set to null if you don't want to do any chmod() operation
* @param int $mask new bitmask to use for all files
* returns the template name
* returns the resource name for this template class
* returns the resource identifier for this template, false here as strings don't have identifiers
* returns the template source of this template
* returns an unique value identifying the current version of this template,
* in this case it's the md4 hash of the content
* returns the compiler used by this template, if it was just compiled, or null
* marks this template as compile-forced, which means it will be recompiled even if it
* was already saved and wasn't modified since the last compilation. do not use this in production,
* it's only meant to be used in development (and the development of dwoo particularly)
* returns the cached template output file name, true if it's cache-able but not cached
* or false if it's not cached
* @param Dwoo $dwoo the dwoo instance that requests it
$cacheLength =
$dwoo->getCacheTime();
if ($cacheLength ===
0) {
// already checked, return cache file
// cache is still valid and can be loaded
self::$cache['cached'][$this->cacheId] =
true;
* caches the provided output into the cache file
* @param Dwoo $dwoo the dwoo instance that requests it
* @param string $output the template output
* @return mixed full path of the cached file or false upon failure
public function cache(Dwoo $dwoo, $output)
$cacheDir =
$dwoo->getCacheDir();
// the code below is courtesy of Rasmus Schultz,
// thanks for his help on avoiding concurency issues
$temp =
tempnam($cacheDir, 'temp');
if (!($file =
@fopen($temp, 'wb'))) {
$temp =
$cacheDir .
uniqid('temp');
if (!($file =
@fopen($temp, 'wb'))) {
trigger_error('Error writing temporary file \''.
$temp.
'\'', E_USER_WARNING);
if (!@rename($temp, $cachedFile)) {
if ($this->chmod !==
null) {
self::$cache['cached'][$this->cacheId] =
true;
* clears the cached template if it's older than the given time
* @param Dwoo $dwoo the dwoo instance that was used to cache that template
* @param int $olderThan minimum time (in seconds) required for the cache to be cleared
* @return bool true if the cache was not present or if it was deleted, false if it remains there
public function clearCache(Dwoo $dwoo, $olderThan = -
1)
* returns the compiled template file name
* @param Dwoo $dwoo the dwoo instance that requests it
* @param Dwoo_ICompiler $compiler the compiler that must be used
// already checked, return compiled file
self::$cache['compiled'][$this->compileId] =
true;
if ($compiler ===
null) {
if ($compiler ===
null ||
$compiler ===
array('Dwoo_Compiler', 'compilerFactory')) {
$compiler->setCustomPlugins($dwoo->getCustomPlugins());
$compiler->setSecurityPolicy($dwoo->getSecurityPolicy());
if ($this->chmod !==
null) {
self::$cache['compiled'][$this->compileId] =
true;
* Checks if compiled file is valid (it exists)
* @return boolean True cache file existance
* returns a new template string object with the resource id being the template source code
* @param Dwoo $dwoo the dwoo instance requiring it
* @param mixed $resourceId the filename (relative to this template's dir) of the template to include
* @param int $cacheTime duration of the cache validity for this template,
* if null it defaults to the Dwoo instance that will
* @param string $cacheId the unique cache identifier of this page or anything else that
* makes this template's content unique, if null it defaults
* @param string $compileId the unique compiled identifier, which is used to distinguish this
* template from others, if null it defaults to the filename+bits of the path
* @param Dwoo_ITemplate $parentTemplate the template that is requesting a new template object (through
* an include, extends or any other plugin)
* @return Dwoo_Template_String
public static function templateFactory(Dwoo $dwoo, $resourceId, $cacheTime =
null, $cacheId =
null, $compileId =
null, Dwoo_ITemplate $parentTemplate =
null)
return new self($resourceId, $cacheTime, $cacheId, $compileId);
* returns the full compiled file name and assigns a default value to it if
* @param Dwoo $dwoo the dwoo instance that requests the file name
* @return string the full path to the compiled file
// no compile id was provided, set default
return $dwoo->getCompileDir() .
$this->compileId.
'.d'.
Dwoo::RELEASE_TAG.
'.php';
* returns the full cached file name and assigns a default value to it if
* @param Dwoo $dwoo the dwoo instance that requests the file name
* @return string the full path to the cached file
// no cache id provided, use request_uri as default
if (isset
($_SERVER['REQUEST_URI']) ===
true) {
$cacheId =
$_SERVER['REQUEST_URI'];
} elseif (isset
($_SERVER['SCRIPT_FILENAME']) && isset
($_SERVER['argv'])) {
$cacheId =
$_SERVER['SCRIPT_FILENAME'].
'-'.
implode('-', $_SERVER['argv']);
// force compiled id generation
return $dwoo->getCacheDir() .
$this->cacheId.
'.html';
* returns some php code that will check if this template has been modified or not
* if the function returns null, the template will be instanciated and then the Uid checked
* ensures the given path exists
* @param string $path any path
* @param string $baseDir the base directory where the directory is created
* ($path must still contain the full path, $baseDir
* is only used for unix permissions)
if ($this->chmod ===
null) {
mkdir($path, $chmod, true);
// enforce the correct mode for all directories created
if (strpos(PHP_OS, 'WIN') !==
0 &&
$baseDir !==
null) {
foreach ($folders as $folder) {
$baseDir .=
$folder .
DIRECTORY_SEPARATOR;
Documentation generated on Sun, 07 Feb 2010 17:53:58 +0000 by phpDocumentor 1.4.0