Author: Raymond Bosman Date: 2006-09-25 18:00:01 +0200 (Mon, 25 Sep 2006) New Revision: 3582
Log: - Added a caching prototype. Added: trunk/Template/src/parsers/ast_to_ast/implementations/cache.php trunk/Template/src/parsers/source_to_tst/implementations/cache.php trunk/Template/src/syntax_trees/ast/nodes/root.php trunk/Template/src/syntax_trees/tst/nodes/cache.php Modified: trunk/Template/src/parsers/ast/implementations/ast_tree_output.php trunk/Template/src/parsers/ast_to_ast/implementations/ast_walker.php trunk/Template/src/parsers/ast_to_php/implementations/php_generator.php trunk/Template/src/parsers/source_to_tst/implementations/block.php trunk/Template/src/parsers/tst_to_ast/implementations/tst_to_ast_transformer.php trunk/Template/src/template.php trunk/Template/src/template_autoload.php Modified: trunk/Template/src/parsers/ast/implementations/ast_tree_output.php =================================================================== --- trunk/Template/src/parsers/ast/implementations/ast_tree_output.php 2006-09-25 15:56:08 UTC (rev 3581) +++ trunk/Template/src/parsers/ast/implementations/ast_tree_output.php 2006-09-25 16:00:01 UTC (rev 3582) @@ -110,6 +110,11 @@ $this->text .= $this->outputNode( $node ); } + public function visitRootAstNode( ezcTemplateBodyAstNode $node ) + { + $this->text .= $this->outputNode( $node ); + } + public function visitGenericStatementAstNode( ezcTemplateGenericStatementAstNode $node ) { $this->text .= $this->outputNode( $node ); Modified: trunk/Template/src/parsers/ast_to_ast/implementations/ast_walker.php =================================================================== --- trunk/Template/src/parsers/ast_to_ast/implementations/ast_walker.php 2006-09-25 15:56:08 UTC (rev 3581) +++ trunk/Template/src/parsers/ast_to_ast/implementations/ast_walker.php 2006-09-25 16:00:01 UTC (rev 3582) @@ -19,6 +19,7 @@ { public $nodePath = array(); + public $statements = array(); public function __construct( ) { @@ -160,20 +161,38 @@ public function visitBodyAstNode( ezcTemplateBodyAstNode $body ) { array_unshift( $this->nodePath, $body ); + array_unshift( $this->statements, 0); + $b = clone( $body ); + + for( $i = 0; $i < sizeof( $b->statements ); $i++) + { + $this->statements[0] = $i; + $this->acceptAndUpdate( $b->statements[$i] ); + } + + $body = $b; + + array_shift( $this->statements ); + array_shift( $this->nodePath ); + } + + public function visitRootAstNode( ezcTemplateRootAstNode &$body ) + { + array_unshift( $this->nodePath, $body ); + array_unshift( $this->statements, 0); + for( $i = 0; $i < sizeof( $body->statements ); $i++) { + $this->statements[0] = $i; $this->acceptAndUpdate( $body->statements[$i] ); } -/* - foreach ( $body->statements as $statement ) - { - $ret = $statement->accept( $this ); - } - */ + + array_shift( $this->statements ); array_shift( $this->nodePath ); } + public function visitGenericStatementAstNode( ezcTemplateGenericStatementAstNode $statement ) { array_unshift( $this->nodePath, $statement ); Added: trunk/Template/src/parsers/ast_to_ast/implementations/cache.php =================================================================== --- trunk/Template/src/parsers/ast_to_ast/implementations/cache.php 2006-09-25 15:56:08 UTC (rev 3581) +++ trunk/Template/src/parsers/ast_to_ast/implementations/cache.php 2006-09-25 16:00:01 UTC (rev 3582) @@ -0,0 +1,132 @@ +<?php +/** + * File containing the ezcTemplateAstToAstContextAppender + * + * @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 + * @access private + */ +/** + * @package Template + * @version //autogen// + * @access private + */ +class ezcTemplateAstToAstCache extends ezcTemplateAstWalker +{ + private $template = null; + private $uniqueID = null; + + public function __construct( $template ) + { + $this->template = $template; + } + + public function __destruct() + { + } + + public function visitRootAstNode( ezcTemplateRootAstNode &$type ) + { + if( !$type->cacheTemplate ) + { + return; + } + + $this->uniqueID = uniqid(); + + + + // Need to make a tree duplication? + // To prevent tree madness.. + + ////////////////////// + // Write: $c = + // $c = new ezcCacheStorageFilePlain("/tmp/cache/"); + + // if ( ( $data = $c->restore( "pizza" ) ) === false ) + // { + // $dataOfFirstItem = "Plain cache stored on " . date( 'Y-m-d, H:i:s' ); + // $c->store( "pizzaboer", $dataOfFirstItem ); + // } + + // Create $cache = new ... + $createCacheLine = new ezcTemplateGenericStatementAstNode( new ezcTemplateAssignmentOperatorAstNode( new ezcTemplateVariableAstNode( "cache" ), new ezcTemplateNewAstNode ("ezcCacheStorageFilePlain", array( new ezcTemplateLiteralAstNode("/tmp/cache/") ) ) ) ); + + $createDataLine = new ezcTemplateParenthesisAstNode( new ezcTemplateAssignmentOperatorAstNode( new ezcTemplateVariableAstNode( "data" ), new ezcTemplateObjectAccessOperatorAstNode( new ezcTemplateVariableAstNode( "cache"), new ezcTemplateFunctionCallAstNode( "restore", array(new ezcTemplateLiteralAstNode($this->uniqueID) ) ) ) ) ) ; + + + $compare = new ezcTemplateIdenticalOperatorAstNode( $createDataLine, new ezcTemplateLiteralAstNode( false) ); + + $cb = new ezcTemplateConditionBodyAstNode(); + $cb->condition = $compare; + + $cb->body = new ezcTemplateBodyAstNode(); + $cb->body->statements = $type->statements; + + $if = new ezcTemplateIfAstNode(); + $if->conditions[] = $cb; + + $type->statements = array(); + $type->statements[] = $createCacheLine; + $type->statements[] = $if; + + + parent::visitRootAstNode( $type ); + + // return $data; + $type->statements[] = new ezcTemplateGenericStatementAstNode( new ezcTemplateReturnAstNode ( new ezcTemplateVariableAstNode( "data") ) ); + + + return; + } + + public function visitReturnAstNode( ezcTemplateReturnAstNode $return ) + { + // Write the to cache. + // $cache->store( "template_identifier", $data ); + + $a = new ezcTemplateGenericStatementAstNode( new ezcTemplateObjectAccessOperatorAstNode( new ezcTemplateVariableAstNode( "cache"), new ezcTemplateFunctionCallAstNode( "store", array(new ezcTemplateLiteralAstNode($this->uniqueID), new ezcTemplateVariableAstNode("_ezcTemplate_output") ) ) ) ); + + array_splice($this->nodePath[0]->statements, $this->statements[0], 0, array($a) ); + + + /* + + echo ("Number: " . $this->statements[0] . "\n" ); + + var_dump ($this->nodePath[0]->statements[$this->statements[0]]); + + exit(); + foreach( $this->nodePath as $n ) + { + echo get_class( $n ) . "\n"; + } + exit(); + + var_dump( $return ); + */ + return; + + } + + + + +/* + public function visitOutputAstNode( ezcTemplateOutputAstNode $type ) + { + parent::visitOutputAstNode( $type ); + + if ( $type->isRaw ) + { + return $type; + } + return $this->context->transformOutput( $type->expression ); + } + */ + + +} +?> Property changes on: trunk/Template/src/parsers/ast_to_ast/implementations/cache.php ___________________________________________________________________ Name: svn:eol-style + native Modified: trunk/Template/src/parsers/ast_to_php/implementations/php_generator.php =================================================================== --- trunk/Template/src/parsers/ast_to_php/implementations/php_generator.php 2006-09-25 15:56:08 UTC (rev 3581) +++ trunk/Template/src/parsers/ast_to_php/implementations/php_generator.php 2006-09-25 16:00:01 UTC (rev 3582) @@ -587,6 +587,21 @@ } /** + * Visits a code element containing a body of statements. + * A body consists of a series of statements in sequence. + * + * @param ezcTemplateBodyAstNode $body The code element containing the body. + */ + public function visitRootAstNode( ezcTemplateRootAstNode $body ) + { + foreach ( $body->statements as $statement ) + { + $statement->accept( $this ); + } + } + + + /** * Visits a code element containing a generic statement. * The expression is evaluated and and a semi-colon added to end the statement. * A generic statement contains a generic code expression but is terminated with a semi-colon. Modified: trunk/Template/src/parsers/source_to_tst/implementations/block.php =================================================================== --- trunk/Template/src/parsers/source_to_tst/implementations/block.php 2006-09-25 15:56:08 UTC (rev 3581) +++ trunk/Template/src/parsers/source_to_tst/implementations/block.php 2006-09-25 16:00:01 UTC (rev 3582) @@ -137,6 +137,15 @@ return true; } + // Parse the cache blocks. + $cacheParser = new ezcTemplateCacheSourceToTstParser( $this->parser, $this, null ); + $cacheParser->block = $this->block; + if( $this->parseOptionalType( $cacheParser, null ) ) + { + return true; + } + + // Try to parse custom blocks, these are pluggable and follows a generic syntax. $customBlockParser = new ezcTemplateCustomBlockSourceToTstParser( $this->parser, $this, null ); $customBlockParser->block = $this->block; Added: trunk/Template/src/parsers/source_to_tst/implementations/cache.php =================================================================== --- trunk/Template/src/parsers/source_to_tst/implementations/cache.php 2006-09-25 15:56:08 UTC (rev 3581) +++ trunk/Template/src/parsers/source_to_tst/implementations/cache.php 2006-09-25 16:00:01 UTC (rev 3582) @@ -0,0 +1,190 @@ +<?php +/** + * File containing the ezcTemplateCacheSourceToTstParser 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 + * @access private + */ +/** + * Parser for custom template blocks following a generic syntax. + * + * @package Template + * @version //autogen// + * @access private + */ +class ezcTemplateCacheSourceToTstParser extends ezcTemplateSourceToTstParser +{ + /** + * Passes control to parent. + */ + function __construct( ezcTemplateParser $parser, /*ezcTemplateSourceToTstParser*/ $parentParser, /*ezcTemplateCursor*/ $startCursor ) + { + parent::__construct( $parser, $parentParser, $startCursor ); + $this->block = null; + } + + /** + * Parses the expression by using the ezcTemplateExpressionSourceToTstParser class. + */ + protected function parseCurrent( ezcTemplateCursor $cursor ) + { + /* + if ( $this->block->isClosingBlock ) + { + $this->findNextElement(); + + $matches = $cursor->pregMatchComplete( "#^([a-zA-Z_][a-zA-Z0-9_-]*)(?:[^a-zA-Z])#i" ); + if ( $matches === false ) + return false; + + $name = $matches[1][0]; + $cursor->advance( strlen( $name ) ); + $this->findNextElement( $cursor ); + + if( !$cursor->match( "}" ) ) + { + throw new ezcTemplateParserException( $this->parser->source, $this->startCursor, $this->currentCursor, ezcTemplateSourceToTstErrorMessages::MSG_EXPECT_CURLY_BRACE_CLOSE ); + } + + $cb = new ezcTemplateCustomBlockTstNode( $this->parser->source, $this->startCursor, $cursor ); + + $cb->isClosingBlock = true; + + $this->appendElement( $cb ); + return true; + } + */ + + if( $cursor->match( "cache_template" ) ) + { + $cacheNode = new ezcTemplateCacheTstNode( $this->parser->source, $this->startCursor, $cursor ); + + //$cacheNode->isClosingBlock = false; + $cacheNode->templateCache = true; + $this->appendElement( $cacheNode); + + $this->findNextElement( $cursor ); + + if( !$cursor->match( "}" ) ) + { + die ("Expected an '}' at the end of 'cache_template'"); + } + + return true; + // Need to cache the template + } + + + + + + return false; + + // Check for the name of the custom block + // Note: The code inside the ( ?: ) brace ensures that the next character + // is not an alphabetical character ie. a word boundary + $matches = $cursor->pregMatchComplete( "#^([a-zA-Z_][a-zA-Z0-9_-]*)(?:[^a-zA-Z])#i" ); + if ( $matches === false ) + { + return false; + } + + $name = $matches[1][0]; + + $cursor->advance( strlen( $name ) ); + $this->findNextElement( $cursor ); + + //$def = ezcTemplateCustomBlockManager::getInstance()->getDefinition( $name ); + + $def = $this->getCustomBlockDefinition( $name ); + + if( $def === false ) + { + return false; + } + + $cb = new ezcTemplateCustomBlockTstNode( $this->parser->source, $this->startCursor, $cursor ); + $cb->definition = $def; + $this->block->isNestingBlock = $cb->isNestingBlock = $def->hasCloseTag; + + if( isset( $def->startExpressionName ) && $def->startExpressionName != "" ) + { + if( !in_array( $def->startExpressionName, $def->optionalParameters ) && !in_array( $def->startExpressionName, $def->requiredParameters ) ) + { + throw new ezcTemplateParserException( $this->parser->source, $this->startCursor, $this->currentCursor, + sprintf( ezcTemplateSourceToTstErrorMessages::MSG_EXPECT_REQUIRED_OR_OPTIONAL_PARAMETER_DEFINITION_IN_CUSTOM_BLOCK, $def->startExpressionName ) ); + } + + if ( !$this->parseOptionalType( 'Expression', null, false ) ) + { + if( in_array( $def->startExpressionName, $def->requiredParameters ) ) + { + throw new ezcTemplateParserException( $this->parser->source, $this->startCursor, $this->currentCursor, ezcTemplateSourceToTstErrorMessages::MSG_EXPECT_EXPRESSION ); + } + } + else + { + $cb->namedParameters[ $def->startExpressionName ] = $this->lastParser->rootOperator; + $this->findNextElement( $cursor ); + } + } + + while( !$cursor->match( "}" ) ) + { + $match = $cursor->pregMatch( "#^[a-zA-Z_][a-zA-Z0-9_-]*#"); + if( !$match ) + { + throw new ezcTemplateParserException( $this->parser->source, $this->startCursor, $this->currentCursor, + sprintf( ezcTemplateSourceToTstErrorMessages::MSG_UNEXPECTED_TOKEN, $cursor->current( 1 ) ) ); + + } + + if( !in_array( $match, $def->optionalParameters ) && !in_array( $match, $def->requiredParameters ) ) + { + throw new ezcTemplateParserException( $this->parser->source, $this->startCursor, $this->currentCursor, + sprintf( ezcTemplateSourceToTstErrorMessages::MSG_UNKNOWN_CUSTOM_BLOCK_PARAMETER, $match) ); + } + + if ( array_key_exists( $match, $cb->namedParameters ) ) + { + throw new ezcTemplateParserException( $this->parser->source, $this->startCursor, $this->currentCursor, + sprintf( ezcTemplateSourceToTstErrorMessages::MSG_REASSIGNMENT_CUSTOM_BLOCK_PARAMETER, $match ) ); + } + + $this->findNextElement( $cursor ); + // The '=' is optional. + if( $cursor->match( "=" ) ) + { + $this->findNextElement( $cursor ); + } + + // The parameter has an expression. + if ( !$this->parseOptionalType( 'Expression', null, false ) ) + { + throw new ezcTemplateParserException( $this->parser->source, $this->startCursor, $this->currentCursor, ezcTemplateSourceToTstErrorMessages::MSG_EXPECT_EXPRESSION ); + } + + // Append the parameter to the "namedParameters" array. + $cb->namedParameters[ $match ] = $this->lastParser->rootOperator; + } + + // Check if all requiredParameters are set. + foreach( $def->requiredParameters as $val ) + { + if( !array_key_exists( $val, $cb->namedParameters) ) + { + throw new ezcTemplateParserException( $this->parser->source, $this->startCursor, $this->currentCursor, + sprintf( ezcTemplateSourceToTstErrorMessages::MSG_MISSING_CUSTOM_BLOCK_PARAMETER, $val ) ); + } + } + + $this->appendElement( $cb ); + + return true; + } +} + +?> Property changes on: trunk/Template/src/parsers/source_to_tst/implementations/cache.php ___________________________________________________________________ Name: svn:eol-style + native 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-09-25 15:56:08 UTC (rev 3581) +++ trunk/Template/src/parsers/tst_to_ast/implementations/tst_to_ast_transformer.php 2006-09-25 16:00:01 UTC (rev 3582) @@ -243,7 +243,7 @@ { if ( $this->programNode === null ) { - $this->programNode = new ezcTemplateBodyAstNode(); + $this->programNode = new ezcTemplateRootAstNode(); $this->outputVariable->push( "_ezcTemplate_output" ); $this->programNode->appendStatement( $this->outputVariable->getInitializationAst() ); @@ -268,6 +268,18 @@ } } + public function visitCacheTstNode( ezcTemplateCacheTstNode $type ) + { + if( $type->templateCache ) + { + // Modify the root node. + $this->programNode->cacheTemplate = true; + + return new ezcTemplateNopAstNode(); + } + + } + public function visitCycleControlTstNode( ezcTemplateCycleControlTstNode $cycle ) { if ( $cycle->name == "increment" || $cycle->name == "decrement" || $cycle->name == "reset" ) Added: trunk/Template/src/syntax_trees/ast/nodes/root.php =================================================================== --- trunk/Template/src/syntax_trees/ast/nodes/root.php 2006-09-25 15:56:08 UTC (rev 3581) +++ trunk/Template/src/syntax_trees/ast/nodes/root.php 2006-09-25 16:00:01 UTC (rev 3582) @@ -0,0 +1,30 @@ +<?php +/** + * File containing the ezcTemplateRootAstNode 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 + * @access private + */ +/** + * Represents the root node of the AST tree. This node may contain settings of the template. + * + * @package Template + * @version //autogen// + * @access private + */ +class ezcTemplateRootAstNode extends ezcTemplateBodyAstNode +{ + public $cacheTemplate = false; + + /** + * Initialize with function name code and optional arguments + */ + public function __construct( Array $statements = null ) + { + parent::__construct(); + } +} +?> Property changes on: trunk/Template/src/syntax_trees/ast/nodes/root.php ___________________________________________________________________ Name: svn:eol-style + native Added: trunk/Template/src/syntax_trees/tst/nodes/cache.php =================================================================== --- trunk/Template/src/syntax_trees/tst/nodes/cache.php 2006-09-25 15:56:08 UTC (rev 3581) +++ trunk/Template/src/syntax_trees/tst/nodes/cache.php 2006-09-25 16:00:01 UTC (rev 3582) @@ -0,0 +1,40 @@ +<?php +/** + * File containing the ezcTemplateCacheTstNode 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 + * @access private + */ +/** + * The cache node contains the possible caching information. + * + * @package Template + * @version //autogen// + * @access private + */ +class ezcTemplateCacheTstNode extends ezcTemplateExpressionTstNode +{ + + public $templateCache = false; + + + /** + * + * @param ezcTemplateSource $source + * @param ezcTemplateCursor $start + * @param ezcTemplateCursor $end + */ + public function __construct( ezcTemplateSourceCode $source, /*ezcTemplateCursor*/ $start, /*ezcTemplateCursor*/ $end ) + { + parent::__construct( $source, $start, $end ); + } + + public function getTreeProperties() + { + return array( 'templateCache' => $this->templateCache); + } +} +?> Property changes on: trunk/Template/src/syntax_trees/tst/nodes/cache.php ___________________________________________________________________ Name: svn:eol-style + native Modified: trunk/Template/src/template.php =================================================================== --- trunk/Template/src/template.php 2006-09-25 15:56:08 UTC (rev 3581) +++ trunk/Template/src/template.php 2006-09-25 16:00:01 UTC (rev 3582) @@ -254,6 +254,11 @@ $astToAst = new ezcTemplateAstToAstAssignmentOptimizer(); $tstToAst->programNode->accept( $astToAst ); + // Run the cacher. + $astToAst = new ezcTemplateAstToAstCache( $this ); + $tstToAst->programNode->accept( $astToAst ); + + $g = new ezcTemplateAstToPhpGenerator( $compiled->path ); // Write to the file. $tstToAst->programNode->accept( $g ); } Modified: trunk/Template/src/template_autoload.php =================================================================== --- trunk/Template/src/template_autoload.php 2006-09-25 15:56:08 UTC (rev 3581) +++ trunk/Template/src/template_autoload.php 2006-09-25 16:00:01 UTC (rev 3582) @@ -123,7 +123,9 @@ "ezcTemplateIncludeTstNode" => "Template/syntax_trees/tst/nodes/include.php", "ezcTemplateReturnTstNode" => "Template/syntax_trees/tst/nodes/return.php", "ezcTemplateIdentifierTstNode" => "Template/syntax_trees/tst/nodes/identifier.php", + "ezcTemplateCacheTstNode" => "Template/syntax_trees/tst/nodes/cache.php", + // Syntax trees / AST "ezcTemplateAstBuilder" => "Template/syntax_trees/ast/ast_builder.php", @@ -136,6 +138,7 @@ "ezcTemplateAstNodeVisitor" => "Template/syntax_trees/ast/interfaces/ast_visitor.php", // Syntax trees / AST nodes + "ezcTemplateRootAstNode" => "Template/syntax_trees/ast/nodes/root.php", "ezcTemplateNopAstNode" => "Template/syntax_trees/ast/nodes/nop.php", "ezcTemplateLiteralAstNode" => "Template/syntax_trees/ast/nodes/literal.php", "ezcTemplateLiteralArrayAstNode" => "Template/syntax_trees/ast/nodes/literalarray.php", @@ -267,6 +270,7 @@ "ezcTemplateDelimiterSourceToTstParser" => "Template/parsers/source_to_tst/implementations/delimiter.php", "ezcTemplateCycleSourceToTstParser" => "Template/parsers/source_to_tst/implementations/cycle.php", "ezcTemplateIncludeSourceToTstParser" => "Template/parsers/source_to_tst/implementations/include.php", + "ezcTemplateCacheSourceToTstParser" => "Template/parsers/source_to_tst/implementations/cache.php", @@ -285,6 +289,7 @@ "ezcTemplateAstWalker" => "Template/parsers/ast_to_ast/implementations/ast_walker.php", "ezcTemplateAstToAstContextAppender" => "Template/parsers/ast_to_ast/implementations/context_appender.php", "ezcTemplateAstToAstAssignmentOptimizer" => "Template/parsers/ast_to_ast/implementations/assignment_optimizer.php", + "ezcTemplateAstToAstCache" => "Template/parsers/ast_to_ast/implementations/cache.php", // Parsers / ast_to_php implementations "ezcTemplateAstToPhpGenerator" => "Template/parsers/ast_to_php/implementations/php_generator.php", -- svn-components mailing list svn-components@lists.ez.no http://lists.ez.no/mailman/listinfo/svn-components