Commit b076166a by Seldaek

+ Both cache and compile IDs can now have slashes in them to create subfolders

in the cache/compile dirs + Added a DWOO_CHMOD constant that, if set before you include Dwoo, allows you to define the file mode of all the file/directories Dwoo will write, defaults to 0777 git-svn-id: svn://dwoo.org/dwoo/trunk@54 0598d79b-80c4-4d41-97ba-ac86fbbd088b
parent 52cd67ff
...@@ -31,6 +31,11 @@ ...@@ -31,6 +31,11 @@
of its compile() method of its compile() method
+ API: Added a bunch of utility functions to Dwoo_Compiler, allowing compiled + API: Added a bunch of utility functions to Dwoo_Compiler, allowing compiled
plugins to access more of the compiler internals plugins to access more of the compiler internals
+ Both cache and compile IDs can now have slashes in them to create subfolders
in the cache/compile dirs
+ Added a DWOO_CHMOD constant that, if set before you include Dwoo, allows you
to define the file mode of all the file/directories Dwoo will write, defaults
to 0777
+ Added a 'data' argument to {include} to be able to feed data directly into it + Added a 'data' argument to {include} to be able to feed data directly into it
* Fixed a potential concurrency issue (thanks to Rasmus Schultz for the patch) * Fixed a potential concurrency issue (thanks to Rasmus Schultz for the patch)
* Moved all files to Dwoo/Class.php excepted for the core Dwoo.php file * Moved all files to Dwoo/Class.php excepted for the core Dwoo.php file
......
...@@ -13,6 +13,8 @@ if(defined('DWOO_CACHE_DIRECTORY') === false) ...@@ -13,6 +13,8 @@ if(defined('DWOO_CACHE_DIRECTORY') === false)
define('DWOO_CACHE_DIRECTORY', DWOO_DIRECTORY.'cache'.DIRECTORY_SEPARATOR); define('DWOO_CACHE_DIRECTORY', DWOO_DIRECTORY.'cache'.DIRECTORY_SEPARATOR);
if(defined('DWOO_COMPILE_DIRECTORY') === false) if(defined('DWOO_COMPILE_DIRECTORY') === false)
define('DWOO_COMPILE_DIRECTORY', DWOO_DIRECTORY.'compiled'.DIRECTORY_SEPARATOR); define('DWOO_COMPILE_DIRECTORY', DWOO_DIRECTORY.'compiled'.DIRECTORY_SEPARATOR);
if(defined('DWOO_CHMOD') === false)
define('DWOO_CHMOD', 0777);
if(is_writable(DWOO_CACHE_DIRECTORY) === false) if(is_writable(DWOO_CACHE_DIRECTORY) === false)
throw new Dwoo_Exception('Dwoo cache directory must be writable, either chmod "'.DWOO_CACHE_DIRECTORY.'" to make it writable or define DWOO_CACHE_DIRECTORY to a writable directory before including Dwoo.php'); throw new Dwoo_Exception('Dwoo cache directory must be writable, either chmod "'.DWOO_CACHE_DIRECTORY.'" to make it writable or define DWOO_CACHE_DIRECTORY to a writable directory before including Dwoo.php');
if(is_writable(DWOO_COMPILE_DIRECTORY) === false) if(is_writable(DWOO_COMPILE_DIRECTORY) === false)
......
...@@ -60,6 +60,18 @@ interface Dwoo_ITemplate ...@@ -60,6 +60,18 @@ interface Dwoo_ITemplate
public function getCacheTime(); public function getCacheTime();
/** /**
* sets the cache duration for this template
*
* can be used to set it after the object is created if you did not provide
* it in the constructor
*
* @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
* -1 = infinite cache
*/
public function setCacheTime($seconds = null);
/**
* returns the cached template output file name, true if it's cache-able but not cached * returns the cached template output file name, true if it's cache-able but not cached
* or false if it's not cached * or false if it's not cached
* *
......
...@@ -47,25 +47,15 @@ class Dwoo_Template_File extends Dwoo_Template_String ...@@ -47,25 +47,15 @@ class Dwoo_Template_File extends Dwoo_Template_String
$this->name = basename($file); $this->name = basename($file);
$this->cacheTime = $cacheTime; $this->cacheTime = $cacheTime;
// no compile id provided, generate a kind of unique kind of readable one from the filename if($compileId !== null)
if($compileId === null)
{ {
$parts = explode('/', strtr($file, '\\.', '/_')); $this->compileId = strtr($compileId, '\\%?=!:;'.PATH_SEPARATOR, '/-------');
$compileId = array_pop($parts);
$compileId = substr(array_pop($parts), 0, 5) .'_'. $compileId;
$compileId = substr(array_pop($parts), 0, 5) .'_'. $compileId;
} }
$this->compileId = $compileId;
// no cache id provided, use request_uri if available if($cacheId !== null)
if($cacheId === null)
{ {
if(isset($_SERVER['REQUEST_URI']) === true) $this->cacheId = strtr($cacheId, '\\%?=!:;'.PATH_SEPARATOR, '/-------');
$cacheId = $_SERVER['REQUEST_URI'];
elseif(isset($_SERVER['SCRIPT_FILENAME']) && isset($_SERVER['argv']))
$cacheId = $_SERVER['SCRIPT_FILENAME'].'-'.implode('-', $_SERVER['argv']);
} }
$this->cacheId = $this->compileId . strtr($cacheId, '\\/%?=!:;', '--------');
} }
/** /**
...@@ -77,7 +67,7 @@ class Dwoo_Template_File extends Dwoo_Template_String ...@@ -77,7 +67,7 @@ class Dwoo_Template_File extends Dwoo_Template_String
*/ */
public function getCompiledTemplate(Dwoo $dwoo, Dwoo_ICompiler $compiler = null) public function getCompiledTemplate(Dwoo $dwoo, Dwoo_ICompiler $compiler = null)
{ {
$compiledFile = $dwoo->getCompileDir() . $this->compileId.'.dwoo'.Dwoo::RELEASE_TAG.'.php'; $compiledFile = $this->getCompiledFilename($dwoo);
// already checked, return compiled file // already checked, return compiled file
if($this->compilationEnforced !== true && isset(self::$cache['compiled'][$this->compileId]) === true) if($this->compilationEnforced !== true && isset(self::$cache['compiled'][$this->compileId]) === true)
...@@ -111,7 +101,9 @@ class Dwoo_Template_File extends Dwoo_Template_String ...@@ -111,7 +101,9 @@ class Dwoo_Template_File extends Dwoo_Template_String
$compiler->setCustomPlugins($dwoo->getCustomPlugins()); $compiler->setCustomPlugins($dwoo->getCustomPlugins());
$compiler->setSecurityPolicy($dwoo->getSecurityPolicy()); $compiler->setSecurityPolicy($dwoo->getSecurityPolicy());
$this->makeDirectory(dirname($compiledFile));
file_put_contents($compiledFile, $compiler->compile($dwoo, $this)); file_put_contents($compiledFile, $compiler->compile($dwoo, $this));
chmod($compiledFile, DWOO_CHMOD);
self::$cache['compiled'][$this->compileId] = true; self::$cache['compiled'][$this->compileId] = true;
} }
...@@ -202,4 +194,21 @@ class Dwoo_Template_File extends Dwoo_Template_String ...@@ -202,4 +194,21 @@ class Dwoo_Template_File extends Dwoo_Template_String
return new Dwoo_Template_File($resourceId, $cacheTime, $cacheId, $compileId); return new Dwoo_Template_File($resourceId, $cacheTime, $cacheId, $compileId);
} }
/**
* returns the full compiled file name and assigns a default value to it if
* required
*
* @param Dwoo $dwoo the dwoo instance that requests the file name
* @return string the full path to the compiled file
*/
protected function getCompiledFilename(Dwoo $dwoo)
{
// no compile id was provided, set default
if($this->compileId===null)
{
$this->compileId = implode('/', array_slice(explode('/', strtr($this->file, '\\', '/')), -3));
}
return $dwoo->getCompileDir() . $this->compileId.'.d'.Dwoo::RELEASE_TAG.'.php';
}
} }
...@@ -99,22 +99,15 @@ class Dwoo_Template_String implements Dwoo_ITemplate ...@@ -99,22 +99,15 @@ class Dwoo_Template_String implements Dwoo_ITemplate
$this->name = hash('md4', $templateString); $this->name = hash('md4', $templateString);
$this->cacheTime = $cacheTime; $this->cacheTime = $cacheTime;
// no compile id provided, set it to an md5 hash of the template if($compileId !== null)
if($compileId === null)
{ {
$compileId = $this->name; $this->compileId = strtr($compileId, '\\%?=!:;'.PATH_SEPARATOR, '/-------');
} }
$this->compileId = $compileId;
// no cache id provided, use request_uri if($cacheId !== null)
if($cacheId === null)
{ {
if(isset($_SERVER['REQUEST_URI']) === true) $this->cacheId = strtr($cacheId, '\\%?=!:;'.PATH_SEPARATOR, '/-------');
$cacheId = $_SERVER['REQUEST_URI'];
elseif(isset($_SERVER['SCRIPT_FILENAME']) && isset($_SERVER['argv']))
$cacheId = $_SERVER['SCRIPT_FILENAME'].'-'.implode('-', $_SERVER['argv']);
} }
$this->cacheId = $this->compileId . strtr($cacheId, '\\/%?=!:;', '--------');
} }
/** /**
...@@ -130,6 +123,21 @@ class Dwoo_Template_String implements Dwoo_ITemplate ...@@ -130,6 +123,21 @@ class Dwoo_Template_String implements Dwoo_ITemplate
} }
/** /**
* sets the cache duration for this template
*
* can be used to set it after the object is created if you did not provide
* it in the constructor
*
* @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
* -1 = infinite cache
*/
public function setCacheTime($seconds = null)
{
$this->cacheTime = $seconds;
}
/**
* returns the template name * returns the template name
* *
* @return string * @return string
...@@ -209,7 +217,7 @@ class Dwoo_Template_String implements Dwoo_ITemplate ...@@ -209,7 +217,7 @@ class Dwoo_Template_String implements Dwoo_ITemplate
*/ */
public function getCachedTemplate(Dwoo $dwoo) public function getCachedTemplate(Dwoo $dwoo)
{ {
$cachedFile = $dwoo->getCacheDir() . $this->cacheId.'.html'; $cachedFile = $this->getCacheFilename($dwoo);
if($this->cacheTime !== null) if($this->cacheTime !== null)
$cacheLength = $this->cacheTime; $cacheLength = $this->cacheTime;
else else
...@@ -247,7 +255,7 @@ class Dwoo_Template_String implements Dwoo_ITemplate ...@@ -247,7 +255,7 @@ class Dwoo_Template_String implements Dwoo_ITemplate
public function cache(Dwoo $dwoo, $output) public function cache(Dwoo $dwoo, $output)
{ {
$cacheDir = $dwoo->getCacheDir(); $cacheDir = $dwoo->getCacheDir();
$cachedFile = $cacheDir . $this->cacheId.'.html'; $cachedFile = $this->getCacheFilename($dwoo);
// the code below is courtesy of Rasmus Schultz, // the code below is courtesy of Rasmus Schultz,
// thanks for his help on avoiding concurency issues // thanks for his help on avoiding concurency issues
...@@ -265,13 +273,14 @@ class Dwoo_Template_String implements Dwoo_ITemplate ...@@ -265,13 +273,14 @@ class Dwoo_Template_String implements Dwoo_ITemplate
fwrite($file, $output); fwrite($file, $output);
fclose($file); fclose($file);
$this->makeDirectory(dirname($cachedFile));
if(!@rename($temp, $cachedFile)) if(!@rename($temp, $cachedFile))
{ {
@unlink($cachedFile); @unlink($cachedFile);
@rename($temp, $cachedFile); @rename($temp, $cachedFile);
} }
@chmod($cachedFile, 0777); chmod($cachedFile, DWOO_CHMOD);
self::$cache['cached'][$this->cacheId] = true; self::$cache['cached'][$this->cacheId] = true;
...@@ -287,7 +296,7 @@ class Dwoo_Template_String implements Dwoo_ITemplate ...@@ -287,7 +296,7 @@ class Dwoo_Template_String implements Dwoo_ITemplate
*/ */
public function clearCache(Dwoo $dwoo, $olderThan = -1) public function clearCache(Dwoo $dwoo, $olderThan = -1)
{ {
$cachedFile = $dwoo->getCacheDir() . $this->cacheId.'.html'; $cachedFile = $this->getCacheFilename($dwoo);
return !file_exists($cachedFile) || (filectime($cachedFile) < (time() - $olderThan) && unlink($cachedFile)); return !file_exists($cachedFile) || (filectime($cachedFile) < (time() - $olderThan) && unlink($cachedFile));
} }
...@@ -301,7 +310,7 @@ class Dwoo_Template_String implements Dwoo_ITemplate ...@@ -301,7 +310,7 @@ class Dwoo_Template_String implements Dwoo_ITemplate
*/ */
public function getCompiledTemplate(Dwoo $dwoo, Dwoo_ICompiler $compiler = null) public function getCompiledTemplate(Dwoo $dwoo, Dwoo_ICompiler $compiler = null)
{ {
$compiledFile = $dwoo->getCompileDir() . $this->compileId.'.dwoo'.Dwoo::RELEASE_TAG.'.php'; $compiledFile = $this->getCompiledFilename($dwoo);
// already checked, return compiled file // already checked, return compiled file
if($this->compilationEnforced !== true && isset(self::$cache['compiled'][$this->compileId]) === true) if($this->compilationEnforced !== true && isset(self::$cache['compiled'][$this->compileId]) === true)
...@@ -335,7 +344,9 @@ class Dwoo_Template_String implements Dwoo_ITemplate ...@@ -335,7 +344,9 @@ class Dwoo_Template_String implements Dwoo_ITemplate
$compiler->setCustomPlugins($dwoo->getCustomPlugins()); $compiler->setCustomPlugins($dwoo->getCustomPlugins());
$compiler->setSecurityPolicy($dwoo->getSecurityPolicy()); $compiler->setSecurityPolicy($dwoo->getSecurityPolicy());
$this->makeDirectory(dirname($compiledFile));
file_put_contents($compiledFile, $compiler->compile($dwoo, $this)); file_put_contents($compiledFile, $compiler->compile($dwoo, $this));
chmod($compiledFile, DWOO_CHMOD);
self::$cache['compiled'][$this->compileId] = true; self::$cache['compiled'][$this->compileId] = true;
} }
...@@ -361,4 +372,55 @@ class Dwoo_Template_String implements Dwoo_ITemplate ...@@ -361,4 +372,55 @@ class Dwoo_Template_String implements Dwoo_ITemplate
{ {
return false; return false;
} }
/**
* returns the full compiled file name and assigns a default value to it if
* required
*
* @param Dwoo $dwoo the dwoo instance that requests the file name
* @return string the full path to the compiled file
*/
protected function getCompiledFilename(Dwoo $dwoo)
{
// no compile id was provided, set default
if($this->compileId===null)
{
$this->compileId = $this->name;
}
return $dwoo->getCompileDir() . $this->compileId.'.d'.Dwoo::RELEASE_TAG.'.php';
}
/**
* returns the full cached file name and assigns a default value to it if
* required
*
* @param Dwoo $dwoo the dwoo instance that requests the file name
* @return string the full path to the cached file
*/
protected function getCacheFilename(Dwoo $dwoo)
{
// no cache id provided, use request_uri as default
if($this->cacheId === null)
{
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']);
$this->cacheId = strtr($cacheId, '\\%?=!:;'.PATH_SEPARATOR, '/-------');
}
return $dwoo->getCacheDir() . $this->cacheId.'.html';
}
/**
* ensures the given path exists
*
* @param string $path any path
*/
protected function makeDirectory($path)
{
if(is_dir($path) === true)
return;
mkdir($path, DWOO_CHMOD, true);
}
} }
...@@ -19,7 +19,7 @@ ...@@ -19,7 +19,7 @@
* @date 2008-04-09 * @date 2008-04-09
* @package Dwoo * @package Dwoo
*/ */
class Dwoo_Plugin_smartyinterface extends Dwoo_Plugin class Dwoo_Plugin_smartyinterface extends Dwoo_Block_Plugin
{ {
public function init($__funcname, $__functype, array $rest=array()) {} public function init($__funcname, $__functype, array $rest=array()) {}
......
...@@ -28,13 +28,23 @@ class DwooTests extends PHPUnit_Framework_TestSuite { ...@@ -28,13 +28,23 @@ class DwooTests extends PHPUnit_Framework_TestSuite {
return $suite; return $suite;
} }
protected function tearDown() protected function tearDown()
{ {
foreach(new DirectoryIterator(TEST_DIRECTORY.'/temp/compiled') as $file) { $this->clearDir(TEST_DIRECTORY.'/temp/compiled', true);
if(!$file->isDot() && !$file->isDir()) }
unlink(TEST_DIRECTORY.'/temp/compiled/'.(string)$file);
} protected function clearDir($path, $emptyOnly=false)
} {
if(is_dir($path))
{
foreach(glob($path.'/*') as $f)
$this->clearDir($f);
if(!$emptyOnly)
rmdir($path);
}
else
unlink($path);
}
} }
// Evaluates two strings and ignores differences in line endings (\r\n == \n == \r) // Evaluates two strings and ignores differences in line endings (\r\n == \n == \r)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment