Author: Raymond Bosman
Date: 2007-05-04 16:20:26 +0200 (Fri, 04 May 2007)
New Revision: 5055
Log:
- Cache keys can contain expressions.
- The {cache_template} will cache everything under this statement; allowing
expressions, variable declaration, etc. outside the cache.
Added:
trunk/Template/tests/templates/cache_block_with_function_key.tpl
trunk/Template/tests/templates/cache_template.tpl
trunk/Template/tests/templates/cache_template_keys_function.tpl
trunk/Template/tests/templates/cache_template_wrong_order.tpl
Modified:
trunk/Template/src/error_messages.php
trunk/Template/src/parsers/source_to_tst/implementations/cache.php
trunk/Template/src/parsers/tst_to_ast/implementations/tst_to_ast_cached_transformer.php
trunk/Template/src/parsers/tst_to_ast/implementations/tst_to_ast_transformer.php
trunk/Template/src/parsers/tst_to_tst/implementations/cache_preparation.php
trunk/Template/src/template.php
trunk/Template/tests/cache_test.php
Modified: trunk/Template/src/error_messages.php
===================================================================
--- trunk/Template/src/error_messages.php 2007-05-04 13:17:52 UTC (rev
5054)
+++ trunk/Template/src/error_messages.php 2007-05-04 14:20:26 UTC (rev
5055)
@@ -28,7 +28,6 @@
const MSG_EXPECT_MODULO = "Expecting a modulo";
const MSG_EXPECT_NON_MODIFYING_OPERAND = "Expecting an operand
without a pre- or post operator.";
const MSG_EXPECT_VARIABLE = "Expecting a variable";
- const MSG_EXPECT_USE_VARIABLE = "Expecting a variable that
is defined with {use}";
const MSG_EXPECT_VALUE = "Expected two operands that
are not an array.";
Modified: trunk/Template/src/parsers/source_to_tst/implementations/cache.php
===================================================================
--- trunk/Template/src/parsers/source_to_tst/implementations/cache.php
2007-05-04 13:17:52 UTC (rev 5054)
+++ trunk/Template/src/parsers/source_to_tst/implementations/cache.php
2007-05-04 14:20:26 UTC (rev 5055)
@@ -112,7 +112,7 @@
{
$this->findNextElement( $cursor );
- if ( ! $this->parseOptionalType( "Variable",
$this->currentCursor, false ) )
+ if ( ! $this->parseOptionalType( "Expression",
$this->currentCursor, false ) )
{
throw new ezcTemplateParserException(
$this->parser->source, $this->startCursor, $this->currentCursor,
ezcTemplateSourceToTstErrorMessages::MSG_EXPECT_VARIABLE );
}
Modified:
trunk/Template/src/parsers/tst_to_ast/implementations/tst_to_ast_cached_transformer.php
===================================================================
---
trunk/Template/src/parsers/tst_to_ast/implementations/tst_to_ast_cached_transformer.php
2007-05-04 13:17:52 UTC (rev 5054)
+++
trunk/Template/src/parsers/tst_to_ast/implementations/tst_to_ast_cached_transformer.php
2007-05-04 14:20:26 UTC (rev 5055)
@@ -22,8 +22,6 @@
*/
protected $cacheTemplate = null;
- protected $preparedCache = null;
-
private $template = null;
private $cacheName = null;
@@ -42,12 +40,11 @@
protected $cacheBlockCounter = 0;
- public function __construct( $parser, $cacheTemplate, $preparedCache )
+ public function __construct( $parser, $cacheTemplate )
{
parent::__construct( $parser );
$this->cacheTemplate = $cacheTemplate;
- $this->preparedCache = $preparedCache;
// XXX
$this->template = $parser->template;
@@ -247,11 +244,22 @@
protected function translateCacheKeys($tstKeys)
{
$cacheKeys = array();
+ $i = 0;
foreach ( $tstKeys as $key => $value )
{
// Translate the 'old' variableName to the new name.
$k = $value->accept($this);
+ // If the cachekey is an expression, assign it to a variable.
+ if (!$k instanceof ezcTemplateVariableAstNode )
+ {
+ $var = $this->createVariableNode( self::INTERNAL_PREFIX .
"cachekey" . $i );
+ $createVar = new ezcTemplateGenericStatementAstNode( new
ezcTemplateAssignmentOperatorAstNode( $var, $k ) );
+ $this->programNode->appendStatement( $createVar );
+
+ $k = $var;
+ }
+
$type = $this->parser->symbolTable->retrieve($k->name);
if ( substr( $k->name, 0, 12) == "this->send->")
{
@@ -265,6 +273,8 @@
{
$cacheKeys[$k->name] = $k->name;
}
+
+ $i++;
}
return $cacheKeys;
@@ -302,27 +312,9 @@
$cb->appendStatement( $this->_fopenCacheFileWriteMode() ); // $fp =
fopen( $this->cache, "w" );
$cb->appendStatement( $this->_fwritePhpOpen() ); //
fwrite( $fp, "<" . "?php\n" );
- $cb->appendStatement( $this->_assignEmptyString("total" .
$this->cacheLevel) ); // $total = ""
- $cb->appendStatement( $this->_assignEmptyString(
self::INTERNAL_PREFIX. "output") );
- }
-
- // TEST VERSION for cacheblock.
- protected function startCachingCacheBlock( $cb )
- {
- // / startCaching();
- $cplen = strlen(
$this->parser->template->usedConfiguration->compilePath );
- if ($this->template->usedConfiguration->cacheManager !== false )
- {
- $cb->appendStatement( new ezcTemplateGenericStatementAstNode( new
ezcTemplateFunctionCallAstNode(
"\$this->template->usedConfiguration->cacheManager->startCaching", array( new
ezcTemplateVariableAstNode("this->template"), new ezcTemplateLiteralAstNode(
$this->parser->template->stream ), new
ezcTemplateVariableAstNode("_ezcTemplateCache" ), new
ezcTemplateVariableAstNode("_ezcCacheKeys") ) ) ) );
- }
-
- $cb->appendStatement( $this->_fopenCacheFileWriteMode() ); // $fp =
fopen( $this->cache, "w" );
-
- $cb->appendStatement( $this->_fwritePhpOpen() ); //
fwrite( $fp, "<" . "?php\n" );
- // $cb->appendStatement( $this->_assignEmptyString("total") ); //
$total = ""
$cb->appendStatement( $this->_assignVariable(self::INTERNAL_PREFIX .
"output", "total".$this->cacheLevel) );
- $cb->appendStatement( $this->_assignEmptyString(
self::INTERNAL_PREFIX. "output") );
+ $cb->appendStatement( $this->_assignEmptyString(
self::INTERNAL_PREFIX. "output") );
}
@@ -331,17 +323,22 @@
foreach ( $type->elements as $element )
{
$astNode = $element->accept( $this );
- if ( is_array( $astNode ) )
+ if ( !is_array( $astNode ) )
+ {
+ $astNode = array($astNode);
+ }
+
+ foreach ( $astNode as $ast )
{
- foreach ( $astNode as $ast )
+ if ( $ast instanceof ezcTemplateStatementAstNode )
{
$cb->statements[] = $ast;
}
+ else
+ {
+ throw new ezcTemplateInternalException ("Expected an
ezcTemplateStatementAstNode: ". __FILE__ . ":" . __LINE__ );
+ }
}
- else
- {
- $cb->statements[] = $astNode;
- }
}
}
@@ -364,28 +361,6 @@
}
- protected function addUseVariables()
- {
- // Add the 'use' statement, that is removed by the prepareCache walker.
- foreach ( $this->preparedCache->useVariableTst as $useVariable)
- {
- $use = $useVariable->accept($this);
-
- if ( is_array( $use ) )
- {
- foreach ( $use as $u )
- {
- $this->programNode->appendStatement( $u );
- }
- }
- else
- {
- $this->programNode->appendStatement( $use );
- }
- }
- }
-
-
public function visitProgramTstNode( ezcTemplateProgramTstNode $type )
{
// Is the whole template cached?
@@ -395,17 +370,49 @@
return parent::visitProgramTstNode($type);
}
- // Cache the whole template.
+ // Cache the template.
$this->cacheLevel++;
+ $this->prepareProgram(); // Program operations, nothing to do with
caching.
+
+ // Start inserting nodes, until the CacheTstNode is found.
+ $elemLen = sizeof( $type->elements);
+ for( $i = 0; $i < $elemLen; $i++)
+ {
+ $element = $type->elements[$i];
+ if( $element instanceof ezcTemplateCacheTstNode )
+ {
+ break;
+ }
+ $astNode = $element->accept( $this );
+ if ( !is_array( $astNode ) )
+ {
+ $astNode = array($astNode);
+ }
- $this->prepareProgram(); // Program operations, nothing to do with
caching.
+ foreach ( $astNode as $ast )
+ {
+ if ( $ast instanceof ezcTemplateStatementAstNode )
+ {
+ $this->programNode->appendStatement($ast);
+ }
+ else
+ {
+ throw new ezcTemplateInternalException ("Expected an
ezcTemplateStatementAstNode: ". __FILE__ . ":" . __LINE__ );
+ }
+ }
+ }
- $this->addUseVariables();
- $cacheKeys = $this->translateCacheKeys($this->cacheTemplate->keys);
+ // Remove the nodes already added.
+ $newType = array();
+ for ($k = $i; $k < $elemLen; $k++)
+ {
+ $newType[] = $type->elements[$k];
+ }
+ $type->elements = $newType;
+ $cacheKeys = $this->translateCacheKeys($this->cacheTemplate->keys);
$this->addCacheKeys( $this->programNode, $cacheKeys );
-
$ttl = $this->translateTTL($this->cacheTemplate->ttl);
$ttlStatements = $this->checkTTL( $ttl );
@@ -423,18 +430,16 @@
$cb->condition = $this->notFileExistsCache();
$cb->body = new ezcTemplateBodyAstNode();
-
$this->startCaching($cb->body);
$this->insertInCache( $cb->body, $type );
+
// Create the 'else' part. The else should 'include' (and execute) the
cached file.
$if->conditions[] = $else = new ezcTemplateConditionBodyAstNode();
$else->body = new ezcTemplateBodyAstNode();
-
$else->body->statements = array();
$else->body->statements[] = $this->_includeCache();
-
if ($this->template->usedConfiguration->cacheManager !== false )
{
$cb->body->appendStatement( new
ezcTemplateGenericStatementAstNode( new ezcTemplateFunctionCallAstNode(
"\$this->template->usedConfiguration->cacheManager->stopCaching", array() )));
@@ -455,7 +460,6 @@
$programNode->appendStatement( new ezcTemplatePhpCodeAstNode(
'$_ezcCacheKeys = array();' ."\n" ) );
$cacheBlock = $cacheBlock === false ? "" : "[cb".$cacheBlock . "]";
-
foreach ( $cacheKeys as $key => $value )
{
$programNode->appendStatement( new ezcTemplatePhpCodeAstNode(
'$_ezcCacheKeys[\''.$key.'\'] = '. 'is_object( $'.$value.' ) && method_exists(
$'.$value.', "cacheKey" ) ? $'.$value.'->cacheKey() : $'. $value . ";\n" ) );
@@ -493,18 +497,10 @@
$time = new ezcTemplateFunctionCallAstNode( "time", array() );
$time->checkAndSetTypeHint();
- // if ( file_exists( \$_ezcTemplateCache ) && filemtime(
\$_ezcTemplateCache ) + ( /*[ TTL ]*/ ) < time() )
- // {
- // echo 'REMOVE THE FILE';
- // // unlink( [FILE] )
- // }\n" );
-
$cb->condition = new ezcTemplateLogicalAndOperatorAstNode( new
ezcTemplateFunctionCallAstNode( "file_exists", array(new
ezcTemplateVariableAstNode( "_ezcTemplateCache" ) ) ), new
ezcTemplateLessThanOperatorAstNode( new ezcTemplateAdditionOperatorAstNode( new
ezcTemplateFunctionCallAstNode( "filemtime", array(new
ezcTemplateVariableAstNode( "_ezcTemplateCache" ) )), new
ezcTemplateParenthesisAstNode( $ttl ) ) , $time ) );
$cb->body = new ezcTemplateBodyAstNode();
-
$cb->body->statements = array();
-
$cb->body->statements[] = new ezcTemplateGenericStatementAstNode(
new ezcTemplateFunctionCallAstNode( "unlink", array( new
ezcTemplateVariableAstNode( "_ezcTemplateCache" ) ) ) );
$statements[] = $if;
@@ -514,9 +510,6 @@
}
-
-
-
public function visitReturnTstNode( ezcTemplateReturnTstNode $node )
{
if ( $this->isInDynamicBlock )
@@ -526,7 +519,6 @@
}
$astNodes = array();
-
foreach ( $node->variables as $var => $expr )
{
$assign = new ezcTemplateAssignmentOperatorAstNode();
@@ -636,41 +628,8 @@
public function visitCacheTstNode( ezcTemplateCacheTstNode $type )
{
- if ( $type->type == ezcTemplateCacheTstNode::TYPE_CACHE_TEMPLATE ) //
Can be removed?
- {
- // Modify the root node.
- $this->programNode->cacheTemplate = true;
-
- foreach ( $type->keys as $key )
- {
-
- // Translate the 'old' variableName to the new name.
- $k = $key->accept($this);
-
-
- if ( $this->parser->symbolTable->retrieve($key->name) !==
ezcTemplateSymbolTable::IMPORT)
- {
-
- throw new ezcTemplateParserException( $type->source,
$type->endCursor, $type->endCursor,
ezcTemplateSourceToTstErrorMessages::MSG_EXPECT_USE_VARIABLE );
- }
-
- $this->programNode->cacheKeys[] = $k->name;
- }
-
- // And translate the ttl.
- if ( $type->ttl != null )
- {
- $this->programNode->ttl = $type->ttl->accept($this);
- }
-
- return new ezcTemplateNopAstNode();
- }
- else
- {
- // $cb = new ezcTemplateCacheBlockAstNode(
$type->elements->accept($this) );
-
- return $cb;
- }
+ // This <cache_template> tst node is handled by visitProgramTstNode
+ return new ezcTemplateNopAstNode();
}
@@ -684,10 +643,7 @@
$this->cacheLevel++;
$statements = new ezcTemplateBodyAstNode();
-
$cacheKeys = $this->translateCacheKeys($type->keys);
-
- $this->addUseVariables();
$this->addCacheKeys( $statements, $cacheKeys,
$this->cacheBlockCounter++ );
$ttl = $this->translateTTL($type->ttl);
@@ -708,7 +664,7 @@
$cb->condition = $this->notFileExistsCache();
$cb->body = new ezcTemplateBodyAstNode();
- $this->startCachingCacheBlock($cb->body);
+ $this->startCaching($cb->body);
$this->insertInCache( $cb->body, $type );
// Create the 'else' part. The else should 'include' (and execute) the
cached file.
@@ -728,15 +684,8 @@
// Outside.
$statements->appendStatement( $if );
-
- // RETURN STATEMENT outside..
- // $statements->appendStatement( new ezcTemplateReturnAstNode(
$this->outputVariable->getAst()) );
-
-
$this->cacheLevel--;
-
return $statements->statements;
-
}
}
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
2007-05-04 13:17:52 UTC (rev 5054)
+++
trunk/Template/src/parsers/tst_to_ast/implementations/tst_to_ast_transformer.php
2007-05-04 14:20:26 UTC (rev 5055)
@@ -559,30 +559,20 @@
foreach ( $type->elements as $element )
{
$astNode = $element->accept( $this );
- if ( is_array( $astNode ) )
+ if ( !is_array( $astNode ) )
{
- foreach ( $astNode as $ast )
- {
- if ( $ast instanceof ezcTemplateStatementAstNode )
- {
- $this->programNode->appendStatement( $ast );
- }
- else
- {
- throw new ezcTemplateInternalException ("Expected
an ezcTemplateStatementAstNode: ". __FILE__ . ":" . __LINE__ );
-
- }
- }
+ $astNode = array($astNode);
}
- else
+
+ foreach ( $astNode as $ast )
{
- if ( $astNode instanceof ezcTemplateStatementAstNode )
+ if ( $ast instanceof ezcTemplateStatementAstNode )
{
- $this->programNode->appendStatement( $astNode );
+ $this->programNode->appendStatement( $ast );
}
else
{
- throw new ezcTemplateInternalException ("Expected an
ezcTemplateStatementAstNode: ". __FILE__ . ":" . __LINE__ );
+ throw new ezcTemplateInternalException ("Expected an
ezcTemplateStatementAstNode: ". __FILE__ . ":" . __LINE__ );
}
}
}
@@ -1638,7 +1628,7 @@
{
$this->declaredVariables[ $type->variable->name ] = true;
- if ( $this->parser->symbolTable->retrieve( $type->variable->name ) ==
ezcTemplateSymbolTable::CYCLE )
+ if ( $this->parser->symbolTable->retrieve( $type->variable->name ) ==
ezcTemplateSymbolTable::CYCLE )
{
$this->noProperty = true;
$var = $type->variable->accept( $this );
Modified:
trunk/Template/src/parsers/tst_to_tst/implementations/cache_preparation.php
===================================================================
--- trunk/Template/src/parsers/tst_to_tst/implementations/cache_preparation.php
2007-05-04 13:17:52 UTC (rev 5054)
+++ trunk/Template/src/parsers/tst_to_tst/implementations/cache_preparation.php
2007-05-04 14:20:26 UTC (rev 5055)
@@ -24,6 +24,7 @@
/* Move the USE variable to the top of the program.
*/
+ /*
public function visitDeclarationTstNode( ezcTemplateDeclarationTstNode
$node )
{
$symbolTable = ezcTemplateSymbolTable::getInstance();
@@ -43,6 +44,7 @@
$this->acceptAndUpdate( $node->expression );
}
}
+ */
}
?>
Modified: trunk/Template/src/template.php
===================================================================
--- trunk/Template/src/template.php 2007-05-04 13:17:52 UTC (rev 5054)
+++ trunk/Template/src/template.php 2007-05-04 14:20:26 UTC (rev 5055)
@@ -321,10 +321,7 @@
$fetchCacheInfo = new ezcTemplateFetchCacheInformation();
$this->properties["tstTree"]->accept( $fetchCacheInfo );
- $cachePreparation = new ezcTemplateCachePreparation();
- $this->properties["tstTree"]->accept( $cachePreparation );
-
- $tstToAst = new ezcTemplateTstToAstCachedTransformer (
$parser, $fetchCacheInfo->cacheTst, $cachePreparation );
+ $tstToAst = new ezcTemplateTstToAstCachedTransformer (
$parser, $fetchCacheInfo->cacheTst);
}
else
{
Modified: trunk/Template/tests/cache_test.php
===================================================================
--- trunk/Template/tests/cache_test.php 2007-05-04 13:17:52 UTC (rev 5054)
+++ trunk/Template/tests/cache_test.php 2007-05-04 14:20:26 UTC (rev 5055)
@@ -194,7 +194,23 @@
$this->assertEquals( "\n\n[Bart Simpson]\n", $out); // No Id given.
}
+ public function testCacheBlockWithFunctionKeys()
+ {
+ $t = new ezcTemplate();
+ $t->send->user = new TestUser( "Bart", "Simpson" );
+ $out = $t->process( "cache_block_with_function_key.tpl");
+ $this->assertEquals( "\n[Bart Simpson]\n", $out);
+
+ $t->send->user = new TestUser( "Lisa", "Simpson" );
+ $out = $t->process( "cache_block_with_function_key.tpl");
+ $this->assertEquals( "\n[Bart Simpson]\n", $out); // No Id given.
+
+ $t->send->user = new TestUser( "Homer", "Simpson" );
+ $out = $t->process( "cache_block_with_function_key.tpl");
+ $this->assertEquals( "\n[Homer Simpson]\n", $out); // No Id given.
+ }
+
public function testCacheBlockInCacheBlock()
{
$t = new ezcTemplate();
@@ -411,9 +427,47 @@
//
/////////////////////////////////////////////////////////////////////////////////////////
// Test the dynamic block.
+ // Test the use variables.
// Tested in the regression_test:
// - Variable declaration in the dynamic block.
+
+ public function testWrongOrder()
+ {
+ $t = new ezcTemplate( );
+ $t->send->user = new TestUser( "Bernard", "Black" );
+
+ try
+ {
+ $out = $t->process( "cache_template_wrong_order.tpl");
+ $this->fail("Expected an exception");
+ }
+ catch( Exception $e)
+ {
+ }
+ }
+
+ public function testFunctionAsKey()
+ {
+ $t = new ezcTemplate( );
+ $t->send->user = new TestUser( "Bernard", "Black" );
+ $out = $t->process( "cache_template_keys_function.tpl");
+ $this->assertEquals( "\nHello world\n", $out );
+ }
+
+
+ // Test if a string before and after the {cache_template} works.
+ public function testPartialCalc()
+ {
+ $t = new ezcTemplate( );
+ $t->send->user = new TestUser( "Bernard", "Black" );
+
+ $out = $t->process( "cache_template.tpl");
+ $this->assertEquals( "\nBefore\n\n\nAfter\n", $out );
+ }
+
+
+
// Test whether the dynamic block changes when the {use} variable changes.
public function testDynamicBlock()
{
@@ -567,25 +621,6 @@
}
- public function testCacheKeyFromNonUse()
- {
- // Should call a function, if it's defined.
- $t = new ezcTemplate();
- try
- {
- $out = $t->process( "cache_wrong_key.tpl");
- $this->fail("Expected an exception");
- }
- catch( ezcTemplateParserException $e)
- {
- if( strpos($e->getMessage(),
ezcTemplateSourceToTstErrorMessages::MSG_EXPECT_USE_VARIABLE) === false)
- {
- $this->fail("Expected an exception");
- }
-
- }
- }
-
//
/////////////////////////////////////////////////////////////////////////////////////////////////
// Test TTL
Added: trunk/Template/tests/templates/cache_block_with_function_key.tpl
===================================================================
--- trunk/Template/tests/templates/cache_block_with_function_key.tpl
2007-05-04 13:17:52 UTC (rev 5054)
+++ trunk/Template/tests/templates/cache_block_with_function_key.tpl
2007-05-04 14:20:26 UTC (rev 5055)
@@ -0,0 +1,6 @@
+{use $user}
+
+{cache_block keys str_len($user->name)}
+[{$user->name}]
+{/cache_block}
+
Property changes on:
trunk/Template/tests/templates/cache_block_with_function_key.tpl
___________________________________________________________________
Name: svn:eol-style
+ native
Added: trunk/Template/tests/templates/cache_template.tpl
===================================================================
--- trunk/Template/tests/templates/cache_template.tpl 2007-05-04 13:17:52 UTC
(rev 5054)
+++ trunk/Template/tests/templates/cache_template.tpl 2007-05-04 14:20:26 UTC
(rev 5055)
@@ -0,0 +1,6 @@
+{use $user}
+
+Before
+{cache_template}
+
+After
Property changes on: trunk/Template/tests/templates/cache_template.tpl
___________________________________________________________________
Name: svn:eol-style
+ native
Added: trunk/Template/tests/templates/cache_template_keys_function.tpl
===================================================================
--- trunk/Template/tests/templates/cache_template_keys_function.tpl
2007-05-04 13:17:52 UTC (rev 5054)
+++ trunk/Template/tests/templates/cache_template_keys_function.tpl
2007-05-04 14:20:26 UTC (rev 5055)
@@ -0,0 +1,2 @@
+{cache_template keys str_len("aa"), str_replace("a", "b", "c", "d")}
+Hello world
Property changes on:
trunk/Template/tests/templates/cache_template_keys_function.tpl
___________________________________________________________________
Name: svn:eol-style
+ native
Added: trunk/Template/tests/templates/cache_template_wrong_order.tpl
===================================================================
--- trunk/Template/tests/templates/cache_template_wrong_order.tpl
2007-05-04 13:17:52 UTC (rev 5054)
+++ trunk/Template/tests/templates/cache_template_wrong_order.tpl
2007-05-04 14:20:26 UTC (rev 5055)
@@ -0,0 +1,4 @@
+{cache_template keys $user}
+{use $user}
+
+{$user->name}
Property changes on:
trunk/Template/tests/templates/cache_template_wrong_order.tpl
___________________________________________________________________
Name: svn:eol-style
+ native
--
svn-components mailing list
[email protected]
http://lists.ez.no/mailman/listinfo/svn-components