Commit 1c727de8 by Seldaek

Massive Dwoo_Loader overhaul/cleanup

git-svn-id: svn://dwoo.org/dwoo/trunk@104 0598d79b-80c4-4d41-97ba-ac86fbbd088b
parent 77501e61
...@@ -8,12 +8,19 @@ ...@@ -8,12 +8,19 @@
! BC Break: Dwoo_ITemplate::cache() must now return the cached file name or false if ! BC Break: Dwoo_ITemplate::cache() must now return the cached file name or false if
caching failed, only affects you if you had a custom template class and caching failed, only affects you if you had a custom template class and
implemented cache() yourself implemented cache() yourself
! BC Break: Dwoo_Loader is not static anymore so anything you did on it directly
will break. Use $dwoo->getLoader()->addDirectory() instead of
Dwoo_Loader::addDirectory() for example
! BC Break: DWOO_COMPILE_DIRECTORY and DWOO_CACHE_DIRECTORY are gone, set those
paths in Dwoo's constructor (i.e. new Dwoo('compiledir', 'cachedir')) if you
need to override the default ones
+ Plugins: Added {dynamic} that allows cached templates to have dynamic + Plugins: Added {dynamic} that allows cached templates to have dynamic
(non-cached) parts, when rendering a cached page, the dynamic parts can still (non-cached) parts, when rendering a cached page, the dynamic parts can still
use the variables you provides use the variables you provides
+ Plugins: Added {a} to build 'a' html tags
+ Plugins: Added {tif} that acts as a ternary if / allows you to use a ternary + Plugins: Added {tif} that acts as a ternary if / allows you to use a ternary
operator inside it operator inside it
+ API: Added a Dwoo_ILoader interface if you want to provide a custom plugin
loading solution (use $dwoo->setLoader($myLoader))
+ Added line numbers in compilation errors and improved several error messages + Added line numbers in compilation errors and improved several error messages
+ Added magic object-access methods to Dwoo_Data, so you can assign values by + Added magic object-access methods to Dwoo_Data, so you can assign values by
doing $data->var = $val; instead of $data->assign('var', $val); doing $data->var = $val; instead of $data->assign('var', $val);
......
<?php <?php
set_include_path(get_include_path().PATH_SEPARATOR.dirname(__FILE__)); define('DWOO_DIRECTORY', dirname(__FILE__).DIRECTORY_SEPARATOR);
set_include_path(get_include_path() . PATH_SEPARATOR . DWOO_DIRECTORY);
include 'Dwoo/Loader.php'; include 'Dwoo/Loader.php';
include 'Dwoo/Exception.php'; include 'Dwoo/Exception.php';
...@@ -19,23 +20,15 @@ include 'Dwoo/Template/String.php'; ...@@ -19,23 +20,15 @@ include 'Dwoo/Template/String.php';
include 'Dwoo/Template/File.php'; include 'Dwoo/Template/File.php';
include 'Dwoo/Data.php'; include 'Dwoo/Data.php';
define('DWOO_DIRECTORY', dirname(__FILE__).DIRECTORY_SEPARATOR); // TODO BC Checks, remove
if (defined('DWOO_CACHE_DIRECTORY') === false) if (defined('DWOO_CACHE_DIRECTORY'))
define('DWOO_CACHE_DIRECTORY', DWOO_DIRECTORY.'cache'.DIRECTORY_SEPARATOR); throw new Dwoo_Exception('DWOO_CACHE_DIRECTORY is deprecated, you should now set this in Dwoo\'s constructor using new Dwoo([ $compileDir [, $cacheDir ]])');
if (defined('DWOO_COMPILE_DIRECTORY') === false) if (defined('DWOO_COMPILE_DIRECTORY'))
define('DWOO_COMPILE_DIRECTORY', DWOO_DIRECTORY.'compiled'.DIRECTORY_SEPARATOR); throw new Dwoo_Exception('DWOO_COMPILE_DIRECTORY is deprecated, you should now set this in Dwoo\'s constructor using new Dwoo([ $compileDir [, $cacheDir ]])');
// end
if (defined('DWOO_CHMOD') === false) if (defined('DWOO_CHMOD') === false)
define('DWOO_CHMOD', 0777); define('DWOO_CHMOD', 0777);
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');
if (is_writable(DWOO_COMPILE_DIRECTORY) === false)
throw new Dwoo_Exception('Dwoo compile directory must be writable, either chmod "'.DWOO_COMPILE_DIRECTORY.'" to make it writable or define DWOO_COMPILE_DIRECTORY to a writable directory before including Dwoo.php');
// include class paths or rebuild paths if the cache file isn't there
if ((file_exists(DWOO_COMPILE_DIRECTORY.DIRECTORY_SEPARATOR.'classpath.cache.php') && include DWOO_COMPILE_DIRECTORY.DIRECTORY_SEPARATOR.'classpath.cache.php') === false)
Dwoo_Loader::rebuildClassPathCache(DWOO_DIRECTORY.'plugins', DWOO_COMPILE_DIRECTORY.DIRECTORY_SEPARATOR.'classpath.cache.php');
Dwoo_Loader::loadPlugin('topLevelBlock');
/** /**
* main dwoo class, allows communication between the compiler, template and data classes * main dwoo class, allows communication between the compiler, template and data classes
...@@ -211,6 +204,13 @@ class Dwoo ...@@ -211,6 +204,13 @@ class Dwoo
); );
/** /**
* the dwoo loader object used to load plugins by this dwoo instance
*
* @var Dwoo_ILoader
*/
protected $loader = null;
/**
* currently rendered template, set to null when not-rendering * currently rendered template, set to null when not-rendering
* *
* @var Dwoo_ITemplate * @var Dwoo_ITemplate
...@@ -267,12 +267,29 @@ class Dwoo ...@@ -267,12 +267,29 @@ class Dwoo
protected $buffer; protected $buffer;
/** /**
* constructor, sets the cache and compile dir to the default values * constructor, sets the cache and compile dir to the default values if not provided
*
* @param string $compileDir path to the compiled directory, defaults to lib/compiled
* @param string $cacheDir path to the cache directory, defaults to lib/cache
*/ */
public function __construct() public function __construct($compileDir = null, $cacheDir = null)
{ {
$this->cacheDir = DWOO_CACHE_DIRECTORY.DIRECTORY_SEPARATOR; if ($cacheDir === null) {
$this->compileDir = DWOO_COMPILE_DIRECTORY.DIRECTORY_SEPARATOR; $this->cacheDir = DWOO_DIRECTORY.'cache'.DIRECTORY_SEPARATOR;
} else {
$this->cacheDir = $cacheDir.DIRECTORY_SEPARATOR;
}
if ($compileDir === null) {
$this->compileDir = DWOO_DIRECTORY.'compiled'.DIRECTORY_SEPARATOR;
} else {
$this->compileDir = $compileDir.DIRECTORY_SEPARATOR;
}
if (is_writable($this->cacheDir) === false)
throw new Dwoo_Exception('Dwoo cache directory must be writable, chmod "'.$this->cacheDir.'" to make it writable');
if (is_writable($this->compileDir) === false)
throw new Dwoo_Exception('Dwoo compile directory must be writable, chmod "'.$this->compileDir.'" to make it writable');
} }
/** /**
...@@ -523,7 +540,7 @@ class Dwoo ...@@ -523,7 +540,7 @@ class Dwoo
$class = 'Dwoo_Filter_'.$name; $class = 'Dwoo_Filter_'.$name;
if (!class_exists($class, false) && !function_exists($class)) { if (!class_exists($class, false) && !function_exists($class)) {
Dwoo_Loader::loadPlugin($name); $this->getLoader()->loadPlugin($name);
} }
if (class_exists($class, false)) { if (class_exists($class, false)) {
...@@ -603,6 +620,30 @@ class Dwoo ...@@ -603,6 +620,30 @@ class Dwoo
/* /*
* --------- getters and setters --------- * --------- getters and setters ---------
*/ */
/**
* sets the loader object to use to load plugins
*
* @param Dwoo_ILoader $loader loader object
*/
public function setLoader(Dwoo_ILoader $loader)
{
$this->loader = $loader;
}
/**
* returns the current loader object or a default one if none is currently found
*
* @param Dwoo_ILoader
*/
public function getLoader()
{
if ($this->loader === null) {
$this->loader = new Dwoo_Loader($this->compileDir);
}
return $this->loader;
}
/** /**
* returns the custom plugins loaded * returns the custom plugins loaded
......
...@@ -302,16 +302,12 @@ class Dwoo_Compiler implements Dwoo_ICompiler ...@@ -302,16 +302,12 @@ class Dwoo_Compiler implements Dwoo_ICompiler
$name = str_replace('Dwoo_Processor_', '', $callback); $name = str_replace('Dwoo_Processor_', '', $callback);
$class = 'Dwoo_Processor_'.$name; $class = 'Dwoo_Processor_'.$name;
if (!class_exists($class, false) && !function_exists($class)) {
Dwoo_Loader::loadPlugin($name);
}
if (class_exists($class, false)) { if (class_exists($class, false)) {
$callback = array(new $class($this), 'process'); $callback = array(new $class($this), 'process');
} elseif (function_exists($class)) { } elseif (function_exists($class)) {
$callback = $class; $callback = $class;
} else { } else {
throw new Dwoo_Exception('Wrong pre-processor name, when using autoload the filter must be in one of your plugin dir as "name.php" containg a class or function named "Dwoo_Processor_name"'); $callback = array('autoload'=>true, 'class'=>$class, 'name'=>$name);
} }
$this->processors['pre'][] = $callback; $this->processors['pre'][] = $callback;
...@@ -331,10 +327,10 @@ class Dwoo_Compiler implements Dwoo_ICompiler ...@@ -331,10 +327,10 @@ class Dwoo_Compiler implements Dwoo_ICompiler
unset($this->processors['pre'][$index]); unset($this->processors['pre'][$index]);
} elseif (($index = array_search('Dwoo_Processor_'.str_replace('Dwoo_Processor_', '', $callback), $this->processors['pre'], true)) !== false) { } elseif (($index = array_search('Dwoo_Processor_'.str_replace('Dwoo_Processor_', '', $callback), $this->processors['pre'], true)) !== false) {
unset($this->processors['pre'][$index]); unset($this->processors['pre'][$index]);
} else { } else {
$class = 'Dwoo_Processor_' . str_replace('Dwoo_Processor_', '', $callback); $class = 'Dwoo_Processor_' . str_replace('Dwoo_Processor_', '', $callback);
foreach ($this->processors['pre'] as $index=>$proc) { foreach ($this->processors['pre'] as $index=>$proc) {
if (is_array($proc) && $proc[0] instanceof $class) { if (is_array($proc) && ($proc[0] instanceof $class) || (isset($proc['class']) && $proc['class'] == $class)) {
unset($this->processors['pre'][$index]); unset($this->processors['pre'][$index]);
break; break;
} }
...@@ -355,16 +351,12 @@ class Dwoo_Compiler implements Dwoo_ICompiler ...@@ -355,16 +351,12 @@ class Dwoo_Compiler implements Dwoo_ICompiler
$name = str_replace('Dwoo_Processor_', '', $callback); $name = str_replace('Dwoo_Processor_', '', $callback);
$class = 'Dwoo_Processor_'.$name; $class = 'Dwoo_Processor_'.$name;
if (!class_exists($class, false) && !function_exists($class)) {
Dwoo_Loader::loadPlugin($name);
}
if (class_exists($class, false)) { if (class_exists($class, false)) {
$callback = array(new $class($this), 'process'); $callback = array(new $class($this), 'process');
} elseif (function_exists($class)) { } elseif (function_exists($class)) {
$callback = $class; $callback = $class;
} else { } else {
throw new Dwoo_Exception('Wrong post-processor name, when using autoload the processor must be in one of your plugin dir as "name.php" containg a class or function named "Dwoo_Processor_name"'); $callback = array('autoload'=>true, 'class'=>$class, 'name'=>$name);
} }
$this->processors['post'][] = $callback; $this->processors['post'][] = $callback;
...@@ -372,7 +364,7 @@ class Dwoo_Compiler implements Dwoo_ICompiler ...@@ -372,7 +364,7 @@ class Dwoo_Compiler implements Dwoo_ICompiler
$this->processors['post'][] = $callback; $this->processors['post'][] = $callback;
} }
} }
/** /**
* removes a postprocessor from the compiler * removes a postprocessor from the compiler
* *
...@@ -387,13 +379,40 @@ class Dwoo_Compiler implements Dwoo_ICompiler ...@@ -387,13 +379,40 @@ class Dwoo_Compiler implements Dwoo_ICompiler
} else { } else {
$class = 'Dwoo_Processor_' . str_replace('Dwoo_Processor_', '', $callback); $class = 'Dwoo_Processor_' . str_replace('Dwoo_Processor_', '', $callback);
foreach ($this->processors['post'] as $index=>$proc) { foreach ($this->processors['post'] as $index=>$proc) {
if (is_array($proc) && $proc[0] instanceof $class) { if (is_array($proc) && ($proc[0] instanceof $class) || (isset($proc['class']) && $proc['class'] == $class)) {
unset($this->processors['post'][$index]); unset($this->processors['post'][$index]);
break; break;
} }
} }
} }
} }
/**
* internal function to autoload processors at runtime if required
*
* @param string $class the class/function name
* @param string $name the plugin name (without Dwoo_Plugin_ prefix)
*/
protected function loadProcessor($class, $name)
{
if (!class_exists($class, false) && !function_exists($class)) {
try {
$this->dwoo->getLoader()->loadPlugin($name);
} catch (Dwoo_Exception $e) {
throw new Dwoo_Exception('Processor '.$name.' could not be found in your plugin directories, please ensure it is in a file named '.$name.'.php in the plugin directory');
}
}
if (class_exists($class, false)) {
return array(new $class($this), 'process');
}
if (function_exists($class)) {
return $class;
}
throw new Dwoo_Exception('Wrong processor name, when using autoload the processor must be in one of your plugin dir as "name.php" containg a class or function named "Dwoo_Processor_name"');
}
/** /**
* adds the custom plugins loaded into Dwoo to the compiler so it can load them * adds the custom plugins loaded into Dwoo to the compiler so it can load them
...@@ -465,7 +484,7 @@ class Dwoo_Compiler implements Dwoo_ICompiler ...@@ -465,7 +484,7 @@ class Dwoo_Compiler implements Dwoo_ICompiler
if ($isOffset) { if ($isOffset) {
$this->line += $number; $this->line += $number;
} else { } else {
$this->line = $position; $this->line = $number;
} }
} }
...@@ -540,6 +559,9 @@ class Dwoo_Compiler implements Dwoo_ICompiler ...@@ -540,6 +559,9 @@ class Dwoo_Compiler implements Dwoo_ICompiler
// runs preprocessors // runs preprocessors
foreach ($this->processors['pre'] as $preProc) { foreach ($this->processors['pre'] as $preProc) {
if (is_array($preProc) && isset($preProc['autoload'])) {
$preProc = $this->loadProcessor($preProc['class'], $preProc['name']);
}
if (is_array($preProc) && $preProc[0] instanceof Dwoo_Processor) { if (is_array($preProc) && $preProc[0] instanceof Dwoo_Processor) {
$tpl = call_user_func($preProc, $tpl); $tpl = call_user_func($preProc, $tpl);
} else { } else {
...@@ -668,6 +690,9 @@ class Dwoo_Compiler implements Dwoo_ICompiler ...@@ -668,6 +690,9 @@ class Dwoo_Compiler implements Dwoo_ICompiler
if ($this->debug) echo 'PROCESSING POSTPROCESSORS<br>'; if ($this->debug) echo 'PROCESSING POSTPROCESSORS<br>';
foreach ($this->processors['post'] as $postProc) { foreach ($this->processors['post'] as $postProc) {
if (is_array($postProc) && isset($postProc['autoload'])) {
$postProc = $this->loadProcessor($postProc['class'], $postProc['name']);
}
if (is_array($postProc) && $postProc[0] instanceof Dwoo_Processor) { if (is_array($postProc) && $postProc[0] instanceof Dwoo_Processor) {
$compiled = call_user_func($postProc, $compiled); $compiled = call_user_func($postProc, $compiled);
} else { } else {
...@@ -687,19 +712,19 @@ class Dwoo_Compiler implements Dwoo_ICompiler ...@@ -687,19 +712,19 @@ class Dwoo_Compiler implements Dwoo_ICompiler
case Dwoo::BLOCK_PLUGIN: case Dwoo::BLOCK_PLUGIN:
case Dwoo::CLASS_PLUGIN: case Dwoo::CLASS_PLUGIN:
$output .= "if (class_exists('Dwoo_Plugin_$plugin', false)===false)\n\tDwoo_Loader::loadPlugin('$plugin');\n"; $output .= "if (class_exists('Dwoo_Plugin_$plugin', false)===false)\n\t\$this->loader->loadPlugin('$plugin');\n";
break; break;
case Dwoo::FUNC_PLUGIN: case Dwoo::FUNC_PLUGIN:
$output .= "if (function_exists('Dwoo_Plugin_$plugin')===false)\n\tDwoo_Loader::loadPlugin('$plugin');\n"; $output .= "if (function_exists('Dwoo_Plugin_$plugin')===false)\n\t\$this->loader->loadPlugin('$plugin');\n";
break; break;
case Dwoo::SMARTY_MODIFIER: case Dwoo::SMARTY_MODIFIER:
$output .= "if (function_exists('smarty_modifier_$plugin')===false)\n\tDwoo_Loader::loadPlugin('$plugin');\n"; $output .= "if (function_exists('smarty_modifier_$plugin')===false)\n\t\$this->loader->loadPlugin('$plugin');\n";
break; break;
case Dwoo::SMARTY_FUNCTION: case Dwoo::SMARTY_FUNCTION:
$output .= "if (function_exists('smarty_function_$plugin')===false)\n\tDwoo_Loader::loadPlugin('$plugin');\n"; $output .= "if (function_exists('smarty_function_$plugin')===false)\n\t\$this->loader->loadPlugin('$plugin');\n";
break; break;
case Dwoo::SMARTY_BLOCK: case Dwoo::SMARTY_BLOCK:
$output .= "if (function_exists('smarty_block_$plugin')===false)\n\tDwoo_Loader::loadPlugin('$plugin');\n"; $output .= "if (function_exists('smarty_block_$plugin')===false)\n\t\$this->loader->loadPlugin('$plugin');\n";
break; break;
default: default:
throw new Dwoo_Compilation_Exception($this, 'Type error for '.$plugin.' with type'.$type); throw new Dwoo_Compilation_Exception($this, 'Type error for '.$plugin.' with type'.$type);
...@@ -831,7 +856,7 @@ class Dwoo_Compiler implements Dwoo_ICompiler ...@@ -831,7 +856,7 @@ class Dwoo_Compiler implements Dwoo_ICompiler
{ {
$class = 'Dwoo_Plugin_'.$type; $class = 'Dwoo_Plugin_'.$type;
if (class_exists($class, false) === false) { if (class_exists($class, false) === false) {
Dwoo_Loader::loadPlugin($type); $this->dwoo->getLoader()->loadPlugin($type);
} }
$params = $this->mapParams($params, array($class, 'init'), $paramtype); $params = $this->mapParams($params, array($class, 'init'), $paramtype);
...@@ -877,7 +902,7 @@ class Dwoo_Compiler implements Dwoo_ICompiler ...@@ -877,7 +902,7 @@ class Dwoo_Compiler implements Dwoo_ICompiler
{ {
$class = 'Dwoo_Plugin_'.$type; $class = 'Dwoo_Plugin_'.$type;
if (class_exists($class, false) === false) { if (class_exists($class, false) === false) {
Dwoo_Loader::loadPlugin($type); $this->dwoo->getLoader()->loadPlugin($type);
} }
$this->stack[] = array('type' => $type, 'params' => $params, 'custom' => false, 'buffer' => null); $this->stack[] = array('type' => $type, 'params' => $params, 'custom' => false, 'buffer' => null);
$this->curBlock =& $this->stack[count($this->stack)-1]; $this->curBlock =& $this->stack[count($this->stack)-1];
...@@ -2311,7 +2336,7 @@ class Dwoo_Compiler implements Dwoo_ICompiler ...@@ -2311,7 +2336,7 @@ class Dwoo_Compiler implements Dwoo_ICompiler
} else { } else {
if ($pluginType===-1) { if ($pluginType===-1) {
try { try {
Dwoo_Loader::loadPlugin($name, isset($phpFunc)===false); $this->dwoo->getLoader()->loadPlugin($name, isset($phpFunc)===false);
} catch (Exception $e) { } catch (Exception $e) {
if (isset($phpFunc)) { if (isset($phpFunc)) {
$pluginType = Dwoo::NATIVE_PLUGIN; $pluginType = Dwoo::NATIVE_PLUGIN;
......
<?php
/**
* interface for dwoo plugin loaders
*
* 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.
*
* This file is released under the LGPL
* "GNU Lesser General Public License"
* More information can be found here:
* {@link http://www.gnu.org/copyleft/lesser.html}
*
* @author Jordi Boggiano <j.boggiano@seld.be>
* @copyright Copyright (c) 2008, Jordi Boggiano
* @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
* @link http://dwoo.org/
* @version 0.9.1
* @date 2008-05-30
* @package Dwoo
*/
interface Dwoo_ILoader
{
/**
* loads a plugin file
*
* the second parameter is used to avoid permanent rehashing when using php functions,
* however this means that if you have add a plugin that overrides a php function you have
* to delete the classpath.cache file(s) by hand to force a rehash of the plugins
*
* @param string $class the plugin name, without the Dwoo_Plugin_ prefix
* @param bool $forceRehash if true, the class path caches will be rebuilt if the plugin is not found, in case it has just been added, defaults to true
*/
public static function loadPlugin($class, $forceRehash = true);
}
...@@ -27,7 +27,7 @@ class Dwoo_Loader ...@@ -27,7 +27,7 @@ class Dwoo_Loader
* @see addDirectory * @see addDirectory
* @var array * @var array
*/ */
protected static $paths = array(); protected $paths = array();
/** /**
* stores the plugins names/paths relationships * stores the plugins names/paths relationships
...@@ -35,22 +35,46 @@ class Dwoo_Loader ...@@ -35,22 +35,46 @@ class Dwoo_Loader
* *
* @see addDirectory * @see addDirectory
* @var array * @var array
* @access protected */
protected $classPath = array();
/**
* path where class paths cache files are written
*
* @var string
*/
protected $cacheDir;
/**
* legacy/transitional var for BC with old classpath.cache files, do NOT rely on it
*
* will be deleted sooner or later
*
* TODO remove this and compat code in addDirectory
*/ */
public static $classpath = array(); public static $classpath = array();
public function __construct($cacheDir)
{
$this->cacheDir = $cacheDir . DIRECTORY_SEPARATOR;
// include class paths or rebuild paths if the cache file isn't there
if ((file_exists($this->cacheDir.'classpath.cache.php') && include $this->cacheDir.'classpath.cache.php') == false) {
$this->rebuildClassPathCache(DWOO_DIRECTORY.'plugins', $this->cacheDir.'classpath.cache.php');
}
}
/** /**
* rebuilds class paths, scans the given directory recursively and saves all paths in the given file * rebuilds class paths, scans the given directory recursively and saves all paths in the given file
* *
* @param string $path the plugin path to scan * @param string $path the plugin path to scan
* @param string $cacheFile the file where to store the plugin paths cache, it will be overwritten * @param string $cacheFile the file where to store the plugin paths cache, it will be overwritten
* @access protected
*/ */
public static function rebuildClassPathCache($path, $cacheFile) protected function rebuildClassPathCache($path, $cacheFile)
{ {
if ($cacheFile!==false) { if ($cacheFile!==false) {
$tmp = self::$classpath; $tmp = $this->classPath;
self::$classpath = array(); $this->classPath = array();
} }
// iterates over all files/folders // iterates over all files/folders
...@@ -58,19 +82,19 @@ class Dwoo_Loader ...@@ -58,19 +82,19 @@ class Dwoo_Loader
if (is_array($list)) { if (is_array($list)) {
foreach ($list as $f) { foreach ($list as $f) {
if (is_dir($f)) { if (is_dir($f)) {
self::rebuildClassPathCache($f, false); $this->rebuildClassPathCache($f, false);
} else { } else {
self::$classpath[str_replace(array('function.','block.','modifier.','outputfilter.','filter.','prefilter.','postfilter.','pre.','post.','output.','shared.','helper.'), '', basename($f, '.php'))] = $f; $this->classPath[str_replace(array('function.','block.','modifier.','outputfilter.','filter.','prefilter.','postfilter.','pre.','post.','output.','shared.','helper.'), '', basename($f, '.php'))] = $f;
} }
} }
} }
// save in file if it's the first call (not recursed) // save in file if it's the first call (not recursed)
if ($cacheFile!==false) { if ($cacheFile!==false) {
if (!file_put_contents($cacheFile, '<?php Dwoo_Loader::$classpath = '.var_export(self::$classpath, true).' + Dwoo_Loader::$classpath; ?>')) { if (!file_put_contents($cacheFile, '<?php $this->classPath = '.var_export($this->classPath, true).' + $this->classPath;')) {
throw new Dwoo_Exception('Could not write into '.$cacheFile.', either because the folder is not there (create it) or because of the chmod configuration (please ensure this directory is writable by php)'); throw new Dwoo_Exception('Could not write into '.$cacheFile.', either because the folder is not there (create it) or because of the chmod configuration (please ensure this directory is writable by php), alternatively you can change the directory used with $dwoo->setCompileDir() or provide a custom loader object with $dwoo->setLoader()');
} }
self::$classpath += $tmp; $this->classPath += $tmp;
} }
} }
...@@ -80,17 +104,17 @@ class Dwoo_Loader ...@@ -80,17 +104,17 @@ class Dwoo_Loader
* @param string $class the plugin name, without the Dwoo_Plugin_ prefix * @param string $class the plugin name, without the Dwoo_Plugin_ prefix
* @param bool $forceRehash if true, the class path caches will be rebuilt if the plugin is not found, in case it has just been added, defaults to true * @param bool $forceRehash if true, the class path caches will be rebuilt if the plugin is not found, in case it has just been added, defaults to true
*/ */
public static function loadPlugin($class, $forceRehash = true) public function loadPlugin($class, $forceRehash = true)
{ {
// a new class was added or the include failed so we rebuild the cache // a new class was added or the include failed so we rebuild the cache
if (!isset(self::$classpath[$class]) || !include self::$classpath[$class]) { if (!isset($this->classPath[$class]) || !include $this->classPath[$class]) {
if ($forceRehash) { if ($forceRehash) {
self::rebuildClassPathCache(DWOO_DIRECTORY . 'plugins', DWOO_COMPILE_DIRECTORY . DIRECTORY_SEPARATOR . 'classpath.cache.php'); $this->rebuildClassPathCache(DWOO_DIRECTORY . 'plugins', $this->cacheDir . 'classpath.cache.php');
foreach (self::$paths as $path=>$file) { foreach ($this->paths as $path=>$file) {
self::rebuildClassPathCache($path, $file); $this->rebuildClassPathCache($path, $file);
} }
if (isset(self::$classpath[$class])) { if (isset($this->classPath[$class])) {
include self::$classpath[$class]; include $this->classPath[$class];
} else { } else {
throw new Dwoo_Exception('Plugin <em>'.$class.'</em> can not be found, maybe you forgot to bind it if it\'s a custom plugin ?', E_USER_NOTICE); throw new Dwoo_Exception('Plugin <em>'.$class.'</em> can not be found, maybe you forgot to bind it if it\'s a custom plugin ?', E_USER_NOTICE);
} }
...@@ -112,16 +136,21 @@ class Dwoo_Loader ...@@ -112,16 +136,21 @@ class Dwoo_Loader
* *
* @param string $pluginDir the plugin path to scan * @param string $pluginDir the plugin path to scan
*/ */
public static function addDirectory($pluginDir) public function addDirectory($pluginDir)
{ {
if (!isset(self::$paths[$pluginDir])) { $cacheFile = $this->cacheDir . 'classpath-'.substr(strtr($pluginDir, ':/\\.', '----'), strlen($pluginDir) > 80 ? -80 : 0).'.cache.php';
$cacheFile = DWOO_COMPILE_DIRECTORY . DIRECTORY_SEPARATOR . 'classpath-'.substr(strtr($pluginDir, ':/\\.', '----'), strlen($pluginDir) > 80 ? -80 : 0).'.cache.php'; $this->paths[$pluginDir] = $cacheFile;
self::$paths[$pluginDir] = $cacheFile; if (file_exists($cacheFile)) {
if (file_exists($cacheFile)) { include $cacheFile;
include $cacheFile; // BC code, will be removed
} else { if (!empty(Dwoo_Loader::$classpath)) {
Dwoo_Loader::rebuildClassPathCache($pluginDir, $cacheFile); $this->classPath = Dwoo_Loader::$classpath + $this->classPath;
Dwoo_Loader::$classpath = array();
$this->rebuildClassPathCache($pluginDir, $cacheFile);
} }
// end
} else {
$this->rebuildClassPathCache($pluginDir, $cacheFile);
} }
} }
} }
...@@ -209,7 +209,7 @@ class Dwoo_Smarty__Adapter extends Dwoo ...@@ -209,7 +209,7 @@ class Dwoo_Smarty__Adapter extends Dwoo
if (!empty($this->plugins_dir)) { if (!empty($this->plugins_dir)) {
foreach ($this->plugins_dir as $dir) { foreach ($this->plugins_dir as $dir) {
Dwoo_Loader::addDirectory(rtrim($dir, '\\/')); $this->getLoader()->addDirectory(rtrim($dir, '\\/'));
} }
} }
......
...@@ -11,7 +11,7 @@ class BlockTests extends PHPUnit_Framework_TestCase ...@@ -11,7 +11,7 @@ class BlockTests extends PHPUnit_Framework_TestCase
{ {
// extend this class and override this in your constructor to test a modded compiler // extend this class and override this in your constructor to test a modded compiler
$this->compiler = new Dwoo_Compiler(); $this->compiler = new Dwoo_Compiler();
$this->dwoo = new Dwoo(); $this->dwoo = new Dwoo(DWOO_COMPILE_DIR, DWOO_CACHE_DIR);
} }
public function testAutoEscape() public function testAutoEscape()
......
...@@ -10,7 +10,7 @@ class BugTests extends PHPUnit_Framework_TestCase ...@@ -10,7 +10,7 @@ class BugTests extends PHPUnit_Framework_TestCase
public function __construct() public function __construct()
{ {
$this->compiler = new Dwoo_Compiler(); $this->compiler = new Dwoo_Compiler();
$this->dwoo = new Dwoo(); $this->dwoo = new Dwoo(DWOO_COMPILE_DIR, DWOO_CACHE_DIR);
} }
public function testBlockStackBufferingBug() public function testBlockStackBufferingBug()
......
...@@ -11,7 +11,7 @@ class CallTests extends PHPUnit_Framework_TestCase ...@@ -11,7 +11,7 @@ class CallTests extends PHPUnit_Framework_TestCase
{ {
// extend this class and override this in your constructor to test a modded compiler // extend this class and override this in your constructor to test a modded compiler
$this->compiler = new Dwoo_Compiler(); $this->compiler = new Dwoo_Compiler();
$this->dwoo = new Dwoo(); $this->dwoo = new Dwoo(DWOO_COMPILE_DIR, DWOO_CACHE_DIR);
} }
public function testCustomFunctionPlugin() public function testCustomFunctionPlugin()
......
...@@ -12,7 +12,7 @@ class CompilerTests extends PHPUnit_Framework_TestCase ...@@ -12,7 +12,7 @@ class CompilerTests extends PHPUnit_Framework_TestCase
{ {
// extend this class and override this in your constructor to test a modded compiler // extend this class and override this in your constructor to test a modded compiler
$this->compiler = new Dwoo_Compiler(); $this->compiler = new Dwoo_Compiler();
$this->dwoo = new Dwoo(); $this->dwoo = new Dwoo(DWOO_COMPILE_DIR, DWOO_CACHE_DIR);
} }
public function testVarReplacement() public function testVarReplacement()
...@@ -408,6 +408,10 @@ replace="BAR" ...@@ -408,6 +408,10 @@ replace="BAR"
{ {
$cmp = new Dwoo_Compiler(); $cmp = new Dwoo_Compiler();
$cmp->addPreProcessor('__BAAAAD__', true); $cmp->addPreProcessor('__BAAAAD__', true);
$tpl = new Dwoo_Template_String('');
$tpl->forceCompilation();
$this->dwoo->get($tpl, array(), $cmp);
} }
/** /**
...@@ -417,6 +421,10 @@ replace="BAR" ...@@ -417,6 +421,10 @@ replace="BAR"
{ {
$cmp = new Dwoo_Compiler(); $cmp = new Dwoo_Compiler();
$cmp->addPostProcessor('__BAAAAD__', true); $cmp->addPostProcessor('__BAAAAD__', true);
$tpl = new Dwoo_Template_String('');
$tpl->forceCompilation();
$this->dwoo->get($tpl, array(), $cmp);
} }
/** /**
......
...@@ -10,15 +10,17 @@ class CoreTests extends PHPUnit_Framework_TestCase ...@@ -10,15 +10,17 @@ class CoreTests extends PHPUnit_Framework_TestCase
public function __construct() public function __construct()
{ {
$this->compiler = new Dwoo_Compiler(); $this->compiler = new Dwoo_Compiler();
$this->dwoo = new Dwoo(); $this->dwoo = new Dwoo(DWOO_COMPILE_DIR, DWOO_CACHE_DIR);
} }
public function testCoverConstructorsEtc() public function testCoverConstructorsEtc()
{ {
// extend this class and override this in your constructor to test a modded compiler // extend this class and override this in your constructor to test a modded compiler
$this->compiler = new Dwoo_Compiler(); $this->compiler = new Dwoo_Compiler();
$this->dwoo = new Dwoo(); $this->dwoo = new Dwoo(DWOO_COMPILE_DIR, DWOO_CACHE_DIR);
Dwoo_Loader::rebuildClassPathCache(DWOO_DIRECTORY.'plugins', DWOO_COMPILE_DIRECTORY.DIRECTORY_SEPARATOR.'classpath.cache.php'); $tpl = new Dwoo_Template_String('');
$tpl->forceCompilation();
$this->assertEquals("", $this->dwoo->get($tpl, array(), $this->compiler));
// fixes the init call not being called (which is normal) // fixes the init call not being called (which is normal)
$fixCall = new Dwoo_Plugin_topLevelBlock($this->dwoo); $fixCall = new Dwoo_Plugin_topLevelBlock($this->dwoo);
...@@ -85,7 +87,7 @@ class CoreTests extends PHPUnit_Framework_TestCase ...@@ -85,7 +87,7 @@ class CoreTests extends PHPUnit_Framework_TestCase
public function testGetSetSecurityPolicy() public function testGetSetSecurityPolicy()
{ {
$dwoo = new Dwoo(); $dwoo = new Dwoo(DWOO_COMPILE_DIR, DWOO_CACHE_DIR);
$policy = new Dwoo_Security_Policy(); $policy = new Dwoo_Security_Policy();
$policy->setConstantHandling(Dwoo_Security_Policy::CONST_ALLOW); $policy->setConstantHandling(Dwoo_Security_Policy::CONST_ALLOW);
$dwoo->setSecurityPolicy($policy); $dwoo->setSecurityPolicy($policy);
...@@ -249,7 +251,7 @@ class CoreTests extends PHPUnit_Framework_TestCase ...@@ -249,7 +251,7 @@ class CoreTests extends PHPUnit_Framework_TestCase
public function testCachedTemplateWithDwoo_Cache() public function testCachedTemplateWithDwoo_Cache()
{ {
$dwoo = new Dwoo(); $dwoo = new Dwoo(DWOO_COMPILE_DIR, DWOO_CACHE_DIR);
$dwoo->setCacheTime(10); $dwoo->setCacheTime(10);
$tpl = new Dwoo_Template_String('foo{$foo}bar', null, 'cachetest2'); $tpl = new Dwoo_Template_String('foo{$foo}bar', null, 'cachetest2');
$tpl->forceCompilation(); $tpl->forceCompilation();
...@@ -263,7 +265,7 @@ class CoreTests extends PHPUnit_Framework_TestCase ...@@ -263,7 +265,7 @@ class CoreTests extends PHPUnit_Framework_TestCase
public function testClearCacheOnTemplateClass() public function testClearCacheOnTemplateClass()
{ {
$dwoo = new Dwoo(); $dwoo = new Dwoo(DWOO_COMPILE_DIR, DWOO_CACHE_DIR);
$dwoo->setCacheTime(10); $dwoo->setCacheTime(10);
$tpl = new Dwoo_Template_String('foo{$foo}bar', null, 'cachetest2'); $tpl = new Dwoo_Template_String('foo{$foo}bar', null, 'cachetest2');
$tpl->forceCompilation(); $tpl->forceCompilation();
...@@ -278,7 +280,7 @@ class CoreTests extends PHPUnit_Framework_TestCase ...@@ -278,7 +280,7 @@ class CoreTests extends PHPUnit_Framework_TestCase
public function testTemplateGetSet() public function testTemplateGetSet()
{ {
$dwoo = new Dwoo(); $dwoo = new Dwoo(DWOO_COMPILE_DIR, DWOO_CACHE_DIR);
$dwoo->setCacheTime(10); $dwoo->setCacheTime(10);
$tpl = new Dwoo_Template_String('foo'); $tpl = new Dwoo_Template_String('foo');
$tpl2 = new Dwoo_Template_File('./resources/test.html'); $tpl2 = new Dwoo_Template_File('./resources/test.html');
......
...@@ -11,7 +11,7 @@ class DataTests extends PHPUnit_Framework_TestCase ...@@ -11,7 +11,7 @@ class DataTests extends PHPUnit_Framework_TestCase
{ {
// extend this class and override this in your constructor to test a modded compiler // extend this class and override this in your constructor to test a modded compiler
$this->compiler = new Dwoo_Compiler(); $this->compiler = new Dwoo_Compiler();
$this->dwoo = new Dwoo(); $this->dwoo = new Dwoo(DWOO_COMPILE_DIR, DWOO_CACHE_DIR);
$this->tpl = new Dwoo_Template_String('{$var}{$var2}{$var3}{$var4}'); $this->tpl = new Dwoo_Template_String('{$var}{$var2}{$var3}{$var4}');
$this->tpl->forceCompilation(); $this->tpl->forceCompilation();
} }
......
...@@ -3,8 +3,9 @@ ...@@ -3,8 +3,9 @@
error_reporting(E_ALL|E_STRICT); error_reporting(E_ALL|E_STRICT);
if (!ini_get('date.timezone')) if (!ini_get('date.timezone'))
date_default_timezone_set('CET'); date_default_timezone_set('CET');
define('DWOO_CACHE_DIRECTORY', dirname(__FILE__).DIRECTORY_SEPARATOR.'temp'.DIRECTORY_SEPARATOR.'cache'); define('DWOO_CACHE_DIR', dirname(__FILE__).DIRECTORY_SEPARATOR.'temp'.DIRECTORY_SEPARATOR.'cache');
define('DWOO_COMPILE_DIRECTORY', dirname(__FILE__).DIRECTORY_SEPARATOR.'temp'.DIRECTORY_SEPARATOR.'compiled'); define('DWOO_COMPILE_DIR', dirname(__FILE__).DIRECTORY_SEPARATOR.'temp'.DIRECTORY_SEPARATOR.'compiled');
require dirname(dirname(__FILE__)).DIRECTORY_SEPARATOR.'lib'.DIRECTORY_SEPARATOR.'Dwoo.php'; require dirname(dirname(__FILE__)).DIRECTORY_SEPARATOR.'lib'.DIRECTORY_SEPARATOR.'Dwoo.php';
define('TEST_DIRECTORY', dirname(__FILE__)); define('TEST_DIRECTORY', dirname(__FILE__));
......
...@@ -11,7 +11,7 @@ class FiltersTests extends PHPUnit_Framework_TestCase ...@@ -11,7 +11,7 @@ class FiltersTests extends PHPUnit_Framework_TestCase
{ {
// extend this class and override this in your constructor to test a modded compiler // extend this class and override this in your constructor to test a modded compiler
$this->compiler = new Dwoo_Compiler(); $this->compiler = new Dwoo_Compiler();
$this->dwoo = new Dwoo(); $this->dwoo = new Dwoo(DWOO_COMPILE_DIR, DWOO_CACHE_DIR);
} }
public function testHtmlFormat() public function testHtmlFormat()
...@@ -19,7 +19,7 @@ class FiltersTests extends PHPUnit_Framework_TestCase ...@@ -19,7 +19,7 @@ class FiltersTests extends PHPUnit_Framework_TestCase
$tpl = new Dwoo_Template_String("<html><body><div><p>a<em>b</em>c<hr /></p><textarea>a\n b</textarea></div></body><html>"); $tpl = new Dwoo_Template_String("<html><body><div><p>a<em>b</em>c<hr /></p><textarea>a\n b</textarea></div></body><html>");
$tpl->forceCompilation(); $tpl->forceCompilation();
$dwoo = new Dwoo(); $dwoo = new Dwoo(DWOO_COMPILE_DIR, DWOO_CACHE_DIR);
$dwoo->addFilter('html_format', true); $dwoo->addFilter('html_format', true);
$this->assertEquals(str_replace("\r", '', <<<SNIPPET $this->assertEquals(str_replace("\r", '', <<<SNIPPET
......
...@@ -11,7 +11,7 @@ class FuncTests extends PHPUnit_Framework_TestCase ...@@ -11,7 +11,7 @@ class FuncTests extends PHPUnit_Framework_TestCase
{ {
// extend this class and override this in your constructor to test a modded compiler // extend this class and override this in your constructor to test a modded compiler
$this->compiler = new Dwoo_Compiler(); $this->compiler = new Dwoo_Compiler();
$this->dwoo = new Dwoo(); $this->dwoo = new Dwoo(DWOO_COMPILE_DIR, DWOO_CACHE_DIR);
} }
public function testA() public function testA()
......
...@@ -11,7 +11,7 @@ class HelperTests extends PHPUnit_Framework_TestCase ...@@ -11,7 +11,7 @@ class HelperTests extends PHPUnit_Framework_TestCase
{ {
// extend this class and override this in your constructor to test a modded compiler // extend this class and override this in your constructor to test a modded compiler
$this->compiler = new Dwoo_Compiler(); $this->compiler = new Dwoo_Compiler();
$this->dwoo = new Dwoo(); $this->dwoo = new Dwoo(DWOO_COMPILE_DIR, DWOO_CACHE_DIR);
} }
public function testArray() public function testArray()
......
...@@ -13,7 +13,7 @@ class SecurityTests extends PHPUnit_Framework_TestCase ...@@ -13,7 +13,7 @@ class SecurityTests extends PHPUnit_Framework_TestCase
public function __construct() public function __construct()
{ {
$this->compiler = new Dwoo_Compiler(); $this->compiler = new Dwoo_Compiler();
$this->dwoo = new Dwoo(); $this->dwoo = new Dwoo(DWOO_COMPILE_DIR, DWOO_CACHE_DIR);
$this->policy = new Dwoo_Security_Policy(); $this->policy = new Dwoo_Security_Policy();
$this->dwoo->setSecurityPolicy($this->policy); $this->dwoo->setSecurityPolicy($this->policy);
} }
......
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