Author: Raymond Bosman
Date: 2006-08-30 14:02:42 +0200 (Wed, 30 Aug 2006)
New Revision: 3419
Log:
- Adding new extensions is done via the configuration class.
- New extensions are no longer specified via an extension object but by class
name. (The method calls were static anyway).
- Renamed the isNestingBlock property to hasCloseTag.
- Removed the custom_block_manager and custom_function_manager.
# It's always good to throw out useless managers..
Added:
trunk/Template/src/exceptions/custom_block_exception.php
trunk/Template/src/structs/custom_extension.php
Removed:
trunk/Template/src/custom_block_manager.php
trunk/Template/src/custom_function_manager.php
Modified:
trunk/Template/ChangeLog
trunk/Template/src/configuration.php
trunk/Template/src/functions/functions.php
trunk/Template/src/parsers/source_to_tst/implementations/custom_block.php
trunk/Template/src/parsers/tst_to_ast/implementations/tst_to_ast_transformer.php
trunk/Template/src/structs/custom_block_definition.php
trunk/Template/src/structs/custom_function_definition.php
trunk/Template/src/template.php
trunk/Template/src/template_autoload.php
trunk/Template/tests/custom_blocks/brainfuck.php
trunk/Template/tests/custom_blocks/testblocks.php
trunk/Template/tests/regression_test.php
Modified: trunk/Template/ChangeLog
===================================================================
--- trunk/Template/ChangeLog 2006-08-29 14:16:29 UTC (rev 3418)
+++ trunk/Template/ChangeLog 2006-08-30 12:02:42 UTC (rev 3419)
@@ -1,3 +1,5 @@
+- Added custom blocks and custom functions.
+
1.0 - Monday 12 June 2006
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Modified: trunk/Template/src/configuration.php
===================================================================
--- trunk/Template/src/configuration.php 2006-08-29 14:16:29 UTC (rev
3418)
+++ trunk/Template/src/configuration.php 2006-08-30 12:02:42 UTC (rev
3419)
@@ -56,6 +56,8 @@
'templatePath' => ".",
'compilePath' => ".",
'checkModifiedTemplates' => true,
+ 'customBlocks' => array(),
+ 'customFunctions' => array(),
);
/**
* Returns the value of the property $name.
@@ -78,6 +80,8 @@
case 'templatePath':
case 'compilePath':
case 'checkModifiedTemplates':
+ case 'customBlocks':
+ case 'customFunctions':
return $this->properties[$name];
default:
@@ -113,6 +117,8 @@
case 'templatePath':
case 'compilePath':
case 'checkModifiedTemplates':
+ case 'customBlocks':
+ case 'customFunctions':
$this->properties[$name] = $value;
break;
@@ -149,7 +155,7 @@
* All requested templates are search from the defined $templatePath.
* Use an empty string to fetch templates from the root of the filesystem.
*
- * All compiled templates are placed in subfulders under the compiled
templates.
+ * All compiled templates are placed in subfolders under the compiled
templates.
* Use an empty string to compile templates at the root of the filesystem.
*/
public function __construct( $templatePath = ".", $compilePath = ".",
$context = null )
@@ -174,5 +180,47 @@
return self::$instanceList[$name];
}
+
+
+ /**
+ * Adds custom tags or function to the customBlock or customFunction
property and
+ * indirectly add the custom extension to the template language.
+ *
+ * The parameter $customBlockClass expects a class that implements either
+ * the interface ezcTemplateCustomBlock, ezcTemplateCustomFunction, or
both.
+ *
+ * @param string $customClass
+ * @return void
+ */
+ public function addExtension( $customClass )
+ {
+ if( !is_string( $customClass ) )
+ {
+ throw new ezcTemplateCustomBlockException( "Could not add the
extension $customClass, because the given value is not a string." );
+ }
+
+ $implements = class_implements( $customClass );
+
+ $added = false;
+ if( in_array( "ezcTemplateCustomBlock", $implements ) )
+ {
+ $this->properties["customBlocks"][$customClass] = $customClass;
+ $added = true;
+ }
+
+ if( in_array( "ezcTemplateCustomFunction", $implements ) )
+ {
+ $this->properties["customFunctions"][$customClass] = $customClass;
+ $added = true;
+ }
+
+ if( !$added)
+ {
+ throw new ezcTemplateCustomBlockException( "Could not add the
extension $customClass. Does it implement ezcTemplateCustomBlock or
ezcTemplateCustomFunction?" );
+ }
+ }
+
+
+
}
?>
Deleted: trunk/Template/src/custom_block_manager.php
===================================================================
--- trunk/Template/src/custom_block_manager.php 2006-08-29 14:16:29 UTC (rev
3418)
+++ trunk/Template/src/custom_block_manager.php 2006-08-30 12:02:42 UTC (rev
3419)
@@ -1,52 +0,0 @@
-<?php
-
-class ezcTemplateCustomBlockManager
-{
- private static $instance = null;
-
- private $customBlockClasses = array();
-
- static public function getInstance()
- {
- if ( self::$instance === null)
- {
- self::$instance = new ezcTemplateCustomBlockManager();
- }
-
- return self::$instance;
- }
-
- public function reset()
- {
- $this->customBlockClasses = array();
- }
-
- public function addClass( ezcTemplateCustomBlock $classInstance )
- {
- $name = get_class( $classInstance );
-
- // Check for redeclaration.
- if ( isset( $this->customBlockClasses[ $name ] ) )
- {
- return false;
- }
-
- $this->customBlockClasses[ $name ] = $classInstance;
- return true;
- }
-
- public function getDefinition( $name )
- {
- foreach( $this->customBlockClasses as $class )
- {
- $def = $class->getCustomBlockDefinition( $name );
-
- if( $def instanceof ezcTemplateCustomBlockDefinition )
- return $def;
- }
-
- return false;
- }
-}
-
-?>
Deleted: trunk/Template/src/custom_function_manager.php
===================================================================
--- trunk/Template/src/custom_function_manager.php 2006-08-29 14:16:29 UTC
(rev 3418)
+++ trunk/Template/src/custom_function_manager.php 2006-08-30 12:02:42 UTC
(rev 3419)
@@ -1,53 +0,0 @@
-<?php
-
-class ezcTemplateCustomFunctionManager
-{
- private static $instance = null;
-
- private $customFunctionClasses = array();
-
- static public function getInstance()
- {
- if ( self::$instance === null)
- {
- self::$instance = new ezcTemplateCustomFunctionManager();
- }
-
- return self::$instance;
- }
-
- public function reset()
- {
- $this->customBlockClasses = array();
- }
-
- public function addClass( ezcTemplateCustomFunction $classInstance )
- {
- $name = get_class( $classInstance );
-
- // Check for redeclaration.
- if ( isset( $this->customFunctionClasses[ $name ] ) )
- {
- return false;
- }
-
- $this->customFunctionClasses[ $name ] = $classInstance;
- return true;
- }
-
- public function getDefinition( $name )
- {
-
- foreach( $this->customFunctionClasses as $class )
- {
- $def = $class->getCustomFunctionDefinition( $name );
-
- if( $def instanceof ezcTemplateCustomFunctionDefinition )
- return $def;
- }
-
- return false;
- }
-}
-
-?>
Added: trunk/Template/src/exceptions/custom_block_exception.php
===================================================================
--- trunk/Template/src/exceptions/custom_block_exception.php 2006-08-29
14:16:29 UTC (rev 3418)
+++ trunk/Template/src/exceptions/custom_block_exception.php 2006-08-30
12:02:42 UTC (rev 3419)
@@ -0,0 +1,28 @@
+<?php
+/**
+ * File containing the ezcTemplateCustomBlockException class
+ *
+ * @package Template
+ * @version //autogen//
+ * @copyright Copyright (C) 2005, 2006 eZ systems as. All rights reserved.
+ * @license http://ez.no/licenses/new_bsd New BSD License
+ */
+/**
+ *
+ * @package Template
+ * @version //autogen//
+ */
+class ezcTemplateCustomBlockException extends Exception
+{
+ /**
+ * Initialises the CustomBlock exception with the given message.
+ *
+ * @param string $message
+ */
+ public function __construct( $message )
+ {
+ parent::__construct( $message );
+ }
+
+}
+?>
Property changes on: trunk/Template/src/exceptions/custom_block_exception.php
___________________________________________________________________
Name: svn:eol-style
+ native
Modified: trunk/Template/src/functions/functions.php
===================================================================
--- trunk/Template/src/functions/functions.php 2006-08-29 14:16:29 UTC (rev
3418)
+++ trunk/Template/src/functions/functions.php 2006-08-30 12:02:42 UTC (rev
3419)
@@ -18,9 +18,11 @@
{
protected $functionToClass;
protected $errorMessage = "";
+ private $parser = null;
- public function __construct()
+ public function __construct( ezcTemplateParser $parser )
{
+ $this->parser = $parser;
$this->functionToClass = include( "function_to_class.php" );
}
@@ -189,6 +191,21 @@
return null;
}
+ public function getCustomFunction( $name )
+ {
+ foreach( $this->parser->template->configuration->customFunctions as
$class )
+ {
+ $def = call_user_func( array( $class,
"getCustomFunctionDefinition" ), $name );
+
+ if( $def instanceof ezcTemplateCustomFunctionDefinition )
+ {
+ return $def;
+ }
+ }
+
+ return false;
+ }
+
public function getAstTree( $functionName, $parameters )
{
// Try the built-in template functions.
@@ -202,7 +219,10 @@
}
// Try the custom template functions.
- $def = ezcTemplateCustomFunctionManager::getInstance()->getDefinition(
$functionName );
+// $def =
ezcTemplateCustomFunctionManager::getInstance()->getDefinition( $functionName );
+
+ $def = $this->getCustomFunction( $functionName );
+
if( $def !== false )
{
$givenParameters = sizeof( $parameters);
Modified:
trunk/Template/src/parsers/source_to_tst/implementations/custom_block.php
===================================================================
--- trunk/Template/src/parsers/source_to_tst/implementations/custom_block.php
2006-08-29 14:16:29 UTC (rev 3418)
+++ trunk/Template/src/parsers/source_to_tst/implementations/custom_block.php
2006-08-30 12:02:42 UTC (rev 3419)
@@ -26,6 +26,23 @@
$this->block = null;
}
+
+ function getCustomBlockDefinition( $name )
+ {
+ foreach( $this->parser->template->configuration->customBlocks as
$class )
+ {
+ $def = call_user_func( array( $class, "getCustomBlockDefinition"
), $name );
+
+ if( $def instanceof ezcTemplateCustomBlockDefinition )
+ {
+ return $def;
+ }
+ }
+
+ return false;
+ }
+
+
/**
* Parses the expression by using the
ezcTemplateExpressionSourceToTstParser class.
*/
@@ -70,7 +87,10 @@
$cursor->advance( strlen( $name ) );
$this->findNextElement( $cursor );
- $def = ezcTemplateCustomBlockManager::getInstance()->getDefinition(
$name );
+ //$def = ezcTemplateCustomBlockManager::getInstance()->getDefinition(
$name );
+
+ $def = $this->getCustomBlockDefinition( $name );
+
if( $def === false )
{
return false;
@@ -78,8 +98,7 @@
$cb = new ezcTemplateCustomBlockTstNode( $this->parser->source,
$this->startCursor, $cursor );
$cb->definition = $def;
- $this->block->isNestingBlock = $def->isNestingBlock;// $requireNesting;
- $cb->isNestingBlock = $def->isNestingBlock;
+ $this->block->isNestingBlock = $cb->isNestingBlock = $def->hasCloseTag;
if( isset( $def->startExpressionName ) && $def->startExpressionName !=
"" )
{
Modified:
trunk/Template/src/parsers/tst_to_ast/implementations/tst_to_ast_transformer.php
===================================================================
---
trunk/Template/src/parsers/tst_to_ast/implementations/tst_to_ast_transformer.php
2006-08-29 14:16:29 UTC (rev 3418)
+++
trunk/Template/src/parsers/tst_to_ast/implementations/tst_to_ast_transformer.php
2006-08-30 12:02:42 UTC (rev 3419)
@@ -49,7 +49,7 @@
public function __construct( $parser )
{
- $this->functions = new ezcTemplateFunctions();
+ $this->functions = new ezcTemplateFunctions( $parser );
$this->parser = $parser;
$this->outputVariable = new ezcTemplateOutputVariableManager();
}
@@ -191,7 +191,7 @@
$params->value[] = $value->accept($this);
}
- if( $def->isNestingBlock )
+ if( $def->hasCloseTag )
{
$this->outputVariable->push( $this->getUniqueVariableName(
"_ezcTemplate_custom" ) );
$iniCustom = $this->outputVariable->getInitializationAst();
Modified: trunk/Template/src/structs/custom_block_definition.php
===================================================================
--- trunk/Template/src/structs/custom_block_definition.php 2006-08-29
14:16:29 UTC (rev 3418)
+++ trunk/Template/src/structs/custom_block_definition.php 2006-08-30
12:02:42 UTC (rev 3419)
@@ -1,11 +1,12 @@
<?php
-class ezcTemplateCustomBlockDefinition extends ezcBaseStruct
+class ezcTemplateCustomBlockDefinition extends ezcTemplateCustomExtension
{
public $class;
public $method;
- public $isNestingBlock;
+ public $hasCloseTag;
+
public $hasStartExpression;
public $startExpressionName;
public $optionalParameters = array();
Added: trunk/Template/src/structs/custom_extension.php
===================================================================
--- trunk/Template/src/structs/custom_extension.php 2006-08-29 14:16:29 UTC
(rev 3418)
+++ trunk/Template/src/structs/custom_extension.php 2006-08-30 12:02:42 UTC
(rev 3419)
@@ -0,0 +1,8 @@
+<?php
+
+class ezcTemplateCustomExtension extends ezcBaseStruct
+{
+}
+
+
+?>
Property changes on: trunk/Template/src/structs/custom_extension.php
___________________________________________________________________
Name: svn:eol-style
+ native
Modified: trunk/Template/src/structs/custom_function_definition.php
===================================================================
--- trunk/Template/src/structs/custom_function_definition.php 2006-08-29
14:16:29 UTC (rev 3418)
+++ trunk/Template/src/structs/custom_function_definition.php 2006-08-30
12:02:42 UTC (rev 3419)
@@ -1,9 +1,10 @@
<?php
-class ezcTemplateCustomFunctionDefinition extends ezcBaseStruct
+class ezcTemplateCustomFunctionDefinition extends ezcTemplateCustomExtension
{
public $class;
public $method;
+
public $parameters = array();
}
Modified: trunk/Template/src/template.php
===================================================================
--- trunk/Template/src/template.php 2006-08-29 14:16:29 UTC (rev 3418)
+++ trunk/Template/src/template.php 2006-08-30 12:02:42 UTC (rev 3419)
@@ -111,7 +111,6 @@
if ( get_class( $this->properties[$name] ) !=
'ezcTemplateConfiguration' )
{
throw new Exception( "Static method
ezcTemplateConfiguration::getInstance() did not return an object of class
ezcTemplateConfiguration" );
- // $this->properties[$name] = new
ezcTemplateConfiguration();
}
}
return $this->properties[$name];
@@ -301,29 +300,6 @@
10, 36 );
}
- /**
- * Adds custom tags or function to the template language.
- *
- * The parameter $customBlockClass expects a class that implements either
- * the interface ezcTemplateCustomBlock, ezcTemplateCustomFunction, or
both.
- *
- * @param ezcTemplateCustomBlock|ezcTemplateCustomFunctoin
$customBlockClass
- * @return void
- */
- public function addExtension( $customClass )
- {
- if ( $customClass instanceof ezcTemplateCustomBlock )
- {
- ezcTemplateCustomBlockManager::getInstance()->addClass(
$customClass );
- }
-
- if ( $customClass instanceof ezcTemplateCustomFunction )
- {
- ezcTemplateCustomFunctionManager::getInstance()->addClass(
$customClass );
- }
- }
-
-
// /**
// * Locates the source template file named $source and returns an
// * ezcTemplateSource object which can be queried.
Modified: trunk/Template/src/template_autoload.php
===================================================================
--- trunk/Template/src/template_autoload.php 2006-08-29 14:16:29 UTC (rev
3418)
+++ trunk/Template/src/template_autoload.php 2006-08-30 12:02:42 UTC (rev
3419)
@@ -43,6 +43,7 @@
"ezcTemplateCustomBlock" =>
"Template/interfaces/custom_block.php",
"ezcTemplateCustomFunction" =>
"Template/interfaces/custom_function.php",
+ "ezcTemplateCustomExtension" =>
"Template/structs/custom_extension.php",
"ezcTemplateCustomBlockDefinition" =>
"Template/structs/custom_block_definition.php",
"ezcTemplateCustomFunctionDefinition" =>
"Template/structs/custom_function_definition.php",
@@ -310,6 +311,7 @@
"ezcTemplateTstNodeException" =>
"Template/exceptions/element_exception.php",
"ezcTemplateParserException" =>
"Template/exceptions/parser_exception.php",
+ "ezcTemplateCustomBlockException" =>
"Template/exceptions/custom_block_exception.php",
Modified: trunk/Template/tests/custom_blocks/brainfuck.php
===================================================================
--- trunk/Template/tests/custom_blocks/brainfuck.php 2006-08-29 14:16:29 UTC
(rev 3418)
+++ trunk/Template/tests/custom_blocks/brainfuck.php 2006-08-30 12:02:42 UTC
(rev 3419)
@@ -26,7 +26,7 @@
$def = new ezcTemplateCustomBlockDefinition();
$def->class = __CLASS__;
$def->method = "emulate";
- $def->isNestingBlock = true;
+ $def->hasCloseTag = true;
$def->requiredParameters = array();
$def->optionalParameters = array("buffer_size");
@@ -36,7 +36,7 @@
$def = new ezcTemplateCustomBlockDefinition();
$def->class = __CLASS__;
$def->method = "emulate_inline";
- $def->isNestingBlock = false;
+ $def->hasCloseTag = false;
$def->startExpressionName = "code";
$def->requiredParameters = array("code");
Modified: trunk/Template/tests/custom_blocks/testblocks.php
===================================================================
--- trunk/Template/tests/custom_blocks/testblocks.php 2006-08-29 14:16:29 UTC
(rev 3418)
+++ trunk/Template/tests/custom_blocks/testblocks.php 2006-08-30 12:02:42 UTC
(rev 3419)
@@ -30,6 +30,7 @@
}
}
+
public static function noParameters()
{
return "NoParameter";
@@ -53,7 +54,7 @@
$def = new ezcTemplateCustomBlockDefinition();
$def->class = __CLASS__;
$def->method = "reflectParameters";
- $def->isNestingBlock = true;
+ $def->hasCloseTag = true;
$def->startExpressionName = "";
$def->requiredParameters = array();
$def->optionalParameters = array("optional");
@@ -63,7 +64,7 @@
$def = new ezcTemplateCustomBlockDefinition();
$def->class = __CLASS__;
$def->method = "reflectParameters";
- $def->isNestingBlock = true;
+ $def->hasCloseTag = true;
$def->startExpressionName = "";
$def->requiredParameters = array("required");
$def->optionalParameters = array();
@@ -73,7 +74,7 @@
$def = new ezcTemplateCustomBlockDefinition();
$def->class = __CLASS__;
$def->method = "reflectParameters";
- $def->isNestingBlock = true;
+ $def->hasCloseTag = true;
$def->startExpressionName = "";
$def->requiredParameters = array("required");
$def->optionalParameters = array("optional");
@@ -83,7 +84,7 @@
$def = new ezcTemplateCustomBlockDefinition();
$def->class = __CLASS__;
$def->method = "reflectParameters";
- $def->isNestingBlock = true;
+ $def->hasCloseTag = true;
$def->startExpressionName = "start_expression";
$def->requiredParameters = array("start_expression");
$def->optionalParameters = array();
@@ -93,7 +94,7 @@
$def = new ezcTemplateCustomBlockDefinition();
$def->class = __CLASS__;
$def->method = "reflectParameters";
- $def->isNestingBlock = true;
+ $def->hasCloseTag = true;
$def->startExpressionName = "start_expression";
$def->requiredParameters = array();
$def->optionalParameters = array("start_expression");
@@ -103,7 +104,7 @@
$def = new ezcTemplateCustomBlockDefinition();
$def->class = __CLASS__;
$def->method = "reflectParameters";
- $def->isNestingBlock = true;
+ $def->hasCloseTag = true;
$def->startExpressionName = "start_expresssion";
$def->requiredParameters = array();
$def->optionalParameters = array("bla");
@@ -116,7 +117,7 @@
$def = new ezcTemplateCustomBlockDefinition();
$def->class = __CLASS__;
$def->method = "reflectParameters";
- $def->isNestingBlock = false;
+ $def->hasCloseTag = false;
$def->startExpressionName = "";
$def->requiredParameters = array();
$def->optionalParameters = array("optional");
@@ -126,7 +127,7 @@
$def = new ezcTemplateCustomBlockDefinition();
$def->class = __CLASS__;
$def->method = "reflectParameters";
- $def->isNestingBlock = false;
+ $def->hasCloseTag = false;
$def->startExpressionName = "";
$def->requiredParameters = array("required");
$def->optionalParameters = array();
@@ -136,7 +137,7 @@
$def = new ezcTemplateCustomBlockDefinition();
$def->class = __CLASS__;
$def->method = "reflectParameters";
- $def->isNestingBlock = false;
+ $def->hasCloseTag = false;
$def->startExpressionName = "";
$def->requiredParameters = array("required");
$def->optionalParameters = array("optional");
@@ -146,7 +147,7 @@
$def = new ezcTemplateCustomBlockDefinition();
$def->class = __CLASS__;
$def->method = "reflectParameters";
- $def->isNestingBlock = false;
+ $def->hasCloseTag = false;
$def->startExpressionName = "start_expression";
$def->requiredParameters = array("start_expression");
$def->optionalParameters = array();
@@ -156,7 +157,7 @@
$def = new ezcTemplateCustomBlockDefinition();
$def->class = __CLASS__;
$def->method = "reflectParameters";
- $def->isNestingBlock = false;
+ $def->hasCloseTag = false;
$def->startExpressionName = "start_expression";
$def->requiredParameters = array();
$def->optionalParameters = array("start_expression");
@@ -166,7 +167,7 @@
$def = new ezcTemplateCustomBlockDefinition();
$def->class = __CLASS__;
$def->method = "reflectParameters";
- $def->isNestingBlock = false;
+ $def->hasCloseTag = false;
$def->startExpressionName = "start_expresssion";
$def->requiredParameters = array();
$def->optionalParameters = array("bla");
@@ -174,6 +175,7 @@
}
}
+
public static function reflectParameters( $parameters, $source = null )
{
return print_r( $parameters, true );
Modified: trunk/Template/tests/regression_test.php
===================================================================
--- trunk/Template/tests/regression_test.php 2006-08-29 14:16:29 UTC (rev
3418)
+++ trunk/Template/tests/regression_test.php 2006-08-30 12:02:42 UTC (rev
3419)
@@ -16,9 +16,9 @@
include_once ("custom_blocks/brainfuck.php");
class ezcTemplateRegressionTest extends ezcTestCase
{
- public $requestRegeneration = true;
+ public $requestRegeneration = false;
- public $showTreesOnFailure = false;
+ public $showTreesOnFailure = true;
private $stdin = null;
@@ -97,14 +97,14 @@
foreach( $directories as $directory )
{
$template = new ezcTemplate();
- $template->addExtension( new BrainFuck() );
- $template->addExtension( new TestBlocks() );
-
$dir = dirname( $directory );
$base = basename( $directory );
$template->configuration = new ezcTemplateConfiguration( $dir,
$this->getTempDir() );
+ $template->configuration->addExtension( "BrainFuck" );
+ $template->configuration->addExtension( "TestBlocks" );
+
if( preg_match("#^(\w+)@(\w+)\..*$#", $base, $match ) )
{
$contextClass = "ezcTemplate". ucfirst( strtolower( $match[2]
) ) . "Context";
@@ -115,7 +115,6 @@
$template->configuration->context = new ezcTemplateNoContext();
}
-
$send = substr( $directory, 0, -3 ) . ".send";
if( file_exists( $send ) )
{
--
svn-components mailing list
[email protected]
http://lists.ez.no/mailman/listinfo/svn-components