Commit 7d4083db by Seldaek

* Adding a bunch of util functions for advanced sub-template hacking

git-svn-id: svn://dwoo.org/dwoo/trunk@237 0598d79b-80c4-4d41-97ba-ac86fbbd088b
parent 1e5416f2
...@@ -490,6 +490,16 @@ class Dwoo_Compiler implements Dwoo_ICompiler ...@@ -490,6 +490,16 @@ class Dwoo_Compiler implements Dwoo_ICompiler
} }
/** /**
* marks a template plugin as being called, which means its source must be included in the compiled template
*
* @param string $name function name
*/
public function useTemplatePlugin($name)
{
$this->templatePlugins[$name]['called'] = true;
}
/**
* adds the custom plugins loaded into Dwoo to the compiler so it can load them * adds the custom plugins loaded into Dwoo to the compiler so it can load them
* *
* @see Dwoo::addPlugin * @see Dwoo::addPlugin
...@@ -828,6 +838,11 @@ class Dwoo_Compiler implements Dwoo_ICompiler ...@@ -828,6 +838,11 @@ class Dwoo_Compiler implements Dwoo_ICompiler
} }
$output .= $compiled."\n?>"; $output .= $compiled."\n?>";
foreach ($this->templatePlugins as $function => $attr) {
if (isset($attr['called']) && $attr['called'] === true && !isset($attr['checked'])) {
$this->resolveSubTemplateDependencies($function);
}
}
foreach ($this->templatePlugins as $function) { foreach ($this->templatePlugins as $function) {
if (isset($function['called']) && $function['called'] === true) { if (isset($function['called']) && $function['called'] === true) {
$output .= $function['body']; $output .= $function['body'];
...@@ -857,6 +872,23 @@ class Dwoo_Compiler implements Dwoo_ICompiler ...@@ -857,6 +872,23 @@ class Dwoo_Compiler implements Dwoo_ICompiler
} }
/** /**
* checks what sub-templates are used in every sub-template so that we're sure they are all compiled
*
* @param string $function the sub-template name
*/
protected function resolveSubTemplateDependencies($function)
{
$body = $this->templatePlugins[$function]['body'];
foreach ($this->templatePlugins as $func => $attr) {
if ($func !== $function && !isset($attr['called']) && strpos($body, 'Dwoo_Plugin_'.$func) !== false) {
$this->templatePlugins[$func]['called'] = true;
$this->resolveSubTemplateDependencies($func);
}
}
$this->templatePlugins[$function]['checked'] = true;
}
/**
* adds compiled content to the current block * adds compiled content to the current block
* *
* @param string $content the content to push * @param string $content the content to push
...@@ -1627,43 +1659,23 @@ class Dwoo_Compiler implements Dwoo_ICompiler ...@@ -1627,43 +1659,23 @@ class Dwoo_Compiler implements Dwoo_ICompiler
// transforms the parameter array from (x=>array('paramname'=>array(values))) to (paramname=>array(values)) // transforms the parameter array from (x=>array('paramname'=>array(values))) to (paramname=>array(values))
$map = array(); $map = array();
foreach ($this->templatePlugins[$func]['params'] as $param=>$defValue) { foreach ($this->templatePlugins[$func]['params'] as $param=>$defValue) {
$map[] = array($param, $defValue !== null, $defValue); if ($param == 'rest') {
} $param = '*';
$ps = array();
foreach ($params as $p) {
if (is_array($p[1])) {
$ps[$p[0]] = $p[1];
} else {
$ps[] = $p;
} }
} $hasDefault = $defValue !== null;
if ($defValue == 'null') {
$paramlist = array(); $defValue = null;
// loops over the param map and assigns values from the template or default value for unset optional params } elseif ($defValue == 'false') {
while (list($k,$v) = each($map)) { $defValue = false;
if (isset($ps[$v[0]])) { } elseif ($defValue == 'true') {
// parameter is defined as named param $defValue = true;
$paramlist[$v[0]] = $ps[$v[0]]; } elseif (preg_match('#^([\'"]).*?\1$#', $defValue)) {
unset($ps[$v[0]]); $defValue = substr($defValue, 1, -1);
} elseif (isset($ps[$k])) {
// parameter is defined as ordered param
$paramlist[$v[0]] = $ps[$k];
unset($ps[$k]);
} elseif ($v[1]===false) {
// parameter is not defined and not optional, throw error
throw new Dwoo_Compilation_Exception($this, 'Argument '.$k.'/'.$v[0].' missing for template function '.$func);
} elseif ($v[2]===null) {
// enforce lowercased null if default value is null (php outputs NULL with var export)
$paramlist[$v[0]] = array('null', null);
} else {
// outputs default value with var_export
$paramlist[$v[0]] = array($v[2], $v[2]);
} }
$map[] = array($param, $hasDefault, $defValue);
} }
$params = $paramlist; $params = $this->mapParams($params, null, $state, $map);
unset($ps, $map, $paramlist, $v, $k);
} }
// only keep php-syntax-safe values for non-block plugins // only keep php-syntax-safe values for non-block plugins
...@@ -2849,6 +2861,18 @@ class Dwoo_Compiler implements Dwoo_ICompiler ...@@ -2849,6 +2861,18 @@ class Dwoo_Compiler implements Dwoo_ICompiler
} }
/** /**
* allows a plugin to load another one at compile time, this will also mark
* it as used by this template so it will be loaded at runtime (which can be
* useful for compiled plugins that rely on another plugin when their compiled
* code runs)
*
* @param string $name the plugin name
*/
public function loadPlugin($name) {
$this->getPluginType($name);
}
/**
* runs htmlentities over the matched <?php ?> blocks when the security policy enforces that * runs htmlentities over the matched <?php ?> blocks when the security policy enforces that
* *
* @param array $match matched php block * @param array $match matched php block
...@@ -2865,11 +2889,14 @@ class Dwoo_Compiler implements Dwoo_ICompiler ...@@ -2865,11 +2889,14 @@ class Dwoo_Compiler implements Dwoo_ICompiler
* @param array $params the array of parameters * @param array $params the array of parameters
* @param callback $callback the function or method to reflect on to find out the required parameters * @param callback $callback the function or method to reflect on to find out the required parameters
* @param int $callType the type of call in the template, 0 = no params, 1 = php-style call, 2 = named parameters call * @param int $callType the type of call in the template, 0 = no params, 1 = php-style call, 2 = named parameters call
* @param array $map the parameter map to use, if not provided it will be built from the callback
* @return array parameters sorted in the correct order with missing optional parameters filled * @return array parameters sorted in the correct order with missing optional parameters filled
*/ */
protected function mapParams(array $params, $callback, $callType=2) protected function mapParams(array $params, $callback, $callType=2, $map = null)
{ {
$map = $this->getParamMap($callback); if (!$map) {
$map = $this->getParamMap($callback);
}
$paramlist = array(); $paramlist = array();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment