Arbit - project tracking

Dwoo

History

Diff

495dc1 9315dc a/CHANGELOG
5 5 count (just a helper that counts anything). It won't affect you unless
6 6 you built some plugin depending on isArray, in which case you should
7 7 check all works fine still
8 ++ Moved Dwoo code to Dwoo_Core that is extended by Dwoo, so you can use
9 + the Dwoo directory as an svn:externals without problems now and just
10 + use Dwoo_Core in place of Dwoo in your code
8 11 + Improved parsing of array() to support real php array syntax as well
9 12 as variables as array keys, thanks to acecream for the help
10 13 + Improved parsing of named parameters that can now be quoted
495dc1 9315dc a/lib/Dwoo.compiled.php
2 -<?php define('DWOO_DIRECTORY', dirname(__FILE__) . DIRECTORY_SEPARATOR); class Dwoo { const VERSION = '1.1.1'; const RELEASE_TAG = 17; const CLASS_PLUGIN = 1; const FUNC_PLUGIN = 2; const NATIVE_PLUGIN = 4; const BLOCK_PLUGIN = 8; const COMPILABLE_PLUGIN = 16; const CUSTOM_PLUGIN = 32; const SMARTY_MODIFIER = 64; const SMARTY_BLOCK = 128; const SMARTY_FUNCTION = 256; const PROXY_PLUGIN = 512; const TEMPLATE_PLUGIN = 1024; protected $charset = 'utf-8'; public $globals; protected $compileDir; protected $cacheDir; protected $cacheTime = 0; protected $securityPolicy = null; protected $plugins = array(); protected $filters = array(); protected $resources = array ( 'file' => array ( 'class' => 'Dwoo_Template_File', 'compiler' => null ), 'string' => array ( 'class' => 'Dwoo_Template_String', 'compiler' => null ) ); protected $loader = null; protected $template = null; protected $runtimePlugins; public $data; public $scope; protected $scopeTree; protected $stack; protected $curBlock; protected $buffer; protected $pluginProxy; public function __construct($compileDir = null, $cacheDir = null) { if ($compileDir !== null) { $this->setCompileDir($compileDir); } if ($cacheDir !== null) { $this->setCacheDir($cacheDir); } $this->initGlobals(); } public function __clone() { $this->template = null; unset($this->data); } public function output($tpl, $data = array(), Dwoo_ICompiler $compiler = null) { return $this->get($tpl, $data, $compiler, true); } public function get($_tpl, $data = array(), $_compiler = null, $_output = false) { if ($this->template instanceof Dwoo_ITemplate) { $proxy = clone $this; return $proxy->get($_tpl, $data, $_compiler, $_output); } if ($_tpl instanceof Dwoo_ITemplate) { } elseif (is_string($_tpl) && file_exists($_tpl)) { $_tpl = new Dwoo_Template_File($_tpl); } else { throw new Dwoo_Exception('Dwoo->get/Dwoo->output\'s first argument must be a Dwoo_ITemplate (i.e. Dwoo_Template_File) or a valid path to a template file', E_USER_NOTICE); } $this->template = $_tpl; if ($data instanceof Dwoo_IDataProvider) { $this->data = $data->getData(); } elseif (is_array($data)) { $this->data = $data; } else { throw new Dwoo_Exception('Dwoo->get/Dwoo->output\'s data argument must be a Dwoo_IDataProvider object (i.e. Dwoo_Data) or an associative array', E_USER_NOTICE); } $this->globals['template'] = $_tpl->getName(); $this->initRuntimeVars($_tpl); $file = $_tpl->getCachedTemplate($this); $doCache = $file === true; $cacheLoaded = is_string($file); if ($cacheLoaded === true) { if ($_output === true) { include $file; $this->template = null; } else { ob_start(); include $file; $this->template = null; return ob_get_clean(); } } else { if ($doCache === true) { $dynamicId = uniqid(); } $compiledTemplate = $_tpl->getCompiledTemplate($this, $_compiler); $out = include $compiledTemplate; if ($out === false) { $_tpl->forceCompilation(); $compiledTemplate = $_tpl->getCompiledTemplate($this, $_compiler); $out = include $compiledTemplate; } if ($doCache === true) { $out = preg_replace('/(<%|%>|<\?php|<\?|\?>)/', '<?php /*'.$dynamicId.'*/ echo \'$1\'; ?>', $out); if (!class_exists('Dwoo_plugin_dynamic', false)) { $this->getLoader()->loadPlugin('dynamic'); } $out = Dwoo_Plugin_dynamic::unescape($out, $dynamicId, $compiledTemplate); } foreach ($this->filters as $filter) { if (is_array($filter) && $filter[0] instanceof Dwoo_Filter) { $out = call_user_func($filter, $out); } else { $out = call_user_func($filter, $this, $out); } } if ($doCache === true) { $file = $_tpl->cache($this, $out); if ($_output === true) { include $file; $this->template = null; } else { ob_start(); include $file; $this->template = null; return ob_get_clean(); } } else { $this->template = null; if ($_output === true) { echo $out; } return $out; } } } protected function initGlobals() { $this->globals = array ( 'version' => self::VERSION, 'ad' => '<a href="http://dwoo.org/">Powered by Dwoo</a>', 'now' => $_SERVER['REQUEST_TIME'], 'charset' => $this->charset, ); } protected function initRuntimeVars(Dwoo_ITemplate $tpl) { $this->runtimePlugins = array(); $this->scope =& $this->data; $this->scopeTree = array(); $this->stack = array(); $this->curBlock = null; $this->buffer = ''; } public function addPlugin($name, $callback, $compilable = false) { $compilable = $compilable ? self::COMPILABLE_PLUGIN : 0; if (is_array($callback)) { if (is_subclass_of(is_object($callback[0]) ? get_class($callback[0]) : $callback[0], 'Dwoo_Block_Plugin')) { $this->plugins[$name] = array('type'=>self::BLOCK_PLUGIN | $compilable, 'callback'=>$callback, 'class'=>(is_object($callback[0]) ? get_class($callback[0]) : $callback[0])); } else { $this->plugins[$name] = array('type'=>self::CLASS_PLUGIN | $compilable, 'callback'=>$callback, 'class'=>(is_object($callback[0]) ? get_class($callback[0]) : $callback[0]), 'function'=>$callback[1]); } } elseif (class_exists($callback, false)) { if (is_subclass_of($callback, 'Dwoo_Block_Plugin')) { $this->plugins[$name] = array('type'=>self::BLOCK_PLUGIN | $compilable, 'callback'=>$callback, 'class'=>$callback); } else { $this->plugins[$name] = array('type'=>self::CLASS_PLUGIN | $compilable, 'callback'=>$callback, 'class'=>$callback, 'function'=>'process'); } } elseif (function_exists($callback)) { $this->plugins[$name] = array('type'=>self::FUNC_PLUGIN | $compilable, 'callback'=>$callback); } else { throw new Dwoo_Exception('Callback could not be processed correctly, please check that the function/class you used exists'); } } public function removePlugin($name) { if (isset($this->plugins[$name])) { unset($this->plugins[$name]); } } public function addFilter($callback, $autoload = false) { if ($autoload) { $class = 'Dwoo_Filter_'.$callback; if (!class_exists($class, false) && !function_exists($class)) { try { $this->getLoader()->loadPlugin($callback); } catch (Dwoo_Exception $e) { if (strstr($callback, 'Dwoo_Filter_')) { throw new Dwoo_Exception('Wrong filter name : '.$callback.', the "Dwoo_Filter_" prefix should not be used, please only use "'.str_replace('Dwoo_Filter_', '', $callback).'"'); } else { throw new Dwoo_Exception('Wrong filter name : '.$callback.', when using autoload the filter must be in one of your plugin dir as "name.php" containg a class or function named "Dwoo_Filter_name"'); } } } if (class_exists($class, false)) { $callback = array(new $class($this), 'process'); } elseif (function_exists($class)) { $callback = $class; } else { throw new Dwoo_Exception('Wrong filter name : '.$callback.', when using autoload the filter must be in one of your plugin dir as "name.php" containg a class or function named "Dwoo_Filter_name"'); } $this->filters[] = $callback; } else { $this->filters[] = $callback; } } public function removeFilter($callback) { if (($index = array_search('Dwoo_Filter_'.$callback, $this->filters, true)) !== false) { unset($this->filters[$index]); } elseif (($index = array_search($callback, $this->filters, true)) !== false) { unset($this->filters[$index]); } else { $class = 'Dwoo_Filter_' . $callback; foreach ($this->filters as $index=>$filter) { if (is_array($filter) && $filter[0] instanceof $class) { unset($this->filters[$index]); break; } } } } public function addResource($name, $class, $compilerFactory = null) { if (strlen($name) < 2) { throw new Dwoo_Exception('Resource names must be at least two-character long to avoid conflicts with Windows paths'); } if (!class_exists($class)) { throw new Dwoo_Exception('Resource class does not exist'); } $interfaces = class_implements($class); if (in_array('Dwoo_ITemplate', $interfaces) === false) { throw new Dwoo_Exception('Resource class must implement Dwoo_ITemplate'); } $this->resources[$name] = array('class'=>$class, 'compiler'=>$compilerFactory); } public function removeResource($name) { unset($this->resources[$name]); if ($name==='file') { $this->resources['file'] = array('class'=>'Dwoo_Template_File', 'compiler'=>null); } } public function setLoader(Dwoo_ILoader $loader) { $this->loader = $loader; } public function getLoader() { if ($this->loader === null) { $this->loader = new Dwoo_Loader($this->getCompileDir()); } return $this->loader; } public function getCustomPlugins() { return $this->plugins; } public function getCacheDir() { if ($this->cacheDir === null) { $this->setCacheDir(dirname(__FILE__).DIRECTORY_SEPARATOR.'cache'.DIRECTORY_SEPARATOR); } return $this->cacheDir; } public function setCacheDir($dir) { $this->cacheDir = rtrim($dir, '/\\').DIRECTORY_SEPARATOR; if (is_writable($this->cacheDir) === false) { throw new Dwoo_Exception('The cache directory must be writable, chmod "'.$this->cacheDir.'" to make it writable'); } } public function getCompileDir() { if ($this->compileDir === null) { $this->setCompileDir(dirname(__FILE__).DIRECTORY_SEPARATOR.'compiled'.DIRECTORY_SEPARATOR); } return $this->compileDir; } public function setCompileDir($dir) { $this->compileDir = rtrim($dir, '/\\').DIRECTORY_SEPARATOR; if (is_writable($this->compileDir) === false) { throw new Dwoo_Exception('The compile directory must be writable, chmod "'.$this->compileDir.'" to make it writable'); } } public function getCacheTime() { return $this->cacheTime; } public function setCacheTime($seconds) { $this->cacheTime = (int) $seconds; } public function getCharset() { return $this->charset; } public function setCharset($charset) { $this->charset = strtolower((string) $charset); } public function getTemplate() { return $this->template; } public function setTemplate(Dwoo_ITemplate $tpl) { $this->template = $tpl; } public function setDefaultCompilerFactory($resourceName, $compilerFactory) { $this->resources[$resourceName]['compiler'] = $compilerFactory; } public function getDefaultCompilerFactory($resourceName) { return $this->resources[$resourceName]['compiler']; } public function setSecurityPolicy(Dwoo_Security_Policy $policy = null) { $this->securityPolicy = $policy; } public function getSecurityPolicy() { return $this->securityPolicy; } public function setPluginProxy(Dwoo_IPluginProxy $pluginProxy) { $this->pluginProxy = $pluginProxy; } public function getPluginProxy() { return $this->pluginProxy; } public function isCached(Dwoo_ITemplate $tpl) { return is_string($tpl->getCachedTemplate($this)); } public function clearCache($olderThan=-1) { $cacheDirs = new RecursiveDirectoryIterator($this->getCacheDir()); $cache = new RecursiveIteratorIterator($cacheDirs); $expired = time() - $olderThan; $count = 0; foreach ($cache as $file) { if ($cache->isDot() || $cache->isDir() || substr($file, -5) !== '.html') { continue; } if ($cache->getCTime() < $expired) { $count += unlink((string) $file) ? 1 : 0; } } return $count; } public function templateFactory($resourceName, $resourceId, $cacheTime = null, $cacheId = null, $compileId = null, Dwoo_ITemplate $parentTemplate = null) { if (isset($this->resources[$resourceName])) { return call_user_func(array($this->resources[$resourceName]['class'], 'templateFactory'), $this, $resourceId, $cacheTime, $cacheId, $compileId, $parentTemplate); } else { throw new Dwoo_Exception('Unknown resource type : '.$resourceName); } } public function isArray($value, $checkIsEmpty=false) { if (is_array($value) === true || $value instanceof ArrayAccess) { if ($checkIsEmpty === false) { return true; } else { return $this->count($value); } } } public function isTraversable($value, $checkIsEmpty=false) { if (is_array($value) === true) { if ($checkIsEmpty === false) { return true; } else { return count($value) > 0; } } elseif ($value instanceof Traversable) { if ($checkIsEmpty === false) { return true; } else { return $this->count($value); } } return false; } public function count($value) { if (is_array($value) === true || $value instanceof Countable) { return count($value); } elseif ($value instanceof ArrayAccess) { if ($value->offsetExists(0)) { return true; } } elseif ($value instanceof Iterator) { $value->rewind(); if ($value->valid()) { return true; } } elseif ($value instanceof Traversable) { foreach ($value as $dummy) { return true; } } return 0; } public function triggerError($message, $level=E_USER_NOTICE) { if (!($tplIdentifier = $this->template->getResourceIdentifier())) { $tplIdentifier = $this->template->getResourceName(); } trigger_error('Dwoo error (in '.$tplIdentifier.') : '.$message, $level); } public function addStack($blockName, array $args=array()) { if (isset($this->plugins[$blockName])) { $class = $this->plugins[$blockName]['class']; } else { $class = 'Dwoo_Plugin_'.$blockName; } if ($this->curBlock !== null) { $this->curBlock->buffer(ob_get_contents()); ob_clean(); } else { $this->buffer .= ob_get_contents(); ob_clean(); } $block = new $class($this); $cnt = count($args); if ($cnt===0) { $block->init(); } elseif ($cnt===1) { $block->init($args[0]); } elseif ($cnt===2) { $block->init($args[0], $args[1]); } elseif ($cnt===3) { $block->init($args[0], $args[1], $args[2]); } elseif ($cnt===4) { $block->init($args[0], $args[1], $args[2], $args[3]); } else { call_user_func_array(array($block,'init'), $args); } $this->stack[] = $this->curBlock = $block; return $block; } public function delStack() { $args = func_get_args(); $this->curBlock->buffer(ob_get_contents()); ob_clean(); $cnt = count($args); if ($cnt===0) { $this->curBlock->end(); } elseif ($cnt===1) { $this->curBlock->end($args[0]); } elseif ($cnt===2) { $this->curBlock->end($args[0], $args[1]); } elseif ($cnt===3) { $this->curBlock->end($args[0], $args[1], $args[2]); } elseif ($cnt===4) { $this->curBlock->end($args[0], $args[1], $args[2], $args[3]); } else { call_user_func_array(array($this->curBlock, 'end'), $args); } $tmp = array_pop($this->stack); if (count($this->stack) > 0) { $this->curBlock = end($this->stack); $this->curBlock->buffer($tmp->process()); } else { $this->curBlock = null; echo $tmp->process(); } unset($tmp); } public function getParentBlock(Dwoo_Block_Plugin $block) { $index = array_search($block, $this->stack, true); if ($index !== false && $index > 0) { return $this->stack[$index-1]; } return false; } public function findBlock($type) { if (isset($this->plugins[$type])) { $type = $this->plugins[$type]['class']; } else { $type = 'Dwoo_Plugin_'.str_replace('Dwoo_Plugin_', '', $type); } $keys = array_keys($this->stack); while (($key = array_pop($keys)) !== false) { if ($this->stack[$key] instanceof $type) { return $this->stack[$key]; } } return false; } public function getObjectPlugin($class) { if (isset($this->runtimePlugins[$class])) { return $this->runtimePlugins[$class]; } return $this->runtimePlugins[$class] = new $class($this); } public function classCall($plugName, array $params = array()) { $class = 'Dwoo_Plugin_'.$plugName; $plugin = $this->getObjectPlugin($class); $cnt = count($params); if ($cnt===0) { return $plugin->process(); } elseif ($cnt===1) { return $plugin->process($params[0]); } elseif ($cnt===2) { return $plugin->process($params[0], $params[1]); } elseif ($cnt===3) { return $plugin->process($params[0], $params[1], $params[2]); } elseif ($cnt===4) { return $plugin->process($params[0], $params[1], $params[2], $params[3]); } else { return call_user_func_array(array($plugin, 'process'), $params); } } public function arrayMap($callback, array $params) { if ($params[0] === $this) { $addThis = true; array_shift($params); } if ((is_array($params[0]) || ($params[0] instanceof Iterator && $params[0] instanceof ArrayAccess))) { if (empty($params[0])) { return $params[0]; } $out = array(); $cnt = count($params); if (isset($addThis)) { array_unshift($params, $this); $items = $params[1]; $keys = array_keys($items); if (is_string($callback) === false) { while (($i = array_shift($keys)) !== null) { $out[] = call_user_func_array($callback, array(1=>$items[$i]) + $params); } } elseif ($cnt===1) { while (($i = array_shift($keys)) !== null) { $out[] = $callback($this, $items[$i]); } } elseif ($cnt===2) { while (($i = array_shift($keys)) !== null) { $out[] = $callback($this, $items[$i], $params[2]); } } elseif ($cnt===3) { while (($i = array_shift($keys)) !== null) { $out[] = $callback($this, $items[$i], $params[2], $params[3]); } } else { while (($i = array_shift($keys)) !== null) { $out[] = call_user_func_array($callback, array(1=>$items[$i]) + $params); } } } else { $items = $params[0]; $keys = array_keys($items); if (is_string($callback) === false) { while (($i = array_shift($keys)) !== null) { $out[] = call_user_func_array($callback, array($items[$i]) + $params); } } elseif ($cnt===1) { while (($i = array_shift($keys)) !== null) { $out[] = $callback($items[$i]); } } elseif ($cnt===2) { while (($i = array_shift($keys)) !== null) { $out[] = $callback($items[$i], $params[1]); } } elseif ($cnt===3) { while (($i = array_shift($keys)) !== null) { $out[] = $callback($items[$i], $params[1], $params[2]); } } elseif ($cnt===4) { while (($i = array_shift($keys)) !== null) { $out[] = $callback($items[$i], $params[1], $params[2], $params[3]); } } else { while (($i = array_shift($keys)) !== null) { $out[] = call_user_func_array($callback, array($items[$i]) + $params); } } } return $out; } else { return $params[0]; } } public function readVarInto($varstr, $data, $safeRead = false) { if ($data === null) { return null; } if (is_array($varstr) === false) { preg_match_all('#(\[|->|\.)?((?:[^.[\]-]|-(?!>))+)\]?#i', $varstr, $m); } else { $m = $varstr; } unset($varstr); while (list($k, $sep) = each($m[1])) { if ($sep === '.' || $sep === '[' || $sep === '') { if ((is_array($data) || $data instanceof ArrayAccess) && ($safeRead === false || isset($data[$m[2][$k]]))) { $data = $data[$m[2][$k]]; } else { return null; } } else { if (is_object($data) && ($safeRead === false || isset($data->$m[2][$k]) || is_callable(array($data, '__get')))) { $data = $data->$m[2][$k]; } else { return null; } } } return $data; } public function readParentVar($parentLevels, $varstr = null) { $tree = $this->scopeTree; $cur = $this->data; while ($parentLevels--!==0) { array_pop($tree); } while (($i = array_shift($tree)) !== null) { if (is_object($cur)) { $cur = $cur->$i; } else { $cur = $cur[$i]; } } if ($varstr!==null) { return $this->readVarInto($varstr, $cur); } else { return $cur; } } public function readVar($varstr) { if (is_array($varstr)===true) { $m = $varstr; unset($varstr); } else { if (strstr($varstr, '.') === false && strstr($varstr, '[') === false && strstr($varstr, '->') === false) { if ($varstr === 'dwoo') { return $this->globals; } elseif ($varstr === '__' || $varstr === '_root' ) { return $this->data; $varstr = substr($varstr, 6); } elseif ($varstr === '_' || $varstr === '_parent') { $varstr = '.'.$varstr; $tree = $this->scopeTree; $cur = $this->data; array_pop($tree); while (($i = array_shift($tree)) !== null) { if (is_object($cur)) { $cur = $cur->$i; } else { $cur = $cur[$i]; } } return $cur; } $cur = $this->scope; if (isset($cur[$varstr])) { return $cur[$varstr]; } else { return null; } } if (substr($varstr, 0, 1) === '.') { $varstr = 'dwoo'.$varstr; } preg_match_all('#(\[|->|\.)?((?:[^.[\]-]|-(?!>))+)\]?#i', $varstr, $m); } $i = $m[2][0]; if ($i === 'dwoo') { $cur = $this->globals; array_shift($m[2]); array_shift($m[1]); switch ($m[2][0]) { case 'get': $cur = $_GET; break; case 'post': $cur = $_POST; break; case 'session': $cur = $_SESSION; break; case 'cookies': case 'cookie': $cur = $_COOKIE; break; case 'server': $cur = $_SERVER; break; case 'env': $cur = $_ENV; break; case 'request': $cur = $_REQUEST; break; case 'const': array_shift($m[2]); if (defined($m[2][0])) { return constant($m[2][0]); } else { return null; } } if ($cur !== $this->globals) { array_shift($m[2]); array_shift($m[1]); } } elseif ($i === '__' || $i === '_root') { $cur = $this->data; array_shift($m[2]); array_shift($m[1]); } elseif ($i === '_' || $i === '_parent') { $tree = $this->scopeTree; $cur = $this->data; while (true) { array_pop($tree); array_shift($m[2]); array_shift($m[1]); if (current($m[2]) === '_' || current($m[2]) === '_parent') { continue; } while (($i = array_shift($tree)) !== null) { if (is_object($cur)) { $cur = $cur->$i; } else { $cur = $cur[$i]; } } break; } } else { $cur = $this->scope; } while (list($k, $sep) = each($m[1])) { if ($sep === '.' || $sep === '[' || $sep === '') { if ((is_array($cur) || $cur instanceof ArrayAccess) && isset($cur[$m[2][$k]])) { $cur = $cur[$m[2][$k]]; } else { return null; } } elseif ($sep === '->') { if (is_object($cur)) { $cur = $cur->$m[2][$k]; } else { return null; } } else { return null; } } return $cur; } public function assignInScope($value, $scope) { $tree =& $this->scopeTree; $data =& $this->data; if (!is_string($scope)) { return $this->triggerError('Assignments must be done into strings, ('.gettype($scope).') '.var_export($scope, true).' given', E_USER_ERROR); } if (strstr($scope, '.') === false && strstr($scope, '->') === false) { $this->scope[$scope] = $value; } else { preg_match_all('#(\[|->|\.)?([^.[\]-]+)\]?#i', $scope, $m); $cur =& $this->scope; $last = array(array_pop($m[1]), array_pop($m[2])); while (list($k, $sep) = each($m[1])) { if ($sep === '.' || $sep === '[' || $sep === '') { if (is_array($cur) === false) { $cur = array(); } $cur =& $cur[$m[2][$k]]; } elseif ($sep === '->') { if (is_object($cur) === false) { $cur = new stdClass; } $cur =& $cur->$m[2][$k]; } else { return false; } } if ($last[0] === '.' || $last[0] === '[' || $last[0] === '') { if (is_array($cur) === false) { $cur = array(); } $cur[$last[1]] = $value; } elseif ($last[0] === '->') { if (is_object($cur) === false) { $cur = new stdClass; } $cur->$last[1] = $value; } else { return false; } } } public function setScope($scope, $absolute = false) { $old = $this->scopeTree; if (is_string($scope)===true) { $scope = explode('.', $scope); } if ($absolute===true) { $this->scope =& $this->data; $this->scopeTree = array(); } while (($bit = array_shift($scope)) !== null) { if ($bit === '_' || $bit === '_parent') { array_pop($this->scopeTree); $this->scope =& $this->data; $cnt = count($this->scopeTree); for ($i=0;$i<$cnt;$i++) $this->scope =& $this->scope[$this->scopeTree[$i]]; } elseif ($bit === '__' || $bit === '_root') { $this->scope =& $this->data; $this->scopeTree = array(); } elseif (isset($this->scope[$bit])) { $this->scope =& $this->scope[$bit]; $this->scopeTree[] = $bit; } else { unset($this->scope); $this->scope = null; } } return $old; } public function getData() { return $this->data; } public function &getScope() { return $this->scope; } public function __call($method, $args) { $proxy = $this->getPluginProxy(); if (!$proxy) { throw new Dwoo_Exception('Call to undefined method '.__CLASS__.'::'.$method.'()'); } return call_user_func_array($proxy->getCallback($method), $args); } } interface Dwoo_IPluginProxy { public function handles($name); public function getCode($name, $params); public function getCallback($name); public function getLoader($name); } interface Dwoo_IElseable { } interface Dwoo_ILoader { public function loadPlugin($class, $forceRehash = true); } class Dwoo_Loader implements Dwoo_ILoader { protected $paths = array(); protected $classPath = array(); protected $cacheDir; protected $corePluginDir; public function __construct($cacheDir) { $this->corePluginDir = DWOO_DIRECTORY . 'plugins'; $this->cacheDir = rtrim($cacheDir, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR; $cacheFile = $this->cacheDir.'classpath.cache.d'.Dwoo::RELEASE_TAG.'.php'; if (file_exists($cacheFile)) { $classpath = file_get_contents($cacheFile); $this->classPath = unserialize($classpath) + $this->classPath; } else { $this->rebuildClassPathCache($this->corePluginDir, $cacheFile); } } protected function rebuildClassPathCache($path, $cacheFile) { if ($cacheFile!==false) { $tmp = $this->classPath; $this->classPath = array(); } $list = glob(rtrim($path, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . '*'); if (is_array($list)) { foreach ($list as $f) { if (is_dir($f)) { $this->rebuildClassPathCache($f, false); } else { $this->classPath[str_replace(array('function.','block.','modifier.','outputfilter.','filter.','prefilter.','postfilter.','pre.','post.','output.','shared.','helper.'), '', basename($f, '.php'))] = $f; } } } if ($cacheFile!==false) { if (!file_put_contents($cacheFile, serialize($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), alternatively you can change the directory used with $dwoo->setCompileDir() or provide a custom loader object with $dwoo->setLoader()'); } $this->classPath += $tmp; } } public function loadPlugin($class, $forceRehash = true) { if (!isset($this->classPath[$class]) || !(include $this->classPath[$class])) { if ($forceRehash) { $this->rebuildClassPathCache($this->corePluginDir, $this->cacheDir . 'classpath.cache.d'.Dwoo::RELEASE_TAG.'.php'); foreach ($this->paths as $path=>$file) { $this->rebuildClassPathCache($path, $file); } if (isset($this->classPath[$class])) { include $this->classPath[$class]; } 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); } } 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); } } } public function addDirectory($pluginDirectory) { $pluginDir = realpath($pluginDirectory); if (!$pluginDir) { throw new Dwoo_Exception('Plugin directory does not exist or can not be read : '.$pluginDirectory); } $cacheFile = $this->cacheDir . 'classpath-'.substr(strtr($pluginDir, '/\\:'.PATH_SEPARATOR, '----'), strlen($pluginDir) > 80 ? -80 : 0).'.d'.Dwoo::RELEASE_TAG.'.php'; $this->paths[$pluginDir] = $cacheFile; if (file_exists($cacheFile)) { $classpath = file_get_contents($cacheFile); $this->classPath = unserialize($classpath) + $this->classPath; } else { $this->rebuildClassPathCache($pluginDir, $cacheFile); } } } class Dwoo_Exception extends Exception { } class Dwoo_Security_Policy { const PHP_ENCODE = 1; const PHP_REMOVE = 2; const PHP_ALLOW = 3; const CONST_DISALLOW = false; const CONST_ALLOW = true; protected $allowedPhpFunctions = array ( 'str_repeat', 'number_format', 'htmlentities', 'htmlspecialchars', 'long2ip', 'strlen', 'list', 'empty', 'count', 'sizeof', 'in_array', 'is_array', ); protected $allowedDirectories = array(); protected $phpHandling = self::PHP_REMOVE; protected $constHandling = self::CONST_DISALLOW; public function allowPhpFunction($func) { if (is_array($func)) foreach ($func as $fname) $this->allowedPhpFunctions[strtolower($fname)] = true; else $this->allowedPhpFunctions[strtolower($func)] = true; } public function disallowPhpFunction($func) { if (is_array($func)) foreach ($func as $fname) unset($this->allowedPhpFunctions[strtolower($fname)]); else unset($this->allowedPhpFunctions[strtolower($func)]); } public function getAllowedPhpFunctions() { return $this->allowedPhpFunctions; } public function allowDirectory($path) { if (is_array($path)) foreach ($path as $dir) $this->allowedDirectories[realpath($dir)] = true; else $this->allowedDirectories[realpath($path)] = true; } public function disallowDirectory($path) { if (is_array($path)) foreach ($path as $dir) unset($this->allowedDirectories[realpath($dir)]); else unset($this->allowedDirectories[realpath($path)]); } public function getAllowedDirectories() { return $this->allowedDirectories; } public function setPhpHandling($level = self::PHP_REMOVE) { $this->phpHandling = $level; } public function getPhpHandling() { return $this->phpHandling; } public function setConstantHandling($level = self::CONST_DISALLOW) { $this->constHandling = $level; } public function getConstantHandling() { return $this->constHandling; } } class Dwoo_Security_Exception extends Dwoo_Exception { } interface Dwoo_ICompilable { } interface Dwoo_ICompiler { public function compile(Dwoo $dwoo, Dwoo_ITemplate $template); public function setCustomPlugins(array $customPlugins); public function setSecurityPolicy(Dwoo_Security_Policy $policy = null); } interface Dwoo_IDataProvider { public function getData(); } interface Dwoo_ITemplate { public function getCacheTime(); public function setCacheTime($seconds = null); public function getCachedTemplate(Dwoo $dwoo); public function cache(Dwoo $dwoo, $output); public function clearCache(Dwoo $dwoo, $olderThan = -1); public function getCompiledTemplate(Dwoo $dwoo, Dwoo_ICompiler $compiler = null); public function getName(); public function getResourceName(); public function getResourceIdentifier(); public function getSource(); public function getUid(); public function getCompiler(); public function getIsModifiedCode(); public static function templateFactory(Dwoo $dwoo, $resourceId, $cacheTime = null, $cacheId = null, $compileId = null, Dwoo_ITemplate $parentTemplate = null); } interface Dwoo_ICompilable_Block { } abstract class Dwoo_Plugin { protected $dwoo; public function __construct(Dwoo $dwoo) { $this->dwoo = $dwoo; } public static function paramsToAttributes(array $params, $delim = '\'') { if (isset($params['*'])) { $params = array_merge($params, $params['*']); unset($params['*']); } $out = ''; foreach ($params as $attr=>$val) { $out .= ' '.$attr.'='; if (trim($val, '"\'')=='' || $val=='null') { $out .= str_replace($delim, '\\'.$delim, '""'); } elseif (substr($val, 0, 1) === $delim && substr($val, -1) === $delim) { $out .= str_replace($delim, '\\'.$delim, '"'.substr($val, 1, -1).'"'); } else { $out .= str_replace($delim, '\\'.$delim, '"') . $delim . '.'.$val.'.' . $delim . str_replace($delim, '\\'.$delim, '"'); } } return ltrim($out); } } abstract class Dwoo_Block_Plugin extends Dwoo_Plugin { protected $buffer = ''; public function buffer($input) { $this->buffer .= $input; } public function end() { } public function process() { return $this->buffer; } public static function preProcessing(Dwoo_Compiler $compiler, array $params, $prepend, $append, $type) { return Dwoo_Compiler::PHP_OPEN.$prepend.'$this->addStack("'.$type.'", array('.Dwoo_Compiler::implode_r($compiler->getCompiledParams($params)).'));'.$append.Dwoo_Compiler::PHP_CLOSE; } public static function postProcessing(Dwoo_Compiler $compiler, array $params, $prepend, $append, $content) { return $content . Dwoo_Compiler::PHP_OPEN.$prepend.'$this->delStack();'.$append.Dwoo_Compiler::PHP_CLOSE; } } abstract class Dwoo_Filter { protected $dwoo; public function __construct(Dwoo $dwoo) { $this->dwoo = $dwoo; } abstract public function process($input); } abstract class Dwoo_Processor { protected $compiler; public function __construct(Dwoo_Compiler $compiler) { $this->compiler = $compiler; } abstract public function process($input); } class Dwoo_Template_String implements Dwoo_ITemplate { protected $name; protected $compileId; protected $cacheId; protected $cacheTime; protected $compilationEnforced; protected static $cache = array('cached'=>array(), 'compiled'=>array()); protected $compiler; protected $chmod = 0777; public function __construct($templateString, $cacheTime = null, $cacheId = null, $compileId = null) { $this->template = $templateString; if (function_exists('hash')) { $this->name = hash('md4', $templateString); } else { $this->name = md5($templateString); } $this->cacheTime = $cacheTime; if ($compileId !== null) { $this->compileId = str_replace('../', '__', strtr($compileId, '\\%?=!:;'.PATH_SEPARATOR, '/-------')); } if ($cacheId !== null) { $this->cacheId = str_replace('../', '__', strtr($cacheId, '\\%?=!:;'.PATH_SEPARATOR, '/-------')); } } public function getCacheTime() { return $this->cacheTime; } public function setCacheTime($seconds = null) { $this->cacheTime = $seconds; } public function getChmod() { return $this->chmod; } public function setChmod($mask = null) { $this->chmod = $mask; } public function getName() { return $this->name; } public function getResourceName() { return 'string'; } public function getResourceIdentifier() { return false; } public function getSource() { return $this->template; } public function getUid() { return $this->name; } public function getCompiler() { return $this->compiler; } public function forceCompilation() { $this->compilationEnforced = true; } public function getCachedTemplate(Dwoo $dwoo) { if ($this->cacheTime !== null) { $cacheLength = $this->cacheTime; } else { $cacheLength = $dwoo->getCacheTime(); } if ($cacheLength === 0) { return false; } $cachedFile = $this->getCacheFilename($dwoo); if (isset(self::$cache['cached'][$this->cacheId]) === true && file_exists($cachedFile)) { return $cachedFile; } elseif ($this->compilationEnforced !== true && file_exists($cachedFile) && ($cacheLength === -1 || filemtime($cachedFile) > ($_SERVER['REQUEST_TIME'] - $cacheLength)) && $this->isValidCompiledFile($this->getCompiledFilename($dwoo))) { self::$cache['cached'][$this->cacheId] = true; return $cachedFile; } else { return true; } } public function cache(Dwoo $dwoo, $output) { $cacheDir = $dwoo->getCacheDir(); $cachedFile = $this->getCacheFilename($dwoo); $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); return false; } } fwrite($file, $output); fclose($file); $this->makeDirectory(dirname($cachedFile), $cacheDir); if (!@rename($temp, $cachedFile)) { @unlink($cachedFile); @rename($temp, $cachedFile); } if ($this->chmod !== null) { chmod($cachedFile, $this->chmod); } self::$cache['cached'][$this->cacheId] = true; return $cachedFile; } public function clearCache(Dwoo $dwoo, $olderThan = -1) { $cachedFile = $this->getCacheFilename($dwoo); return !file_exists($cachedFile) || (filectime($cachedFile) < (time() - $olderThan) && unlink($cachedFile)); } public function getCompiledTemplate(Dwoo $dwoo, Dwoo_ICompiler $compiler = null) { $compiledFile = $this->getCompiledFilename($dwoo); if ($this->compilationEnforced !== true && isset(self::$cache['compiled'][$this->compileId]) === true) { } elseif ($this->compilationEnforced !== true && $this->isValidCompiledFile($compiledFile)) { self::$cache['compiled'][$this->compileId] = true; } else { $this->compilationEnforced = false; if ($compiler === null) { $compiler = $dwoo->getDefaultCompilerFactory($this->getResourceName()); if ($compiler === null || $compiler === array('Dwoo_Compiler', 'compilerFactory')) { if (class_exists('Dwoo_Compiler', false) === false) { include DWOO_DIRECTORY . 'Dwoo/Compiler.php'; } $compiler = Dwoo_Compiler::compilerFactory(); } else { $compiler = call_user_func($compiler); } } $this->compiler = $compiler; $compiler->setCustomPlugins($dwoo->getCustomPlugins()); $compiler->setSecurityPolicy($dwoo->getSecurityPolicy()); $this->makeDirectory(dirname($compiledFile), $dwoo->getCompileDir()); file_put_contents($compiledFile, $compiler->compile($dwoo, $this)); if ($this->chmod !== null) { chmod($compiledFile, $this->chmod); } self::$cache['compiled'][$this->compileId] = true; } return $compiledFile; } protected function isValidCompiledFile($file) { return file_exists($file); } public static function templateFactory(Dwoo $dwoo, $resourceId, $cacheTime = null, $cacheId = null, $compileId = null, Dwoo_ITemplate $parentTemplate = null) { return new self($resourceId, $cacheTime, $cacheId, $compileId); } protected function getCompiledFilename(Dwoo $dwoo) { if ($this->compileId===null) { $this->compileId = $this->name; } return $dwoo->getCompileDir() . $this->compileId.'.d'.Dwoo::RELEASE_TAG.'.php'; } protected function getCacheFilename(Dwoo $dwoo) { 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']); } else { $cacheId = ''; } $this->getCompiledFilename($dwoo); $this->cacheId = str_replace('../', '__', $this->compileId . strtr($cacheId, '\\%?=!:;'.PATH_SEPARATOR, '/-------')); } return $dwoo->getCacheDir() . $this->cacheId.'.html'; } public function getIsModifiedCode() { return null; } protected function makeDirectory($path, $baseDir = null) { if (is_dir($path) === true) { return; } if ($this->chmod === null) { $chmod = 0777; } else { $chmod = $this->chmod; } mkdir($path, $chmod, true); if (strpos(PHP_OS, 'WIN') !== 0 && $baseDir !== null) { $path = strtr(str_replace($baseDir, '', $path), '\\', '/'); $folders = explode('/', trim($path, '/')); foreach ($folders as $folder) { $baseDir .= $folder . DIRECTORY_SEPARATOR; chmod($baseDir, $chmod); } } } } class Dwoo_Template_File extends Dwoo_Template_String { protected $file; protected $includePath = null; protected $resolvedPath = null; public function __construct($file, $cacheTime = null, $cacheId = null, $compileId = null, $includePath = null) { $this->file = $file; $this->name = basename($file); $this->cacheTime = $cacheTime; if ($compileId !== null) { $this->compileId = str_replace('../', '__', strtr($compileId, '\\%?=!:;'.PATH_SEPARATOR, '/-------')); } if ($cacheId !== null) { $this->cacheId = str_replace('../', '__', strtr($cacheId, '\\%?=!:;'.PATH_SEPARATOR, '/-------')); } if (is_string($includePath)) { $this->includePath = array($includePath); } elseif (is_array($includePath)) { $this->includePath = $includePath; } } public function setIncludePath($paths) { if (is_array($paths) === false) { $paths = array($paths); } $this->includePath = $paths; $this->resolvedPath = null; } public function getIncludePath() { return $this->includePath; } protected function isValidCompiledFile($file) { return parent::isValidCompiledFile($file) && (int)$this->getUid() <= filemtime($file); } public function getSource() { return file_get_contents($this->getResourceIdentifier()); } public function getResourceName() { return 'file'; } public function getResourceIdentifier() { if ($this->resolvedPath !== null) { return $this->resolvedPath; } elseif ($this->includePath === null) { return $this->file; } else { foreach ($this->includePath as $path) { $path = rtrim($path, DIRECTORY_SEPARATOR); if (file_exists($path.DIRECTORY_SEPARATOR.$this->file) === true) { $this->resolvedPath = $path . DIRECTORY_SEPARATOR . $this->file; return $this->resolvedPath; } } throw new Dwoo_Exception('Template "'.$this->file.'" could not be found in any of your include path(s)'); } } public function getUid() { return (string) filemtime($this->getResourceIdentifier()); } public static function templateFactory(Dwoo $dwoo, $resourceId, $cacheTime = null, $cacheId = null, $compileId = null, Dwoo_ITemplate $parentTemplate = null) { if (DIRECTORY_SEPARATOR === '\\') { $resourceId = str_replace(array("\t", "\n", "\r", "\f", "\v"), array('\\t', '\\n', '\\r', '\\f', '\\v'), $resourceId); } $resourceId = strtr($resourceId, '\\', '/'); $includePath = null; if (file_exists($resourceId) === false) { if ($parentTemplate === null) { $parentTemplate = $dwoo->getTemplate(); } if ($parentTemplate instanceof Dwoo_Template_File) { if ($includePath = $parentTemplate->getIncludePath()) { if (strstr($resourceId, '../')) { throw new Dwoo_Exception('When using an include path you can not reference a template into a parent directory (using ../)'); } } else { $resourceId = dirname($parentTemplate->getResourceIdentifier()).DIRECTORY_SEPARATOR.$resourceId; if (file_exists($resourceId) === false) { return null; } } } else { return null; } } if ($policy = $dwoo->getSecurityPolicy()) { while (true) { if (preg_match('{^([a-z]+?)://}i', $resourceId)) { throw new Dwoo_Security_Exception('The security policy prevents you to read files from external sources : <em>'.$resourceId.'</em>.'); } if ($includePath) { break; } $resourceId = realpath($resourceId); $dirs = $policy->getAllowedDirectories(); foreach ($dirs as $dir=>$dummy) { if (strpos($resourceId, $dir) === 0) { break 2; } } throw new Dwoo_Security_Exception('The security policy prevents you to read <em>'.$resourceId.'</em>'); } } $class = 'Dwoo_Template_File'; if ($parentTemplate) { $class = get_class($parentTemplate); } return new $class($resourceId, $cacheTime, $cacheId, $compileId, $includePath); } protected function getCompiledFilename(Dwoo $dwoo) { if ($this->compileId===null) { $this->compileId = str_replace('../', '__', strtr($this->getResourceIdentifier(), '\\:', '/-')); } return $dwoo->getCompileDir() . $this->compileId.'.d'.Dwoo::RELEASE_TAG.'.php'; } public function getIsModifiedCode() { return '"'.$this->getUid().'" == filemtime('.var_export($this->getResourceIdentifier(), true).')'; } } class Dwoo_Data implements Dwoo_IDataProvider { protected $data = array(); public function getData() { return $this->data; } public function clear($name = null) { if ($name === null) { $this->data = array(); } elseif (is_array($name)) { foreach ($name as $index) unset($this->data[$index]); } else { unset($this->data[$name]); } } public function setData(array $data) { $this->data = $data; } public function mergeData(array $data) { $args = func_get_args(); while (list(,$v) = each($args)) { if (is_array($v)) { $this->data = array_merge($this->data, $v); } } } public function assign($name, $val = null) { if (is_array($name)) { reset($name); while (list($k,$v) = each($name)) $this->data[$k] = $v; } else { $this->data[$name] = $val; } } public function __set($name, $value) { $this->assign($name, $value); } public function assignByRef($name, &$val) { $this->data[$name] =& $val; } public function append($name, $val = null, $merge = false) { if (is_array($name)) { foreach ($name as $key=>$val) { if (isset($this->data[$key]) && !is_array($this->data[$key])) { settype($this->data[$key], 'array'); } if ($merge === true && is_array($val)) { $this->data[$key] = $val + $this->data[$key]; } else { $this->data[$key][] = $val; } } } elseif ($val !== null) { if (isset($this->data[$name]) && !is_array($this->data[$name])) { settype($this->data[$name], 'array'); } elseif (!isset($this->data[$name])) { $this->data[$name] = array(); } if ($merge === true && is_array($val)) { $this->data[$name] = $val + $this->data[$name]; } else { $this->data[$name][] = $val; } } } public function appendByRef($name, &$val, $merge = false) { if (isset($this->data[$name]) && !is_array($this->data[$name])) { settype($this->data[$name], 'array'); } if ($merge === true && is_array($val)) { foreach ($val as $key => &$val) { $this->data[$name][$key] =& $val; } } else { $this->data[$name][] =& $val; } } public function isAssigned($name) { return isset($this->data[$name]); } public function __isset($name) { return isset($this->data[$name]); } public function unassign($name) { unset($this->data[$name]); } public function __unset($name) { unset($this->data[$name]); } public function get($name) { return $this->__get($name); } public function __get($name) { if (isset($this->data[$name])) { return $this->data[$name]; } else { throw new Dwoo_Exception('Tried to read a value that was not assigned yet : "'.$name.'"'); } } }
2 +<?php define('DWOO_DIRECTORY', dirname(__FILE__) . DIRECTORY_SEPARATOR); class Dwoo_Core { const VERSION = '1.1.1'; const RELEASE_TAG = 17; const CLASS_PLUGIN = 1; const FUNC_PLUGIN = 2; const NATIVE_PLUGIN = 4; const BLOCK_PLUGIN = 8; const COMPILABLE_PLUGIN = 16; const CUSTOM_PLUGIN = 32; const SMARTY_MODIFIER = 64; const SMARTY_BLOCK = 128; const SMARTY_FUNCTION = 256; const PROXY_PLUGIN = 512; const TEMPLATE_PLUGIN = 1024; protected $charset = 'utf-8'; public $globals; protected $compileDir; protected $cacheDir; protected $cacheTime = 0; protected $securityPolicy = null; protected $plugins = array(); protected $filters = array(); protected $resources = array ( 'file' => array ( 'class' => 'Dwoo_Template_File', 'compiler' => null ), 'string' => array ( 'class' => 'Dwoo_Template_String', 'compiler' => null ) ); protected $loader = null; protected $template = null; protected $runtimePlugins; public $data; public $scope; protected $scopeTree; protected $stack; protected $curBlock; protected $buffer; protected $pluginProxy; public function __construct($compileDir = null, $cacheDir = null) { if ($compileDir !== null) { $this->setCompileDir($compileDir); } if ($cacheDir !== null) { $this->setCacheDir($cacheDir); } $this->initGlobals(); } public function __clone() { $this->template = null; unset($this->data); } public function output($tpl, $data = array(), Dwoo_ICompiler $compiler = null) { return $this->get($tpl, $data, $compiler, true); } public function get($_tpl, $data = array(), $_compiler = null, $_output = false) { if ($this->template instanceof Dwoo_ITemplate) { $proxy = clone $this; return $proxy->get($_tpl, $data, $_compiler, $_output); } if ($_tpl instanceof Dwoo_ITemplate) { } elseif (is_string($_tpl) && file_exists($_tpl)) { $_tpl = new Dwoo_Template_File($_tpl); } else { throw new Dwoo_Exception('Dwoo->get/Dwoo->output\'s first argument must be a Dwoo_ITemplate (i.e. Dwoo_Template_File) or a valid path to a template file', E_USER_NOTICE); } $this->template = $_tpl; if ($data instanceof Dwoo_IDataProvider) { $this->data = $data->getData(); } elseif (is_array($data)) { $this->data = $data; } else { throw new Dwoo_Exception('Dwoo->get/Dwoo->output\'s data argument must be a Dwoo_IDataProvider object (i.e. Dwoo_Data) or an associative array', E_USER_NOTICE); } $this->globals['template'] = $_tpl->getName(); $this->initRuntimeVars($_tpl); $file = $_tpl->getCachedTemplate($this); $doCache = $file === true; $cacheLoaded = is_string($file); if ($cacheLoaded === true) { if ($_output === true) { include $file; $this->template = null; } else { ob_start(); include $file; $this->template = null; return ob_get_clean(); } } else { if ($doCache === true) { $dynamicId = uniqid(); } $compiledTemplate = $_tpl->getCompiledTemplate($this, $_compiler); $out = include $compiledTemplate; if ($out === false) { $_tpl->forceCompilation(); $compiledTemplate = $_tpl->getCompiledTemplate($this, $_compiler); $out = include $compiledTemplate; } if ($doCache === true) { $out = preg_replace('/(<%|%>|<\?php|<\?|\?>)/', '<?php /*'.$dynamicId.'*/ echo \'$1\'; ?>', $out); if (!class_exists('Dwoo_plugin_dynamic', false)) { $this->getLoader()->loadPlugin('dynamic'); } $out = Dwoo_Plugin_dynamic::unescape($out, $dynamicId, $compiledTemplate); } foreach ($this->filters as $filter) { if (is_array($filter) && $filter[0] instanceof Dwoo_Filter) { $out = call_user_func($filter, $out); } else { $out = call_user_func($filter, $this, $out); } } if ($doCache === true) { $file = $_tpl->cache($this, $out); if ($_output === true) { include $file; $this->template = null; } else { ob_start(); include $file; $this->template = null; return ob_get_clean(); } } else { $this->template = null; if ($_output === true) { echo $out; } return $out; } } } protected function initGlobals() { $this->globals = array ( 'version' => self::VERSION, 'ad' => '<a href="http://dwoo.org/">Powered by Dwoo</a>', 'now' => $_SERVER['REQUEST_TIME'], 'charset' => $this->charset, ); } protected function initRuntimeVars(Dwoo_ITemplate $tpl) { $this->runtimePlugins = array(); $this->scope =& $this->data; $this->scopeTree = array(); $this->stack = array(); $this->curBlock = null; $this->buffer = ''; } public function addPlugin($name, $callback, $compilable = false) { $compilable = $compilable ? self::COMPILABLE_PLUGIN : 0; if (is_array($callback)) { if (is_subclass_of(is_object($callback[0]) ? get_class($callback[0]) : $callback[0], 'Dwoo_Block_Plugin')) { $this->plugins[$name] = array('type'=>self::BLOCK_PLUGIN | $compilable, 'callback'=>$callback, 'class'=>(is_object($callback[0]) ? get_class($callback[0]) : $callback[0])); } else { $this->plugins[$name] = array('type'=>self::CLASS_PLUGIN | $compilable, 'callback'=>$callback, 'class'=>(is_object($callback[0]) ? get_class($callback[0]) : $callback[0]), 'function'=>$callback[1]); } } elseif (class_exists($callback, false)) { if (is_subclass_of($callback, 'Dwoo_Block_Plugin')) { $this->plugins[$name] = array('type'=>self::BLOCK_PLUGIN | $compilable, 'callback'=>$callback, 'class'=>$callback); } else { $this->plugins[$name] = array('type'=>self::CLASS_PLUGIN | $compilable, 'callback'=>$callback, 'class'=>$callback, 'function'=>'process'); } } elseif (function_exists($callback)) { $this->plugins[$name] = array('type'=>self::FUNC_PLUGIN | $compilable, 'callback'=>$callback); } else { throw new Dwoo_Exception('Callback could not be processed correctly, please check that the function/class you used exists'); } } public function removePlugin($name) { if (isset($this->plugins[$name])) { unset($this->plugins[$name]); } } public function addFilter($callback, $autoload = false) { if ($autoload) { $class = 'Dwoo_Filter_'.$callback; if (!class_exists($class, false) && !function_exists($class)) { try { $this->getLoader()->loadPlugin($callback); } catch (Dwoo_Exception $e) { if (strstr($callback, 'Dwoo_Filter_')) { throw new Dwoo_Exception('Wrong filter name : '.$callback.', the "Dwoo_Filter_" prefix should not be used, please only use "'.str_replace('Dwoo_Filter_', '', $callback).'"'); } else { throw new Dwoo_Exception('Wrong filter name : '.$callback.', when using autoload the filter must be in one of your plugin dir as "name.php" containg a class or function named "Dwoo_Filter_name"'); } } } if (class_exists($class, false)) { $callback = array(new $class($this), 'process'); } elseif (function_exists($class)) { $callback = $class; } else { throw new Dwoo_Exception('Wrong filter name : '.$callback.', when using autoload the filter must be in one of your plugin dir as "name.php" containg a class or function named "Dwoo_Filter_name"'); } $this->filters[] = $callback; } else { $this->filters[] = $callback; } } public function removeFilter($callback) { if (($index = array_search('Dwoo_Filter_'.$callback, $this->filters, true)) !== false) { unset($this->filters[$index]); } elseif (($index = array_search($callback, $this->filters, true)) !== false) { unset($this->filters[$index]); } else { $class = 'Dwoo_Filter_' . $callback; foreach ($this->filters as $index=>$filter) { if (is_array($filter) && $filter[0] instanceof $class) { unset($this->filters[$index]); break; } } } } public function addResource($name, $class, $compilerFactory = null) { if (strlen($name) < 2) { throw new Dwoo_Exception('Resource names must be at least two-character long to avoid conflicts with Windows paths'); } if (!class_exists($class)) { throw new Dwoo_Exception('Resource class does not exist'); } $interfaces = class_implements($class); if (in_array('Dwoo_ITemplate', $interfaces) === false) { throw new Dwoo_Exception('Resource class must implement Dwoo_ITemplate'); } $this->resources[$name] = array('class'=>$class, 'compiler'=>$compilerFactory); } public function removeResource($name) { unset($this->resources[$name]); if ($name==='file') { $this->resources['file'] = array('class'=>'Dwoo_Template_File', 'compiler'=>null); } } public function setLoader(Dwoo_ILoader $loader) { $this->loader = $loader; } public function getLoader() { if ($this->loader === null) { $this->loader = new Dwoo_Loader($this->getCompileDir()); } return $this->loader; } public function getCustomPlugins() { return $this->plugins; } public function getCacheDir() { if ($this->cacheDir === null) { $this->setCacheDir(dirname(__FILE__).DIRECTORY_SEPARATOR.'cache'.DIRECTORY_SEPARATOR); } return $this->cacheDir; } public function setCacheDir($dir) { $this->cacheDir = rtrim($dir, '/\\').DIRECTORY_SEPARATOR; if (is_writable($this->cacheDir) === false) { throw new Dwoo_Exception('The cache directory must be writable, chmod "'.$this->cacheDir.'" to make it writable'); } } public function getCompileDir() { if ($this->compileDir === null) { $this->setCompileDir(dirname(__FILE__).DIRECTORY_SEPARATOR.'compiled'.DIRECTORY_SEPARATOR); } return $this->compileDir; } public function setCompileDir($dir) { $this->compileDir = rtrim($dir, '/\\').DIRECTORY_SEPARATOR; if (is_writable($this->compileDir) === false) { throw new Dwoo_Exception('The compile directory must be writable, chmod "'.$this->compileDir.'" to make it writable'); } } public function getCacheTime() { return $this->cacheTime; } public function setCacheTime($seconds) { $this->cacheTime = (int) $seconds; } public function getCharset() { return $this->charset; } public function setCharset($charset) { $this->charset = strtolower((string) $charset); } public function getTemplate() { return $this->template; } public function setTemplate(Dwoo_ITemplate $tpl) { $this->template = $tpl; } public function setDefaultCompilerFactory($resourceName, $compilerFactory) { $this->resources[$resourceName]['compiler'] = $compilerFactory; } public function getDefaultCompilerFactory($resourceName) { return $this->resources[$resourceName]['compiler']; } public function setSecurityPolicy(Dwoo_Security_Policy $policy = null) { $this->securityPolicy = $policy; } public function getSecurityPolicy() { return $this->securityPolicy; } public function setPluginProxy(Dwoo_IPluginProxy $pluginProxy) { $this->pluginProxy = $pluginProxy; } public function getPluginProxy() { return $this->pluginProxy; } public function isCached(Dwoo_ITemplate $tpl) { return is_string($tpl->getCachedTemplate($this)); } public function clearCache($olderThan=-1) { $cacheDirs = new RecursiveDirectoryIterator($this->getCacheDir()); $cache = new RecursiveIteratorIterator($cacheDirs); $expired = time() - $olderThan; $count = 0; foreach ($cache as $file) { if ($cache->isDot() || $cache->isDir() || substr($file, -5) !== '.html') { continue; } if ($cache->getCTime() < $expired) { $count += unlink((string) $file) ? 1 : 0; } } return $count; } public function templateFactory($resourceName, $resourceId, $cacheTime = null, $cacheId = null, $compileId = null, Dwoo_ITemplate $parentTemplate = null) { if (isset($this->resources[$resourceName])) { return call_user_func(array($this->resources[$resourceName]['class'], 'templateFactory'), $this, $resourceId, $cacheTime, $cacheId, $compileId, $parentTemplate); } else { throw new Dwoo_Exception('Unknown resource type : '.$resourceName); } } public function isArray($value, $checkIsEmpty=false) { if (is_array($value) === true || $value instanceof ArrayAccess) { if ($checkIsEmpty === false) { return true; } else { return $this->count($value); } } } public function isTraversable($value, $checkIsEmpty=false) { if (is_array($value) === true) { if ($checkIsEmpty === false) { return true; } else { return count($value) > 0; } } elseif ($value instanceof Traversable) { if ($checkIsEmpty === false) { return true; } else { return $this->count($value); } } return false; } public function count($value) { if (is_array($value) === true || $value instanceof Countable) { return count($value); } elseif ($value instanceof ArrayAccess) { if ($value->offsetExists(0)) { return true; } } elseif ($value instanceof Iterator) { $value->rewind(); if ($value->valid()) { return true; } } elseif ($value instanceof Traversable) { foreach ($value as $dummy) { return true; } } return 0; } public function triggerError($message, $level=E_USER_NOTICE) { if (!($tplIdentifier = $this->template->getResourceIdentifier())) { $tplIdentifier = $this->template->getResourceName(); } trigger_error('Dwoo error (in '.$tplIdentifier.') : '.$message, $level); } public function addStack($blockName, array $args=array()) { if (isset($this->plugins[$blockName])) { $class = $this->plugins[$blockName]['class']; } else { $class = 'Dwoo_Plugin_'.$blockName; } if ($this->curBlock !== null) { $this->curBlock->buffer(ob_get_contents()); ob_clean(); } else { $this->buffer .= ob_get_contents(); ob_clean(); } $block = new $class($this); $cnt = count($args); if ($cnt===0) { $block->init(); } elseif ($cnt===1) { $block->init($args[0]); } elseif ($cnt===2) { $block->init($args[0], $args[1]); } elseif ($cnt===3) { $block->init($args[0], $args[1], $args[2]); } elseif ($cnt===4) { $block->init($args[0], $args[1], $args[2], $args[3]); } else { call_user_func_array(array($block,'init'), $args); } $this->stack[] = $this->curBlock = $block; return $block; } public function delStack() { $args = func_get_args(); $this->curBlock->buffer(ob_get_contents()); ob_clean(); $cnt = count($args); if ($cnt===0) { $this->curBlock->end(); } elseif ($cnt===1) { $this->curBlock->end($args[0]); } elseif ($cnt===2) { $this->curBlock->end($args[0], $args[1]); } elseif ($cnt===3) { $this->curBlock->end($args[0], $args[1], $args[2]); } elseif ($cnt===4) { $this->curBlock->end($args[0], $args[1], $args[2], $args[3]); } else { call_user_func_array(array($this->curBlock, 'end'), $args); } $tmp = array_pop($this->stack); if (count($this->stack) > 0) { $this->curBlock = end($this->stack); $this->curBlock->buffer($tmp->process()); } else { $this->curBlock = null; echo $tmp->process(); } unset($tmp); } public function getParentBlock(Dwoo_Block_Plugin $block) { $index = array_search($block, $this->stack, true); if ($index !== false && $index > 0) { return $this->stack[$index-1]; } return false; } public function findBlock($type) { if (isset($this->plugins[$type])) { $type = $this->plugins[$type]['class']; } else { $type = 'Dwoo_Plugin_'.str_replace('Dwoo_Plugin_', '', $type); } $keys = array_keys($this->stack); while (($key = array_pop($keys)) !== false) { if ($this->stack[$key] instanceof $type) { return $this->stack[$key]; } } return false; } public function getObjectPlugin($class) { if (isset($this->runtimePlugins[$class])) { return $this->runtimePlugins[$class]; } return $this->runtimePlugins[$class] = new $class($this); } public function classCall($plugName, array $params = array()) { $class = 'Dwoo_Plugin_'.$plugName; $plugin = $this->getObjectPlugin($class); $cnt = count($params); if ($cnt===0) { return $plugin->process(); } elseif ($cnt===1) { return $plugin->process($params[0]); } elseif ($cnt===2) { return $plugin->process($params[0], $params[1]); } elseif ($cnt===3) { return $plugin->process($params[0], $params[1], $params[2]); } elseif ($cnt===4) { return $plugin->process($params[0], $params[1], $params[2], $params[3]); } else { return call_user_func_array(array($plugin, 'process'), $params); } } public function arrayMap($callback, array $params) { if ($params[0] === $this) { $addThis = true; array_shift($params); } if ((is_array($params[0]) || ($params[0] instanceof Iterator && $params[0] instanceof ArrayAccess))) { if (empty($params[0])) { return $params[0]; } $out = array(); $cnt = count($params); if (isset($addThis)) { array_unshift($params, $this); $items = $params[1]; $keys = array_keys($items); if (is_string($callback) === false) { while (($i = array_shift($keys)) !== null) { $out[] = call_user_func_array($callback, array(1=>$items[$i]) + $params); } } elseif ($cnt===1) { while (($i = array_shift($keys)) !== null) { $out[] = $callback($this, $items[$i]); } } elseif ($cnt===2) { while (($i = array_shift($keys)) !== null) { $out[] = $callback($this, $items[$i], $params[2]); } } elseif ($cnt===3) { while (($i = array_shift($keys)) !== null) { $out[] = $callback($this, $items[$i], $params[2], $params[3]); } } else { while (($i = array_shift($keys)) !== null) { $out[] = call_user_func_array($callback, array(1=>$items[$i]) + $params); } } } else { $items = $params[0]; $keys = array_keys($items); if (is_string($callback) === false) { while (($i = array_shift($keys)) !== null) { $out[] = call_user_func_array($callback, array($items[$i]) + $params); } } elseif ($cnt===1) { while (($i = array_shift($keys)) !== null) { $out[] = $callback($items[$i]); } } elseif ($cnt===2) { while (($i = array_shift($keys)) !== null) { $out[] = $callback($items[$i], $params[1]); } } elseif ($cnt===3) { while (($i = array_shift($keys)) !== null) { $out[] = $callback($items[$i], $params[1], $params[2]); } } elseif ($cnt===4) { while (($i = array_shift($keys)) !== null) { $out[] = $callback($items[$i], $params[1], $params[2], $params[3]); } } else { while (($i = array_shift($keys)) !== null) { $out[] = call_user_func_array($callback, array($items[$i]) + $params); } } } return $out; } else { return $params[0]; } } public function readVarInto($varstr, $data, $safeRead = false) { if ($data === null) { return null; } if (is_array($varstr) === false) { preg_match_all('#(\[|->|\.)?((?:[^.[\]-]|-(?!>))+)\]?#i', $varstr, $m); } else { $m = $varstr; } unset($varstr); while (list($k, $sep) = each($m[1])) { if ($sep === '.' || $sep === '[' || $sep === '') { if ((is_array($data) || $data instanceof ArrayAccess) && ($safeRead === false || isset($data[$m[2][$k]]))) { $data = $data[$m[2][$k]]; } else { return null; } } else { if (is_object($data) && ($safeRead === false || isset($data->$m[2][$k]) || is_callable(array($data, '__get')))) { $data = $data->$m[2][$k]; } else { return null; } } } return $data; } public function readParentVar($parentLevels, $varstr = null) { $tree = $this->scopeTree; $cur = $this->data; while ($parentLevels--!==0) { array_pop($tree); } while (($i = array_shift($tree)) !== null) { if (is_object($cur)) { $cur = $cur->$i; } else { $cur = $cur[$i]; } } if ($varstr!==null) { return $this->readVarInto($varstr, $cur); } else { return $cur; } } public function readVar($varstr) { if (is_array($varstr)===true) { $m = $varstr; unset($varstr); } else { if (strstr($varstr, '.') === false && strstr($varstr, '[') === false && strstr($varstr, '->') === false) { if ($varstr === 'dwoo') { return $this->globals; } elseif ($varstr === '__' || $varstr === '_root' ) { return $this->data; $varstr = substr($varstr, 6); } elseif ($varstr === '_' || $varstr === '_parent') { $varstr = '.'.$varstr; $tree = $this->scopeTree; $cur = $this->data; array_pop($tree); while (($i = array_shift($tree)) !== null) { if (is_object($cur)) { $cur = $cur->$i; } else { $cur = $cur[$i]; } } return $cur; } $cur = $this->scope; if (isset($cur[$varstr])) { return $cur[$varstr]; } else { return null; } } if (substr($varstr, 0, 1) === '.') { $varstr = 'dwoo'.$varstr; } preg_match_all('#(\[|->|\.)?((?:[^.[\]-]|-(?!>))+)\]?#i', $varstr, $m); } $i = $m[2][0]; if ($i === 'dwoo') { $cur = $this->globals; array_shift($m[2]); array_shift($m[1]); switch ($m[2][0]) { case 'get': $cur = $_GET; break; case 'post': $cur = $_POST; break; case 'session': $cur = $_SESSION; break; case 'cookies': case 'cookie': $cur = $_COOKIE; break; case 'server': $cur = $_SERVER; break; case 'env': $cur = $_ENV; break; case 'request': $cur = $_REQUEST; break; case 'const': array_shift($m[2]); if (defined($m[2][0])) { return constant($m[2][0]); } else { return null; } } if ($cur !== $this->globals) { array_shift($m[2]); array_shift($m[1]); } } elseif ($i === '__' || $i === '_root') { $cur = $this->data; array_shift($m[2]); array_shift($m[1]); } elseif ($i === '_' || $i === '_parent') { $tree = $this->scopeTree; $cur = $this->data; while (true) { array_pop($tree); array_shift($m[2]); array_shift($m[1]); if (current($m[2]) === '_' || current($m[2]) === '_parent') { continue; } while (($i = array_shift($tree)) !== null) { if (is_object($cur)) { $cur = $cur->$i; } else { $cur = $cur[$i]; } } break; } } else { $cur = $this->scope; } while (list($k, $sep) = each($m[1])) { if ($sep === '.' || $sep === '[' || $sep === '') { if ((is_array($cur) || $cur instanceof ArrayAccess) && isset($cur[$m[2][$k]])) { $cur = $cur[$m[2][$k]]; } else { return null; } } elseif ($sep === '->') { if (is_object($cur)) { $cur = $cur->$m[2][$k]; } else { return null; } } else { return null; } } return $cur; } public function assignInScope($value, $scope) { $tree =& $this->scopeTree; $data =& $this->data; if (!is_string($scope)) { return $this->triggerError('Assignments must be done into strings, ('.gettype($scope).') '.var_export($scope, true).' given', E_USER_ERROR); } if (strstr($scope, '.') === false && strstr($scope, '->') === false) { $this->scope[$scope] = $value; } else { preg_match_all('#(\[|->|\.)?([^.[\]-]+)\]?#i', $scope, $m); $cur =& $this->scope; $last = array(array_pop($m[1]), array_pop($m[2])); while (list($k, $sep) = each($m[1])) { if ($sep === '.' || $sep === '[' || $sep === '') { if (is_array($cur) === false) { $cur = array(); } $cur =& $cur[$m[2][$k]]; } elseif ($sep === '->') { if (is_object($cur) === false) { $cur = new stdClass; } $cur =& $cur->$m[2][$k]; } else { return false; } } if ($last[0] === '.' || $last[0] === '[' || $last[0] === '') { if (is_array($cur) === false) { $cur = array(); } $cur[$last[1]] = $value; } elseif ($last[0] === '->') { if (is_object($cur) === false) { $cur = new stdClass; } $cur->$last[1] = $value; } else { return false; } } } public function setScope($scope, $absolute = false) { $old = $this->scopeTree; if (is_string($scope)===true) { $scope = explode('.', $scope); } if ($absolute===true) { $this->scope =& $this->data; $this->scopeTree = array(); } while (($bit = array_shift($scope)) !== null) { if ($bit === '_' || $bit === '_parent') { array_pop($this->scopeTree); $this->scope =& $this->data; $cnt = count($this->scopeTree); for ($i=0;$i<$cnt;$i++) $this->scope =& $this->scope[$this->scopeTree[$i]]; } elseif ($bit === '__' || $bit === '_root') { $this->scope =& $this->data; $this->scopeTree = array(); } elseif (isset($this->scope[$bit])) { $this->scope =& $this->scope[$bit]; $this->scopeTree[] = $bit; } else { unset($this->scope); $this->scope = null; } } return $old; } public function getData() { return $this->data; } public function &getScope() { return $this->scope; } public function __call($method, $args) { $proxy = $this->getPluginProxy(); if (!$proxy) { throw new Dwoo_Exception('Call to undefined method '.__CLASS__.'::'.$method.'()'); } return call_user_func_array($proxy->getCallback($method), $args); } } class Dwoo extends Dwoo_Core { } interface Dwoo_IPluginProxy { public function handles($name); public function getCode($name, $params); public function getCallback($name); public function getLoader($name); } interface Dwoo_IElseable { } interface Dwoo_ILoader { public function loadPlugin($class, $forceRehash = true); } class Dwoo_Loader implements Dwoo_ILoader { protected $paths = array(); protected $classPath = array(); protected $cacheDir; protected $corePluginDir; public function __construct($cacheDir) { $this->corePluginDir = DWOO_DIRECTORY . 'plugins'; $this->cacheDir = rtrim($cacheDir, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR; $cacheFile = $this->cacheDir.'classpath.cache.d'.Dwoo_Core::RELEASE_TAG.'.php'; if (file_exists($cacheFile)) { $classpath = file_get_contents($cacheFile); $this->classPath = unserialize($classpath) + $this->classPath; } else { $this->rebuildClassPathCache($this->corePluginDir, $cacheFile); } } protected function rebuildClassPathCache($path, $cacheFile) { if ($cacheFile!==false) { $tmp = $this->classPath; $this->classPath = array(); } $list = glob(rtrim($path, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . '*'); if (is_array($list)) { foreach ($list as $f) { if (is_dir($f)) { $this->rebuildClassPathCache($f, false); } else { $this->classPath[str_replace(array('function.','block.','modifier.','outputfilter.','filter.','prefilter.','postfilter.','pre.','post.','output.','shared.','helper.'), '', basename($f, '.php'))] = $f; } } } if ($cacheFile!==false) { if (!file_put_contents($cacheFile, serialize($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), alternatively you can change the directory used with $dwoo->setCompileDir() or provide a custom loader object with $dwoo->setLoader()'); } $this->classPath += $tmp; } } public function loadPlugin($class, $forceRehash = true) { if (!isset($this->classPath[$class]) || !(include $this->classPath[$class])) { if ($forceRehash) { $this->rebuildClassPathCache($this->corePluginDir, $this->cacheDir . 'classpath.cache.d'.Dwoo_Core::RELEASE_TAG.'.php'); foreach ($this->paths as $path=>$file) { $this->rebuildClassPathCache($path, $file); } if (isset($this->classPath[$class])) { include $this->classPath[$class]; } 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); } } 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); } } } public function addDirectory($pluginDirectory) { $pluginDir = realpath($pluginDirectory); if (!$pluginDir) { throw new Dwoo_Exception('Plugin directory does not exist or can not be read : '.$pluginDirectory); } $cacheFile = $this->cacheDir . 'classpath-'.substr(strtr($pluginDir, '/\\:'.PATH_SEPARATOR, '----'), strlen($pluginDir) > 80 ? -80 : 0).'.d'.Dwoo_Core::RELEASE_TAG.'.php'; $this->paths[$pluginDir] = $cacheFile; if (file_exists($cacheFile)) { $classpath = file_get_contents($cacheFile); $this->classPath = unserialize($classpath) + $this->classPath; } else { $this->rebuildClassPathCache($pluginDir, $cacheFile); } } } class Dwoo_Exception extends Exception { } class Dwoo_Security_Policy { const PHP_ENCODE = 1; const PHP_REMOVE = 2; const PHP_ALLOW = 3; const CONST_DISALLOW = false; const CONST_ALLOW = true; protected $allowedPhpFunctions = array ( 'str_repeat', 'number_format', 'htmlentities', 'htmlspecialchars', 'long2ip', 'strlen', 'list', 'empty', 'count', 'sizeof', 'in_array', 'is_array', ); protected $allowedDirectories = array(); protected $phpHandling = self::PHP_REMOVE; protected $constHandling = self::CONST_DISALLOW; public function allowPhpFunction($func) { if (is_array($func)) foreach ($func as $fname) $this->allowedPhpFunctions[strtolower($fname)] = true; else $this->allowedPhpFunctions[strtolower($func)] = true; } public function disallowPhpFunction($func) { if (is_array($func)) foreach ($func as $fname) unset($this->allowedPhpFunctions[strtolower($fname)]); else unset($this->allowedPhpFunctions[strtolower($func)]); } public function getAllowedPhpFunctions() { return $this->allowedPhpFunctions; } public function allowDirectory($path) { if (is_array($path)) foreach ($path as $dir) $this->allowedDirectories[realpath($dir)] = true; else $this->allowedDirectories[realpath($path)] = true; } public function disallowDirectory($path) { if (is_array($path)) foreach ($path as $dir) unset($this->allowedDirectories[realpath($dir)]); else unset($this->allowedDirectories[realpath($path)]); } public function getAllowedDirectories() { return $this->allowedDirectories; } public function setPhpHandling($level = self::PHP_REMOVE) { $this->phpHandling = $level; } public function getPhpHandling() { return $this->phpHandling; } public function setConstantHandling($level = self::CONST_DISALLOW) { $this->constHandling = $level; } public function getConstantHandling() { return $this->constHandling; } } class Dwoo_Security_Exception extends Dwoo_Exception { } interface Dwoo_ICompilable { } interface Dwoo_ICompiler { public function compile(Dwoo_Core $dwoo, Dwoo_ITemplate $template); public function setCustomPlugins(array $customPlugins); public function setSecurityPolicy(Dwoo_Security_Policy $policy = null); } interface Dwoo_IDataProvider { public function getData(); } interface Dwoo_ITemplate { public function getCacheTime(); public function setCacheTime($seconds = null); public function getCachedTemplate(Dwoo_Core $dwoo); public function cache(Dwoo_Core $dwoo, $output); public function clearCache(Dwoo_Core $dwoo, $olderThan = -1); public function getCompiledTemplate(Dwoo_Core $dwoo, Dwoo_ICompiler $compiler = null); public function getName(); public function getResourceName(); public function getResourceIdentifier(); public function getSource(); public function getUid(); public function getCompiler(); public function getIsModifiedCode(); public static function templateFactory(Dwoo_Core $dwoo, $resourceId, $cacheTime = null, $cacheId = null, $compileId = null, Dwoo_ITemplate $parentTemplate = null); } interface Dwoo_ICompilable_Block { } abstract class Dwoo_Plugin { protected $dwoo; public function __construct(Dwoo_Core $dwoo) { $this->dwoo = $dwoo; } public static function paramsToAttributes(array $params, $delim = '\'') { if (isset($params['*'])) { $params = array_merge($params, $params['*']); unset($params['*']); } $out = ''; foreach ($params as $attr=>$val) { $out .= ' '.$attr.'='; if (trim($val, '"\'')=='' || $val=='null') { $out .= str_replace($delim, '\\'.$delim, '""'); } elseif (substr($val, 0, 1) === $delim && substr($val, -1) === $delim) { $out .= str_replace($delim, '\\'.$delim, '"'.substr($val, 1, -1).'"'); } else { $out .= str_replace($delim, '\\'.$delim, '"') . $delim . '.'.$val.'.' . $delim . str_replace($delim, '\\'.$delim, '"'); } } return ltrim($out); } } abstract class Dwoo_Block_Plugin extends Dwoo_Plugin { protected $buffer = ''; public function buffer($input) { $this->buffer .= $input; } public function end() { } public function process() { return $this->buffer; } public static function preProcessing(Dwoo_Compiler $compiler, array $params, $prepend, $append, $type) { return Dwoo_Compiler::PHP_OPEN.$prepend.'$this->addStack("'.$type.'", array('.Dwoo_Compiler::implode_r($compiler->getCompiledParams($params)).'));'.$append.Dwoo_Compiler::PHP_CLOSE; } public static function postProcessing(Dwoo_Compiler $compiler, array $params, $prepend, $append, $content) { return $content . Dwoo_Compiler::PHP_OPEN.$prepend.'$this->delStack();'.$append.Dwoo_Compiler::PHP_CLOSE; } } abstract class Dwoo_Filter { protected $dwoo; public function __construct(Dwoo_Core $dwoo) { $this->dwoo = $dwoo; } abstract public function process($input); } abstract class Dwoo_Processor { protected $compiler; public function __construct(Dwoo_Compiler $compiler) { $this->compiler = $compiler; } abstract public function process($input); } class Dwoo_Template_String implements Dwoo_ITemplate { protected $name; protected $compileId; protected $cacheId; protected $cacheTime; protected $compilationEnforced; protected static $cache = array('cached'=>array(), 'compiled'=>array()); protected $compiler; protected $chmod = 0777; public function __construct($templateString, $cacheTime = null, $cacheId = null, $compileId = null) { $this->template = $templateString; if (function_exists('hash')) { $this->name = hash('md4', $templateString); } else { $this->name = md5($templateString); } $this->cacheTime = $cacheTime; if ($compileId !== null) { $this->compileId = str_replace('../', '__', strtr($compileId, '\\%?=!:;'.PATH_SEPARATOR, '/-------')); } if ($cacheId !== null) { $this->cacheId = str_replace('../', '__', strtr($cacheId, '\\%?=!:;'.PATH_SEPARATOR, '/-------')); } } public function getCacheTime() { return $this->cacheTime; } public function setCacheTime($seconds = null) { $this->cacheTime = $seconds; } public function getChmod() { return $this->chmod; } public function setChmod($mask = null) { $this->chmod = $mask; } public function getName() { return $this->name; } public function getResourceName() { return 'string'; } public function getResourceIdentifier() { return false; } public function getSource() { return $this->template; } public function getUid() { return $this->name; } public function getCompiler() { return $this->compiler; } public function forceCompilation() { $this->compilationEnforced = true; } public function getCachedTemplate(Dwoo_Core $dwoo) { if ($this->cacheTime !== null) { $cacheLength = $this->cacheTime; } else { $cacheLength = $dwoo->getCacheTime(); } if ($cacheLength === 0) { return false; } $cachedFile = $this->getCacheFilename($dwoo); if (isset(self::$cache['cached'][$this->cacheId]) === true && file_exists($cachedFile)) { return $cachedFile; } elseif ($this->compilationEnforced !== true && file_exists($cachedFile) && ($cacheLength === -1 || filemtime($cachedFile) > ($_SERVER['REQUEST_TIME'] - $cacheLength)) && $this->isValidCompiledFile($this->getCompiledFilename($dwoo))) { self::$cache['cached'][$this->cacheId] = true; return $cachedFile; } else { return true; } } public function cache(Dwoo_Core $dwoo, $output) { $cacheDir = $dwoo->getCacheDir(); $cachedFile = $this->getCacheFilename($dwoo); $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); return false; } } fwrite($file, $output); fclose($file); $this->makeDirectory(dirname($cachedFile), $cacheDir); if (!@rename($temp, $cachedFile)) { @unlink($cachedFile); @rename($temp, $cachedFile); } if ($this->chmod !== null) { chmod($cachedFile, $this->chmod); } self::$cache['cached'][$this->cacheId] = true; return $cachedFile; } public function clearCache(Dwoo_Core $dwoo, $olderThan = -1) { $cachedFile = $this->getCacheFilename($dwoo); return !file_exists($cachedFile) || (filectime($cachedFile) < (time() - $olderThan) && unlink($cachedFile)); } public function getCompiledTemplate(Dwoo_Core $dwoo, Dwoo_ICompiler $compiler = null) { $compiledFile = $this->getCompiledFilename($dwoo); if ($this->compilationEnforced !== true && isset(self::$cache['compiled'][$this->compileId]) === true) { } elseif ($this->compilationEnforced !== true && $this->isValidCompiledFile($compiledFile)) { self::$cache['compiled'][$this->compileId] = true; } else { $this->compilationEnforced = false; if ($compiler === null) { $compiler = $dwoo->getDefaultCompilerFactory($this->getResourceName()); if ($compiler === null || $compiler === array('Dwoo_Compiler', 'compilerFactory')) { if (class_exists('Dwoo_Compiler', false) === false) { include DWOO_DIRECTORY . 'Dwoo/Compiler.php'; } $compiler = Dwoo_Compiler::compilerFactory(); } else { $compiler = call_user_func($compiler); } } $this->compiler = $compiler; $compiler->setCustomPlugins($dwoo->getCustomPlugins()); $compiler->setSecurityPolicy($dwoo->getSecurityPolicy()); $this->makeDirectory(dirname($compiledFile), $dwoo->getCompileDir()); file_put_contents($compiledFile, $compiler->compile($dwoo, $this)); if ($this->chmod !== null) { chmod($compiledFile, $this->chmod); } self::$cache['compiled'][$this->compileId] = true; } return $compiledFile; } protected function isValidCompiledFile($file) { return file_exists($file); } public static function templateFactory(Dwoo_Core $dwoo, $resourceId, $cacheTime = null, $cacheId = null, $compileId = null, Dwoo_ITemplate $parentTemplate = null) { return new self($resourceId, $cacheTime, $cacheId, $compileId); } protected function getCompiledFilename(Dwoo_Core $dwoo) { if ($this->compileId===null) { $this->compileId = $this->name; } return $dwoo->getCompileDir() . $this->compileId.'.d'.Dwoo_Core::RELEASE_TAG.'.php'; } protected function getCacheFilename(Dwoo_Core $dwoo) { 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']); } else { $cacheId = ''; } $this->getCompiledFilename($dwoo); $this->cacheId = str_replace('../', '__', $this->compileId . strtr($cacheId, '\\%?=!:;'.PATH_SEPARATOR, '/-------')); } return $dwoo->getCacheDir() . $this->cacheId.'.html'; } public function getIsModifiedCode() { return null; } protected function makeDirectory($path, $baseDir = null) { if (is_dir($path) === true) { return; } if ($this->chmod === null) { $chmod = 0777; } else { $chmod = $this->chmod; } mkdir($path, $chmod, true); if (strpos(PHP_OS, 'WIN') !== 0 && $baseDir !== null) { $path = strtr(str_replace($baseDir, '', $path), '\\', '/'); $folders = explode('/', trim($path, '/')); foreach ($folders as $folder) { $baseDir .= $folder . DIRECTORY_SEPARATOR; chmod($baseDir, $chmod); } } } } class Dwoo_Template_File extends Dwoo_Template_String { protected $file; protected $includePath = null; protected $resolvedPath = null; public function __construct($file, $cacheTime = null, $cacheId = null, $compileId = null, $includePath = null) { $this->file = $file; $this->name = basename($file); $this->cacheTime = $cacheTime; if ($compileId !== null) { $this->compileId = str_replace('../', '__', strtr($compileId, '\\%?=!:;'.PATH_SEPARATOR, '/-------')); } if ($cacheId !== null) { $this->cacheId = str_replace('../', '__', strtr($cacheId, '\\%?=!:;'.PATH_SEPARATOR, '/-------')); } if (is_string($includePath)) { $this->includePath = array($includePath); } elseif (is_array($includePath)) { $this->includePath = $includePath; } } public function setIncludePath($paths) { if (is_array($paths) === false) { $paths = array($paths); } $this->includePath = $paths; $this->resolvedPath = null; } public function getIncludePath() { return $this->includePath; } protected function isValidCompiledFile($file) { return parent::isValidCompiledFile($file) && (int)$this->getUid() <= filemtime($file); } public function getSource() { return file_get_contents($this->getResourceIdentifier()); } public function getResourceName() { return 'file'; } public function getResourceIdentifier() { if ($this->resolvedPath !== null) { return $this->resolvedPath; } elseif ($this->includePath === null) { return $this->file; } else { foreach ($this->includePath as $path) { $path = rtrim($path, DIRECTORY_SEPARATOR); if (file_exists($path.DIRECTORY_SEPARATOR.$this->file) === true) { $this->resolvedPath = $path . DIRECTORY_SEPARATOR . $this->file; return $this->resolvedPath; } } throw new Dwoo_Exception('Template "'.$this->file.'" could not be found in any of your include path(s)'); } } public function getUid() { return (string) filemtime($this->getResourceIdentifier()); } public static function templateFactory(Dwoo_Core $dwoo, $resourceId, $cacheTime = null, $cacheId = null, $compileId = null, Dwoo_ITemplate $parentTemplate = null) { if (DIRECTORY_SEPARATOR === '\\') { $resourceId = str_replace(array("\t", "\n", "\r", "\f", "\v"), array('\\t', '\\n', '\\r', '\\f', '\\v'), $resourceId); } $resourceId = strtr($resourceId, '\\', '/'); $includePath = null; if (file_exists($resourceId) === false) { if ($parentTemplate === null) { $parentTemplate = $dwoo->getTemplate(); } if ($parentTemplate instanceof Dwoo_Template_File) { if ($includePath = $parentTemplate->getIncludePath()) { if (strstr($resourceId, '../')) { throw new Dwoo_Exception('When using an include path you can not reference a template into a parent directory (using ../)'); } } else { $resourceId = dirname($parentTemplate->getResourceIdentifier()).DIRECTORY_SEPARATOR.$resourceId; if (file_exists($resourceId) === false) { return null; } } } else { return null; } } if ($policy = $dwoo->getSecurityPolicy()) { while (true) { if (preg_match('{^([a-z]+?)://}i', $resourceId)) { throw new Dwoo_Security_Exception('The security policy prevents you to read files from external sources : <em>'.$resourceId.'</em>.'); } if ($includePath) { break; } $resourceId = realpath($resourceId); $dirs = $policy->getAllowedDirectories(); foreach ($dirs as $dir=>$dummy) { if (strpos($resourceId, $dir) === 0) { break 2; } } throw new Dwoo_Security_Exception('The security policy prevents you to read <em>'.$resourceId.'</em>'); } } $class = 'Dwoo_Template_File'; if ($parentTemplate) { $class = get_class($parentTemplate); } return new $class($resourceId, $cacheTime, $cacheId, $compileId, $includePath); } protected function getCompiledFilename(Dwoo_Core $dwoo) { if ($this->compileId===null) { $this->compileId = str_replace('../', '__', strtr($this->getResourceIdentifier(), '\\:', '/-')); } return $dwoo->getCompileDir() . $this->compileId.'.d'.Dwoo_Core::RELEASE_TAG.'.php'; } public function getIsModifiedCode() { return '"'.$this->getUid().'" == filemtime('.var_export($this->getResourceIdentifier(), true).')'; } } class Dwoo_Data implements Dwoo_IDataProvider { protected $data = array(); public function getData() { return $this->data; } public function clear($name = null) { if ($name === null) { $this->data = array(); } elseif (is_array($name)) { foreach ($name as $index) unset($this->data[$index]); } else { unset($this->data[$name]); } } public function setData(array $data) { $this->data = $data; } public function mergeData(array $data) { $args = func_get_args(); while (list(,$v) = each($args)) { if (is_array($v)) { $this->data = array_merge($this->data, $v); } } } public function assign($name, $val = null) { if (is_array($name)) { reset($name); while (list($k,$v) = each($name)) $this->data[$k] = $v; } else { $this->data[$name] = $val; } } public function __set($name, $value) { $this->assign($name, $value); } public function assignByRef($name, &$val) { $this->data[$name] =& $val; } public function append($name, $val = null, $merge = false) { if (is_array($name)) { foreach ($name as $key=>$val) { if (isset($this->data[$key]) && !is_array($this->data[$key])) { settype($this->data[$key], 'array'); } if ($merge === true && is_array($val)) { $this->data[$key] = $val + $this->data[$key]; } else { $this->data[$key][] = $val; } } } elseif ($val !== null) { if (isset($this->data[$name]) && !is_array($this->data[$name])) { settype($this->data[$name], 'array'); } elseif (!isset($this->data[$name])) { $this->data[$name] = array(); } if ($merge === true && is_array($val)) { $this->data[$name] = $val + $this->data[$name]; } else { $this->data[$name][] = $val; } } } public function appendByRef($name, &$val, $merge = false) { if (isset($this->data[$name]) && !is_array($this->data[$name])) { settype($this->data[$name], 'array'); } if ($merge === true && is_array($val)) { foreach ($val as $key => &$val) { $this->data[$name][$key] =& $val; } } else { $this->data[$name][] =& $val; } } public function isAssigned($name) { return isset($this->data[$name]); } public function __isset($name) { return isset($this->data[$name]); } public function unassign($name) { unset($this->data[$name]); } public function __unset($name) { unset($this->data[$name]); } public function get($name) { return $this->__get($name); } public function __get($name) { if (isset($this->data[$name])) { return $this->data[$name]; } else { throw new Dwoo_Exception('Tried to read a value that was not assigned yet : "'.$name.'"'); } } }
495dc1 9315dc a/lib/Dwoo.php
2 2 <?php
3 3
4 -define('DWOO_DIRECTORY', dirname(__FILE__) . DIRECTORY_SEPARATOR);
5 -
6 4 /**
7 - * main dwoo class, allows communication between the compiler, template and data classes
8 - *
9 - * <pre>
10 - * requirements :
11 - * php 5.2.0 or above (might work below, it's a rough estimate)
12 - * SPL and PCRE extensions (for php versions prior to 5.3.0)
13 - * mbstring extension for some string manipulation plugins (especially if you intend to use UTF-8)
14 - * recommended :
15 - * hash extension (for Dwoo_Template_String - minor performance boost)
16 - *
17 - * project created :
18 - * 2008-01-05
19 - * </pre>
5 + * micro-class that extends Dwoo_Core for compatibility purposes
20 6 *
21 7 * This software is provided 'as-is', without any express or implied warranty.
22 8 * In no event will the authors be held liable for any damages arising from the use of this software.
26 12 * @license http://dwoo.org/LICENSE Modified BSD License
27 13 * @link http://dwoo.org/
28 14 * @version 1.1.0
29 - * @date 2009-07-18
15 + * @date 2010-02-28
30 16 * @package Dwoo
17 + * @see Dwoo_Core
31 18 */
32 -class Dwoo
19 +class Dwoo extends Dwoo_Core
33 20 {
34 - /**
35 - * current version number
36 - *
37 - * @var string
38 - */
39 - const VERSION = '1.1.1';
40 -
41 - /**
42 - * unique number of this dwoo release
43 - *
44 - * this can be used by templates classes to check whether the compiled template
45 - * has been compiled before this release or not, so that old templates are
46 - * recompiled automatically when Dwoo is updated
47 - */
48 - const RELEASE_TAG = 17;
49 -
50 - /**#@+
51 - * constants that represents all plugin types
52 - *
53 - * these are bitwise-operation-safe values to allow multiple types
54 - * on a single plugin
55 - *
56 - * @var int
57 - */
58 - const CLASS_PLUGIN = 1;
59 - const FUNC_PLUGIN = 2;
60 - const NATIVE_PLUGIN = 4;
61 - const BLOCK_PLUGIN = 8;
62 - const COMPILABLE_PLUGIN = 16;
63 - const CUSTOM_PLUGIN = 32;
64 - const SMARTY_MODIFIER = 64;
65 - const SMARTY_BLOCK = 128;
66 - const SMARTY_FUNCTION = 256;
67 - const PROXY_PLUGIN = 512;
68 - const TEMPLATE_PLUGIN = 1024;
69 - /**#@-*/
70 -
71 - /**
72 - * character set of the template, used by string manipulation plugins
73 - *
74 - * it must be lowercase, but setCharset() will take care of that
75 - *
76 - * @see setCharset
77 - * @see getCharset
78 - * @var string
79 - */
80 - protected $charset = 'utf-8';
81 -
82 - /**
83 - * global variables that are accessible through $dwoo.* in the templates
84 - *
85 - * default values include:
86 - *
87 - * $dwoo.version - current version number
88 - * $dwoo.ad - a Powered by Dwoo link pointing to dwoo.org
89 - * $dwoo.now - the current time
90 - * $dwoo.template - the current template filename
91 - * $dwoo.charset - the character set used by the template
92 - *
93 - * on top of that, foreach and other plugins can store special values in there,
94 - * see their documentation for more details.
95 - *
96 - * @private
97 - * @var array
98 - */
99 - public $globals;
100 -
101 - /**
102 - * directory where the compiled templates are stored
103 - *
104 - * defaults to DWOO_COMPILEDIR (= dwoo_dir/compiled by default)
105 - *
106 - * @var string
107 - */
108 - protected $compileDir;
109 -
110 - /**
111 - * directory where the cached templates are stored
112 - *
113 - * defaults to DWOO_CACHEDIR (= dwoo_dir/cache by default)
114 - *
115 - * @var string
116 - */
117 - protected $cacheDir;
118 -
119 - /**
120 - * defines how long (in seconds) the cached files must remain valid
121 - *
122 - * can be overriden on a per-template basis
123 - *
124 - * -1 = never delete
125 - * 0 = disabled
126 - * >0 = duration in seconds
127 - *
128 - * @var int
129 - */
130 - protected $cacheTime = 0;
131 -
132 - /**
133 - * security policy object
134 - *
135 - * @var Dwoo_Security_Policy
136 - */
137 - protected $securityPolicy = null;
138 -
139 - /**
140 - * stores the custom plugins callbacks
141 - *
142 - * @see addPlugin
143 - * @see removePlugin
144 - * @var array
145 - */
146 - protected $plugins = array();
147 -
148 - /**
149 - * stores the filter callbacks
150 - *
151 - * @see addFilter
152 - * @see removeFilter
153 - * @var array
154 - */
155 - protected $filters = array();
156 -
157 - /**
158 - * stores the resource types and associated
159 - * classes / compiler classes
160 - *
161 - * @var array
162 - */
163 - protected $resources = array
164 - (
165 - 'file' => array
166 - (
167 - 'class' => 'Dwoo_Template_File',
168 - 'compiler' => null
169 - ),
170 - 'string' => array
171 - (
172 - 'class' => 'Dwoo_Template_String',
173 - 'compiler' => null
174 - )
175 - );
176 -
177 - /**
178 - * the dwoo loader object used to load plugins by this dwoo instance
179 - *
180 - * @var Dwoo_ILoader
181 - */
182 - protected $loader = null;
183 -
184 - /**
185 - * currently rendered template, set to null when not-rendering
186 - *
187 - * @var Dwoo_ITemplate
188 - */
189 - protected $template = null;
190 -
191 - /**
192 - * stores the instances of the class plugins during template runtime
193 - *
194 - * @var array
195 - */
196 - protected $runtimePlugins;
197 -
198 - /**
199 - * stores the data during template runtime
200 - *
201 - * @var array
202 - * @private
203 - */
204 - public $data;
205 -
206 - /**
207 - * stores the current scope during template runtime
208 - *
209 - * this should ideally not be accessed directly from outside template code
210 - *
211 - * @var mixed
212 - * @private
213 - */
214 - public $scope;
215 -
216 - /**
217 - * stores the scope tree during template runtime
218 - *
219 - * @var array
220 - */
221 - protected $scopeTree;
222 -
223 - /**
224 - * stores the block plugins stack during template runtime
225 - *
226 - * @var array
227 - */
228 - protected $stack;
229 -
230 - /**
231 - * stores the current block plugin at the top of the stack during template runtime
232 - *
233 - * @var Dwoo_Block_Plugin
234 - */
235 - protected $curBlock;
236 -
237 - /**
238 - * stores the output buffer during template runtime
239 - *
240 - * @var string
241 - */
242 - protected $buffer;
243 -
244 - /**
245 - * stores plugin proxy
246 - *
247 - * @var Dwoo_IPluginProxy
248 - */
249 - protected $pluginProxy;
250 -
251 - /**
252 - * constructor, sets the cache and compile dir to the default values if not provided
253 - *
254 - * @param string $compileDir path to the compiled directory, defaults to lib/compiled
255 - * @param string $cacheDir path to the cache directory, defaults to lib/cache
256 - */
257 - public function __construct($compileDir = null, $cacheDir = null)
258 - {
259 - if ($compileDir !== null) {
260 - $this->setCompileDir($compileDir);
261 - }
262 - if ($cacheDir !== null) {
263 - $this->setCacheDir($cacheDir);
264 - }
265 - $this->initGlobals();
266 - }
267 -
268 - /**
269 - * resets some runtime variables to allow a cloned object to be used to render sub-templates
270 - */
271 - public function __clone()
272 - {
273 - $this->template = null;
274 - unset($this->data);
275 - }
276 -
277 - /**
278 - * outputs the template instead of returning it, this is basically a shortcut for get(*, *, *, true)
279 - *
280 - * @see get
281 - * @param mixed $tpl template, can either be a Dwoo_ITemplate object (i.e. Dwoo_Template_File), a valid path to a template, or
282 - * a template as a string it is recommended to provide a Dwoo_ITemplate as it will probably make things faster,
283 - * especially if you render a template multiple times
284 - * @param mixed $data the data to use, can either be a Dwoo_IDataProvider object (i.e. Dwoo_Data) or an associative array. if you're
285 - * rendering the template from cache, it can be left null
286 - * @param Dwoo_ICompiler $compiler the compiler that must be used to compile the template, if left empty a default
287 - * Dwoo_Compiler will be used.
288 - * @return string nothing or the template output if $output is true
289 - */
290 - public function output($tpl, $data = array(), Dwoo_ICompiler $compiler = null)
291 - {
292 - return $this->get($tpl, $data, $compiler, true);
293 - }
294 -
295 - /**
296 - * returns the given template rendered using the provided data and optional compiler
297 - *
298 - * @param mixed $tpl template, can either be a Dwoo_ITemplate object (i.e. Dwoo_Template_File), a valid path to a template, or
299 - * a template as a string it is recommended to provide a Dwoo_ITemplate as it will probably make things faster,
300 - * especially if you render a template multiple times
301 - * @param mixed $data the data to use, can either be a Dwoo_IDataProvider object (i.e. Dwoo_Data) or an associative array. if you're
302 - * rendering the template from cache, it can be left null
303 - * @param Dwoo_ICompiler $compiler the compiler that must be used to compile the template, if left empty a default
304 - * Dwoo_Compiler will be used.
305 - * @param bool $output flag that defines whether the function returns the output of the template (false, default) or echoes it directly (true)
306 - * @return string nothing or the template output if $output is true
307 - */
308 - public function get($_tpl, $data = array(), $_compiler = null, $_output = false)
309 - {
310 - // a render call came from within a template, so we need a new dwoo instance in order to avoid breaking this one
311 - if ($this->template instanceof Dwoo_ITemplate) {
312 - $proxy = clone $this;
313 - return $proxy->get($_tpl, $data, $_compiler, $_output);
314 - }
315 -
316 - // auto-create template if required
317 - if ($_tpl instanceof Dwoo_ITemplate) {
318 - // valid, skip
319 - } elseif (is_string($_tpl) && file_exists($_tpl)) {
320 - $_tpl = new Dwoo_Template_File($_tpl);
321 - } else {
322 - throw new Dwoo_Exception('Dwoo->get/Dwoo->output\'s first argument must be a Dwoo_ITemplate (i.e. Dwoo_Template_File) or a valid path to a template file', E_USER_NOTICE);
323 - }
324 -
325 - // save the current template, enters render mode at the same time
326 - // if another rendering is requested it will be proxied to a new Dwoo instance
327 - $this->template = $_tpl;
328 -
329 - // load data
330 - if ($data instanceof Dwoo_IDataProvider) {
331 - $this->data = $data->getData();
332 - } elseif (is_array($data)) {
333 - $this->data = $data;
334 - } else {
335 - throw new Dwoo_Exception('Dwoo->get/Dwoo->output\'s data argument must be a Dwoo_IDataProvider object (i.e. Dwoo_Data) or an associative array', E_USER_NOTICE);
336 - }
337 -
338 - $this->globals['template'] = $_tpl->getName();
339 - $this->initRuntimeVars($_tpl);
340 -
341 - // try to get cached template
342 - $file = $_tpl->getCachedTemplate($this);
343 - $doCache = $file === true;
344 - $cacheLoaded = is_string($file);
345 -
346 - if ($cacheLoaded === true) {
347 - // cache is present, run it
348 - if ($_output === true) {
349 - include $file;
350 - $this->template = null;
351 - } else {
352 - ob_start();
353 - include $file;
354 - $this->template = null;
355 - return ob_get_clean();
356 - }
357 - } else {
358 - // no cache present
359 - if ($doCache === true) {
360 - $dynamicId = uniqid();
361 - }
362 -
363 - // render template
364 - $compiledTemplate = $_tpl->getCompiledTemplate($this, $_compiler);
365 - $out = include $compiledTemplate;
366 -
367 - // template returned false so it needs to be recompiled
368 - if ($out === false) {
369 - $_tpl->forceCompilation();
370 - $compiledTemplate = $_tpl->getCompiledTemplate($this, $_compiler);
371 - $out = include $compiledTemplate;
372 - }
373 -
374 - if ($doCache === true) {
375 - $out = preg_replace('/(<%|%>|<\?php|<\?|\?>)/', '<?php /*'.$dynamicId.'*/ echo \'$1\'; ?>', $out);
376 - if (!class_exists('Dwoo_plugin_dynamic', false)) {
377 - $this->getLoader()->loadPlugin('dynamic');
378 - }
379 - $out = Dwoo_Plugin_dynamic::unescape($out, $dynamicId, $compiledTemplate);
380 - }
381 -
382 - // process filters
383 - foreach ($this->filters as $filter) {
384 - if (is_array($filter) && $filter[0] instanceof Dwoo_Filter) {
385 - $out = call_user_func($filter, $out);
386 - } else {
387 - $out = call_user_func($filter, $this, $out);
388 - }
389 - }
390 -
391 - if ($doCache === true) {
392 - // building cache
393 - $file = $_tpl->cache($this, $out);
394 -
395 - // run it from the cache to be sure dynamics are rendered
396 - if ($_output === true) {
397 - include $file;
398 - // exit render mode
399 - $this->template = null;
400 - } else {
401 - ob_start();
402 - include $file;
403 - // exit render mode
404 - $this->template = null;
405 - return ob_get_clean();
406 - }
407 - } else {
408 - // no need to build cache
409 - // exit render mode
410 - $this->template = null;
411 - // output
412 - if ($_output === true) {
413 - echo $out;
414 - }
415 - return $out;
416 - }
417 - }
418 - }
419 -
420 - /**
421 - * re-initializes the globals array before each template run
422 - *
423 - * this method is only callede once when the Dwoo object is created
424 - */
425 - protected function initGlobals()
426 - {
427 - $this->globals = array
428 - (
429 - 'version' => self::VERSION,
430 - 'ad' => '<a href="http://dwoo.org/">Powered by Dwoo</a>',
431 - 'now' => $_SERVER['REQUEST_TIME'],
432 - 'charset' => $this->charset,
433 - );
434 - }
435 -
436 - /**
437 - * re-initializes the runtime variables before each template run
438 - *
439 - * override this method to inject data in the globals array if needed, this
440 - * method is called before each template execution
441 - *
442 - * @param Dwoo_ITemplate $tpl the template that is going to be rendered
443 - */
444 - protected function initRuntimeVars(Dwoo_ITemplate $tpl)
445 - {
446 - $this->runtimePlugins = array();
447 - $this->scope =& $this->data;
448 - $this->scopeTree = array();
449 - $this->stack = array();
450 - $this->curBlock = null;
451 - $this->buffer = '';
452 - }
453 -
454 - /*
455 - * --------- settings functions ---------
456 - */
457 -
458 - /**
459 - * adds a custom plugin that is not in one of the plugin directories
460 - *
461 - * @param string $name the plugin name to be used in the templates
462 - * @param callback $callback the plugin callback, either a function name,
463 - * a class name or an array containing an object
464 - * or class name and a method name
465 - * @param bool $compilable if set to true, the plugin is assumed to be compilable
466 - */
467 - public function addPlugin($name, $callback, $compilable = false)
468 - {
469 - $compilable = $compilable ? self::COMPILABLE_PLUGIN : 0;
470 - if (is_array($callback)) {
471 - if (is_subclass_of(is_object($callback[0]) ? get_class($callback[0]) : $callback[0], 'Dwoo_Block_Plugin')) {
472 - $this->plugins[$name] = array('type'=>self::BLOCK_PLUGIN | $compilable, 'callback'=>$callback, 'class'=>(is_object($callback[0]) ? get_class($callback[0]) : $callback[0]));
473 - } else {
474 - $this->plugins[$name] = array('type'=>self::CLASS_PLUGIN | $compilable, 'callback'=>$callback, 'class'=>(is_object($callback[0]) ? get_class($callback[0]) : $callback[0]), 'function'=>$callback[1]);
475 - }
476 - } elseif (class_exists($callback, false)) {
477 - if (is_subclass_of($callback, 'Dwoo_Block_Plugin')) {
478 - $this->plugins[$name] = array('type'=>self::BLOCK_PLUGIN | $compilable, 'callback'=>$callback, 'class'=>$callback);
479 - } else {
480 - $this->plugins[$name] = array('type'=>self::CLASS_PLUGIN | $compilable, 'callback'=>$callback, 'class'=>$callback, 'function'=>'process');
481 - }
482 - } elseif (function_exists($callback)) {
483 - $this->plugins[$name] = array('type'=>self::FUNC_PLUGIN | $compilable, 'callback'=>$callback);
484 - } else {
485 - throw new Dwoo_Exception('Callback could not be processed correctly, please check that the function/class you used exists');
486 - }
487 - }
488 -
489 - /**
490 - * removes a custom plugin
491 - *
492 - * @param string $name the plugin name
493 - */
494 - public function removePlugin($name)
495 - {
496 - if (isset($this->plugins[$name])) {
497 - unset($this->plugins[$name]);
498 - }
499 - }
500 -
501 - /**
502 - * adds a filter to this Dwoo instance, it will be used to filter the output of all the templates rendered by this instance
503 - *
504 - * @param mixed $callback a callback or a filter name if it is autoloaded from a plugin directory
505 - * @param bool $autoload if true, the first parameter must be a filter name from one of the plugin directories
506 - */
507 - public function addFilter($callback, $autoload = false)
508 - {
509 - if ($autoload) {
510 - $class = 'Dwoo_Filter_'.$callback;
511 -
512 - if (!class_exists($class, false) && !function_exists($class)) {
513 - try {
514 - $this->getLoader()->loadPlugin($callback);
515 - } catch (Dwoo_Exception $e) {
516 - if (strstr($callback, 'Dwoo_Filter_')) {
517 - throw new Dwoo_Exception('Wrong filter name : '.$callback.', the "Dwoo_Filter_" prefix should not be used, please only use "'.str_replace('Dwoo_Filter_', '', $callback).'"');
518 - } else {
519 - throw new Dwoo_Exception('Wrong filter name : '.$callback.', when using autoload the filter must be in one of your plugin dir as "name.php" containg a class or function named "Dwoo_Filter_name"');
520 - }
521 - }
522 - }
523 -
524 - if (class_exists($class, false)) {
525 - $callback = array(new $class($this), 'process');
526 - } elseif (function_exists($class)) {
527 - $callback = $class;
528 - } else {
529 - throw new Dwoo_Exception('Wrong filter name : '.$callback.', when using autoload the filter must be in one of your plugin dir as "name.php" containg a class or function named "Dwoo_Filter_name"');
530 - }
531 -
532 - $this->filters[] = $callback;
533 - } else {
534 - $this->filters[] = $callback;
535 - }
536 - }
537 -
538 - /**
539 - * removes a filter
540 - *
541 - * @param mixed $callback callback or filter name if it was autoloaded
542 - */
543 - public function removeFilter($callback)
544 - {
545 - if (($index = array_search('Dwoo_Filter_'.$callback, $this->filters, true)) !== false) {
546 - unset($this->filters[$index]);
547 - } elseif (($index = array_search($callback, $this->filters, true)) !== false) {
548 - unset($this->filters[$index]);
549 - } else {
550 - $class = 'Dwoo_Filter_' . $callback;
551 - foreach ($this->filters as $index=>$filter) {
552 - if (is_array($filter) && $filter[0] instanceof $class) {
553 - unset($this->filters[$index]);
554 - break;
555 - }
556 - }
557 - }
558 - }
559 -
560 - /**
561 - * adds a resource or overrides a default one
562 - *
563 - * @param string $name the resource name
564 - * @param string $class the resource class (which must implement Dwoo_ITemplate)
565 - * @param callback $compilerFactory the compiler factory callback, a function that must return a compiler instance used to compile this resource, if none is provided. by default it will produce a Dwoo_Compiler object
566 - */
567 - public function addResource($name, $class, $compilerFactory = null)
568 - {
569 - if (strlen($name) < 2) {
570 - throw new Dwoo_Exception('Resource names must be at least two-character long to avoid conflicts with Windows paths');
571 - }
572 -
573 - if (!class_exists($class)) {
574 - throw new Dwoo_Exception('Resource class does not exist');
575 - }
576 -
577 - $interfaces = class_implements($class);
578 - if (in_array('Dwoo_ITemplate', $interfaces) === false) {
579 - throw new Dwoo_Exception('Resource class must implement Dwoo_ITemplate');
580 - }
581 -
582 - $this->resources[$name] = array('class'=>$class, 'compiler'=>$compilerFactory);
583 - }
584 -
585 - /**
586 - * removes a custom resource
587 - *
588 - * @param string $name the resource name
589 - */
590 - public function removeResource($name)
591 - {
592 - unset($this->resources[$name]);
593 - if ($name==='file') {
594 - $this->resources['file'] = array('class'=>'Dwoo_Template_File', 'compiler'=>null);
595 - }
596 - }
597 -
598 - /*
599 - * --------- getters and setters ---------
600 - */
601 -
602 - /**
603 - * sets the loader object to use to load plugins
604 - *
605 - * @param Dwoo_ILoader $loader loader object
606 - */
607 - public function setLoader(Dwoo_ILoader $loader)
608 - {
609 - $this->loader = $loader;
610 - }
611 -
612 - /**
613 - * returns the current loader object or a default one if none is currently found
614 - *
615 - * @param Dwoo_ILoader
616 - */
617 - public function getLoader()
618 - {
619 - if ($this->loader === null) {
620 - $this->loader = new Dwoo_Loader($this->getCompileDir());
621 - }
622 -
623 - return $this->loader;
624 - }
625 -
626 - /**
627 - * returns the custom plugins loaded
628 - *
629 - * used by the Dwoo_ITemplate classes to pass the custom plugins to their Dwoo_ICompiler instance
630 - *
631 - * @return array
632 - */
633 - public function getCustomPlugins()
634 - {
635 - return $this->plugins;
636 - }
637 -
638 - /**
639 - * returns the cache directory with a trailing DIRECTORY_SEPARATOR
640 - *
641 - * @return string
642 - */
643 - public function getCacheDir()
644 - {
645 - if ($this->cacheDir === null) {
646 - $this->setCacheDir(dirname(__FILE__).DIRECTORY_SEPARATOR.'cache'.DIRECTORY_SEPARATOR);
647 - }
648 -
649 - return $this->cacheDir;
650 - }
651 -
652 - /**
653 - * sets the cache directory and automatically appends a DIRECTORY_SEPARATOR
654 - *
655 - * @param string $dir the cache directory
656 - */
657 - public function setCacheDir($dir)
658 - {
659 - $this->cacheDir = rtrim($dir, '/\\').DIRECTORY_SEPARATOR;
660 - if (is_writable($this->cacheDir) === false) {
661 - throw new Dwoo_Exception('The cache directory must be writable, chmod "'.$this->cacheDir.'" to make it writable');
662 - }
663 - }
664 -
665 - /**
666 - * returns the compile directory with a trailing DIRECTORY_SEPARATOR
667 - *
668 - * @return string
669 - */
670 - public function getCompileDir()
671 - {
672 - if ($this->compileDir === null) {
673 - $this->setCompileDir(dirname(__FILE__).DIRECTORY_SEPARATOR.'compiled'.DIRECTORY_SEPARATOR);
674 - }
675 -
676 - return $this->compileDir;
677 - }
678 -
679 - /**
680 - * sets the compile directory and automatically appends a DIRECTORY_SEPARATOR
681 - *
682 - * @param string $dir the compile directory
683 - */
684 - public function setCompileDir($dir)
685 - {
686 - $this->compileDir = rtrim($dir, '/\\').DIRECTORY_SEPARATOR;
687 - if (is_writable($this->compileDir) === false) {
688 - throw new Dwoo_Exception('The compile directory must be writable, chmod "'.$this->compileDir.'" to make it writable');
689 - }
690 - }
691 -
692 - /**
693 - * returns the default cache time that is used with templates that do not have a cache time set
694 - *
695 - * @return int the duration in seconds
696 - */
697 - public function getCacheTime()
698 - {
699 - return $this->cacheTime;
700 - }
701 -
702 - /**
703 - * sets the default cache time to use with templates that do not have a cache time set
704 - *
705 - * @param int $seconds the duration in seconds
706 - */
707 - public function setCacheTime($seconds)
708 - {
709 - $this->cacheTime = (int) $seconds;
710 - }
711 -
712 - /**
713 - * returns the character set used by the string manipulation plugins
714 - *
715 - * the charset is automatically lowercased
716 - *
717 - * @return string
718 - */
719 - public function getCharset()
720 - {
721 - return $this->charset;
722 - }
723 -
724 - /**
725 - * sets the character set used by the string manipulation plugins
726 - *
727 - * the charset will be automatically lowercased
728 - *
729 - * @param string $charset the character set
730 - */
731 - public function setCharset($charset)
732 - {
733 - $this->charset = strtolower((string) $charset);
734 - }
735 -
736 - /**
737 - * returns the current template being rendered, when applicable, or null
738 - *
739 - * @return Dwoo_ITemplate|null
740 - */
741 - public function getTemplate()
742 - {
743 - return $this->template;
744 - }
745 -
746 - /**
747 - * sets the current template being rendered
748 - *
749 - * @param Dwoo_ITemplate $tpl template object
750 - */
751 - public function setTemplate(Dwoo_ITemplate $tpl)
752 - {
753 - $this->template = $tpl;
754 - }
755 -
756 - /**
757 - * sets the default compiler factory function for the given resource name
758 - *
759 - * a compiler factory must return a Dwoo_ICompiler object pre-configured to fit your needs
760 - *
761 - * @param string $resourceName the resource name (i.e. file, string)
762 - * @param callback $compilerFactory the compiler factory callback
763 - */
764 - public function setDefaultCompilerFactory($resourceName, $compilerFactory)
765 - {
766 - $this->resources[$resourceName]['compiler'] = $compilerFactory;
767 - }
768 -
769 - /**
770 - * returns the default compiler factory function for the given resource name
771 - *
772 - * @param string $resourceName the resource name
773 - * @return callback the compiler factory callback
774 - */
775 - public function getDefaultCompilerFactory($resourceName)
776 - {
777 - return $this->resources[$resourceName]['compiler'];
778 - }
779 -
780 - /**
781 - * sets the security policy object to enforce some php security settings
782 - *
783 - * use this if untrusted persons can modify templates
784 - *
785 - * @param Dwoo_Security_Policy $policy the security policy object
786 - */
787 - public function setSecurityPolicy(Dwoo_Security_Policy $policy = null)
788 - {
789 - $this->securityPolicy = $policy;
790 - }
791 -
792 - /**
793 - * returns the current security policy object or null by default
794 - *
795 - * @return Dwoo_Security_Policy|null the security policy object if any
796 - */
797 - public function getSecurityPolicy()
798 - {
799 - return $this->securityPolicy;
800 - }
801 -
802 - /**
803 - * sets the object that must be used as a plugin proxy when plugin can't be found
804 - * by dwoo's loader
805 - *
806 - * @param Dwoo_IPluginProxy $pluginProxy the proxy object
807 - */
808 - public function setPluginProxy(Dwoo_IPluginProxy $pluginProxy) {
809 - $this->pluginProxy = $pluginProxy;
810 - }
811 -
812 - /**
813 - * returns the current plugin proxy object or null by default
814 - *
815 - * @param Dwoo_IPluginProxy|null the proxy object if any
816 - */
817 - public function getPluginProxy() {
818 - return $this->pluginProxy;
819 - }
820 -
821 - /*
822 - * --------- util functions ---------
823 - */
824 -
825 - /**
826 - * [util function] checks whether the given template is cached or not
827 - *
828 - * @param Dwoo_ITemplate $tpl the template object
829 - * @return bool
830 - */
831 - public function isCached(Dwoo_ITemplate $tpl)
832 - {
833 - return is_string($tpl->getCachedTemplate($this));
834 - }
835 -
836 - /**
837 - * [util function] clears the cached templates if they are older than the given time
838 - *
839 - * @param int $olderThan minimum time (in seconds) required for a cached template to be cleared
840 - * @return int the amount of templates cleared
841 - */
842 - public function clearCache($olderThan=-1)
843 - {
844 - $cacheDirs = new RecursiveDirectoryIterator($this->getCacheDir());
845 - $cache = new RecursiveIteratorIterator($cacheDirs);
846 - $expired = time() - $olderThan;
847 - $count = 0;
848 - foreach ($cache as $file) {
849 - if ($cache->isDot() || $cache->isDir() || substr($file, -5) !== '.html') {
850 - continue;
851 - }
852 - if ($cache->getCTime() < $expired) {
853 - $count += unlink((string) $file) ? 1 : 0;
854 - }
855 - }
856 - return $count;
857 - }
858 -
859 - /**
860 - * [util function] fetches a template object of the given resource
861 - *
862 - * @param string $resourceName the resource name (i.e. file, string)
863 - * @param string $resourceId the resource identifier (i.e. file path)
864 - * @param int $cacheTime the cache time setting for this resource
865 - * @param string $cacheId the unique cache identifier
866 - * @param string $compileId the unique compiler identifier
867 - * @return Dwoo_ITemplate
868 - */
869 - public function templateFactory($resourceName, $resourceId, $cacheTime = null, $cacheId = null, $compileId = null, Dwoo_ITemplate $parentTemplate = null)
870 - {
871 - if (isset($this->resources[$resourceName])) {
872 - // TODO could be changed to $this->resources[$resourceName]['class']::templateFactory(..) in 5.3 maybe
873 - return call_user_func(array($this->resources[$resourceName]['class'], 'templateFactory'), $this, $resourceId, $cacheTime, $cacheId, $compileId, $parentTemplate);
874 - } else {
875 - throw new Dwoo_Exception('Unknown resource type : '.$resourceName);
876 - }
877 - }
878 -
879 - /**
880 - * [util function] checks if the input is an array or arrayaccess object, optionally it can also check if it's empty
881 - *
882 - * @param mixed $value the variable to check
883 - * @param bool $checkIsEmpty if true, the function will also check if the array|arrayaccess is empty,
884 - * and return true only if it's not empty
885 - * @return int|bool true if it's an array|arrayaccess (or the item count if $checkIsEmpty is true) or false if it's not an array|arrayaccess (or 0 if $checkIsEmpty is true)
886 - */
887 - public function isArray($value, $checkIsEmpty=false)
888 - {
889 - if (is_array($value) === true || $value instanceof ArrayAccess) {
890 - if ($checkIsEmpty === false) {
891 - return true;
892 - } else {
893 - return $this->count($value);
894 - }
895 - }
896 - }
897 -
898 - /**
899 - * [util function] checks if the input is an array or a traversable object, optionally it can also check if it's empty
900 - *
901 - * @param mixed $value the variable to check
902 - * @param bool $checkIsEmpty if true, the function will also check if the array|traversable is empty,
903 - * and return true only if it's not empty
904 - * @return int|bool true if it's an array|traversable (or the item count if $checkIsEmpty is true) or false if it's not an array|traversable (or 0 if $checkIsEmpty is true)
905 - */
906 - public function isTraversable($value, $checkIsEmpty=false)
907 - {
908 - if (is_array($value) === true) {
909 - if ($checkIsEmpty === false) {
910 - return true;
911 - } else {
912 - return count($value) > 0;
913 - }
914 - } elseif ($value instanceof Traversable) {
915 - if ($checkIsEmpty === false) {
916 - return true;
917 - } else {
918 - return $this->count($value);
919 - }
920 - }
921 - return false;
922 - }
923 -
924 - /**
925 - * [util function] counts an array or arrayaccess/traversable object
926 - * @param mixed $value
927 - * @return int|bool the count for arrays and objects that implement countable, true for other objects that don't, and 0 for empty elements
928 - */
929 - public function count($value)
930 - {
931 - if (is_array($value) === true || $value instanceof Countable) {
932 - return count($value);
933 - } elseif ($value instanceof ArrayAccess) {
934 - if ($value->offsetExists(0)) {
935 - return true;
936 - }
937 - } elseif ($value instanceof Iterator) {
938 - $value->rewind();
939 - if ($value->valid()) {
940 - return true;
941 - }
942 - } elseif ($value instanceof Traversable) {
943 - foreach ($value as $dummy) {
944 - return true;
945 - }
946 - }
947 - return 0;
948 - }
949 -
950 - /**
951 - * [util function] triggers a dwoo error
952 - *
953 - * @param string $message the error message
954 - * @param int $level the error level, one of the PHP's E_* constants
955 - */
956 - public function triggerError($message, $level=E_USER_NOTICE)
957 - {
958 - if (!($tplIdentifier = $this->template->getResourceIdentifier())) {
959 - $tplIdentifier = $this->template->getResourceName();
960 - }
961 - trigger_error('Dwoo error (in '.$tplIdentifier.') : '.$message, $level);
962 - }
963 -
964 - /*
965 - * --------- runtime functions ---------
966 - */
967 -
968 - /**
969 - * [runtime function] adds a block to the block stack
970 - *
971 - * @param string $blockName the block name (without Dwoo_Plugin_ prefix)
972 - * @param array $args the arguments to be passed to the block's init() function
973 - * @return Dwoo_Block_Plugin the newly created block
974 - */
975 - public function addStack($blockName, array $args=array())
976 - {
977 - if (isset($this->plugins[$blockName])) {
978 - $class = $this->plugins[$blockName]['class'];
979 - } else {
980 - $class = 'Dwoo_Plugin_'.$blockName;
981 - }
982 -
983 - if ($this->curBlock !== null) {
984 - $this->curBlock->buffer(ob_get_contents());
985 - ob_clean();
986 - } else {
987 - $this->buffer .= ob_get_contents();
988 - ob_clean();
989 - }
990 -
991 - $block = new $class($this);
992 -
993 - $cnt = count($args);
994 - if ($cnt===0) {
995 - $block->init();
996 - } elseif ($cnt===1) {
997 - $block->init($args[0]);
998 - } elseif ($cnt===2) {
999 - $block->init($args[0], $args[1]);
1000 - } elseif ($cnt===3) {
1001 - $block->init($args[0], $args[1], $args[2]);
1002 - } elseif ($cnt===4) {
1003 - $block->init($args[0], $args[1], $args[2], $args[3]);
1004 - } else {
1005 - call_user_func_array(array($block,'init'), $args);
1006 - }
1007 -
1008 - $this->stack[] = $this->curBlock = $block;
1009 - return $block;
1010 - }
1011 -
1012 - /**
1013 - * [runtime function] removes the plugin at the top of the block stack
1014 - *
1015 - * calls the block buffer() function, followed by a call to end()
1016 - * and finally a call to process()
1017 - */
1018 - public function delStack()
1019 - {
1020 - $args = func_get_args();
1021 -
1022 - $this->curBlock->buffer(ob_get_contents());
1023 - ob_clean();
1024 -
1025 - $cnt = count($args);
1026 - if ($cnt===0) {
1027 - $this->curBlock->end();
1028 - } elseif ($cnt===1) {
1029 - $this->curBlock->end($args[0]);
1030 - } elseif ($cnt===2) {
1031 - $this->curBlock->end($args[0], $args[1]);
1032 - } elseif ($cnt===3) {
1033 - $this->curBlock->end($args[0], $args[1], $args[2]);
1034 - } elseif ($cnt===4) {
1035 - $this->curBlock->end($args[0], $args[1], $args[2], $args[3]);
1036 - } else {
1037 - call_user_func_array(array($this->curBlock, 'end'), $args);
1038 - }
1039 -
1040 - $tmp = array_pop($this->stack);
1041 -
1042 - if (count($this->stack) > 0) {
1043 - $this->curBlock = end($this->stack);
1044 - $this->curBlock->buffer($tmp->process());
1045 - } else {
1046 - $this->curBlock = null;
1047 - echo $tmp->process();
1048 - }
1049 -
1050 - unset($tmp);
1051 - }
1052 -
1053 - /**
1054 - * [runtime function] returns the parent block of the given block
1055 - *
1056 - * @param Dwoo_Block_Plugin $block
1057 - * @return Dwoo_Block_Plugin or false if the given block isn't in the stack
1058 - */
1059 - public function getParentBlock(Dwoo_Block_Plugin $block)
1060 - {
1061 - $index = array_search($block, $this->stack, true);
1062 - if ($index !== false && $index > 0) {
1063 - return $this->stack[$index-1];
1064 - }
1065 - return false;
1066 - }
1067 -
1068 - /**
1069 - * [runtime function] finds the closest block of the given type, starting at the top of the stack
1070 - *
1071 - * @param string $type the type of plugin you want to find
1072 - * @return Dwoo_Block_Plugin or false if no plugin of such type is in the stack
1073 - */
1074 - public function findBlock($type)
1075 - {
1076 - if (isset($this->plugins[$type])) {
1077 - $type = $this->plugins[$type]['class'];
1078 - } else {
1079 - $type = 'Dwoo_Plugin_'.str_replace('Dwoo_Plugin_', '', $type);
1080 - }
1081 -
1082 - $keys = array_keys($this->stack);
1083 - while (($key = array_pop($keys)) !== false) {
1084 - if ($this->stack[$key] instanceof $type) {
1085 - return $this->stack[$key];
1086 - }
1087 - }
1088 - return false;
1089 - }
1090 -
1091 - /**
1092 - * [runtime function] returns a Dwoo_Plugin of the given class
1093 - *
1094 - * this is so a single instance of every class plugin is created at each template run,
1095 - * allowing class plugins to have "per-template-run" static variables
1096 - *
1097 - * @private
1098 - * @param string $class the class name
1099 - * @return mixed an object of the given class
1100 - */
1101 - public function getObjectPlugin($class)
1102 - {
1103 - if (isset($this->runtimePlugins[$class])) {
1104 - return $this->runtimePlugins[$class];
1105 - }
1106 - return $this->runtimePlugins[$class] = new $class($this);
1107 - }
1108 -
1109 - /**
1110 - * [runtime function] calls the process() method of the given class-plugin name
1111 - *
1112 - * @param string $plugName the class plugin name (without Dwoo_Plugin_ prefix)
1113 - * @param array $params an array of parameters to send to the process() method
1114 - * @return string the process() return value
1115 - */
1116 - public function classCall($plugName, array $params = array())
1117 - {
1118 - $class = 'Dwoo_Plugin_'.$plugName;
1119 -
1120 - $plugin = $this->getObjectPlugin($class);
1121 -
1122 - $cnt = count($params);
1123 - if ($cnt===0) {
1124 - return $plugin->process();
1125 - } elseif ($cnt===1) {
1126 - return $plugin->process($params[0]);
1127 - } elseif ($cnt===2) {
1128 - return $plugin->process($params[0], $params[1]);
1129 - } elseif ($cnt===3) {
1130 - return $plugin->process($params[0], $params[1], $params[2]);
1131 - } elseif ($cnt===4) {
1132 - return $plugin->process($params[0], $params[1], $params[2], $params[3]);
1133 - } else {
1134 - return call_user_func_array(array($plugin, 'process'), $params);
1135 - }
1136 - }
1137 -
1138 - /**
1139 - * [runtime function] calls a php function
1140 - *
1141 - * @param string $callback the function to call
1142 - * @param array $params an array of parameters to send to the function
1143 - * @return mixed the return value of the called function
1144 - */
1145 - public function arrayMap($callback, array $params)
1146 - {
1147 - if ($params[0] === $this) {
1148 - $addThis = true;
1149 - array_shift($params);
1150 - }
1151 - if ((is_array($params[0]) || ($params[0] instanceof Iterator && $params[0] instanceof ArrayAccess))) {
1152 - if (empty($params[0])) {
1153 - return $params[0];
1154 - }
1155 -
1156 - // array map
1157 - $out = array();
1158 - $cnt = count($params);
1159 -
1160 - if (isset($addThis)) {
1161 - array_unshift($params, $this);
1162 - $items = $params[1];
1163 - $keys = array_keys($items);
1164 -
1165 - if (is_string($callback) === false) {
1166 - while (($i = array_shift($keys)) !== null) {
1167 - $out[] = call_user_func_array($callback, array(1=>$items[$i]) + $params);
1168 - }
1169 - } elseif ($cnt===1) {
1170 - while (($i = array_shift($keys)) !== null) {
1171 - $out[] = $callback($this, $items[$i]);
1172 - }
1173 - } elseif ($cnt===2) {
1174 - while (($i = array_shift($keys)) !== null) {
1175 - $out[] = $callback($this, $items[$i], $params[2]);
1176 - }
1177 - } elseif ($cnt===3) {
1178 - while (($i = array_shift($keys)) !== null) {
1179 - $out[] = $callback($this, $items[$i], $params[2], $params[3]);
1180 - }
1181 - } else {
1182 - while (($i = array_shift($keys)) !== null) {
1183 - $out[] = call_user_func_array($callback, array(1=>$items[$i]) + $params);
1184 - }
1185 - }
1186 - } else {
1187 - $items = $params[0];
1188 - $keys = array_keys($items);
1189 -
1190 - if (is_string($callback) === false) {
1191 - while (($i = array_shift($keys)) !== null) {
1192 - $out[] = call_user_func_array($callback, array($items[$i]) + $params);
1193 - }
1194 - } elseif ($cnt===1) {
1195 - while (($i = array_shift($keys)) !== null) {
1196 - $out[] = $callback($items[$i]);
1197 - }
1198 - } elseif ($cnt===2) {
1199 - while (($i = array_shift($keys)) !== null) {
1200 - $out[] = $callback($items[$i], $params[1]);
1201 - }
1202 - } elseif ($cnt===3) {
1203 - while (($i = array_shift($keys)) !== null) {
1204 - $out[] = $callback($items[$i], $params[1], $params[2]);
1205 - }
1206 - } elseif ($cnt===4) {
1207 - while (($i = array_shift($keys)) !== null) {
1208 - $out[] = $callback($items[$i], $params[1], $params[2], $params[3]);
1209 - }
1210 - } else {
1211 - while (($i = array_shift($keys)) !== null) {
1212 - $out[] = call_user_func_array($callback, array($items[$i]) + $params);
1213 - }
1214 - }
1215 - }
1216 - return $out;
1217 - } else {
1218 - return $params[0];
1219 - }
1220 - }
1221 -
1222 - /**
1223 - * [runtime function] reads a variable into the given data array
1224 - *
1225 - * @param string $varstr the variable string, using dwoo variable syntax (i.e. "var.subvar[subsubvar]->property")
1226 - * @param mixed $data the data array or object to read from
1227 - * @param bool $safeRead if true, the function will check whether the index exists to prevent any notices from being output
1228 - * @return mixed
1229 - */
1230 - public function readVarInto($varstr, $data, $safeRead = false)
1231 - {
1232 - if ($data === null) {
1233 - return null;
1234 - }
1235 -
1236 - if (is_array($varstr) === false) {
1237 - preg_match_all('#(\[|->|\.)?((?:[^.[\]-]|-(?!>))+)\]?#i', $varstr, $m);
1238 - } else {
1239 - $m = $varstr;
1240 - }
1241 - unset($varstr);
1242 -
1243 - while (list($k, $sep) = each($m[1])) {
1244 - if ($sep === '.' || $sep === '[' || $sep === '') {
1245 - if ((is_array($data) || $data instanceof ArrayAccess) && ($safeRead === false || isset($data[$m[2][$k]]))) {
1246 - $data = $data[$m[2][$k]];
1247 - } else {
1248 - return null;
1249 - }
1250 - } else {
1251 - if (is_object($data) && ($safeRead === false || isset($data->$m[2][$k]) || is_callable(array($data, '__get')))) {
1252 - $data = $data->$m[2][$k];
1253 - } else {
1254 - return null;
1255 - }
1256 - }
1257 - }
1258 -
1259 - return $data;
1260 - }
1261 -
1262 - /**
1263 - * [runtime function] reads a variable into the parent scope
1264 - *
1265 - * @param int $parentLevels the amount of parent levels to go from the current scope
1266 - * @param string $varstr the variable string, using dwoo variable syntax (i.e. "var.subvar[subsubvar]->property")
1267 - * @return mixed
1268 - */
1269 - public function readParentVar($parentLevels, $varstr = null)
1270 - {
1271 - $tree = $this->scopeTree;
1272 - $cur = $this->data;
1273 -
1274 - while ($parentLevels--!==0) {
1275 - array_pop($tree);
1276 - }
1277 -
1278 - while (($i = array_shift($tree)) !== null) {
1279 - if (is_object($cur)) {
1280 - $cur = $cur->$i;
1281 - } else {
1282 - $cur = $cur[$i];
1283 - }
1284 - }
1285 -
1286 - if ($varstr!==null) {
1287 - return $this->readVarInto($varstr, $cur);
1288 - } else {
1289 - return $cur;
1290 - }
1291 - }
1292 -
1293 - /**
1294 - * [runtime function] reads a variable into the current scope
1295 - *
1296 - * @param string $varstr the variable string, using dwoo variable syntax (i.e. "var.subvar[subsubvar]->property")
1297 - * @return mixed
1298 - */
1299 - public function readVar($varstr)
1300 - {
1301 - if (is_array($varstr)===true) {
1302 - $m = $varstr;
1303 - unset($varstr);
1304 - } else {
1305 - if (strstr($varstr, '.') === false && strstr($varstr, '[') === false && strstr($varstr, '->') === false) {
1306 - if ($varstr === 'dwoo') {
1307 - return $this->globals;
1308 - } elseif ($varstr === '__' || $varstr === '_root' ) {
1309 - return $this->data;
1310 - $varstr = substr($varstr, 6);
1311 - } elseif ($varstr === '_' || $varstr === '_parent') {
1312 - $varstr = '.'.$varstr;
1313 - $tree = $this->scopeTree;
1314 - $cur = $this->data;
1315 - array_pop($tree);
1316 -
1317 - while (($i = array_shift($tree)) !== null) {
1318 - if (is_object($cur)) {
1319 - $cur = $cur->$i;
1320 - } else {
1321 - $cur = $cur[$i];
1322 - }
1323 - }
1324 -
1325 - return $cur;
1326 - }
1327 -
1328 - $cur = $this->scope;
1329 -
1330 - if (isset($cur[$varstr])) {
1331 - return $cur[$varstr];
1332 - } else {
1333 - return null;
1334 - }
1335 - }
1336 -
1337 - if (substr($varstr, 0, 1) === '.') {
1338 - $varstr = 'dwoo'.$varstr;
1339 - }
1340 -
1341 - preg_match_all('#(\[|->|\.)?((?:[^.[\]-]|-(?!>))+)\]?#i', $varstr, $m);
1342 - }
1343 -
1344 - $i = $m[2][0];
1345 - if ($i === 'dwoo') {
1346 - $cur = $this->globals;
1347 - array_shift($m[2]);
1348 - array_shift($m[1]);
1349 - switch ($m[2][0]) {
1350 -
1351 - case 'get':
1352 - $cur = $_GET;
1353 - break;
1354 - case 'post':
1355 - $cur = $_POST;
1356 - break;
1357 - case 'session':
1358 - $cur = $_SESSION;
1359 - break;
1360 - case 'cookies':
1361 - case 'cookie':
1362 - $cur = $_COOKIE;
1363 - break;
1364 - case 'server':
1365 - $cur = $_SERVER;
1366 - break;
1367 - case 'env':
1368 - $cur = $_ENV;
1369 - break;
1370 - case 'request':
1371 - $cur = $_REQUEST;
1372 - break;
1373 - case 'const':
1374 - array_shift($m[2]);
1375 - if (defined($m[2][0])) {
1376 - return constant($m[2][0]);
1377 - } else {
1378 - return null;
1379 - }
1380 -
1381 - }
1382 - if ($cur !== $this->globals) {
1383 - array_shift($m[2]);
1384 - array_shift($m[1]);
1385 - }
1386 - } elseif ($i === '__' || $i === '_root') {
1387 - $cur = $this->data;
1388 - array_shift($m[2]);
1389 - array_shift($m[1]);
1390 - } elseif ($i === '_' || $i === '_parent') {
1391 - $tree = $this->scopeTree;
1392 - $cur = $this->data;
1393 -
1394 - while (true) {
1395 - array_pop($tree);
1396 - array_shift($m[2]);
1397 - array_shift($m[1]);
1398 - if (current($m[2]) === '_' || current($m[2]) === '_parent') {
1399 - continue;
1400 - }
1401 -
1402 - while (($i = array_shift($tree)) !== null) {
1403 - if (is_object($cur)) {
1404 - $cur = $cur->$i;
1405 - } else {
1406 - $cur = $cur[$i];
1407 - }
1408 - }
1409 - break;
1410 - }
1411 - } else {
1412 - $cur = $this->scope;
1413 - }
1414 -
1415 - while (list($k, $sep) = each($m[1])) {
1416 - if ($sep === '.' || $sep === '[' || $sep === '') {
1417 - if ((is_array($cur) || $cur instanceof ArrayAccess) && isset($cur[$m[2][$k]])) {
1418 - $cur = $cur[$m[2][$k]];
1419 - } else {
1420 - return null;
1421 - }
1422 - } elseif ($sep === '->') {
1423 - if (is_object($cur)) {
1424 - $cur = $cur->$m[2][$k];
1425 - } else {
1426 - return null;
1427 - }
1428 - } else {
1429 - return null;
1430 - }
1431 - }
1432 -
1433 - return $cur;
1434 - }
1435 -
1436 - /**
1437 - * [runtime function] assign the value to the given variable
1438 - *
1439 - * @param mixed $value the value to assign
1440 - * @param string $scope the variable string, using dwoo variable syntax (i.e. "var.subvar[subsubvar]->property")
1441 - * @return bool true if assigned correctly or false if a problem occured while parsing the var string
1442 - */
1443 - public function assignInScope($value, $scope)
1444 - {
1445 - $tree =& $this->scopeTree;
1446 - $data =& $this->data;
1447 -
1448 - if (!is_string($scope)) {
1449 - return $this->triggerError('Assignments must be done into strings, ('.gettype($scope).') '.var_export($scope, true).' given', E_USER_ERROR);
1450 - }
1451 - if (strstr($scope, '.') === false && strstr($scope, '->') === false) {
1452 - $this->scope[$scope] = $value;
1453 - } else {
1454 - // TODO handle _root/_parent scopes ?
1455 - preg_match_all('#(\[|->|\.)?([^.[\]-]+)\]?#i', $scope, $m);
1456 -
1457 - $cur =& $this->scope;
1458 - $last = array(array_pop($m[1]), array_pop($m[2]));
1459 -
1460 - while (list($k, $sep) = each($m[1])) {
1461 - if ($sep === '.' || $sep === '[' || $sep === '') {
1462 - if (is_array($cur) === false) {
1463 - $cur = array();
1464 - }
1465 - $cur =& $cur[$m[2][$k]];
1466 - } elseif ($sep === '->') {
1467 - if (is_object($cur) === false) {
1468 - $cur = new stdClass;
1469 - }
1470 - $cur =& $cur->$m[2][$k];
1471 - } else {
1472 - return false;
1473 - }
1474 - }
1475 -
1476 - if ($last[0] === '.' || $last[0] === '[' || $last[0] === '') {
1477 - if (is_array($cur) === false) {
1478 - $cur = array();
1479 - }
1480 - $cur[$last[1]] = $value;
1481 - } elseif ($last[0] === '->') {
1482 - if (is_object($cur) === false) {
1483 - $cur = new stdClass;
1484 - }
1485 - $cur->$last[1] = $value;
1486 - } else {
1487 - return false;
1488 - }
1489 - }
1490 - }
1491 -
1492 - /**
1493 - * [runtime function] sets the scope to the given scope string or array
1494 - *
1495 - * @param mixed $scope a string i.e. "level1.level2" or an array i.e. array("level1", "level2")
1496 - * @param bool $absolute if true, the scope is set from the top level scope and not from the current scope
1497 - * @return array the current scope tree
1498 - */
1499 - public function setScope($scope, $absolute = false)
1500 - {
1501 - $old = $this->scopeTree;
1502 -
1503 - if (is_string($scope)===true) {
1504 - $scope = explode('.', $scope);
1505 - }
1506 -
1507 - if ($absolute===true) {
1508 - $this->scope =& $this->data;
1509 - $this->scopeTree = array();
1510 - }
1511 -
1512 - while (($bit = array_shift($scope)) !== null) {
1513 - if ($bit === '_' || $bit === '_parent') {
1514 - array_pop($this->scopeTree);
1515 - $this->scope =& $this->data;
1516 - $cnt = count($this->scopeTree);
1517 - for ($i=0;$i<$cnt;$i++)
1518 - $this->scope =& $this->scope[$this->scopeTree[$i]];
1519 - } elseif ($bit === '__' || $bit === '_root') {
1520 - $this->scope =& $this->data;
1521 - $this->scopeTree = array();
1522 - } elseif (isset($this->scope[$bit])) {
1523 - $this->scope =& $this->scope[$bit];
1524 - $this->scopeTree[] = $bit;
1525 - } else {
1526 - unset($this->scope);
1527 - $this->scope = null;
1528 - }
1529 - }
1530 -
1531 - return $old;
1532 - }
1533 -
1534 - /**
1535 - * [runtime function] returns the entire data array
1536 - *
1537 - * @return array
1538 - */
1539 - public function getData()
1540 - {
1541 - return $this->data;
1542 - }
1543 -
1544 - /**
1545 - * [runtime function] returns a reference to the current scope
1546 - *
1547 - * @return &mixed
1548 - */
1549 - public function &getScope()
1550 - {
1551 - return $this->scope;
1552 - }
1553 -
1554 - /**
1555 - * Redirects all calls to unexisting to plugin proxy.
1556 - *
1557 - * @param string Method name
1558 - * @param array List of arguments
1559 - * @return mixed
1560 - */
1561 - public function __call($method, $args) {
1562 - $proxy = $this->getPluginProxy();
1563 - if (!$proxy) {
1564 - throw new Dwoo_Exception('Call to undefined method '.__CLASS__.'::'.$method.'()');
1565 - }
1566 - return call_user_func_array($proxy->getCallback($method), $args);
1567 - }
1568 -}
21 +}
495dc1 9315dc a/lib/Dwoo/Adapters/Agavi/DwooRenderer.php
147 147 $cacheDir = AgaviConfig::get('core.cache_dir') . DIRECTORY_SEPARATOR . self::CACHE_DIR;
148 148 AgaviToolkit::mkdir($cacheDir, $parentMode, true);
149 149
150 - $this->dwoo = new Dwoo($compileDir, $cacheDir);
150 + $this->dwoo = new Dwoo_Core($compileDir, $cacheDir);
151 151
152 152 if (!empty($this->plugin_dir)) {
153 153 $this->dwoo->getLoader()->addDirectory($this->plugin_dir);
495dc1 9315dc a/lib/Dwoo/Adapters/CakePHP/dwoo.php
60 60 $this->_sv_compile_dir = TMP . 'dwoo' . DS . 'compile';
61 61 $this->_sv_cache_dir = TMP . 'dwoo' . DS . 'cache';
62 62
63 - $this->_dwoo = new Dwoo($this->_sv_compile_dir, $this->_sv_cache_dir);
63 + $this->_dwoo = new Dwoo_Core($this->_sv_compile_dir, $this->_sv_cache_dir);
64 64
65 65 $this->_sv_compile_id = $controller->name;
66 66
495dc1 9315dc a/lib/Dwoo/Adapters/CodeIgniter/libraries/Dwootemplate.php
20 20 *
21 21 * @uses the dwoo package from http://dwoo.org
22 22 */
23 -class Dwootemplate extends Dwoo {
23 +class Dwootemplate extends Dwoo_Core {
24 24 protected $dwoo_data = array();
25 25
26 26 /**
495dc1 9315dc a/lib/Dwoo/Adapters/ZendFramework/Dwoo.php
17 17 * @date 2010-02-28
18 18 * @package Dwoo
19 19 */
20 -class Dwoo_Adapters_ZendFramework_Dwoo extends Dwoo
20 +class Dwoo_Adapters_ZendFramework_Dwoo extends Dwoo_Core
21 21 {
22 22 /**
23 23 * Redirects all unknown properties to plugin proxy
495dc1 9315dc a/lib/Dwoo/Adapters/ZendFramework/View.php
145 145 * Binds plugin proxy to the Dwoo.
146 146 *
147 147 * @see Dwoo_Adapters_ZendFramework_View::getPluginProxy();
148 - * @see Dwoo::setPluginProxy();
148 + * @see Dwoo_Core::setPluginProxy();
149 149 */
150 150 protected function preRender()
151 151 {
495dc1 9315dc a/lib/Dwoo/Compiler.php
490 490 *
491 491 * @private
492 492 * @param string $name function name
493 - * @param int $type plugin type (Dwoo::*_PLUGIN)
493 + * @param int $type plugin type (Dwoo_Core::*_PLUGIN)
494 494 */
495 495 public function addUsedPlugin($name, $type)
496 496 {
550 550 /**
551 551 * adds the custom plugins loaded into Dwoo to the compiler so it can load them
552 552 *
553 - * @see Dwoo::addPlugin
553 + * @see Dwoo_Core::addPlugin
554 554 * @param array $customPlugins an array of custom plugins
555 555 */
556 556 public function setCustomPlugins(array $customPlugins)
691 691 * @param string $tpl the template to compile
692 692 * @return string a compiled php string
693 693 */
694 - public function compile(Dwoo $dwoo, Dwoo_ITemplate $template)
694 + public function compile(Dwoo_Core $dwoo, Dwoo_ITemplate $template)
695 695 {
696 696 // init vars
697 697 $tpl = $template->getSource();
854 854
855 855 // build plugin preloader
856 856 foreach ($this->usedPlugins as $plugin=>$type) {
857 - if ($type & Dwoo::CUSTOM_PLUGIN) {
857 + if ($type & Dwoo_Core::CUSTOM_PLUGIN) {
858 858 continue;
859 859 }
860 860
861 861 switch($type) {
862 862
863 - case Dwoo::BLOCK_PLUGIN:
864 - case Dwoo::CLASS_PLUGIN:
863 + case Dwoo_Core::BLOCK_PLUGIN:
864 + case Dwoo_Core::CLASS_PLUGIN:
865 865 $output .= "if (class_exists('Dwoo_Plugin_$plugin', false)===false)\n\t\$this->getLoader()->loadPlugin('$plugin');\n";
866 866 break;
867 - case Dwoo::FUNC_PLUGIN:
867 + case Dwoo_Core::FUNC_PLUGIN:
868 868 $output .= "if (function_exists('Dwoo_Plugin_$plugin')===false)\n\t\$this->getLoader()->loadPlugin('$plugin');\n";
869 869 break;
870 - case Dwoo::SMARTY_MODIFIER:
870 + case Dwoo_Core::SMARTY_MODIFIER:
871 871 $output .= "if (function_exists('smarty_modifier_$plugin')===false)\n\t\$this->getLoader()->loadPlugin('$plugin');\n";
872 872 break;
873 - case Dwoo::SMARTY_FUNCTION:
873 + case Dwoo_Core::SMARTY_FUNCTION:
874 874 $output .= "if (function_exists('smarty_function_$plugin')===false)\n\t\$this->getLoader()->loadPlugin('$plugin');\n";
875 875 break;
876 - case Dwoo::SMARTY_BLOCK:
876 + case Dwoo_Core::SMARTY_BLOCK:
877 877 $output .= "if (function_exists('smarty_block_$plugin')===false)\n\t\$this->getLoader()->loadPlugin('$plugin');\n";
878 878 break;
879 - case Dwoo::PROXY_PLUGIN:
879 + case Dwoo_Core::PROXY_PLUGIN:
880 880 $output .= $this->getDwoo()->getPluginProxy()->getPreloader($plugin);
881 881 break;
882 882 default:
1091 1091 $output = '';
1092 1092
1093 1093 $pluginType = $this->getPluginType($type);
1094 - if ($pluginType & Dwoo::SMARTY_BLOCK) {
1094 + if ($pluginType & Dwoo_Core::SMARTY_BLOCK) {
1095 1095 $type = 'smartyinterface';
1096 1096 }
1097 1097 while (true) {
1707 1707 }
1708 1708
1709 1709 if ($curBlock === 'method' || $func === 'do' || strstr($func, '::') !== false) {
1710 - $pluginType = Dwoo::NATIVE_PLUGIN;
1710 + $pluginType = Dwoo_Core::NATIVE_PLUGIN;
1711 1711 } else {
1712 1712 $pluginType = $this->getPluginType($func);
1713 1713 }
1714 1714
1715 1715 // blocks
1716 - if ($pluginType & Dwoo::BLOCK_PLUGIN) {
1716 + if ($pluginType & Dwoo_Core::BLOCK_PLUGIN) {
1717 1717 if ($curBlock !== 'root' || is_array($parsingParams)) {
1718 1718 throw new Dwoo_Compilation_Exception($this, 'Block plugins can not be used as other plugin\'s arguments');
1719 1719 }
1720 - if ($pluginType & Dwoo::CUSTOM_PLUGIN) {
1720 + if ($pluginType & Dwoo_Core::CUSTOM_PLUGIN) {
1721 1721 return $this->addCustomBlock($func, $params, $state);
1722 1722 } else {
1723 1723 return $this->addBlock($func, $params, $state);
1724 1724 }
1725 - } elseif ($pluginType & Dwoo::SMARTY_BLOCK) {
1725 + } elseif ($pluginType & Dwoo_Core::SMARTY_BLOCK) {
1726 1726 if ($curBlock !== 'root' || is_array($parsingParams)) {
1727 1727 throw new Dwoo_Compilation_Exception($this, 'Block plugins can not be used as other plugin\'s arguments');
1728 1728 }
1739 1739 }
1740 1740
1741 1741 // funcs
1742 - if ($pluginType & Dwoo::NATIVE_PLUGIN || $pluginType & Dwoo::SMARTY_FUNCTION || $pluginType & Dwoo::SMARTY_BLOCK) {
1742 + if ($pluginType & Dwoo_Core::NATIVE_PLUGIN || $pluginType & Dwoo_Core::SMARTY_FUNCTION || $pluginType & Dwoo_Core::SMARTY_BLOCK) {
1743 1743 $params = $this->mapParams($params, null, $state);
1744 - } elseif ($pluginType & Dwoo::CLASS_PLUGIN) {
1745 - if ($pluginType & Dwoo::CUSTOM_PLUGIN) {
1744 + } elseif ($pluginType & Dwoo_Core::CLASS_PLUGIN) {
1745 + if ($pluginType & Dwoo_Core::CUSTOM_PLUGIN) {
1746 1746 $params = $this->mapParams($params, array($this->customPlugins[$func]['class'], $this->customPlugins[$func]['function']), $state);
1747 1747 } else {
1748 - $params = $this->mapParams($params, array('Dwoo_Plugin_'.$func, ($pluginType & Dwoo::COMPILABLE_PLUGIN) ? 'compile' : 'process'), $state);
1748 + $params = $this->mapParams($params, array('Dwoo_Plugin_'.$func, ($pluginType & Dwoo_Core::COMPILABLE_PLUGIN) ? 'compile' : 'process'), $state);
1749 1749 }
1750 - } elseif ($pluginType & Dwoo::FUNC_PLUGIN) {
1751 - if ($pluginType & Dwoo::CUSTOM_PLUGIN) {
1750 + } elseif ($pluginType & Dwoo_Core::FUNC_PLUGIN) {
1751 + if ($pluginType & Dwoo_Core::CUSTOM_PLUGIN) {
1752 1752 $params = $this->mapParams($params, $this->customPlugins[$func]['callback'], $state);
1753 1753 } else {
1754 - $params = $this->mapParams($params, 'Dwoo_Plugin_'.$func.(($pluginType & Dwoo::COMPILABLE_PLUGIN) ? '_compile' : ''), $state);
1754 + $params = $this->mapParams($params, 'Dwoo_Plugin_'.$func.(($pluginType & Dwoo_Core::COMPILABLE_PLUGIN) ? '_compile' : ''), $state);
1755 1755 }
1756 - } elseif ($pluginType & Dwoo::SMARTY_MODIFIER) {
1756 + } elseif ($pluginType & Dwoo_Core::SMARTY_MODIFIER) {
1757 1757 $output = 'smarty_modifier_'.$func.'('.implode(', ', $params).')';
1758 - } elseif ($pluginType & Dwoo::PROXY_PLUGIN) {
1758 + } elseif ($pluginType & Dwoo_Core::PROXY_PLUGIN) {
1759 1759 $params = $this->mapParams($params, $this->getDwoo()->getPluginProxy()->getCallback($func), $state);
1760 - } elseif ($pluginType & Dwoo::TEMPLATE_PLUGIN) {
1760 + } elseif ($pluginType & Dwoo_Core::TEMPLATE_PLUGIN) {
1761 1761 // transforms the parameter array from (x=>array('paramname'=>array(values))) to (paramname=>array(values))
1762 1762 $map = array();
1763 1763 foreach ($this->templatePlugins[$func]['params'] as $param=>$defValue) {
1786 1786 $tokens[$k] = isset($p[2]) ? $p[2] : 0;
1787 1787 $params[$k] = $p[0];
1788 1788 }
1789 - if ($pluginType & Dwoo::NATIVE_PLUGIN) {
1789 + if ($pluginType & Dwoo_Core::NATIVE_PLUGIN) {
1790 1790 if ($func === 'do') {
1791 1791 if (isset($params['*'])) {
1792 1792 $output = implode(';', $params['*']).';';
1806 1806 $output = $func.'()';
1807 1807 }
1808 1808 }
1809 - } elseif ($pluginType & Dwoo::FUNC_PLUGIN) {
1810 - if ($pluginType & Dwoo::COMPILABLE_PLUGIN) {
1811 - if ($pluginType & Dwoo::CUSTOM_PLUGIN) {
1809 + } elseif ($pluginType & Dwoo_Core::FUNC_PLUGIN) {
1810 + if ($pluginType & Dwoo_Core::COMPILABLE_PLUGIN) {
1811 + if ($pluginType & Dwoo_Core::CUSTOM_PLUGIN) {
1812 1812 $funcCompiler = $this->customPlugins[$func]['callback'];
1813 1813 } else {
1814 1814 $funcCompiler = 'Dwoo_Plugin_'.$func.'_compile';
1822 1822 array_unshift($params, '$this');
1823 1823 $params = self::implode_r($params);
1824 1824
1825 - if ($pluginType & Dwoo::CUSTOM_PLUGIN) {
1825 + if ($pluginType & Dwoo_Core::CUSTOM_PLUGIN) {
1826 1826 $callback = $this->customPlugins[$func]['callback'];
1827 1827 $output = 'call_user_func(\''.$callback.'\', '.$params.')';
1828 1828 } else {
1829 1829 $output = 'Dwoo_Plugin_'.$func.'('.$params.')';
1830 1830 }
1831 1831 }
1832 - } elseif ($pluginType & Dwoo::CLASS_PLUGIN) {
1833 - if ($pluginType & Dwoo::COMPILABLE_PLUGIN) {
1834 - if ($pluginType & Dwoo::CUSTOM_PLUGIN) {
1832 + } elseif ($pluginType & Dwoo_Core::CLASS_PLUGIN) {
1833 + if ($pluginType & Dwoo_Core::COMPILABLE_PLUGIN) {
1834 + if ($pluginType & Dwoo_Core::CUSTOM_PLUGIN) {
1835 1835 $callback = $this->customPlugins[$func]['callback'];
1836 1836 if (!is_array($callback)) {
1837 1837 if (!method_exists($callback, 'compile')) {
1852 1852 $output = call_user_func_array($funcCompiler, $params);
1853 1853 } else {
1854 1854 $params = self::implode_r($params);
1855 - if ($pluginType & Dwoo::CUSTOM_PLUGIN) {
1855 + if ($pluginType & Dwoo_Core::CUSTOM_PLUGIN) {
1856 1856 $callback = $this->customPlugins[$func]['callback'];
1857 1857 if (!is_array($callback)) {
1858 1858 if (!method_exists($callback, 'process')) {
1877 1877 $output = '$this->classCall(\''.$func.'\', array('.$params.'))';
1878 1878 }
1879 1879 }
1880 - } elseif ($pluginType & Dwoo::PROXY_PLUGIN) {
1880 + } elseif ($pluginType & Dwoo_Core::PROXY_PLUGIN) {
1881 1881 $output = call_user_func(array($this->dwoo->getPluginProxy(), 'getCode'), $func, $params);
1882 - } elseif ($pluginType & Dwoo::SMARTY_FUNCTION) {
1882 + } elseif ($pluginType & Dwoo_Core::SMARTY_FUNCTION) {
1883 1883 if (isset($params['*'])) {
1884 1884 $params = self::implode_r($params['*'], true);
1885 1885 } else {
1886 1886 $params = '';
1887 1887 }
1888 1888
1889 - if ($pluginType & Dwoo::CUSTOM_PLUGIN) {
1889 + if ($pluginType & Dwoo_Core::CUSTOM_PLUGIN) {
1890 1890 $callback = $this->customPlugins[$func]['callback'];
1891 1891 if (is_array($callback)) {
1892 1892 if (is_object($callback[0])) {
1900 1900 } else {
1901 1901 $output = 'smarty_function_'.$func.'(array('.$params.'), $this)';
1902 1902 }
1903 - } elseif ($pluginType & Dwoo::TEMPLATE_PLUGIN) {
1903 + } elseif ($pluginType & Dwoo_Core::TEMPLATE_PLUGIN) {
1904 1904 array_unshift($params, '$this');
1905 1905 $params = self::implode_r($params);
1906 1906 $output = 'Dwoo_Plugin_'.$func.'_'.$this->templatePlugins[$func]['uuid'].'('.$params.')';
2751 2751 array_unshift($params, array($output, $output));
2752 2752 }
2753 2753
2754 - if ($pluginType & Dwoo::NATIVE_PLUGIN) {
2754 + if ($pluginType & Dwoo_Core::NATIVE_PLUGIN) {
2755 2755 $params = $this->mapParams($params, null, $state);
2756 2756
2757 2757 $params = $params['*'][0];
2763 2763 } else {
2764 2764 $output = $func.'('.$params.')';
2765 2765 }
2766 - } elseif ($pluginType & Dwoo::PROXY_PLUGIN) {
2766 + } elseif ($pluginType & Dwoo_Core::PROXY_PLUGIN) {
2767 2767 $params = $this->mapParams($params, $this->getDwoo()->getPluginProxy()->getCallback($func), $state);
2768 2768 foreach ($params as &$p)
2769 2769 $p = $p[0];
2770 2770 $output = call_user_func(array($this->dwoo->getPluginProxy(), 'getCode'), $func, $params);
2771 - } elseif ($pluginType & Dwoo::SMARTY_MODIFIER) {
2771 + } elseif ($pluginType & Dwoo_Core::SMARTY_MODIFIER) {
2772 2772 $params = $this->mapParams($params, null, $state);
2773 2773 $params = $params['*'][0];
2774 2774
2775 2775 $params = self::implode_r($params);
2776 2776
2777 - if ($pluginType & Dwoo::CUSTOM_PLUGIN) {
2777 + if ($pluginType & Dwoo_Core::CUSTOM_PLUGIN) {
2778 2778 $callback = $this->customPlugins[$func]['callback'];
2779 2779 if (is_array($callback)) {
2780 2780 if (is_object($callback[0])) {
2793 2793 $output = 'smarty_modifier_'.$func.'('.$params.')';
2794 2794 }
2795 2795 } else {
2796 - if ($pluginType & Dwoo::CUSTOM_PLUGIN) {
2796 + if ($pluginType & Dwoo_Core::CUSTOM_PLUGIN) {
2797 2797 $callback = $this->customPlugins[$func]['callback'];
2798 2798 $pluginName = $callback;
2799 2799 } else {
2800 2800 $pluginName = 'Dwoo_Plugin_'.$func;
2801 2801
2802 - if ($pluginType & Dwoo::CLASS_PLUGIN) {
2803 - $callback = array($pluginName, ($pluginType & Dwoo::COMPILABLE_PLUGIN) ? 'compile' : 'process');
2802 + if ($pluginType & Dwoo_Core::CLASS_PLUGIN) {
2803 + $callback = array($pluginName, ($pluginType & Dwoo_Core::COMPILABLE_PLUGIN) ? 'compile' : 'process');
2804 2804 } else {
2805 - $callback = $pluginName . (($pluginType & Dwoo::COMPILABLE_PLUGIN) ? '_compile' : '');
2805 + $callback = $pluginName . (($pluginType & Dwoo_Core::COMPILABLE_PLUGIN) ? '_compile' : '');
2806 2806 }
2807 2807 }
2808 2808
2811 2811 foreach ($params as &$p)
2812 2812 $p = $p[0];
2813 2813
2814 - if ($pluginType & Dwoo::FUNC_PLUGIN) {
2815 - if ($pluginType & Dwoo::COMPILABLE_PLUGIN) {
2814 + if ($pluginType & Dwoo_Core::FUNC_PLUGIN) {
2815 + if ($pluginType & Dwoo_Core::COMPILABLE_PLUGIN) {
2816 2816 if ($mapped) {
2817 2817 throw new Dwoo_Compilation_Exception($this, 'The @ operator can not be used on compiled plugins.');
2818 2818 }
2819 - if ($pluginType & Dwoo::CUSTOM_PLUGIN) {
2819 + if ($pluginType & Dwoo_Core::CUSTOM_PLUGIN) {
2820 2820 $funcCompiler = $this->customPlugins[$func]['callback'];
2821 2821 } else {
2822 2822 $funcCompiler = 'Dwoo_Plugin_'.$func.'_compile';
2834 2834 }
2835 2835 }
2836 2836 } else {
2837 - if ($pluginType & Dwoo::COMPILABLE_PLUGIN) {
2837 + if ($pluginType & Dwoo_Core::COMPILABLE_PLUGIN) {
2838 2838 if ($mapped) {
2839 2839 throw new Dwoo_Compilation_Exception($this, 'The @ operator can not be used on compiled plugins.');
2840 2840 }
2841 - if ($pluginType & Dwoo::CUSTOM_PLUGIN) {
2841 + if ($pluginType & Dwoo_Core::CUSTOM_PLUGIN) {
2842 2842 $callback = $this->customPlugins[$func]['callback'];
2843 2843 if (!is_array($callback)) {
2844 2844 if (!method_exists($callback, 'compile')) {
2860 2860 } else {
2861 2861 $params = self::implode_r($params);
2862 2862
2863 - if ($pluginType & Dwoo::CUSTOM_PLUGIN) {
2863 + if ($pluginType & Dwoo_Core::CUSTOM_PLUGIN) {
2864 2864 if (is_object($callback[0])) {
2865 2865 $output = ($mapped ? '$this->arrayMap' : 'call_user_func_array').'(array($this->plugins[\''.$func.'\'][\'callback\'][0], \''.$callback[1].'\'), array('.$params.'))';
2866 2866 } else {
2928 2928
2929 2929 while ($pluginType <= 0) {
2930 2930 if (isset($this->templatePlugins[$name])) {
2931 - $pluginType = Dwoo::TEMPLATE_PLUGIN | Dwoo::COMPILABLE_PLUGIN;
2931 + $pluginType = Dwoo_Core::TEMPLATE_PLUGIN | Dwoo_Core::COMPILABLE_PLUGIN;
2932 2932 } elseif (isset($this->customPlugins[$name])) {
2933 - $pluginType = $this->customPlugins[$name]['type'] | Dwoo::CUSTOM_PLUGIN;
2933 + $pluginType = $this->customPlugins[$name]['type'] | Dwoo_Core::CUSTOM_PLUGIN;
2934 2934 } elseif (class_exists('Dwoo_Plugin_'.$name, false) !== false) {
2935 2935 if (is_subclass_of('Dwoo_Plugin_'.$name, 'Dwoo_Block_Plugin')) {
2936 - $pluginType = Dwoo::BLOCK_PLUGIN;
2936 + $pluginType = Dwoo_Core::BLOCK_PLUGIN;
2937 2937 } else {
2938 - $pluginType = Dwoo::CLASS_PLUGIN;
2938 + $pluginType = Dwoo_Core::CLASS_PLUGIN;
2939 2939 }
2940 2940 $interfaces = class_implements('Dwoo_Plugin_'.$name, false);
2941 2941 if (in_array('Dwoo_ICompilable', $interfaces) !== false || in_array('Dwoo_ICompilable_Block', $interfaces) !== false) {
2942 - $pluginType |= Dwoo::COMPILABLE_PLUGIN;
2942 + $pluginType |= Dwoo_Core::COMPILABLE_PLUGIN;
2943 2943 }
2944 2944 } elseif (function_exists('Dwoo_Plugin_'.$name) !== false) {
2945 - $pluginType = Dwoo::FUNC_PLUGIN;
2945 + $pluginType = Dwoo_Core::FUNC_PLUGIN;
2946 2946 } elseif (function_exists('Dwoo_Plugin_'.$name.'_compile')) {
2947 - $pluginType = Dwoo::FUNC_PLUGIN | Dwoo::COMPILABLE_PLUGIN;
2947 + $pluginType = Dwoo_Core::FUNC_PLUGIN | Dwoo_Core::COMPILABLE_PLUGIN;
2948 2948 } elseif (function_exists('smarty_modifier_'.$name) !== false) {
2949 - $pluginType = Dwoo::SMARTY_MODIFIER;
2949 + $pluginType = Dwoo_Core::SMARTY_MODIFIER;
2950 2950 } elseif (function_exists('smarty_function_'.$name) !== false) {
2951 - $pluginType = Dwoo::SMARTY_FUNCTION;
2951 + $pluginType = Dwoo_Core::SMARTY_FUNCTION;
2952 2952 } elseif (function_exists('smarty_block_'.$name) !== false) {
2953 - $pluginType = Dwoo::SMARTY_BLOCK;
2953 + $pluginType = Dwoo_Core::SMARTY_BLOCK;
2954 2954 } else {
2955 2955 if ($pluginType===-1) {
2956 2956 try {
2957 2957 $this->dwoo->getLoader()->loadPlugin($name, isset($phpFunc)===false);
2958 2958 } catch (Exception $e) {
2959 2959 if (isset($phpFunc)) {
2960 - $pluginType = Dwoo::NATIVE_PLUGIN;
2960 + $pluginType = Dwoo_Core::NATIVE_PLUGIN;
2961 2961 } elseif (is_object($this->dwoo->getPluginProxy()) && $this->dwoo->getPluginProxy()->handles($name)) {
2962 - $pluginType = Dwoo::PROXY_PLUGIN;
2962 + $pluginType = Dwoo_Core::PROXY_PLUGIN;
2963 2963 break;
2964 2964 } else {
2965 2965 throw $e;
2972 2972 }
2973 2973 }
2974 2974
2975 - if (($pluginType & Dwoo::COMPILABLE_PLUGIN) === 0 && ($pluginType & Dwoo::NATIVE_PLUGIN) === 0 && ($pluginType & Dwoo::PROXY_PLUGIN) === 0) {
2975 + if (($pluginType & Dwoo_Core::COMPILABLE_PLUGIN) === 0 && ($pluginType & Dwoo_Core::NATIVE_PLUGIN) === 0 && ($pluginType & Dwoo_Core::PROXY_PLUGIN) === 0) {
2976 2976 $this->addUsedPlugin($name, $pluginType);
2977 2977 }
2978 2978
3110 3110
3111 3111 $out = array();
3112 3112 foreach ($ref->getParameters() as $param) {
3113 - if (($class = $param->getClass()) !== null && $class->name === 'Dwoo') {
3113 + if (($class = $param->getClass()) !== null && ($class->name === 'Dwoo' || $class->name === 'Dwoo_Core')) {
3114 3114 continue;
3115 3115 }
3116 3116 if (($class = $param->getClass()) !== null && $class->name === 'Dwoo_Compiler') {
3129 3129 * returns a default instance of this compiler, used by default by all Dwoo templates that do not have a
3130 3130 * specific compiler assigned and when you do not override the default compiler factory function
3131 3131 *
3132 - * @see Dwoo::setDefaultCompilerFactory()
3132 + * @see Dwoo_Core::setDefaultCompilerFactory()
3133 3133 * @return Dwoo_Compiler
3134 3134 */
3135 3135 public static function compilerFactory()
495dc1 9315dc /dev/null
2 +<?php
3 +
4 +define('DWOO_DIRECTORY', dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR);
5 +
6 +/**
7 + * main dwoo class, allows communication between the compiler, template and data classes
8 + *
9 + * <pre>
10 + * requirements :
11 + * php 5.2.0 or above (might work below, it's a rough estimate)
12 + * SPL and PCRE extensions (for php versions prior to 5.3.0)
13 + * mbstring extension for some string manipulation plugins (especially if you intend to use UTF-8)
14 + * recommended :
15 + * hash extension (for Dwoo_Template_String - minor performance boost)
16 + *
17 + * project created :
18 + * 2008-01-05
19 + * </pre>
20 + *
21 + * This software is provided 'as-is', without any express or implied warranty.
22 + * In no event will the authors be held liable for any damages arising from the use of this software.
23 + *
24 + * @author Jordi Boggiano <j.boggiano@seld.be>
25 + * @copyright Copyright (c) 2008, Jordi Boggiano
26 + * @license http://dwoo.org/LICENSE Modified BSD License
27 + * @link http://dwoo.org/
28 + * @version 1.1.0
29 + * @date 2009-07-18
30 + * @package Dwoo
31 + */
32 +class Dwoo_Core
33 +{
34 + /**
35 + * current version number
36 + *
37 + * @var string
38 + */
39 + const VERSION = '1.1.1';
40 +
41 + /**
42 + * unique number of this dwoo release
43 + *
44 + * this can be used by templates classes to check whether the compiled template
45 + * has been compiled before this release or not, so that old templates are
46 + * recompiled automatically when Dwoo is updated
47 + */
48 + const RELEASE_TAG = 17;
49 +
50 + /**#@+
51 + * constants that represents all plugin types
52 + *
53 + * these are bitwise-operation-safe values to allow multiple types
54 + * on a single plugin
55 + *
56 + * @var int
57 + */
58 + const CLASS_PLUGIN = 1;
59 + const FUNC_PLUGIN = 2;
60 + const NATIVE_PLUGIN = 4;
61 + const BLOCK_PLUGIN = 8;
62 + const COMPILABLE_PLUGIN = 16;
63 + const CUSTOM_PLUGIN = 32;
64 + const SMARTY_MODIFIER = 64;
65 + const SMARTY_BLOCK = 128;
66 + const SMARTY_FUNCTION = 256;
67 + const PROXY_PLUGIN = 512;
68 + const TEMPLATE_PLUGIN = 1024;
69 + /**#@-*/
70 +
71 + /**
72 + * character set of the template, used by string manipulation plugins
73 + *
74 + * it must be lowercase, but setCharset() will take care of that
75 + *
76 + * @see setCharset
77 + * @see getCharset
78 + * @var string
79 + */
80 + protected $charset = 'utf-8';
81 +
82 + /**
83 + * global variables that are accessible through $dwoo.* in the templates
84 + *
85 + * default values include:
86 + *
87 + * $dwoo.version - current version number
88 + * $dwoo.ad - a Powered by Dwoo link pointing to dwoo.org
89 + * $dwoo.now - the current time
90 + * $dwoo.template - the current template filename
91 + * $dwoo.charset - the character set used by the template
92 + *
93 + * on top of that, foreach and other plugins can store special values in there,
94 + * see their documentation for more details.
95 + *
96 + * @private
97 + * @var array
98 + */
99 + public $globals;
100 +
101 + /**
102 + * directory where the compiled templates are stored
103 + *
104 + * defaults to DWOO_COMPILEDIR (= dwoo_dir/compiled by default)
105 + *
106 + * @var string
107 + */
108 + protected $compileDir;
109 +
110 + /**
111 + * directory where the cached templates are stored
112 + *
113 + * defaults to DWOO_CACHEDIR (= dwoo_dir/cache by default)
114 + *
115 + * @var string
116 + */
117 + protected $cacheDir;
118 +
119 + /**
120 + * defines how long (in seconds) the cached files must remain valid
121 + *
122 + * can be overriden on a per-template basis
123 + *
124 + * -1 = never delete
125 + * 0 = disabled
126 + * >0 = duration in seconds
127 + *
128 + * @var int
129 + */
130 + protected $cacheTime = 0;
131 +
132 + /**
133 + * security policy object
134 + *
135 + * @var Dwoo_Security_Policy
136 + */
137 + protected $securityPolicy = null;
138 +
139 + /**
140 + * stores the custom plugins callbacks
141 + *
142 + * @see addPlugin
143 + * @see removePlugin
144 + * @var array
145 + */
146 + protected $plugins = array();
147 +
148 + /**
149 + * stores the filter callbacks
150 + *
151 + * @see addFilter
152 + * @see removeFilter
153 + * @var array
154 + */
155 + protected $filters = array();
156 +
157 + /**
158 + * stores the resource types and associated
159 + * classes / compiler classes
160 + *
161 + * @var array
162 + */
163 + protected $resources = array
164 + (
165 + 'file' => array
166 + (
167 + 'class' => 'Dwoo_Template_File',
168 + 'compiler' => null
169 + ),
170 + 'string' => array
171 + (
172 + 'class' => 'Dwoo_Template_String',
173 + 'compiler' => null
174 + )
175 + );
176 +
177 + /**
178 + * the dwoo loader object used to load plugins by this dwoo instance
179 + *
180 + * @var Dwoo_ILoader
181 + */
182 + protected $loader = null;
183 +
184 + /**
185 + * currently rendered template, set to null when not-rendering
186 + *
187 + * @var Dwoo_ITemplate
188 + */
189 + protected $template = null;
190 +
191 + /**
192 + * stores the instances of the class plugins during template runtime
193 + *
194 + * @var array
195 + */
196 + protected $runtimePlugins;
197 +
198 + /**
199 + * stores the data during template runtime
200 + *
201 + * @var array
202 + * @private
203 + */
204 + public $data;
205 +
206 + /**
207 + * stores the current scope during template runtime
208 + *
209 + * this should ideally not be accessed directly from outside template code
210 + *
211 + * @var mixed
212 + * @private
213 + */
214 + public $scope;
215 +
216 + /**
217 + * stores the scope tree during template runtime
218 + *
219 + * @var array
220 + */
221 + protected $scopeTree;
222 +
223 + /**
224 + * stores the block plugins stack during template runtime
225 + *
226 + * @var array
227 + */
228 + protected $stack;
229 +
230 + /**
231 + * stores the current block plugin at the top of the stack during template runtime
232 + *
233 + * @var Dwoo_Block_Plugin
234 + */
235 + protected $curBlock;
236 +
237 + /**
238 + * stores the output buffer during template runtime
239 + *
240 + * @var string
241 + */
242 + protected $buffer;
243 +
244 + /**
245 + * stores plugin proxy
246 + *
247 + * @var Dwoo_IPluginProxy
248 + */
249 + protected $pluginProxy;
250 +
251 + /**
252 + * constructor, sets the cache and compile dir to the default values if not provided
253 + *
254 + * @param string $compileDir path to the compiled directory, defaults to lib/compiled
255 + * @param string $cacheDir path to the cache directory, defaults to lib/cache
256 + */
257 + public function __construct($compileDir = null, $cacheDir = null)
258 + {
259 + if ($compileDir !== null) {
260 + $this->setCompileDir($compileDir);
261 + }
262 + if ($cacheDir !== null) {
263 + $this->setCacheDir($cacheDir);
264 + }
265 + $this->initGlobals();
266 + }
267 +
268 + /**
269 + * resets some runtime variables to allow a cloned object to be used to render sub-templates
270 + */
271 + public function __clone()
272 + {
273 + $this->template = null;
274 + unset($this->data);
275 + }
276 +
277 + /**
278 + * outputs the template instead of returning it, this is basically a shortcut for get(*, *, *, true)
279 + *
280 + * @see get
281 + * @param mixed $tpl template, can either be a Dwoo_ITemplate object (i.e. Dwoo_Template_File), a valid path to a template, or
282 + * a template as a string it is recommended to provide a Dwoo_ITemplate as it will probably make things faster,
283 + * especially if you render a template multiple times
284 + * @param mixed $data the data to use, can either be a Dwoo_IDataProvider object (i.e. Dwoo_Data) or an associative array. if you're
285 + * rendering the template from cache, it can be left null
286 + * @param Dwoo_ICompiler $compiler the compiler that must be used to compile the template, if left empty a default
287 + * Dwoo_Compiler will be used.
288 + * @return string nothing or the template output if $output is true
289 + */
290 + public function output($tpl, $data = array(), Dwoo_ICompiler $compiler = null)
291 + {
292 + return $this->get($tpl, $data, $compiler, true);
293 + }
294 +
295 + /**
296 + * returns the given template rendered using the provided data and optional compiler
297 + *
298 + * @param mixed $tpl template, can either be a Dwoo_ITemplate object (i.e. Dwoo_Template_File), a valid path to a template, or
299 + * a template as a string it is recommended to provide a Dwoo_ITemplate as it will probably make things faster,
300 + * especially if you render a template multiple times
301 + * @param mixed $data the data to use, can either be a Dwoo_IDataProvider object (i.e. Dwoo_Data) or an associative array. if you're
302 + * rendering the template from cache, it can be left null
303 + * @param Dwoo_ICompiler $compiler the compiler that must be used to compile the template, if left empty a default
304 + * Dwoo_Compiler will be used.
305 + * @param bool $output flag that defines whether the function returns the output of the template (false, default) or echoes it directly (true)
306 + * @return string nothing or the template output if $output is true
307 + */
308 + public function get($_tpl, $data = array(), $_compiler = null, $_output = false)
309 + {
310 + // a render call came from within a template, so we need a new dwoo instance in order to avoid breaking this one
311 + if ($this->template instanceof Dwoo_ITemplate) {
312 + $proxy = clone $this;
313 + return $proxy->get($_tpl, $data, $_compiler, $_output);
314 + }
315 +
316 + // auto-create template if required
317 + if ($_tpl instanceof Dwoo_ITemplate) {
318 + // valid, skip
319 + } elseif (is_string($_tpl) && file_exists($_tpl)) {
320 + $_tpl = new Dwoo_Template_File($_tpl);
321 + } else {
322 + throw new Dwoo_Exception('Dwoo->get/Dwoo->output\'s first argument must be a Dwoo_ITemplate (i.e. Dwoo_Template_File) or a valid path to a template file', E_USER_NOTICE);
323 + }
324 +
325 + // save the current template, enters render mode at the same time
326 + // if another rendering is requested it will be proxied to a new Dwoo_Core(instance
327 + $this->template = $_tpl;
328 +
329 + // load data
330 + if ($data instanceof Dwoo_IDataProvider) {
331 + $this->data = $data->getData();
332 + } elseif (is_array($data)) {
333 + $this->data = $data;
334 + } else {
335 + throw new Dwoo_Exception('Dwoo->get/Dwoo->output\'s data argument must be a Dwoo_IDataProvider object (i.e. Dwoo_Data) or an associative array', E_USER_NOTICE);
336 + }
337 +
338 + $this->globals['template'] = $_tpl->getName();
339 + $this->initRuntimeVars($_tpl);
340 +
341 + // try to get cached template
342 + $file = $_tpl->getCachedTemplate($this);
343 + $doCache = $file === true;
344 + $cacheLoaded = is_string($file);
345 +
346 + if ($cacheLoaded === true) {
347 + // cache is present, run it
348 + if ($_output === true) {
349 + include $file;
350 + $this->template = null;
351 + } else {
352 + ob_start();
353 + include $file;
354 + $this->template = null;
355 + return ob_get_clean();
356 + }
357 + } else {
358 + // no cache present
359 + if ($doCache === true) {
360 + $dynamicId = uniqid();
361 + }
362 +
363 + // render template
364 + $compiledTemplate = $_tpl->getCompiledTemplate($this, $_compiler);
365 + $out = include $compiledTemplate;
366 +
367 + // template returned false so it needs to be recompiled
368 + if ($out === false) {
369 + $_tpl->forceCompilation();
370 + $compiledTemplate = $_tpl->getCompiledTemplate($this, $_compiler);
371 + $out = include $compiledTemplate;
372 + }
373 +
374 + if ($doCache === true) {
375 + $out = preg_replace('/(<%|%>|<\?php|<\?|\?>)/', '<?php /*'.$dynamicId.'*/ echo \'$1\'; ?>', $out);
376 + if (!class_exists('Dwoo_plugin_dynamic', false)) {
377 + $this->getLoader()->loadPlugin('dynamic');
378 + }
379 + $out = Dwoo_Plugin_dynamic::unescape($out, $dynamicId, $compiledTemplate);
380 + }
381 +
382 + // process filters
383 + foreach ($this->filters as $filter) {
384 + if (is_array($filter) && $filter[0] instanceof Dwoo_Filter) {
385 + $out = call_user_func($filter, $out);
386 + } else {
387 + $out = call_user_func($filter, $this, $out);
388 + }
389 + }
390 +
391 + if ($doCache === true) {
392 + // building cache
393 + $file = $_tpl->cache($this, $out);
394 +
395 + // run it from the cache to be sure dynamics are rendered
396 + if ($_output === true) {
397 + include $file;
398 + // exit render mode
399 + $this->template = null;
400 + } else {
401 + ob_start();
402 + include $file;
403 + // exit render mode
404 + $this->template = null;
405 + return ob_get_clean();
406 + }
407 + } else {
408 + // no need to build cache
409 + // exit render mode
410 + $this->template = null;
411 + // output
412 + if ($_output === true) {
413 + echo $out;
414 + }
415 + return $out;
416 + }
417 + }
418 + }
419 +
420 + /**
421 + * re-initializes the globals array before each template run
422 + *
423 + * this method is only callede once when the Dwoo object is created
424 + */
425 + protected function initGlobals()
426 + {
427 + $this->globals = array
428 + (
429 + 'version' => self::VERSION,
430 + 'ad' => '<a href="http://dwoo.org/">Powered by Dwoo</a>',
431 + 'now' => $_SERVER['REQUEST_TIME'],
432 + 'charset' => $this->charset,
433 + );
434 + }
435 +
436 + /**
437 + * re-initializes the runtime variables before each template run
438 + *
439 + * override this method to inject data in the globals array if needed, this
440 + * method is called before each template execution
441 + *
442 + * @param Dwoo_ITemplate $tpl the template that is going to be rendered
443 + */
444 + protected function initRuntimeVars(Dwoo_ITemplate $tpl)
445 + {
446 + $this->runtimePlugins = array();
447 + $this->scope =& $this->data;
448 + $this->scopeTree = array();
449 + $this->stack = array();
450 + $this->curBlock = null;
451 + $this->buffer = '';
452 + }
453 +
454 + /*
455 + * --------- settings functions ---------
456 + */
457 +
458 + /**
459 + * adds a custom plugin that is not in one of the plugin directories
460 + *
461 + * @param string $name the plugin name to be used in the templates
462 + * @param callback $callback the plugin callback, either a function name,
463 + * a class name or an array containing an object
464 + * or class name and a method name
465 + * @param bool $compilable if set to true, the plugin is assumed to be compilable
466 + */
467 + public function addPlugin($name, $callback, $compilable = false)
468 + {
469 + $compilable = $compilable ? self::COMPILABLE_PLUGIN : 0;
470 + if (is_array($callback)) {
471 + if (is_subclass_of(is_object($callback[0]) ? get_class($callback[0]) : $callback[0], 'Dwoo_Block_Plugin')) {
472 + $this->plugins[$name] = array('type'=>self::BLOCK_PLUGIN | $compilable, 'callback'=>$callback, 'class'=>(is_object($callback[0]) ? get_class($callback[0]) : $callback[0]));
473 + } else {
474 + $this->plugins[$name] = array('type'=>self::CLASS_PLUGIN | $compilable, 'callback'=>$callback, 'class'=>(is_object($callback[0]) ? get_class($callback[0]) : $callback[0]), 'function'=>$callback[1]);
475 + }
476 + } elseif (class_exists($callback, false)) {
477 + if (is_subclass_of($callback, 'Dwoo_Block_Plugin')) {
478 + $this->plugins[$name] = array('type'=>self::BLOCK_PLUGIN | $compilable, 'callback'=>$callback, 'class'=>$callback);
479 + } else {
480 + $this->plugins[$name] = array('type'=>self::CLASS_PLUGIN | $compilable, 'callback'=>$callback, 'class'=>$callback, 'function'=>'process');
481 + }
482 + } elseif (function_exists($callback)) {
483 + $this->plugins[$name] = array('type'=>self::FUNC_PLUGIN | $compilable, 'callback'=>$callback);
484 + } else {
485 + throw new Dwoo_Exception('Callback could not be processed correctly, please check that the function/class you used exists');
486 + }
487 + }
488 +
489 + /**
490 + * removes a custom plugin
491 + *
492 + * @param string $name the plugin name
493 + */
494 + public function removePlugin($name)
495 + {
496 + if (isset($this->plugins[$name])) {
497 + unset($this->plugins[$name]);
498 + }
499 + }
500 +
501 + /**
502 + * adds a filter to this Dwoo instance, it will be used to filter the output of all the templates rendered by this instance
503 + *
504 + * @param mixed $callback a callback or a filter name if it is autoloaded from a plugin directory
505 + * @param bool $autoload if true, the first parameter must be a filter name from one of the plugin directories
506 + */
507 + public function addFilter($callback, $autoload = false)
508 + {
509 + if ($autoload) {
510 + $class = 'Dwoo_Filter_'.$callback;
511 +
512 + if (!class_exists($class, false) && !function_exists($class)) {
513 + try {
514 + $this->getLoader()->loadPlugin($callback);
515 + } catch (Dwoo_Exception $e) {
516 + if (strstr($callback, 'Dwoo_Filter_')) {
517 + throw new Dwoo_Exception('Wrong filter name : '.$callback.', the "Dwoo_Filter_" prefix should not be used, please only use "'.str_replace('Dwoo_Filter_', '', $callback).'"');
518 + } else {
519 + throw new Dwoo_Exception('Wrong filter name : '.$callback.', when using autoload the filter must be in one of your plugin dir as "name.php" containg a class or function named "Dwoo_Filter_name"');
520 + }
521 + }
522 + }
523 +
524 + if (class_exists($class, false)) {
525 + $callback = array(new $class($this), 'process');
526 + } elseif (function_exists($class)) {
527 + $callback = $class;
528 + } else {
529 + throw new Dwoo_Exception('Wrong filter name : '.$callback.', when using autoload the filter must be in one of your plugin dir as "name.php" containg a class or function named "Dwoo_Filter_name"');
530 + }
531 +
532 + $this->filters[] = $callback;
533 + } else {
534 + $this->filters[] = $callback;
535 + }
536 + }
537 +
538 + /**
539 + * removes a filter
540 + *
541 + * @param mixed $callback callback or filter name if it was autoloaded
542 + */
543 + public function removeFilter($callback)
544 + {
545 + if (($index = array_search('Dwoo_Filter_'.$callback, $this->filters, true)) !== false) {
546 + unset($this->filters[$index]);
547 + } elseif (($index = array_search($callback, $this->filters, true)) !== false) {
548 + unset($this->filters[$index]);
549 + } else {
550 + $class = 'Dwoo_Filter_' . $callback;
551 + foreach ($this->filters as $index=>$filter) {
552 + if (is_array($filter) && $filter[0] instanceof $class) {
553 + unset($this->filters[$index]);
554 + break;
555 + }
556 + }
557 + }
558 + }
559 +
560 + /**
561 + * adds a resource or overrides a default one
562 + *
563 + * @param string $name the resource name
564 + * @param string $class the resource class (which must implement Dwoo_ITemplate)
565 + * @param callback $compilerFactory the compiler factory callback, a function that must return a compiler instance used to compile this resource, if none is provided. by default it will produce a Dwoo_Compiler object
566 + */
567 + public function addResource($name, $class, $compilerFactory = null)
568 + {
569 + if (strlen($name) < 2) {
570 + throw new Dwoo_Exception('Resource names must be at least two-character long to avoid conflicts with Windows paths');
571 + }
572 +
573 + if (!class_exists($class)) {
574 + throw new Dwoo_Exception('Resource class does not exist');
575 + }
576 +
577 + $interfaces = class_implements($class);
578 + if (in_array('Dwoo_ITemplate', $interfaces) === false) {
579 + throw new Dwoo_Exception('Resource class must implement Dwoo_ITemplate');
580 + }
581 +
582 + $this->resources[$name] = array('class'=>$class, 'compiler'=>$compilerFactory);
583 + }
584 +
585 + /**
586 + * removes a custom resource
587 + *
588 + * @param string $name the resource name
589 + */
590 + public function removeResource($name)
591 + {
592 + unset($this->resources[$name]);
593 + if ($name==='file') {
594 + $this->resources['file'] = array('class'=>'Dwoo_Template_File', 'compiler'=>null);
595 + }
596 + }
597 +
598 + /*
599 + * --------- getters and setters ---------
600 + */
601 +
602 + /**
603 + * sets the loader object to use to load plugins
604 + *
605 + * @param Dwoo_ILoader $loader loader object
606 + */
607 + public function setLoader(Dwoo_ILoader $loader)
608 + {
609 + $this->loader = $loader;
610 + }
611 +
612 + /**
613 + * returns the current loader object or a default one if none is currently found
614 + *
615 + * @param Dwoo_ILoader
616 + */
617 + public function getLoader()
618 + {
619 + if ($this->loader === null) {
620 + $this->loader = new Dwoo_Loader($this->getCompileDir());
621 + }
622 +
623 + return $this->loader;
624 + }
625 +
626 + /**
627 + * returns the custom plugins loaded
628 + *
629 + * used by the Dwoo_ITemplate classes to pass the custom plugins to their Dwoo_ICompiler instance
630 + *
631 + * @return array
632 + */
633 + public function getCustomPlugins()
634 + {
635 + return $this->plugins;
636 + }
637 +
638 + /**
639 + * returns the cache directory with a trailing DIRECTORY_SEPARATOR
640 + *
641 + * @return string
642 + */
643 + public function getCacheDir()
644 + {
645 + if ($this->cacheDir === null) {
646 + $this->setCacheDir(dirname(__FILE__).DIRECTORY_SEPARATOR.'cache'.DIRECTORY_SEPARATOR);
647 + }
648 +
649 + return $this->cacheDir;
650 + }
651 +
652 + /**
653 + * sets the cache directory and automatically appends a DIRECTORY_SEPARATOR
654 + *
655 + * @param string $dir the cache directory
656 + */
657 + public function setCacheDir($dir)
658 + {
659 + $this->cacheDir = rtrim($dir, '/\\').DIRECTORY_SEPARATOR;
660 + if (is_writable($this->cacheDir) === false) {
661 + throw new Dwoo_Exception('The cache directory must be writable, chmod "'.$this->cacheDir.'" to make it writable');
662 + }
663 + }
664 +
665 + /**
666 + * returns the compile directory with a trailing DIRECTORY_SEPARATOR
667 + *
668 + * @return string
669 + */
670 + public function getCompileDir()
671 + {
672 + if ($this->compileDir === null) {
673 + $this->setCompileDir(dirname(__FILE__).DIRECTORY_SEPARATOR.'compiled'.DIRECTORY_SEPARATOR);
674 + }
675 +
676 + return $this->compileDir;
677 + }
678 +
679 + /**
680 + * sets the compile directory and automatically appends a DIRECTORY_SEPARATOR
681 + *
682 + * @param string $dir the compile directory
683 + */
684 + public function setCompileDir($dir)
685 + {
686 + $this->compileDir = rtrim($dir, '/\\').DIRECTORY_SEPARATOR;
687 + if (is_writable($this->compileDir) === false) {
688 + throw new Dwoo_Exception('The compile directory must be writable, chmod "'.$this->compileDir.'" to make it writable');
689 + }
690 + }
691 +
692 + /**
693 + * returns the default cache time that is used with templates that do not have a cache time set
694 + *
695 + * @return int the duration in seconds
696 + */
697 + public function getCacheTime()
698 + {
699 + return $this->cacheTime;
700 + }
701 +
702 + /**
703 + * sets the default cache time to use with templates that do not have a cache time set
704 + *
705 + * @param int $seconds the duration in seconds
706 + */
707 + public function setCacheTime($seconds)
708 + {
709 + $this->cacheTime = (int) $seconds;
710 + }
711 +
712 + /**
713 + * returns the character set used by the string manipulation plugins
714 + *
715 + * the charset is automatically lowercased
716 + *
717 + * @return string
718 + */
719 + public function getCharset()
720 + {
721 + return $this->charset;
722 + }
723 +
724 + /**
725 + * sets the character set used by the string manipulation plugins
726 + *
727 + * the charset will be automatically lowercased
728 + *
729 + * @param string $charset the character set
730 + */
731 + public function setCharset($charset)
732 + {
733 + $this->charset = strtolower((string) $charset);
734 + }
735 +
736 + /**
737 + * returns the current template being rendered, when applicable, or null
738 + *
739 + * @return Dwoo_ITemplate|null
740 + */
741 + public function getTemplate()
742 + {
743 + return $this->template;
744 + }
745 +
746 + /**
747 + * sets the current template being rendered
748 + *
749 + * @param Dwoo_ITemplate $tpl template object
750 + */
751 + public function setTemplate(Dwoo_ITemplate $tpl)
752 + {
753 + $this->template = $tpl;
754 + }
755 +
756 + /**
757 + * sets the default compiler factory function for the given resource name
758 + *
759 + * a compiler factory must return a Dwoo_ICompiler object pre-configured to fit your needs
760 + *
761 + * @param string $resourceName the resource name (i.e. file, string)
762 + * @param callback $compilerFactory the compiler factory callback
763 + */
764 + public function setDefaultCompilerFactory($resourceName, $compilerFactory)
765 + {
766 + $this->resources[$resourceName]['compiler'] = $compilerFactory;
767 + }
768 +
769 + /**
770 + * returns the default compiler factory function for the given resource name
771 + *
772 + * @param string $resourceName the resource name
773 + * @return callback the compiler factory callback
774 + */
775 + public function getDefaultCompilerFactory($resourceName)
776 + {
777 + return $this->resources[$resourceName]['compiler'];
778 + }
779 +
780 + /**
781 + * sets the security policy object to enforce some php security settings
782 + *
783 + * use this if untrusted persons can modify templates
784 + *
785 + * @param Dwoo_Security_Policy $policy the security policy object
786 + */
787 + public function setSecurityPolicy(Dwoo_Security_Policy $policy = null)
788 + {
789 + $this->securityPolicy = $policy;
790 + }
791 +
792 + /**
793 + * returns the current security policy object or null by default
794 + *
795 + * @return Dwoo_Security_Policy|null the security policy object if any
796 + */
797 + public function getSecurityPolicy()
798 + {
799 + return $this->securityPolicy;
800 + }
801 +
802 + /**
803 + * sets the object that must be used as a plugin proxy when plugin can't be found
804 + * by dwoo's loader
805 + *
806 + * @param Dwoo_IPluginProxy $pluginProxy the proxy object
807 + */
808 + public function setPluginProxy(Dwoo_IPluginProxy $pluginProxy) {
809 + $this->pluginProxy = $pluginProxy;
810 + }
811 +
812 + /**
813 + * returns the current plugin proxy object or null by default
814 + *
815 + * @param Dwoo_IPluginProxy|null the proxy object if any
816 + */
817 + public function getPluginProxy() {
818 + return $this->pluginProxy;
819 + }
820 +
821 + /*
822 + * --------- util functions ---------
823 + */
824 +
825 + /**
826 + * [util function] checks whether the given template is cached or not
827 + *
828 + * @param Dwoo_ITemplate $tpl the template object
829 + * @return bool
830 + */
831 + public function isCached(Dwoo_ITemplate $tpl)
832 + {
833 + return is_string($tpl->getCachedTemplate($this));
834 + }
835 +
836 + /**
837 + * [util function] clears the cached templates if they are older than the given time
838 + *
839 + * @param int $olderThan minimum time (in seconds) required for a cached template to be cleared
840 + * @return int the amount of templates cleared
841 + */
842 + public function clearCache($olderThan=-1)
843 + {
844 + $cacheDirs = new RecursiveDirectoryIterator($this->getCacheDir());
845 + $cache = new RecursiveIteratorIterator($cacheDirs);
846 + $expired = time() - $olderThan;
847 + $count = 0;
848 + foreach ($cache as $file) {
849 + if ($cache->isDot() || $cache->isDir() || substr($file, -5) !== '.html') {
850 + continue;
851 + }
852 + if ($cache->getCTime() < $expired) {
853 + $count += unlink((string) $file) ? 1 : 0;
854 + }
855 + }
856 + return $count;
857 + }
858 +
859 + /**
860 + * [util function] fetches a template object of the given resource
861 + *
862 + * @param string $resourceName the resource name (i.e. file, string)
863 + * @param string $resourceId the resource identifier (i.e. file path)
864 + * @param int $cacheTime the cache time setting for this resource
865 + * @param string $cacheId the unique cache identifier
866 + * @param string $compileId the unique compiler identifier
867 + * @return Dwoo_ITemplate
868 + */
869 + public function templateFactory($resourceName, $resourceId, $cacheTime = null, $cacheId = null, $compileId = null, Dwoo_ITemplate $parentTemplate = null)
870 + {
871 + if (isset($this->resources[$resourceName])) {
872 + // TODO could be changed to $this->resources[$resourceName]['class']::templateFactory(..) in 5.3 maybe
873 + return call_user_func(array($this->resources[$resourceName]['class'], 'templateFactory'), $this, $resourceId, $cacheTime, $cacheId, $compileId, $parentTemplate);
874 + } else {
875 + throw new Dwoo_Exception('Unknown resource type : '.$resourceName);
876 + }
877 + }
878 +
879 + /**
880 + * [util function] checks if the input is an array or arrayaccess object, optionally it can also check if it's empty
881 + *
882 + * @param mixed $value the variable to check
883 + * @param bool $checkIsEmpty if true, the function will also check if the array|arrayaccess is empty,
884 + * and return true only if it's not empty
885 + * @return int|bool true if it's an array|arrayaccess (or the item count if $checkIsEmpty is true) or false if it's not an array|arrayaccess (or 0 if $checkIsEmpty is true)
886 + */
887 + public function isArray($value, $checkIsEmpty=false)
888 + {
889 + if (is_array($value) === true || $value instanceof ArrayAccess) {
890 + if ($checkIsEmpty === false) {
891 + return true;
892 + } else {
893 + return $this->count($value);
894 + }
895 + }
896 + }
897 +
898 + /**
899 + * [util function] checks if the input is an array or a traversable object, optionally it can also check if it's empty
900 + *
901 + * @param mixed $value the variable to check
902 + * @param bool $checkIsEmpty if true, the function will also check if the array|traversable is empty,
903 + * and return true only if it's not empty
904 + * @return int|bool true if it's an array|traversable (or the item count if $checkIsEmpty is true) or false if it's not an array|traversable (or 0 if $checkIsEmpty is true)
905 + */
906 + public function isTraversable($value, $checkIsEmpty=false)
907 + {
908 + if (is_array($value) === true) {
909 + if ($checkIsEmpty === false) {
910 + return true;
911 + } else {
912 + return count($value) > 0;
913 + }
914 + } elseif ($value instanceof Traversable) {
915 + if ($checkIsEmpty === false) {
916 + return true;
917 + } else {
918 + return $this->count($value);
919 + }
920 + }
921 + return false;
922 + }
923 +
924 + /**
925 + * [util function] counts an array or arrayaccess/traversable object
926 + * @param mixed $value
927 + * @return int|bool the count for arrays and objects that implement countable, true for other objects that don't, and 0 for empty elements
928 + */
929 + public function count($value)
930 + {
931 + if (is_array($value) === true || $value instanceof Countable) {
932 + return count($value);
933 + } elseif ($value instanceof ArrayAccess) {
934 + if ($value->offsetExists(0)) {
935 + return true;
936 + }
937 + } elseif ($value instanceof Iterator) {
938 + $value->rewind();
939 + if ($value->valid()) {
940 + return true;
941 + }
942 + } elseif ($value instanceof Traversable) {
943 + foreach ($value as $dummy) {
944 + return true;
945 + }
946 + }
947 + return 0;
948 + }
949 +
950 + /**
951 + * [util function] triggers a dwoo error
952 + *
953 + * @param string $message the error message
954 + * @param int $level the error level, one of the PHP's E_* constants
955 + */
956 + public function triggerError($message, $level=E_USER_NOTICE)
957 + {
958 + if (!($tplIdentifier = $this->template->getResourceIdentifier())) {
959 + $tplIdentifier = $this->template->getResourceName();
960 + }
961 + trigger_error('Dwoo error (in '.$tplIdentifier.') : '.$message, $level);
962 + }
963 +
964 + /*
965 + * --------- runtime functions ---------
966 + */
967 +
968 + /**
969 + * [runtime function] adds a block to the block stack
970 + *
971 + * @param string $blockName the block name (without Dwoo_Plugin_ prefix)
972 + * @param array $args the arguments to be passed to the block's init() function
973 + * @return Dwoo_Block_Plugin the newly created block
974 + */
975 + public function addStack($blockName, array $args=array())
976 + {
977 + if (isset($this->plugins[$blockName])) {
978 + $class = $this->plugins[$blockName]['class'];
979 + } else {
980 + $class = 'Dwoo_Plugin_'.$blockName;
981 + }
982 +
983 + if ($this->curBlock !== null) {
984 + $this->curBlock->buffer(ob_get_contents());
985 + ob_clean();
986 + } else {
987 + $this->buffer .= ob_get_contents();
988 + ob_clean();
989 + }
990 +
991 + $block = new $class($this);
992 +
993 + $cnt = count($args);
994 + if ($cnt===0) {
995 + $block->init();
996 + } elseif ($cnt===1) {
997 + $block->init($args[0]);
998 + } elseif ($cnt===2) {
999 + $block->init($args[0], $args[1]);
1000 + } elseif ($cnt===3) {
1001 + $block->init($args[0], $args[1], $args[2]);
1002 + } elseif ($cnt===4) {
1003 + $block->init($args[0], $args[1], $args[2], $args[3]);
1004 + } else {
1005 + call_user_func_array(array($block,'init'), $args);
1006 + }
1007 +
1008 + $this->stack[] = $this->curBlock = $block;
1009 + return $block;
1010 + }
1011 +
1012 + /**
1013 + * [runtime function] removes the plugin at the top of the block stack
1014 + *
1015 + * calls the block buffer() function, followed by a call to end()
1016 + * and finally a call to process()
1017 + */
1018 + public function delStack()
1019 + {
1020 + $args = func_get_args();
1021 +
1022 + $this->curBlock->buffer(ob_get_contents());
1023 + ob_clean();
1024 +
1025 + $cnt = count($args);
1026 + if ($cnt===0) {
1027 + $this->curBlock->end();
1028 + } elseif ($cnt===1) {
1029 + $this->curBlock->end($args[0]);
1030 + } elseif ($cnt===2) {
1031 + $this->curBlock->end($args[0], $args[1]);
1032 + } elseif ($cnt===3) {
1033 + $this->curBlock->end($args[0], $args[1], $args[2]);
1034 + } elseif ($cnt===4) {
1035 + $this->curBlock->end($args[0], $args[1], $args[2], $args[3]);
1036 + } else {
1037 + call_user_func_array(array($this->curBlock, 'end'), $args);
1038 + }
1039 +
1040 + $tmp = array_pop($this->stack);
1041 +
1042 + if (count($this->stack) > 0) {
1043 + $this->curBlock = end($this->stack);
1044 + $this->curBlock->buffer($tmp->process());
1045 + } else {
1046 + $this->curBlock = null;
1047 + echo $tmp->process();
1048 + }
1049 +
1050 + unset($tmp);
1051 + }
1052 +
1053 + /**
1054 + * [runtime function] returns the parent block of the given block
1055 + *
1056 + * @param Dwoo_Block_Plugin $block
1057 + * @return Dwoo_Block_Plugin or false if the given block isn't in the stack
1058 + */
1059 + public function getParentBlock(Dwoo_Block_Plugin $block)
1060 + {
1061 + $index = array_search($block, $this->stack, true);
1062 + if ($index !== false && $index > 0) {
1063 + return $this->stack[$index-1];
1064 + }
1065 + return false;
1066 + }
1067 +
1068 + /**
1069 + * [runtime function] finds the closest block of the given type, starting at the top of the stack
1070 + *
1071 + * @param string $type the type of plugin you want to find
1072 + * @return Dwoo_Block_Plugin or false if no plugin of such type is in the stack
1073 + */
1074 + public function findBlock($type)
1075 + {
1076 + if (isset($this->plugins[$type])) {
1077 + $type = $this->plugins[$type]['class'];
1078 + } else {
1079 + $type = 'Dwoo_Plugin_'.str_replace('Dwoo_Plugin_', '', $type);
1080 + }
1081 +
1082 + $keys = array_keys($this->stack);
1083 + while (($key = array_pop($keys)) !== false) {
1084 + if ($this->stack[$key] instanceof $type) {
1085 + return $this->stack[$key];
1086 + }
1087 + }
1088 + return false;
1089 + }
1090 +
1091 + /**
1092 + * [runtime function] returns a Dwoo_Plugin of the given class
1093 + *
1094 + * this is so a single instance of every class plugin is created at each template run,
1095 + * allowing class plugins to have "per-template-run" static variables
1096 + *
1097 + * @private
1098 + * @param string $class the class name
1099 + * @return mixed an object of the given class
1100 + */
1101 + public function getObjectPlugin($class)
1102 + {
1103 + if (isset($this->runtimePlugins[$class])) {
1104 + return $this->runtimePlugins[$class];
1105 + }
1106 + return $this->runtimePlugins[$class] = new $class($this);
1107 + }
1108 +
1109 + /**
1110 + * [runtime function] calls the process() method of the given class-plugin name
1111 + *
1112 + * @param string $plugName the class plugin name (without Dwoo_Plugin_ prefix)
1113 + * @param array $params an array of parameters to send to the process() method
1114 + * @return string the process() return value
1115 + */
1116 + public function classCall($plugName, array $params = array())
1117 + {
1118 + $class = 'Dwoo_Plugin_'.$plugName;
1119 +
1120 + $plugin = $this->getObjectPlugin($class);
1121 +
1122 + $cnt = count($params);
1123 + if ($cnt===0) {
1124 + return $plugin->process();
1125 + } elseif ($cnt===1) {
1126 + return $plugin->process($params[0]);
1127 + } elseif ($cnt===2) {
1128 + return $plugin->process($params[0], $params[1]);
1129 + } elseif ($cnt===3) {
1130 + return $plugin->process($params[0], $params[1], $params[2]);
1131 + } elseif ($cnt===4) {
1132 + return $plugin->process($params[0], $params[1], $params[2], $params[3]);
1133 + } else {
1134 + return call_user_func_array(array($plugin, 'process'), $params);
1135 + }
1136 + }
1137 +
1138 + /**
1139 + * [runtime function] calls a php function
1140 + *
1141 + * @param string $callback the function to call
1142 + * @param array $params an array of parameters to send to the function
1143 + * @return mixed the return value of the called function
1144 + */
1145 + public function arrayMap($callback, array $params)
1146 + {
1147 + if ($params[0] === $this) {
1148 + $addThis = true;
1149 + array_shift($params);
1150 + }
1151 + if ((is_array($params[0]) || ($params[0] instanceof Iterator && $params[0] instanceof ArrayAccess))) {
1152 + if (empty($params[0])) {
1153 + return $params[0];
1154 + }
1155 +
1156 + // array map
1157 + $out = array();
1158 + $cnt = count($params);
1159 +
1160 + if (isset($addThis)) {
1161 + array_unshift($params, $this);
1162 + $items = $params[1];
1163 + $keys = array_keys($items);
1164 +
1165 + if (is_string($callback) === false) {
1166 + while (($i = array_shift($keys)) !== null) {
1167 + $out[] = call_user_func_array($callback, array(1=>$items[$i]) + $params);
1168 + }
1169 + } elseif ($cnt===1) {
1170 + while (($i = array_shift($keys)) !== null) {
1171 + $out[] = $callback($this, $items[$i]);
1172 + }
1173 + } elseif ($cnt===2) {
1174 + while (($i = array_shift($keys)) !== null) {
1175 + $out[] = $callback($this, $items[$i], $params[2]);
1176 + }
1177 + } elseif ($cnt===3) {
1178 + while (($i = array_shift($keys)) !== null) {
1179 + $out[] = $callback($this, $items[$i], $params[2], $params[3]);
1180 + }
1181 + } else {
1182 + while (($i = array_shift($keys)) !== null) {
1183 + $out[] = call_user_func_array($callback, array(1=>$items[$i]) + $params);
1184 + }
1185 + }
1186 + } else {
1187 + $items = $params[0];
1188 + $keys = array_keys($items);
1189 +
1190 + if (is_string($callback) === false) {
1191 + while (($i = array_shift($keys)) !== null) {
1192 + $out[] = call_user_func_array($callback, array($items[$i]) + $params);
1193 + }
1194 + } elseif ($cnt===1) {
1195 + while (($i = array_shift($keys)) !== null) {
1196 + $out[] = $callback($items[$i]);
1197 + }
1198 + } elseif ($cnt===2) {
1199 + while (($i = array_shift($keys)) !== null) {
1200 + $out[] = $callback($items[$i], $params[1]);
1201 + }
1202 + } elseif ($cnt===3) {
1203 + while (($i = array_shift($keys)) !== null) {
1204 + $out[] = $callback($items[$i], $params[1], $params[2]);
1205 + }
1206 + } elseif ($cnt===4) {
1207 + while (($i = array_shift($keys)) !== null) {
1208 + $out[] = $callback($items[$i], $params[1], $params[2], $params[3]);
1209 + }
1210 + } else {
1211 + while (($i = array_shift($keys)) !== null) {
1212 + $out[] = call_user_func_array($callback, array($items[$i]) + $params);
1213 + }
1214 + }
1215 + }
1216 + return $out;
1217 + } else {
1218 + return $params[0];
1219 + }
1220 + }
1221 +
1222 + /**
1223 + * [runtime function] reads a variable into the given data array
1224 + *
1225 + * @param string $varstr the variable string, using dwoo variable syntax (i.e. "var.subvar[subsubvar]->property")
1226 + * @param mixed $data the data array or object to read from
1227 + * @param bool $safeRead if true, the function will check whether the index exists to prevent any notices from being output
1228 + * @return mixed
1229 + */
1230 + public function readVarInto($varstr, $data, $safeRead = false)
1231 + {
1232 + if ($data === null) {
1233 + return null;
1234 + }
1235 +
1236 + if (is_array($varstr) === false) {
1237 + preg_match_all('#(\[|->|\.)?((?:[^.[\]-]|-(?!>))+)\]?#i', $varstr, $m);
1238 + } else {
1239 + $m = $varstr;
1240 + }
1241 + unset($varstr);
1242 +
1243 + while (list($k, $sep) = each($m[1])) {
1244 + if ($sep === '.' || $sep === '[' || $sep === '') {
1245 + if ((is_array($data) || $data instanceof ArrayAccess) && ($safeRead === false || isset($data[$m[2][$k]]))) {
1246 + $data = $data[$m[2][$k]];
1247 + } else {
1248 + return null;
1249 + }
1250 + } else {
1251 + if (is_object($data) && ($safeRead === false || isset($data->$m[2][$k]) || is_callable(array($data, '__get')))) {
1252 + $data = $data->$m[2][$k];
1253 + } else {
1254 + return null;
1255 + }
1256 + }
1257 + }
1258 +
1259 + return $data;
1260 + }
1261 +
1262 + /**
1263 + * [runtime function] reads a variable into the parent scope
1264 + *
1265 + * @param int $parentLevels the amount of parent levels to go from the current scope
1266 + * @param string $varstr the variable string, using dwoo variable syntax (i.e. "var.subvar[subsubvar]->property")
1267 + * @return mixed
1268 + */
1269 + public function readParentVar($parentLevels, $varstr = null)
1270 + {
1271 + $tree = $this->scopeTree;
1272 + $cur = $this->data;
1273 +
1274 + while ($parentLevels--!==0) {
1275 + array_pop($tree);
1276 + }
1277 +
1278 + while (($i = array_shift($tree)) !== null) {
1279 + if (is_object($cur)) {
1280 + $cur = $cur->$i;
1281 + } else {
1282 + $cur = $cur[$i];
1283 + }
1284 + }
1285 +
1286 + if ($varstr!==null) {
1287 + return $this->readVarInto($varstr, $cur);
1288 + } else {
1289 + return $cur;
1290 + }
1291 + }
1292 +
1293 + /**
1294 + * [runtime function] reads a variable into the current scope
1295 + *
1296 + * @param string $varstr the variable string, using dwoo variable syntax (i.e. "var.subvar[subsubvar]->property")
1297 + * @return mixed
1298 + */
1299 + public function readVar($varstr)
1300 + {
1301 + if (is_array($varstr)===true) {
1302 + $m = $varstr;
1303 + unset($varstr);
1304 + } else {
1305 + if (strstr($varstr, '.') === false && strstr($varstr, '[') === false && strstr($varstr, '->') === false) {
1306 + if ($varstr === 'dwoo') {
1307 + return $this->globals;
1308 + } elseif ($varstr === '__' || $varstr === '_root' ) {
1309 + return $this->data;
1310 + $varstr = substr($varstr, 6);
1311 + } elseif ($varstr === '_' || $varstr === '_parent') {
1312 + $varstr = '.'.$varstr;
1313 + $tree = $this->scopeTree;
1314 + $cur = $this->data;
1315 + array_pop($tree);
1316 +
1317 + while (($i = array_shift($tree)) !== null) {
1318 + if (is_object($cur)) {
1319 + $cur = $cur->$i;
1320 + } else {
1321 + $cur = $cur[$i];
1322 + }
1323 + }
1324 +
1325 + return $cur;
1326 + }
1327 +
1328 + $cur = $this->scope;
1329 +
1330 + if (isset($cur[$varstr])) {
1331 + return $cur[$varstr];
1332 + } else {
1333 + return null;
1334 + }
1335 + }
1336 +
1337 + if (substr($varstr, 0, 1) === '.') {
1338 + $varstr = 'dwoo'.$varstr;
1339 + }
1340 +
1341 + preg_match_all('#(\[|->|\.)?((?:[^.[\]-]|-(?!>))+)\]?#i', $varstr, $m);
1342 + }
1343 +
1344 + $i = $m[2][0];
1345 + if ($i === 'dwoo') {
1346 + $cur = $this->globals;
1347 + array_shift($m[2]);
1348 + array_shift($m[1]);
1349 + switch ($m[2][0]) {
1350 +
1351 + case 'get':
1352 + $cur = $_GET;
1353 + break;
1354 + case 'post':
1355 + $cur = $_POST;
1356 + break;
1357 + case 'session':
1358 + $cur = $_SESSION;
1359 + break;
1360 + case 'cookies':
1361 + case 'cookie':
1362 + $cur = $_COOKIE;
1363 + break;
1364 + case 'server':
1365 + $cur = $_SERVER;
1366 + break;
1367 + case 'env':
1368 + $cur = $_ENV;
1369 + break;
1370 + case 'request':
1371 + $cur = $_REQUEST;
1372 + break;
1373 + case 'const':
1374 + array_shift($m[2]);
1375 + if (defined($m[2][0])) {
1376 + return constant($m[2][0]);
1377 + } else {
1378 + return null;
1379 + }
1380 +
1381 + }
1382 + if ($cur !== $this->globals) {
1383 + array_shift($m[2]);
1384 + array_shift($m[1]);
1385 + }
1386 + } elseif ($i === '__' || $i === '_root') {
1387 + $cur = $this->data;
1388 + array_shift($m[2]);
1389 + array_shift($m[1]);
1390 + } elseif ($i === '_' || $i === '_parent') {
1391 + $tree = $this->scopeTree;
1392 + $cur = $this->data;
1393 +
1394 + while (true) {
1395 + array_pop($tree);
1396 + array_shift($m[2]);
1397 + array_shift($m[1]);
1398 + if (current($m[2]) === '_' || current($m[2]) === '_parent') {
1399 + continue;
1400 + }
1401 +
1402 + while (($i = array_shift($tree)) !== null) {
1403 + if (is_object($cur)) {
1404 + $cur = $cur->$i;
1405 + } else {
1406 + $cur = $cur[$i];
1407 + }
1408 + }
1409 + break;
1410 + }
1411 + } else {
1412 + $cur = $this->scope;
1413 + }
1414 +
1415 + while (list($k, $sep) = each($m[1])) {
1416 + if ($sep === '.' || $sep === '[' || $sep === '') {
1417 + if ((is_array($cur) || $cur instanceof ArrayAccess) && isset($cur[$m[2][$k]])) {
1418 + $cur = $cur[$m[2][$k]];
1419 + } else {
1420 + return null;
1421 + }
1422 + } elseif ($sep === '->') {
1423 + if (is_object($cur)) {
1424 + $cur = $cur->$m[2][$k];
1425 + } else {
1426 + return null;
1427 + }
1428 + } else {
1429 + return null;
1430 + }
1431 + }
1432 +
1433 + return $cur;
1434 + }
1435 +
1436 + /**
1437 + * [runtime function] assign the value to the given variable
1438 + *
1439 + * @param mixed $value the value to assign
1440 + * @param string $scope the variable string, using dwoo variable syntax (i.e. "var.subvar[subsubvar]->property")
1441 + * @return bool true if assigned correctly or false if a problem occured while parsing the var string
1442 + */
1443 + public function assignInScope($value, $scope)
1444 + {
1445 + $tree =& $this->scopeTree;
1446 + $data =& $this->data;
1447 +
1448 + if (!is_string($scope)) {
1449 + return $this->triggerError('Assignments must be done into strings, ('.gettype($scope).') '.var_export($scope, true).' given', E_USER_ERROR);
1450 + }
1451 + if (strstr($scope, '.') === false && strstr($scope, '->') === false) {
1452 + $this->scope[$scope] = $value;
1453 + } else {
1454 + // TODO handle _root/_parent scopes ?
1455 + preg_match_all('#(\[|->|\.)?([^.[\]-]+)\]?#i', $scope, $m);
1456 +
1457 + $cur =& $this->scope;
1458 + $last = array(array_pop($m[1]), array_pop($m[2]));
1459 +
1460 + while (list($k, $sep) = each($m[1])) {
1461 + if ($sep === '.' || $sep === '[' || $sep === '') {
1462 + if (is_array($cur) === false) {
1463 + $cur = array();
1464 + }
1465 + $cur =& $cur[$m[2][$k]];
1466 + } elseif ($sep === '->') {
1467 + if (is_object($cur) === false) {
1468 + $cur = new stdClass;
1469 + }
1470 + $cur =& $cur->$m[2][$k];
1471 + } else {
1472 + return false;
1473 + }
1474 + }
1475 +
1476 + if ($last[0] === '.' || $last[0] === '[' || $last[0] === '') {
1477 + if (is_array($cur) === false) {
1478 + $cur = array();
1479 + }
1480 + $cur[$last[1]] = $value;
1481 + } elseif ($last[0] === '->') {
1482 + if (is_object($cur) === false) {
1483 + $cur = new stdClass;
1484 + }
1485 + $cur->$last[1] = $value;
1486 + } else {
1487 + return false;
1488 + }
1489 + }
1490 + }
1491 +
1492 + /**
1493 + * [runtime function] sets the scope to the given scope string or array
1494 + *
1495 + * @param mixed $scope a string i.e. "level1.level2" or an array i.e. array("level1", "level2")
1496 + * @param bool $absolute if true, the scope is set from the top level scope and not from the current scope
1497 + * @return array the current scope tree
1498 + */
1499 + public function setScope($scope, $absolute = false)
1500 + {
1501 + $old = $this->scopeTree;
1502 +
1503 + if (is_string($scope)===true) {
1504 + $scope = explode('.', $scope);
1505 + }
1506 +
1507 + if ($absolute===true) {
1508 + $this->scope =& $this->data;
1509 + $this->scopeTree = array();
1510 + }
1511 +
1512 + while (($bit = array_shift($scope)) !== null) {
1513 + if ($bit === '_' || $bit === '_parent') {
1514 + array_pop($this->scopeTree);
1515 + $this->scope =& $this->data;
1516 + $cnt = count($this->scopeTree);
1517 + for ($i=0;$i<$cnt;$i++)
1518 + $this->scope =& $this->scope[$this->scopeTree[$i]];
1519 + } elseif ($bit === '__' || $bit === '_root') {
1520 + $this->scope =& $this->data;
1521 + $this->scopeTree = array();
1522 + } elseif (isset($this->scope[$bit])) {
1523 + $this->scope =& $this->scope[$bit];
1524 + $this->scopeTree[] = $bit;
1525 + } else {
1526 + unset($this->scope);
1527 + $this->scope = null;
1528 + }
1529 + }
1530 +
1531 + return $old;
1532 + }
1533 +
1534 + /**
1535 + * [runtime function] returns the entire data array
1536 + *
1537 + * @return array
1538 + */
1539 + public function getData()
1540 + {
1541 + return $this->data;
1542 + }
1543 +
1544 + /**
1545 + * [runtime function] returns a reference to the current scope
1546 + *
1547 + * @return &mixed
1548 + */
1549 + public function &getScope()
1550 + {
1551 + return $this->scope;
1552 + }
1553 +
1554 + /**
1555 + * Redirects all calls to unexisting to plugin proxy.
1556 + *
1557 + * @param string Method name
1558 + * @param array List of arguments
1559 + * @return mixed
1560 + */
1561 + public function __call($method, $args) {
1562 + $proxy = $this->getPluginProxy();
1563 + if (!$proxy) {
1564 + throw new Dwoo_Exception('Call to undefined method '.__CLASS__.'::'.$method.'()');
1565 + }
1566 + return call_user_func_array($proxy->getCallback($method), $args);
1567 + }
1568 +}
495dc1 9315dc a/lib/Dwoo/Filter.php
28 28 * constructor, if you override it, call parent::__construct($dwoo); or assign
29 29 * the dwoo instance yourself if you need it
30 30 *
31 - * @param Dwoo $dwoo the dwoo instance that runs this plugin
31 + * @param Dwoo_Core $dwoo the dwoo instance that runs this plugin
32 32 */
33 - public function __construct(Dwoo $dwoo)
33 + public function __construct(Dwoo_Core $dwoo)
34 34 {
35 35 $this->dwoo = $dwoo;
36 36 }
495dc1 9315dc a/lib/Dwoo/ICompiler.php
28 28 * @param string $templateStr the template to compile
29 29 * @return string a compiled php code string
30 30 */
31 - public function compile(Dwoo $dwoo, Dwoo_ITemplate $template);
31 + public function compile(Dwoo_Core $dwoo, Dwoo_ITemplate $template);
32 32
33 33 /**
34 34 * adds the custom plugins loaded into Dwoo to the compiler so it can load them
35 35 *
36 - * @see Dwoo::addPlugin
36 + * @see Dwoo_Core::addPlugin
37 37 * @param array $customPlugins an array of custom plugins
38 38 */
39 39 public function setCustomPlugins(array $customPlugins);
495dc1 9315dc a/lib/Dwoo/ITemplate.php
42 42 * returns the cached template output file name, true if it's cache-able but not cached
43 43 * or false if it's not cached
44 44 *
45 - * @param Dwoo $dwoo the dwoo instance that requests it
45 + * @param Dwoo_Core $dwoo the dwoo instance that requests it
46 46 * @return string|bool
47 47 */
48 - public function getCachedTemplate(Dwoo $dwoo);
48 + public function getCachedTemplate(Dwoo_Core $dwoo);
49 49
50 50 /**
51 51 * caches the provided output into the cache file
52 52 *
53 - * @param Dwoo $dwoo the dwoo instance that requests it
53 + * @param Dwoo_Core $dwoo the dwoo instance that requests it
54 54 * @param string $output the template output
55 55 * @return mixed full path of the cached file or false upon failure
56 56 */
57 - public function cache(Dwoo $dwoo, $output);
57 + public function cache(Dwoo_Core $dwoo, $output);
58 58
59 59 /**
60 60 * clears the cached template if it's older than the given time
61 61 *
62 - * @param Dwoo $dwoo the dwoo instance that was used to cache that template
62 + * @param Dwoo_Core $dwoo the dwoo instance that was used to cache that template
63 63 * @param int $olderThan minimum time (in seconds) required for the cache to be cleared
64 64 * @return bool true if the cache was not present or if it was deleted, false if it remains there
65 65 */
66 - public function clearCache(Dwoo $dwoo, $olderThan = -1);
66 + public function clearCache(Dwoo_Core $dwoo, $olderThan = -1);
67 67
68 68 /**
69 69 * returns the compiled template file name
70 70 *
71 - * @param Dwoo $dwoo the dwoo instance that requests it
71 + * @param Dwoo_Core $dwoo the dwoo instance that requests it
72 72 * @param Dwoo_ICompiler $compiler the compiler that must be used
73 73 * @return string
74 74 */
75 - public function getCompiledTemplate(Dwoo $dwoo, Dwoo_ICompiler $compiler = null);
75 + public function getCompiledTemplate(Dwoo_Core $dwoo, Dwoo_ICompiler $compiler = null);
76 76
77 77 /**
78 78 * returns the template name
147 147 * an include, extends or any other plugin)
148 148 * @return Dwoo_ITemplate|null|false
149 149 */
150 - public static function templateFactory(Dwoo $dwoo, $resourceId, $cacheTime = null, $cacheId = null, $compileId = null, Dwoo_ITemplate $parentTemplate = null);
150 + public static function templateFactory(Dwoo_Core $dwoo, $resourceId, $cacheTime = null, $cacheId = null, $compileId = null, Dwoo_ITemplate $parentTemplate = null);
151 151 }
495dc1 9315dc a/lib/Dwoo/Loader.php
49 49 $this->cacheDir = rtrim($cacheDir, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
50 50
51 51 // include class paths or rebuild paths if the cache file isn't there
52 - $cacheFile = $this->cacheDir.'classpath.cache.d'.Dwoo::RELEASE_TAG.'.php';
52 + $cacheFile = $this->cacheDir.'classpath.cache.d'.Dwoo_Core::RELEASE_TAG.'.php';
53 53 if (file_exists($cacheFile)) {
54 54 $classpath = file_get_contents($cacheFile);
55 55 $this->classPath = unserialize($classpath) + $this->classPath;
103 103 // a new class was added or the include failed so we rebuild the cache
104 104 if (!isset($this->classPath[$class]) || !(include $this->classPath[$class])) {
105 105 if ($forceRehash) {
106 - $this->rebuildClassPathCache($this->corePluginDir, $this->cacheDir . 'classpath.cache.d'.Dwoo::RELEASE_TAG.'.php');
106 + $this->rebuildClassPathCache($this->corePluginDir, $this->cacheDir . 'classpath.cache.d'.Dwoo_Core::RELEASE_TAG.'.php');
107 107 foreach ($this->paths as $path=>$file) {
108 108 $this->rebuildClassPathCache($path, $file);
109 109 }
136 136 if (!$pluginDir) {
137 137 throw new Dwoo_Exception('Plugin directory does not exist or can not be read : '.$pluginDirectory);
138 138 }
139 - $cacheFile = $this->cacheDir . 'classpath-'.substr(strtr($pluginDir, '/\\:'.PATH_SEPARATOR, '----'), strlen($pluginDir) > 80 ? -80 : 0).'.d'.Dwoo::RELEASE_TAG.'.php';
139 + $cacheFile = $this->cacheDir . 'classpath-'.substr(strtr($pluginDir, '/\\:'.PATH_SEPARATOR, '----'), strlen($pluginDir) > 80 ? -80 : 0).'.d'.Dwoo_Core::RELEASE_TAG.'.php';
140 140 $this->paths[$pluginDir] = $cacheFile;
141 141 if (file_exists($cacheFile)) {
142 142 $classpath = file_get_contents($cacheFile);
495dc1 9315dc a/lib/Dwoo/Plugin.php
31 31 * constructor, if you override it, call parent::__construct($dwoo); or assign
32 32 * the dwoo instance yourself if you need it
33 33 *
34 - * @param Dwoo $dwoo the dwoo instance that runs this plugin
34 + * @param Dwoo_Core $dwoo the dwoo instance that runs this plugin
35 35 */
36 - public function __construct(Dwoo $dwoo)
36 + public function __construct(Dwoo_Core $dwoo)
37 37 {
38 38 $this->dwoo = $dwoo;
39 39 }
495dc1 9315dc a/lib/Dwoo/Processor.php
28 28 * constructor, if you override it, call parent::__construct($dwoo); or assign
29 29 * the dwoo instance yourself if you need it
30 30 *
31 - * @param Dwoo $dwoo the dwoo instance that runs this plugin
31 + * @param Dwoo_Core $dwoo the dwoo instance that runs this plugin
32 32 */
33 33 public function __construct(Dwoo_Compiler $compiler)
34 34 {
495dc1 9315dc a/lib/Dwoo/Smarty/Adapter.php
30 30 * @date 2009-07-18
31 31 * @package Dwoo
32 32 */
33 -class Dwoo_Smarty__Adapter extends Dwoo
33 +class Dwoo_Smarty__Adapter extends Dwoo_Core
34 34 {
35 35 // magic get/set/call functions that handle unsupported features
36 36 public function __set($p, $v)
495dc1 9315dc a/lib/Dwoo/Template/File.php
170 170 * returns a new template object from the given include name, null if no include is
171 171 * possible (resource not found), or false if include is not permitted by this resource type
172 172 *
173 - * @param Dwoo $dwoo the dwoo instance requiring it
173 + * @param Dwoo_Core $dwoo the dwoo instance requiring it
174 174 * @param mixed $resourceId the filename (relative to this template's dir) of the template to include
175 175 * @param int $cacheTime duration of the cache validity for this template,
176 176 * if null it defaults to the Dwoo instance that will
184 184 * an include, extends or any other plugin)
185 185 * @return Dwoo_Template_File|null
186 186 */
187 - public static function templateFactory(Dwoo $dwoo, $resourceId, $cacheTime = null, $cacheId = null, $compileId = null, Dwoo_ITemplate $parentTemplate = null)
187 + public static function templateFactory(Dwoo_Core $dwoo, $resourceId, $cacheTime = null, $cacheId = null, $compileId = null, Dwoo_ITemplate $parentTemplate = null)
188 188 {
189 189 if (DIRECTORY_SEPARATOR === '\\') {
190 190 $resourceId = str_replace(array("\t", "\n", "\r", "\f", "\v"), array('\\t', '\\n', '\\r', '\\f', '\\v'), $resourceId);
245 245 * returns the full compiled file name and assigns a default value to it if
246 246 * required
247 247 *
248 - * @param Dwoo $dwoo the dwoo instance that requests the file name
248 + * @param Dwoo_Core $dwoo the dwoo instance that requests the file name
249 249 * @return string the full path to the compiled file
250 250 */
251 - protected function getCompiledFilename(Dwoo $dwoo)
251 + protected function getCompiledFilename(Dwoo_Core $dwoo)
252 252 {
253 253 // no compile id was provided, set default
254 254 if ($this->compileId===null) {
255 255 $this->compileId = str_replace('../', '__', strtr($this->getResourceIdentifier(), '\\:', '/-'));
256 256 }
257 - return $dwoo->getCompileDir() . $this->compileId.'.d'.Dwoo::RELEASE_TAG.'.php';
257 + return $dwoo->getCompileDir() . $this->compileId.'.d'.Dwoo_Core::RELEASE_TAG.'.php';
258 258 }
259 259
260 260 /**
495dc1 9315dc a/lib/Dwoo/Template/String.php
243 243 * returns the cached template output file name, true if it's cache-able but not cached
244 244 * or false if it's not cached
245 245 *
246 - * @param Dwoo $dwoo the dwoo instance that requests it
246 + * @param Dwoo_Core $dwoo the dwoo instance that requests it
247 247 * @return string|bool
248 248 */
249 - public function getCachedTemplate(Dwoo $dwoo)
249 + public function getCachedTemplate(Dwoo_Core $dwoo)
250 250 {
251 251 if ($this->cacheTime !== null) {
252 252 $cacheLength = $this->cacheTime;
277 277 /**
278 278 * caches the provided output into the cache file
279 279 *
280 - * @param Dwoo $dwoo the dwoo instance that requests it
280 + * @param Dwoo_Core $dwoo the dwoo instance that requests it
281 281 * @param string $output the template output
282 282 * @return mixed full path of the cached file or false upon failure
283 283 */
284 - public function cache(Dwoo $dwoo, $output)
284 + public function cache(Dwoo_Core $dwoo, $output)
285 285 {
286 286 $cacheDir = $dwoo->getCacheDir();
287 287 $cachedFile = $this->getCacheFilename($dwoo);
318 318 /**
319 319 * clears the cached template if it's older than the given time
320 320 *
321 - * @param Dwoo $dwoo the dwoo instance that was used to cache that template
321 + * @param Dwoo_Core $dwoo the dwoo instance that was used to cache that template
322 322 * @param int $olderThan minimum time (in seconds) required for the cache to be cleared
323 323 * @return bool true if the cache was not present or if it was deleted, false if it remains there
324 324 */
325 - public function clearCache(Dwoo $dwoo, $olderThan = -1)
325 + public function clearCache(Dwoo_Core $dwoo, $olderThan = -1)
326 326 {
327 327 $cachedFile = $this->getCacheFilename($dwoo);
328 328
332 332 /**
333 333 * returns the compiled template file name
334 334 *
335 - * @param Dwoo $dwoo the dwoo instance that requests it
335 + * @param Dwoo_Core $dwoo the dwoo instance that requests it
336 336 * @param Dwoo_ICompiler $compiler the compiler that must be used
337 337 * @return string
338 338 */
339 - public function getCompiledTemplate(Dwoo $dwoo, Dwoo_ICompiler $compiler = null)
339 + public function getCompiledTemplate(Dwoo_Core $dwoo, Dwoo_ICompiler $compiler = null)
340 340 {
341 341 $compiledFile = $this->getCompiledFilename($dwoo);
342 342
391 391 /**
392 392 * returns a new template string object with the resource id being the template source code
393 393 *
394 - * @param Dwoo $dwoo the dwoo instance requiring it
394 + * @param Dwoo_Core $dwoo the dwoo instance requiring it
395 395 * @param mixed $resourceId the filename (relative to this template's dir) of the template to include
396 396 * @param int $cacheTime duration of the cache validity for this template,
397 397 * if null it defaults to the Dwoo instance that will
405 405 * an include, extends or any other plugin)
406 406 * @return Dwoo_Template_String
407 407 */
408 - public static function templateFactory(Dwoo $dwoo, $resourceId, $cacheTime = null, $cacheId = null, $compileId = null, Dwoo_ITemplate $parentTemplate = null)
408 + public static function templateFactory(Dwoo_Core $dwoo, $resourceId, $cacheTime = null, $cacheId = null, $compileId = null, Dwoo_ITemplate $parentTemplate = null)
409 409 {
410 410 return new self($resourceId, $cacheTime, $cacheId, $compileId);
411 411 }
414 414 * returns the full compiled file name and assigns a default value to it if
415 415 * required
416 416 *
417 - * @param Dwoo $dwoo the dwoo instance that requests the file name
417 + * @param Dwoo_Core $dwoo the dwoo instance that requests the file name
418 418 * @return string the full path to the compiled file
419 419 */
420 - protected function getCompiledFilename(Dwoo $dwoo)
420 + protected function getCompiledFilename(Dwoo_Core $dwoo)
421 421 {
422 422 // no compile id was provided, set default
423 423 if ($this->compileId===null) {
424 424 $this->compileId = $this->name;
425 425 }
426 - return $dwoo->getCompileDir() . $this->compileId.'.d'.Dwoo::RELEASE_TAG.'.php';
426 + return $dwoo->getCompileDir() . $this->compileId.'.d'.Dwoo_Core::RELEASE_TAG.'.php';
427 427 }
428 428
429 429 /**
430 430 * returns the full cached file name and assigns a default value to it if
431 431 * required
432 432 *
433 - * @param Dwoo $dwoo the dwoo instance that requests the file name
433 + * @param Dwoo_Core $dwoo the dwoo instance that requests the file name
434 434 * @return string the full path to the cached file
435 435 */
436 - protected function getCacheFilename(Dwoo $dwoo)
436 + protected function getCacheFilename(Dwoo_Core $dwoo)
437 437 {
438 438 // no cache id provided, use request_uri as default
439 439 if ($this->cacheId === null) {
495dc1 9315dc a/lib/dwooAutoload.php
2 2 <?php
3 3
4 -include dirname(__FILE__) . DIRECTORY_SEPARATOR . 'Dwoo.php';
4 +include dirname(__FILE__) . DIRECTORY_SEPARATOR . 'Dwoo'. DIRECTORY_SEPARATOR . 'Core.php';
5 5
6 6 function dwooAutoload($class)
7 7 {
8 - if (substr($class, 0, 5) === 'Dwoo_') {
8 + if (substr($class, 0, 5) === 'Dwoo_' || $class === 'Dwoo') {
9 9 include DWOO_DIRECTORY . strtr($class, '_', DIRECTORY_SEPARATOR).'.php';
10 10 }
11 11 }
495dc1 9315dc a/lib/plugins/builtin/blocks/smartyinterface.php
26 26 $pluginType = $params['__functype'];
27 27 $params = $params['*'];
28 28
29 - if ($pluginType & Dwoo::CUSTOM_PLUGIN) {
29 + if ($pluginType & Dwoo_Core::CUSTOM_PLUGIN) {
30 30 $customPlugins = $compiler->getDwoo()->getCustomPlugins();
31 31 $callback = $customPlugins[$func]['callback'];
32 32 if (is_array($callback)) {
495dc1 9315dc a/lib/plugins/builtin/blocks/template.php
52 52
53 53 public static function postProcessing(Dwoo_Compiler $compiler, array $params, $prepend, $append, $content)
54 54 {
55 - $paramstr = 'Dwoo $dwoo';
55 + $paramstr = 'Dwoo_Core $dwoo';
56 56 $init = 'static $_callCnt = 0;'."\n".
57 57 '$dwoo->scope[\' '.$params['uuid'].'\'.$_callCnt] = array();'."\n".
58 58 '$_scope = $dwoo->setScope(array(\' '.$params['uuid'].'\'.($_callCnt++)));'."\n";
495dc1 9315dc a/lib/plugins/builtin/filters/html_format.php
44 44 * Those tags must however contain only htmlentities-escaped text for everything to work properly.
45 45 * Inline tags are presented on a single line with their content
46 46 *
47 - * @param Dwoo $dwoo the dwoo instance rendering this
47 + * @param Dwoo_Core $dwoo the dwoo instance rendering this
48 48 * @param string $input the xhtml to format
49 49 * @return string formatted xhtml
50 50 */
495dc1 9315dc a/lib/plugins/builtin/functions/capitalize.php
18 18 * @date 2009-07-18
19 19 * @package Dwoo
20 20 */
21 -function Dwoo_Plugin_capitalize(Dwoo $dwoo, $value, $numwords=false)
21 +function Dwoo_Plugin_capitalize(Dwoo_Core $dwoo, $value, $numwords=false)
22 22 {
23 23 if ($numwords || preg_match('#^[^0-9]+$#',$value))
24 24 {
495dc1 9315dc a/lib/plugins/builtin/functions/date_format.php
19 19 * @date 2008-12-24
20 20 * @package Dwoo
21 21 */
22 -function Dwoo_Plugin_date_format(Dwoo $dwoo, $value, $format='%b %e, %Y', $default=null)
22 +function Dwoo_Plugin_date_format(Dwoo_Core $dwoo, $value, $format='%b %e, %Y', $default=null)
23 23 {
24 24 if (!empty($value)) {
25 25 // convert if it's not a valid unix timestamp
495dc1 9315dc a/lib/plugins/builtin/functions/escape.php
19 19 * @date 2008-10-23
20 20 * @package Dwoo
21 21 */
22 -function Dwoo_Plugin_escape(Dwoo $dwoo, $value='', $format='html', $charset=null)
22 +function Dwoo_Plugin_escape(Dwoo_Core $dwoo, $value='', $format='html', $charset=null)
23 23 {
24 24 if ($charset === null) {
25 25 $charset = $dwoo->getCharset();
495dc1 9315dc a/lib/plugins/builtin/functions/eval.php
25 25 * @date 2008-10-23
26 26 * @package Dwoo
27 27 */
28 -function Dwoo_Plugin_eval(Dwoo $dwoo, $var, $assign = null)
28 +function Dwoo_Plugin_eval(Dwoo_Core $dwoo, $var, $assign = null)
29 29 {
30 30 if ($var == '') {
31 31 return;
495dc1 9315dc a/lib/plugins/builtin/functions/fetch.php
18 18 * @date 2009-07-18
19 19 * @package Dwoo
20 20 */
21 -function Dwoo_Plugin_fetch(Dwoo $dwoo, $file, $assign = null)
21 +function Dwoo_Plugin_fetch(Dwoo_Core $dwoo, $file, $assign = null)
22 22 {
23 23 if ($file === '') {
24 24 return;
495dc1 9315dc a/lib/plugins/builtin/functions/include.php
23 23 * @date 2009-07-18
24 24 * @package Dwoo
25 25 */
26 -function Dwoo_Plugin_include(Dwoo $dwoo, $file, $cache_time = null, $cache_id = null, $compile_id = null, $data = '_root', $assign = null, array $rest = array())
26 +function Dwoo_Plugin_include(Dwoo_Core $dwoo, $file, $cache_time = null, $cache_id = null, $compile_id = null, $data = '_root', $assign = null, array $rest = array())
27 27 {
28 28 if ($file === '') {
29 29 return;
495dc1 9315dc a/lib/plugins/builtin/functions/mailto.php
25 25 * @date 2009-07-18
26 26 * @package Dwoo
27 27 */
28 -function Dwoo_Plugin_mailto(Dwoo $dwoo, $address, $text=null, $subject=null, $encode=null, $cc=null, $bcc=null, $newsgroups=null, $followupto=null, $extra=null)
28 +function Dwoo_Plugin_mailto(Dwoo_Core $dwoo, $address, $text=null, $subject=null, $encode=null, $cc=null, $bcc=null, $newsgroups=null, $followupto=null, $extra=null)
29 29 {
30 30 if (empty($address)) {
31 31 return '';
495dc1 9315dc a/lib/plugins/builtin/functions/regex_replace.php
19 19 * @date 2008-10-23
20 20 * @package Dwoo
21 21 */
22 -function Dwoo_Plugin_regex_replace(Dwoo $dwoo, $value, $search, $replace)
22 +function Dwoo_Plugin_regex_replace(Dwoo_Core $dwoo, $value, $search, $replace)
23 23 {
24 24 $search = (array) $search;
25 25 $cnt = count($search);
495dc1 9315dc a/lib/plugins/builtin/functions/reverse.php
18 18 * @date 2008-10-23
19 19 * @package Dwoo
20 20 */
21 -function Dwoo_Plugin_reverse(Dwoo $dwoo, $value, $preserve_keys=false)
21 +function Dwoo_Plugin_reverse(Dwoo_Core $dwoo, $value, $preserve_keys=false)
22 22 {
23 23 if (is_array($value)) {
24 24 return array_reverse($value, $preserve_keys);
495dc1 9315dc a/lib/plugins/builtin/functions/truncate.php
21 21 * @date 2009-07-18
22 22 * @package Dwoo
23 23 */
24 -function Dwoo_Plugin_truncate(Dwoo $dwoo, $value, $length=80, $etc='...', $break=false, $middle=false)
24 +function Dwoo_Plugin_truncate(Dwoo_Core $dwoo, $value, $length=80, $etc='...', $break=false, $middle=false)
25 25 {
26 26 if ($length == 0) {
27 27 return '';
495dc1 9315dc a/tests/BlockTests.php
12 12 {
13 13 // extend this class and override this in your constructor to test a modded compiler
14 14 $this->compiler = new Dwoo_Compiler();
15 - $this->dwoo = new Dwoo(DWOO_COMPILE_DIR, DWOO_CACHE_DIR);
15 + $this->dwoo = new Dwoo_Core(DWOO_COMPILE_DIR, DWOO_CACHE_DIR);
16 16 }
17 17
18 18 public function testA()
57 57 $tpl->forceCompilation();
58 58
59 59 $this->assertEquals("a&lt;b&gt;ca<b>c", $this->dwoo->get($tpl, array('foo'=>'a<b>c'), $cmp));
60 -
60 +
61 61 $cmp->setAutoEscape(false);
62 62 $tpl = new Dwoo_Template_String('{$foo}{auto_escape true}{$foo}{/}');
63 63 $tpl->forceCompilation();
495dc1 9315dc a/tests/BugTests.php
11 11 public function __construct()
12 12 {
13 13 $this->compiler = new Dwoo_Compiler();
14 - $this->dwoo = new Dwoo(DWOO_COMPILE_DIR, DWOO_CACHE_DIR);
14 + $this->dwoo = new Dwoo_Core(DWOO_COMPILE_DIR, DWOO_CACHE_DIR);
15 15 }
16 16
17 17 public function testBlockStackBufferingBug()
495dc1 9315dc a/tests/CallTests.php
12 12 {
13 13 // extend this class and override this in your constructor to test a modded compiler
14 14 $this->compiler = new Dwoo_Compiler();
15 - $this->dwoo = new Dwoo(DWOO_COMPILE_DIR, DWOO_CACHE_DIR);
15 + $this->dwoo = new Dwoo_Core(DWOO_COMPILE_DIR, DWOO_CACHE_DIR);
16 16 }
17 17
18 18 public function testCustomFunctionPlugin()
93 93 }
94 94 }
95 95
96 -function plugin_custom_name(Dwoo $dwoo, $foo, $bar="bar")
96 +function plugin_custom_name(Dwoo_Core $dwoo, $foo, $bar="bar")
97 97 {
98 98 return $foo.$bar;
99 99 }
495dc1 9315dc a/tests/CompilerTests.php
13 13 {
14 14 // extend this class and override this in your constructor to test a modded compiler
15 15 $this->compiler = new Dwoo_Compiler();
16 - $this->dwoo = new Dwoo(DWOO_COMPILE_DIR, DWOO_CACHE_DIR);
16 + $this->dwoo = new Dwoo_Core(DWOO_COMPILE_DIR, DWOO_CACHE_DIR);
17 17 }
18 18
19 19 public function testVarReplacement()
255 255 if (!defined('TEST')) {
256 256 define('TEST', 'Test');
257 257 }
258 - $tpl = new Dwoo_Template_String('{$dwoo.const.TEST} {$dwoo.const.Dwoo::FUNC_PLUGIN*$dwoo.const.Dwoo::BLOCK_PLUGIN}');
258 + $tpl = new Dwoo_Template_String('{$dwoo.const.TEST} {$dwoo.const.Dwoo_Core::FUNC_PLUGIN*$dwoo.const.Dwoo_Core::BLOCK_PLUGIN}');
259 259 $tpl->forceCompilation();
260 260
261 - $this->assertEquals(TEST.' '.(Dwoo::FUNC_PLUGIN*Dwoo::BLOCK_PLUGIN), $this->dwoo->get($tpl, array(), $this->compiler));
261 + $this->assertEquals(TEST.' '.(Dwoo_Core::FUNC_PLUGIN*Dwoo_Core::BLOCK_PLUGIN), $this->dwoo->get($tpl, array(), $this->compiler));
262 262 }
263 263
264 264 public function testShortConstants()
266 266 if (!defined('TEST')) {
267 267 define('TEST', 'Test');
268 268 }
269 - $tpl = new Dwoo_Template_String('{%TEST} {$dwoo.const.Dwoo::FUNC_PLUGIN*%Dwoo::BLOCK_PLUGIN}');
269 + $tpl = new Dwoo_Template_String('{%TEST} {$dwoo.const.Dwoo_Core::FUNC_PLUGIN*%Dwoo_Core::BLOCK_PLUGIN}');
270 270 $tpl->forceCompilation();
271 271
272 - $this->assertEquals(TEST.' '.(Dwoo::FUNC_PLUGIN*Dwoo::BLOCK_PLUGIN), $this->dwoo->get($tpl, array(), $this->compiler));
272 + $this->assertEquals(TEST.' '.(Dwoo_Core::FUNC_PLUGIN*Dwoo_Core::BLOCK_PLUGIN), $this->dwoo->get($tpl, array(), $this->compiler));
273 273 }
274 274
275 275 public function testShortClassConstants()
418 418
419 419 public function testSetStringValToTrueWhenUsingNamedParams()
420 420 {
421 - $this->dwoo->addPlugin('test', create_function('Dwoo $dwoo, $name, $bool=false', 'return $bool ? $name."!" : $name."?";'));
421 + $this->dwoo->addPlugin('test', create_function('Dwoo_Core $dwoo, $name, $bool=false', 'return $bool ? $name."!" : $name."?";'));
422 422 $tpl = new Dwoo_Template_String('{test name="Name"}{test name="Name" bool}');
423 423 $tpl->forceCompilation();
424 424
562 562 {
563 563 $tpl = new Dwoo_Template_String('{getobj()->foo()->Bar("hoy") getobj()->moo}');
564 564 $tpl->forceCompilation();
565 - $dwoo = new Dwoo(DWOO_COMPILE_DIR, DWOO_CACHE_DIR);
565 + $dwoo = new Dwoo_Core(DWOO_COMPILE_DIR, DWOO_CACHE_DIR);
566 566 $dwoo->addPlugin('getobj', array(new PluginHelper(), 'call'));
567 567
568 568 $this->assertEquals('HOYyay', $dwoo->get($tpl, array(), $this->compiler));
571 571 public function testPluginProxy()
572 572 {
573 573 $proxy = new ProxyHelper('baz',true,3);
574 - $dwoo = new Dwoo(DWOO_COMPILE_DIR, DWOO_CACHE_DIR);
574 + $dwoo = new Dwoo_Core(DWOO_COMPILE_DIR, DWOO_CACHE_DIR);
575 575 $dwoo->setPluginProxy($proxy);
576 576 $tpl = new Dwoo_Template_String('{TestProxy("baz", true, 3)}');
577 577 $tpl->forceCompilation();
583 583 {
584 584 $tpl = new Dwoo_Template_String('{getobj()->instance->Bar("hoy")}');
585 585 $tpl->forceCompilation();
586 - $dwoo = new Dwoo(DWOO_COMPILE_DIR, DWOO_CACHE_DIR);
586 + $dwoo = new Dwoo_Core(DWOO_COMPILE_DIR, DWOO_CACHE_DIR);
587 587 $dwoo->addPlugin('getobj', array(new PluginHelper(), 'call'));
588 588
589 589 $this->assertEquals('HOY', $dwoo->get($tpl, array(), $this->compiler));
774 774 $this->instance = $this;
775 775 }
776 776
777 - public function callWithDwoo(Dwoo $dwoo)
777 + public function callWithDwoo(Dwoo_Core $dwoo)
778 778 {
779 779 return $this;
780 780 }
495dc1 9315dc a/tests/CoreTests.php
11 11 public function __construct()
12 12 {
13 13 $this->compiler = new Dwoo_Compiler();
14 - $this->dwoo = new Dwoo(DWOO_COMPILE_DIR, DWOO_CACHE_DIR);
14 + $this->dwoo = new Dwoo_Core(DWOO_COMPILE_DIR, DWOO_CACHE_DIR);
15 15 }
16 16
17 17 public function testCoverConstructorsEtc()
18 18 {
19 19 // extend this class and override this in your constructor to test a modded compiler
20 20 $this->compiler = new Dwoo_Compiler();
21 - $this->dwoo = new Dwoo(DWOO_COMPILE_DIR, DWOO_CACHE_DIR);
21 + $this->dwoo = new Dwoo_Core(DWOO_COMPILE_DIR, DWOO_CACHE_DIR);
22 22 $tpl = new Dwoo_Template_String('');
23 23 $tpl->forceCompilation();
24 24 $this->assertEquals("", $this->dwoo->get($tpl, array(), $this->compiler));
88 88
89 89 public function testGetSetSecurityPolicy()
90 90 {
91 - $dwoo = new Dwoo(DWOO_COMPILE_DIR, DWOO_CACHE_DIR);
91 + $dwoo = new Dwoo_Core(DWOO_COMPILE_DIR, DWOO_CACHE_DIR);
92 92 $policy = new Dwoo_Security_Policy();
93 93 $policy->setConstantHandling(Dwoo_Security_Policy::CONST_ALLOW);
94 94 $dwoo->setSecurityPolicy($policy);
251 251
252 252 public function testCachedTemplateWithDwoo_Cache()
253 253 {
254 - $dwoo = new Dwoo(DWOO_COMPILE_DIR, DWOO_CACHE_DIR);
254 + $dwoo = new Dwoo_Core(DWOO_COMPILE_DIR, DWOO_CACHE_DIR);
255 255 $dwoo->setCacheTime(10);
256 256 $tpl = new Dwoo_Template_String('foo{$foo}bar', null, 'cachetest2');
257 257 $tpl->forceCompilation();
265 265
266 266 public function testClearCacheOnTemplateClass()
267 267 {
268 - $dwoo = new Dwoo(DWOO_COMPILE_DIR, DWOO_CACHE_DIR);
268 + $dwoo = new Dwoo_Core(DWOO_COMPILE_DIR, DWOO_CACHE_DIR);
269 269 $dwoo->setCacheTime(10);
270 270 $tpl = new Dwoo_Template_String('foo{$foo}bar', null, 'cachetest2');
271 271 $tpl->forceCompilation();
280 280
281 281 public function testTemplateGetSet()
282 282 {
283 - $dwoo = new Dwoo(DWOO_COMPILE_DIR, DWOO_CACHE_DIR);
283 + $dwoo = new Dwoo_Core(DWOO_COMPILE_DIR, DWOO_CACHE_DIR);
284 284 $dwoo->setCacheTime(10);
285 285 $tpl = new Dwoo_Template_String('foo');
286 286 $tpl2 = new Dwoo_Template_File('./resources/test.html');
295 295 public function testPluginProxyGetSet()
296 296 {
297 297 $proxy = new ProxyHelper;
298 - $dwoo = new Dwoo();
298 + $dwoo = new Dwoo_Core();
299 299 $dwoo->setPluginProxy($proxy);
300 300 $this->assertEquals($proxy, $dwoo->getPluginProxy());
301 301 }
302 302
303 303 public function testIsArrayArray()
304 304 {
305 - $dwoo = new Dwoo();
305 + $dwoo = new Dwoo_Core();
306 306 $data = array();
307 307 $this->assertEquals(true, $dwoo->isArray($data));
308 308 $this->assertEquals(0, $dwoo->isArray($data, true));
313 313
314 314 public function testIsArrayArrayAccess()
315 315 {
316 - $dwoo = new Dwoo();
316 + $dwoo = new Dwoo_Core();
317 317 $data = new TestArrayAccess(array());
318 318 $this->assertEquals(true, $dwoo->isArray($data));
319 319 $this->assertEquals(0, $dwoo->isArray($data, true));
324 324
325 325 public function testIsArrayCountableArrayAccess()
326 326 {
327 - $dwoo = new Dwoo();
327 + $dwoo = new Dwoo_Core();
328 328 $data = new TestCountableArrayAccess(array());
329 329 $this->assertEquals(true, $dwoo->isArray($data));
330 330 $this->assertEquals(0, $dwoo->isArray($data, true));
335 335
336 336 public function testIsTraversableIterator()
337 337 {
338 - $dwoo = new Dwoo();
338 + $dwoo = new Dwoo_Core();
339 339 $data = new TestIterator(array());
340 340 $this->assertEquals(true, $dwoo->isTraversable($data));
341 341 $this->assertEquals(0, $dwoo->isTraversable($data, true));
346 346
347 347 public function testIsTraversableCountableIterator()
348 348 {
349 - $dwoo = new Dwoo();
349 + $dwoo = new Dwoo_Core();
350 350 $data = new TestCountableIterator(array());
351 351 $this->assertEquals(true, $dwoo->isTraversable($data));
352 352 $this->assertEquals(0, $dwoo->isTraversable($data, true));
495dc1 9315dc a/tests/DataTests.php
12 12 {
13 13 // extend this class and override this in your constructor to test a modded compiler
14 14 $this->compiler = new Dwoo_Compiler();
15 - $this->dwoo = new Dwoo(DWOO_COMPILE_DIR, DWOO_CACHE_DIR);
15 + $this->dwoo = new Dwoo_Core(DWOO_COMPILE_DIR, DWOO_CACHE_DIR);
16 16 $this->tpl = new Dwoo_Template_String('{$var}{$var2}{$var3}{$var4}');
17 17 $this->tpl->forceCompilation();
18 18 }
495dc1 9315dc a/tests/DwooTests.php
30 30 public static function suite() {
31 31 PHPUnit_Util_Filter::addDirectoryToWhitelist(DWOO_DIRECTORY.'plugins/builtin');
32 32 PHPUnit_Util_Filter::addDirectoryToWhitelist(DWOO_DIRECTORY.'Dwoo');
33 - PHPUnit_Util_Filter::addFileToWhitelist(DWOO_DIRECTORY.'Dwoo.php');
34 33 PHPUnit_Util_Filter::removeDirectoryFromWhitelist(DWOO_DIRECTORY.'Dwoo/Adapters');
35 34
36 35 $suite = new self('Dwoo - Unit Tests Report');
495dc1 9315dc a/tests/FiltersTests.php
12 12 {
13 13 // extend this class and override this in your constructor to test a modded compiler
14 14 $this->compiler = new Dwoo_Compiler();
15 - $this->dwoo = new Dwoo(DWOO_COMPILE_DIR, DWOO_CACHE_DIR);
15 + $this->dwoo = new Dwoo_Core(DWOO_COMPILE_DIR, DWOO_CACHE_DIR);
16 16 }
17 17
18 18 public function testHtmlFormat()
20 20 $tpl = new Dwoo_Template_String("<html><body><div><p>a<em>b</em>c<hr /></p><textarea>a\n b</textarea></div></body><html>");
21 21 $tpl->forceCompilation();
22 22
23 - $dwoo = new Dwoo(DWOO_COMPILE_DIR, DWOO_CACHE_DIR);
23 + $dwoo = new Dwoo_Core(DWOO_COMPILE_DIR, DWOO_CACHE_DIR);
24 24 $dwoo->addFilter('html_format', true);
25 25
26 26 $this->assertEquals(str_replace("\r", '', <<<SNIPPET
47 47 $cmp = new Dwoo_Compiler();
48 48 $cmp->addPreProcessor('smarty_compat', true);
49 49
50 - $this->assertEquals('{'.Dwoo::VERSION.'}', $this->dwoo->get($tpl, array(), $cmp));
50 + $this->assertEquals('{'.Dwoo_Core::VERSION.'}', $this->dwoo->get($tpl, array(), $cmp));
51 51 }
52 52 }
495dc1 9315dc a/tests/FuncTests.php
12 12 {
13 13 // extend this class and override this in your constructor to test a modded compiler
14 14 $this->compiler = new Dwoo_Compiler();
15 - $this->dwoo = new Dwoo(DWOO_COMPILE_DIR, DWOO_CACHE_DIR);
15 + $this->dwoo = new Dwoo_Core(DWOO_COMPILE_DIR, DWOO_CACHE_DIR);
16 16 }
17 17
18 18 public function testAssign()
495dc1 9315dc a/tests/HelperTests.php
12 12 {
13 13 // extend this class and override this in your constructor to test a modded compiler
14 14 $this->compiler = new Dwoo_Compiler();
15 - $this->dwoo = new Dwoo(DWOO_COMPILE_DIR, DWOO_CACHE_DIR);
15 + $this->dwoo = new Dwoo_Core(DWOO_COMPILE_DIR, DWOO_CACHE_DIR);
16 16 }
17 17
18 18 public function testArray()
495dc1 9315dc a/tests/LoaderTests.php
12 12 {
13 13 // extend this class and override this in your constructor to test a modded compiler
14 14 $this->compiler = new Dwoo_Compiler();
15 - $this->dwoo = new Dwoo(DWOO_COMPILE_DIR, DWOO_CACHE_DIR);
15 + $this->dwoo = new Dwoo_Core(DWOO_COMPILE_DIR, DWOO_CACHE_DIR);
16 16 }
17 17
18 18 public function testLoaderGetSet()
19 19 {
20 - $dwoo = new Dwoo(DWOO_COMPILE_DIR, DWOO_CACHE_DIR);
20 + $dwoo = new Dwoo_Core(DWOO_COMPILE_DIR, DWOO_CACHE_DIR);
21 21 $loader = new Dwoo_Loader(TEST_DIRECTORY.'/temp/cache');
22 22
23 23 $dwoo->setLoader($loader);
26 26
27 27 public function testPluginLoad()
28 28 {
29 - $dwoo = new Dwoo(DWOO_COMPILE_DIR, DWOO_CACHE_DIR);
29 + $dwoo = new Dwoo_Core(DWOO_COMPILE_DIR, DWOO_CACHE_DIR);
30 30 $loader = new Dwoo_Loader(TEST_DIRECTORY.'/temp/cache');
31 31
32 32 $dwoo->setLoader($loader);
39 39
40 40 public function testRebuildClassPath()
41 41 {
42 - $dwoo = new Dwoo(DWOO_COMPILE_DIR, DWOO_CACHE_DIR);
42 + $dwoo = new Dwoo_Core(DWOO_COMPILE_DIR, DWOO_CACHE_DIR);
43 43 $loader = new Dwoo_Loader(TEST_DIRECTORY.'/temp/cache');
44 44
45 45 $dwoo->setLoader($loader);
46 46 $loader->addDirectory(TEST_DIRECTORY.'/resources/plugins');
47 - file_put_contents(TEST_DIRECTORY.'/resources/plugins/loaderTest2.php', '<?php function Dwoo_Plugin_loaderTest2(Dwoo $dwoo) { return "It works!"; }');
47 + file_put_contents(TEST_DIRECTORY.'/resources/plugins/loaderTest2.php', '<?php function Dwoo_Plugin_loaderTest2(Dwoo_Core $dwoo) { return "It works!"; }');
48 48
49 49 $tpl = new Dwoo_Template_String('{loaderTest2}');
50 50 $tpl->forceCompilation();
495dc1 9315dc a/tests/PluginProxyTests.php
11 11 public function setUp()
12 12 {
13 13 $this->compiler = new Dwoo_Compiler();
14 - $this->dwoo = new Dwoo(DWOO_COMPILE_DIR, DWOO_CACHE_DIR);
14 + $this->dwoo = new Dwoo_Core(DWOO_COMPILE_DIR, DWOO_CACHE_DIR);
15 15 }
16 16
17 17 public function tearDown()
495dc1 9315dc a/tests/PluginTypesTests.php
12 12 {
13 13 // extend this class and override this in your constructor to test a modded compiler
14 14 $this->compiler = new Dwoo_Compiler();
15 - $this->dwoo = new Dwoo(DWOO_COMPILE_DIR, DWOO_CACHE_DIR);
15 + $this->dwoo = new Dwoo_Core(DWOO_COMPILE_DIR, DWOO_CACHE_DIR);
16 16 }
17 17
18 18 // Functions - Dwoo style
84 84 // Functions - Custom style
85 85 public function testCustomCompilableFunctionPlugin()
86 86 {
87 - $dwoo = new Dwoo(DWOO_COMPILE_DIR, DWOO_CACHE_DIR);
87 + $dwoo = new Dwoo_Core(DWOO_COMPILE_DIR, DWOO_CACHE_DIR);
88 88 $dwoo->addPlugin('CustomCompilableFunctionPlugin', 'custom_compilable_plugin', true);
89 89 $tpl = new Dwoo_Template_String('{CustomCompilableFunctionPlugin 4 5}');
90 90 $tpl->forceCompilation();
94 94
95 95 public function testCustomCompilableFunctionPluginAsModifier()
96 96 {
97 - $dwoo = new Dwoo(DWOO_COMPILE_DIR, DWOO_CACHE_DIR);
97 + $dwoo = new Dwoo_Core(DWOO_COMPILE_DIR, DWOO_CACHE_DIR);
98 98 $dwoo->addPlugin('CustomCompilableFunctionPlugin', 'custom_compilable_plugin', true);
99 99 $tpl = new Dwoo_Template_String('{$foo=4}{$foo|CustomCompilableFunctionPlugin:5}');
100 100 $tpl->forceCompilation();
104 104
105 105 public function testCustomFunctionPlugin()
106 106 {
107 - $dwoo = new Dwoo(DWOO_COMPILE_DIR, DWOO_CACHE_DIR);
107 + $dwoo = new Dwoo_Core(DWOO_COMPILE_DIR, DWOO_CACHE_DIR);
108 108 $dwoo->addPlugin('CustomFunctionPlugin', 'custom_plugin');
109 109 $tpl = new Dwoo_Template_String('{CustomFunctionPlugin 4 5}');
110 110 $tpl->forceCompilation();
114 114
115 115 public function testCustomFunctionPluginAsModifier()
116 116 {
117 - $dwoo = new Dwoo(DWOO_COMPILE_DIR, DWOO_CACHE_DIR);
117 + $dwoo = new Dwoo_Core(DWOO_COMPILE_DIR, DWOO_CACHE_DIR);
118 118 $dwoo->addPlugin('CustomFunctionPlugin', 'custom_plugin');
119 119 $tpl = new Dwoo_Template_String('{$foo=4}{$foo|CustomFunctionPlugin:5}');
120 120 $tpl->forceCompilation();
125 125 // Classes - Custom style - Static
126 126 public function testCustomCompilableClassPlugin()
127 127 {
128 - $dwoo = new Dwoo(DWOO_COMPILE_DIR, DWOO_CACHE_DIR);
128 + $dwoo = new Dwoo_Core(DWOO_COMPILE_DIR, DWOO_CACHE_DIR);
129 129 $dwoo->addPlugin('CustomCompilableClassPlugin', array('custom_compilable_class_plugin', 'call'), true);
130 130 $tpl = new Dwoo_Template_String('{CustomCompilableClassPlugin 4 5}');
131 131 $tpl->forceCompilation();
135 135
136 136 public function testCustomCompilableClassPluginAsModifier()
137 137 {
138 - $dwoo = new Dwoo(DWOO_COMPILE_DIR, DWOO_CACHE_DIR);
138 + $dwoo = new Dwoo_Core(DWOO_COMPILE_DIR, DWOO_CACHE_DIR);
139 139 $dwoo->addPlugin('CustomCompilableClassPlugin', array('custom_compilable_class_plugin', 'call'), true);
140 140 $tpl = new Dwoo_Template_String('{$foo=4}{$foo|CustomCompilableClassPlugin:5}');
141 141 $tpl->forceCompilation();
145 145
146 146 public function testCustomClassPlugin()
147 147 {
148 - $dwoo = new Dwoo(DWOO_COMPILE_DIR, DWOO_CACHE_DIR);
148 + $dwoo = new Dwoo_Core(DWOO_COMPILE_DIR, DWOO_CACHE_DIR);
149 149 $dwoo->addPlugin('CustomClassPlugin', array('custom_class_plugin', 'call'));
150 150 $tpl = new Dwoo_Template_String('{CustomClassPlugin 4 5}');
151 151 $tpl->forceCompilation();
155 155
156 156 public function testCustomClassPluginAsModifier()
157 157 {
158 - $dwoo = new Dwoo(DWOO_COMPILE_DIR, DWOO_CACHE_DIR);
158 + $dwoo = new Dwoo_Core(DWOO_COMPILE_DIR, DWOO_CACHE_DIR);
159 159 $dwoo->addPlugin('CustomClassPlugin', array('custom_class_plugin', 'call'));
160 160 $tpl = new Dwoo_Template_String('{$foo=4}{$foo|CustomClassPlugin:5}');
161 161 $tpl->forceCompilation();
166 166 // Classes - Custom style - Instance
167 167 public function testCustomCompilableClassPluginInstance()
168 168 {
169 - $dwoo = new Dwoo(DWOO_COMPILE_DIR, DWOO_CACHE_DIR);
169 + $dwoo = new Dwoo_Core(DWOO_COMPILE_DIR, DWOO_CACHE_DIR);
170 170 $dwoo->addPlugin('CustomCompilableClassPlugin', array(new custom_compilable_class_plugin_obj(), 'call'), true);
171 171 $tpl = new Dwoo_Template_String('{CustomCompilableClassPlugin 4 5}');
172 172 $tpl->forceCompilation();
176 176
177 177 public function testCustomCompilableClassPluginInstanceAsModifier()
178 178 {
179 - $dwoo = new Dwoo(DWOO_COMPILE_DIR, DWOO_CACHE_DIR);
179 + $dwoo = new Dwoo_Core(DWOO_COMPILE_DIR, DWOO_CACHE_DIR);
180 180 $dwoo->addPlugin('CustomCompilableClassPlugin', array(new custom_compilable_class_plugin_obj(), 'call'), true);
181 181 $tpl = new Dwoo_Template_String('{$foo=4}{$foo|CustomCompilableClassPlugin:5}');
182 182 $tpl->forceCompilation();
186 186
187 187 public function testCustomClassPluginInstance()
188 188 {
189 - $dwoo = new Dwoo(DWOO_COMPILE_DIR, DWOO_CACHE_DIR);
189 + $dwoo = new Dwoo_Core(DWOO_COMPILE_DIR, DWOO_CACHE_DIR);
190 190 $dwoo->addPlugin('CustomClassPlugin', array(new custom_class_plugin_obj(), 'call'));
191 191 $tpl = new Dwoo_Template_String('{CustomClassPlugin 4 5}');
192 192 $tpl->forceCompilation();
196 196
197 197 public function testCustomClassPluginInstanceAsModifier()
198 198 {
199 - $dwoo = new Dwoo(DWOO_COMPILE_DIR, DWOO_CACHE_DIR);
199 + $dwoo = new Dwoo_Core(DWOO_COMPILE_DIR, DWOO_CACHE_DIR);
200 200 $dwoo->addPlugin('CustomClassPlugin', array(new custom_class_plugin_obj(), 'call'));
201 201 $tpl = new Dwoo_Template_String('{$foo=4}{$foo|CustomClassPlugin:5}');
202 202 $tpl->forceCompilation();
210 210 return "$number * $number2";
211 211 }
212 212
213 -function Dwoo_Plugin_FunctionPlugin(Dwoo $dwoo, $number, $number2)
213 +function Dwoo_Plugin_FunctionPlugin(Dwoo_Core $dwoo, $number, $number2)
214 214 {
215 215 return $number * $number2;
216 216 }
236 236 return "$number * $number2";
237 237 }
238 238
239 -function custom_plugin(Dwoo $dwoo, $number, $number2)
239 +function custom_plugin(Dwoo_Core $dwoo, $number, $number2)
240 240 {
241 241 return $number * $number2;
242 242 }
495dc1 9315dc a/tests/SecurityTests.php
14 14 public function __construct()
15 15 {
16 16 $this->compiler = new Dwoo_Compiler();
17 - $this->dwoo = new Dwoo(DWOO_COMPILE_DIR, DWOO_CACHE_DIR);
17 + $this->dwoo = new Dwoo_Core(DWOO_COMPILE_DIR, DWOO_CACHE_DIR);
18 18 $this->policy = new Dwoo_Security_Policy();
19 19 $this->dwoo->setSecurityPolicy($this->policy);
20 20 }
495dc1 9315dc a/tests/SmartyTests.php
23 23 public function testSmartyCompat()
24 24 {
25 25 $this->dwoo->assign('arr', array('ab','cd','ef'));
26 - $this->assertEquals('{'.Dwoo::VERSION.'} ab cd ef', $this->dwoo->fetch('smartytest.html'));
26 + $this->assertEquals('{'.Dwoo_Core::VERSION.'} ab cd ef', $this->dwoo->fetch('smartytest.html'));
27 27 }
28 28 }
495dc1 9315dc a/tests/TemplateTests.php
11 11 public function __construct()
12 12 {
13 13 $this->compiler = new Dwoo_Compiler();
14 - $this->dwoo = new Dwoo(DWOO_COMPILE_DIR, DWOO_CACHE_DIR);
14 + $this->dwoo = new Dwoo_Core(DWOO_COMPILE_DIR, DWOO_CACHE_DIR);
15 15 }
16 16
17 17 public function testIncludePath()
495dc1 9315dc a/tests/resources/plugins/loaderTest.php
2 2 <?php
3 3
4 -function Dwoo_Plugin_loaderTest(Dwoo $dwoo)
4 +function Dwoo_Plugin_loaderTest(Dwoo_Core $dwoo)
5 5 {
6 6 return 'Moo';
7 7 }