Commit 8c5c98bf by Seldaek

+ Adds Plugin Proxy functionality

git-svn-id: svn://dwoo.org/dwoo/trunk@130 0598d79b-80c4-4d41-97ba-ac86fbbd088b
parent e01426a6
...@@ -12,6 +12,10 @@ ...@@ -12,6 +12,10 @@
with the creation of compilable xml/html-related plugins with the creation of compilable xml/html-related plugins
+ Syntax: Static methods can be called using {Class::method()} + Syntax: Static methods can be called using {Class::method()}
+ Compiler supports method calls into a method call's parameters + Compiler supports method calls into a method call's parameters
+ API: Added Dwoo->setPluginProxy() and Dwoo_IPluginProxy that allow you to
hook into the compiler's plugin subsystem to provide your own plugin calls.
Thanks to Denis Arh for the patch
=> http://forum.dwoo.org/viewtopic.php?id=70
* Syntax: Math expressions in strings are now only allowed when the entire * Syntax: Math expressions in strings are now only allowed when the entire
expression is delimited, e.g. {"/$foo/$bar"} evaluates as just that while expression is delimited, e.g. {"/$foo/$bar"} evaluates as just that while
{"/`$foo/$bar`"} will result in "/".($foo/$bar), therefore processing the / {"/`$foo/$bar`"} will result in "/".($foo/$bar), therefore processing the /
......
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
define('DWOO_DIRECTORY', dirname(__FILE__).DIRECTORY_SEPARATOR); define('DWOO_DIRECTORY', dirname(__FILE__).DIRECTORY_SEPARATOR);
set_include_path(get_include_path() . PATH_SEPARATOR . DWOO_DIRECTORY); set_include_path(get_include_path() . PATH_SEPARATOR . DWOO_DIRECTORY);
include 'Dwoo/IPluginProxy.php';
include 'Dwoo/ILoader.php'; include 'Dwoo/ILoader.php';
include 'Dwoo/Loader.php'; include 'Dwoo/Loader.php';
include 'Dwoo/Exception.php'; include 'Dwoo/Exception.php';
...@@ -97,6 +98,7 @@ class Dwoo ...@@ -97,6 +98,7 @@ class Dwoo
const SMARTY_MODIFIER = 64; const SMARTY_MODIFIER = 64;
const SMARTY_BLOCK = 128; const SMARTY_BLOCK = 128;
const SMARTY_FUNCTION = 256; const SMARTY_FUNCTION = 256;
const PROXY_PLUGIN = 512;
/**#@-*/ /**#@-*/
/** /**
...@@ -268,6 +270,13 @@ class Dwoo ...@@ -268,6 +270,13 @@ class Dwoo
protected $buffer; protected $buffer;
/** /**
* stores plugin proxy
*
* @var Dwoo_IPluginProxy
*/
protected $pluginProxy;
/**
* constructor, sets the cache and compile dir to the default values if not provided * constructor, sets the cache and compile dir to the default values if not provided
* *
* @param string $compileDir path to the compiled directory, defaults to lib/compiled * @param string $compileDir path to the compiled directory, defaults to lib/compiled
...@@ -804,6 +813,25 @@ class Dwoo ...@@ -804,6 +813,25 @@ class Dwoo
return $this->securityPolicy; return $this->securityPolicy;
} }
/**
* sets the object that must be used as a plugin proxy when plugin can't be found
* by dwoo's loader
*
* @param Dwoo_IPluginProxy $pluginProxy the proxy object
*/
public function setPluginProxy(Dwoo_IPluginProxy $pluginProxy) {
$this->pluginProxy = $pluginProxy;
}
/**
* returns the current plugin proxy object or null by default
*
* @param Dwoo_IPluginProxy|null the proxy object if any
*/
public function getPluginProxy() {
return $this->pluginProxy;
}
/* /*
* --------- util functions --------- * --------- util functions ---------
*/ */
......
...@@ -201,6 +201,13 @@ class Dwoo_Compiler implements Dwoo_ICompiler ...@@ -201,6 +201,13 @@ class Dwoo_Compiler implements Dwoo_ICompiler
protected $curBlock; protected $curBlock;
/** /**
* current dwoo object that uses this compiler, or null
*
* @var Dwoo
*/
protected $dwoo;
/**
* holds an instance of this class, used by getInstance when you don't * holds an instance of this class, used by getInstance when you don't
* provide a custom compiler in order to save resources * provide a custom compiler in order to save resources
* *
...@@ -713,7 +720,10 @@ class Dwoo_Compiler implements Dwoo_ICompiler ...@@ -713,7 +720,10 @@ class Dwoo_Compiler implements Dwoo_ICompiler
// build plugin preloader // build plugin preloader
foreach ($this->usedPlugins as $plugin=>$type) { foreach ($this->usedPlugins as $plugin=>$type) {
if ($type & Dwoo::CUSTOM_PLUGIN) continue; if (($type & Dwoo::CUSTOM_PLUGIN) || ($type & Dwoo::PROXY_PLUGIN)) {
continue;
}
switch($type) { switch($type) {
case Dwoo::BLOCK_PLUGIN: case Dwoo::BLOCK_PLUGIN:
...@@ -1356,7 +1366,7 @@ class Dwoo_Compiler implements Dwoo_ICompiler ...@@ -1356,7 +1366,7 @@ class Dwoo_Compiler implements Dwoo_ICompiler
} }
// funcs // funcs
if ($pluginType & Dwoo::NATIVE_PLUGIN || $pluginType & Dwoo::SMARTY_FUNCTION || $pluginType & Dwoo::SMARTY_BLOCK) { if ($pluginType & Dwoo::NATIVE_PLUGIN || $pluginType & Dwoo::PROXY_PLUGIN || $pluginType & Dwoo::SMARTY_FUNCTION || $pluginType & Dwoo::SMARTY_BLOCK) {
$params = $this->mapParams($params, null, $state); $params = $this->mapParams($params, null, $state);
} elseif ($pluginType & Dwoo::CLASS_PLUGIN) { } elseif ($pluginType & Dwoo::CLASS_PLUGIN) {
if ($pluginType & Dwoo::CUSTOM_PLUGIN) { if ($pluginType & Dwoo::CUSTOM_PLUGIN) {
...@@ -1439,6 +1449,12 @@ class Dwoo_Compiler implements Dwoo_ICompiler ...@@ -1439,6 +1449,12 @@ class Dwoo_Compiler implements Dwoo_ICompiler
$output = '$this->classCall(\''.$func.'\', array('.$params.'))'; $output = '$this->classCall(\''.$func.'\', array('.$params.'))';
} }
} }
} elseif ($pluginType & Dwoo::PROXY_PLUGIN) {
if (isset($params['*'])) {
$output = call_user_func_array(array($this->dwoo->getPluginProxy(), $func), $params['*']);
} else {
$output = call_user_func(array($this->dwoo->getPluginProxy(), $func));
}
} elseif ($pluginType & Dwoo::SMARTY_FUNCTION) { } elseif ($pluginType & Dwoo::SMARTY_FUNCTION) {
$params = $this->implode_r($params['*'], true); $params = $this->implode_r($params['*'], true);
...@@ -2308,6 +2324,10 @@ class Dwoo_Compiler implements Dwoo_ICompiler ...@@ -2308,6 +2324,10 @@ class Dwoo_Compiler implements Dwoo_ICompiler
} else { } else {
$output = $func.'('.$params.')'; $output = $func.'('.$params.')';
} }
} elseif ($pluginType & Dwoo::PROXY_PLUGIN) {
$params = $this->mapParams($params, null, $state);
$params = $params['*'][0];
$output = call_user_func_array(array($this->dwoo->getPluginProxy(), $func), $params);
} elseif ($pluginType & Dwoo::SMARTY_MODIFIER) { } elseif ($pluginType & Dwoo::SMARTY_MODIFIER) {
$params = $this->mapParams($params, null, $state); $params = $this->mapParams($params, null, $state);
$params = $params['*'][0]; $params = $params['*'][0];
...@@ -2476,6 +2496,9 @@ class Dwoo_Compiler implements Dwoo_ICompiler ...@@ -2476,6 +2496,9 @@ class Dwoo_Compiler implements Dwoo_ICompiler
} catch (Exception $e) { } catch (Exception $e) {
if (isset($phpFunc)) { if (isset($phpFunc)) {
$pluginType = Dwoo::NATIVE_PLUGIN; $pluginType = Dwoo::NATIVE_PLUGIN;
} elseif ($this->dwoo->getPluginProxy() && $this->dwoo->getPluginProxy()->loadPlugin($name)) {
$pluginType = Dwoo::PROXY_PLUGIN;
break;
} else { } else {
throw $e; throw $e;
} }
...@@ -2487,7 +2510,7 @@ class Dwoo_Compiler implements Dwoo_ICompiler ...@@ -2487,7 +2510,7 @@ class Dwoo_Compiler implements Dwoo_ICompiler
} }
} }
if (($pluginType & Dwoo::COMPILABLE_PLUGIN) === 0 && ($pluginType & Dwoo::NATIVE_PLUGIN) === 0) { if (($pluginType & Dwoo::COMPILABLE_PLUGIN) === 0 && ($pluginType & Dwoo::NATIVE_PLUGIN) === 0 && ($pluginType & Dwoo::PROXY_PLUGIN) === 0) {
$this->usedPlugins[$name] = $pluginType; $this->usedPlugins[$name] = $pluginType;
} }
......
<?php
/**
* interface that represents a dwoo plugin proxy
*
* This software is provided 'as-is', without any express or implied warranty.
* In no event will the authors be held liable for any damages arising from the use of this software.
*
* This file is released under the LGPL
* "GNU Lesser General Public License"
* More information can be found here:
* {@link http://www.gnu.org/copyleft/lesser.html}
*
* @author Denis Arh <denis@arh.cc>
* @copyright Copyright (c) 2008, Denis Arh (this file + some patched bits here and there)
* @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
* @link http://dwoo.org/
* @version 0.9.1
* @date 2008-05-30
* @package Dwoo
*/
interface Dwoo_IPluginProxy
{
/**
* loads a plugin or returns false on failure
*
* @param string $name the plugin name
* @return bool true if the plugin was successfully loaded
*/
public function loadPlugin($name);
}
\ No newline at end of file
...@@ -527,13 +527,47 @@ replace="BAR" ...@@ -527,13 +527,47 @@ replace="BAR"
$this->assertEquals('a <?php echo "foo"; ?>', $this->dwoo->get($tpl, array('foo'=>'a <?php echo "foo"; ?>'), $this->compiler)); $this->assertEquals('a <?php echo "foo"; ?>', $this->dwoo->get($tpl, array('foo'=>'a <?php echo "foo"; ?>'), $this->compiler));
} }
public function testStaticMethodCall() public function testStaticMethodCall()
{ {
$tpl = new Dwoo_Template_String('{upper MethodCallsHelper::staticFoo(bar "baz")}'); $tpl = new Dwoo_Template_String('{upper MethodCallsHelper::staticFoo(bar "baz")}');
$tpl->forceCompilation(); $tpl->forceCompilation();
$this->assertEquals('-BAZBAR-', $this->dwoo->get($tpl, array(), $this->compiler)); $this->assertEquals('-BAZBAR-', $this->dwoo->get($tpl, array(), $this->compiler));
}
public function testPluginProxy()
{
$proxy = new ProxyHelper('baz',true,3);
$dwoo = new Dwoo();
$dwoo->setPluginProxy($proxy);
$tpl = new Dwoo_Template_String('{TestProxy("baz", true, 3)}');
$tpl->forceCompilation();
$this->assertEquals('valid', $dwoo->get($tpl, array(), $this->compiler));
}
}
class ProxyHelper implements Dwoo_IPluginProxy
{
public function __construct()
{
$this->params = func_get_args();
}
public function loadPlugin($name)
{
return $name === 'TestProxy';
}
public function checkTestProxy()
{
return func_get_args() === $this->params ? 'valid' : 'fubar';
}
public function __call($m, $p)
{
return '$this->getPluginProxy()->check'.$m.'('.implode(',', $p).')';
} }
} }
...@@ -559,7 +593,7 @@ class MethodCallsHelper { ...@@ -559,7 +593,7 @@ class MethodCallsHelper {
return ($int+5).$str; return ($int+5).$str;
} }
public function __toString() { return 'obj'; } public function __toString() { return 'obj'; }
public static function staticFoo($bar, $baz) { public static function staticFoo($bar, $baz) {
return "-$baz$bar-"; return "-$baz$bar-";
} }
......
...@@ -291,4 +291,12 @@ class CoreTests extends PHPUnit_Framework_TestCase ...@@ -291,4 +291,12 @@ class CoreTests extends PHPUnit_Framework_TestCase
$this->assertEquals(hash('md4','foo'), $tpl->getUid()); $this->assertEquals(hash('md4','foo'), $tpl->getUid());
} }
public function testPluginProxyGetSet()
{
$proxy = new ProxyHelper;
$dwoo = new Dwoo();
$dwoo->setPluginProxy($proxy);
$this->assertEquals($proxy, $dwoo->getPluginProxy());
}
} }
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