Author: Raymond Bosman
Date: 2007-03-06 15:04:55 +0100 (Tue, 06 Mar 2007)
New Revision: 4719

Log:
- Added the option to disable caching. 

Modified:
   trunk/Template/src/compiled_code.php
   trunk/Template/src/configuration.php
   
trunk/Template/src/parsers/tst_to_ast/implementations/tst_to_ast_transformer.php
   trunk/Template/src/template.php
   trunk/Template/tests/cache_manager_test.php
   trunk/Template/tests/cache_test.php

Modified: trunk/Template/src/compiled_code.php
===================================================================
--- trunk/Template/src/compiled_code.php        2007-03-06 14:00:15 UTC (rev 
4718)
+++ trunk/Template/src/compiled_code.php        2007-03-06 14:04:55 UTC (rev 
4719)
@@ -186,7 +186,7 @@
      * @throws ezcTemplateOutdatedCompilationException when the template 
should be recompiled. 
      * @return void
      */
-    protected function checkRequirements( $engineID )
+    protected function checkRequirements( $engineID, $compileFlags = array() )
     {
         if ( $this->template->configuration->checkModifiedTemplates &&
              // Do not recompile when the modification times are the same. 
This messes up the caching tests.
@@ -196,6 +196,14 @@
             throw new ezcTemplateOutdatedCompilationException( "The source 
template file '{$this->template->stream}' is newer than '{$this->path}', will 
recompile." );
         }
 
+        // Check if caching is enabled
+        if( isset( $compileFlags["disableCache"] ) )
+        {
+            if( $this->template->configuration->disableCache != 
$compileFlags["disableCache"] )
+            {
+                throw new ezcTemplateOutdatedCompilationException( "The 
compileFlag 'disableCache' has been changed, will recompile." );
+            }
+        }
 
         // Check if the engine ID differs
         if ( $engineID !== self::ENGINE_ID )

Modified: trunk/Template/src/configuration.php
===================================================================
--- trunk/Template/src/configuration.php        2007-03-06 14:00:15 UTC (rev 
4718)
+++ trunk/Template/src/configuration.php        2007-03-06 14:04:55 UTC (rev 
4719)
@@ -73,6 +73,7 @@
      */
      private $properties = array( 'context' => false,
                                   'cacheManager' => false,
+                                  'disableCache' => false,
                                   'templatePath' => ".",
                                   'compilePath' => ".",
                                   'cachedTemplatesPath' => null,
@@ -98,6 +99,7 @@
         {
             case 'context': 
             case 'cacheManager':
+            case 'disableCache':
             case 'templatePath': 
             case 'compilePath': 
             case 'cachedTemplatesPath':       // Relative path to the 
compilePath
@@ -162,6 +164,7 @@
             case 'checkModifiedTemplates': 
             case 'customBlocks': 
             case 'customFunctions': 
+            case 'disableCache':
                 $this->properties[$name] = $value;
                 break;
 
@@ -182,9 +185,8 @@
         switch ( $name )
         {
             case 'context': 
-                return true;
-
             case 'cacheManager': 
+            case 'disableCache': 
                 return true;
 
             case 'templatePath': 

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-03-06 14:00:15 UTC (rev 4718)
+++ 
trunk/Template/src/parsers/tst_to_ast/implementations/tst_to_ast_transformer.php
    2007-03-06 14:04:55 UTC (rev 4719)
@@ -408,7 +408,11 @@
         $programNode->appendStatement( new ezcTemplateEolCommentAstNode( "If 
you modify this file your changes will be lost when it is regenerated." ) );
 
         // Add: $this->checkRequirements()
-        $args = array( new ezcTemplateLiteralAstNode( 
ezcTemplateCompiledCode::ENGINE_ID ) );
+        $compileFlags = new ezcTemplateLiteralArrayAstNode();
+        $compileFlags->keys[] = new ezcTemplateLiteralAstNode("disableCache");
+        $compileFlags->value[] = new ezcTemplateLiteralAstNode( 
$this->parser->template->configuration->disableCache );
+
+        $args = array( new ezcTemplateLiteralAstNode( 
ezcTemplateCompiledCode::ENGINE_ID ), $compileFlags );
         $call = new ezcTemplateFunctionCallAstNode( "checkRequirements", $args 
);
         $programNode->appendStatement( new ezcTemplateGenericStatementAstNode( 
new ezcTemplateReferenceOperatorAstNode( new ezcTemplateVariableAstNode( "this" 
), $call ) ) );
     }
@@ -698,22 +702,38 @@
      */
 
     /**
-     * @return ezcTemplateAstNode
+     * Return NOP.
+     *
+     * @return ezcTemplateNopAstNode
      */
     public function visitCacheTstNode( ezcTemplateCacheTstNode $type )
     {
-        throw new ezcTemplateInternalException( "visitCacheTstNode is handled 
in the (wrong) tst_to_ast_transformer" );
+        return new ezcTemplateNopAstNode();
     }
 
 
     /**
-     * @return ezcTemplateAstNode
+     * Skips the dynamic block and process the statements inside.
+     *
+     * @return array(ezcTemplateAstNode)
      */
-    public function visitDynamicBlockTstNode( ezcTemplateDynamicBlockTstNode 
$type )
+    public function visitDynamicBlockTstNode( ezcTemplateDynamicBlockTstNode 
$node )
     {
-        throw new ezcTemplateInternalException( "visitDynamicTstNode is 
handled in the (wrong) tst_to_ast_transformer" );
+        $t = $this->createBody( $node->elements );
+        return $t->statements; 
     }
 
+    /**
+     * Skips the cache block and process the statements inside.
+     *
+     * @return array(ezcTemplateAstNode)
+     */
+    public function visitCacheBlockTstNode( ezcTemplateCacheBlockTstNode $node 
)
+    {
+        $t = $this->createBody( $node->elements );
+        return $t->statements; 
+    }
+ 
 
     /**
      * @return ezcTemplateAstNode
@@ -833,8 +853,6 @@
      */
     public function visitLiteralArrayTstNode( ezcTemplateLiteralArrayTstNode 
$type )
     {
-        // return new ezcTemplateLiteralArrayAstNode();
-
         $astVal = array();
         foreach ( $type->value as $key => $val )
         {
@@ -852,7 +870,6 @@
         $ast->value = $astVal;
         $ast->keys = $astKeys;
 
-
         return $ast;
     }
 

Modified: trunk/Template/src/template.php
===================================================================
--- trunk/Template/src/template.php     2007-03-06 14:00:15 UTC (rev 4718)
+++ trunk/Template/src/template.php     2007-03-06 14:04:55 UTC (rev 4719)
@@ -304,7 +304,7 @@
             // Check if caching is needed.
 
 //            if ( $fetchCacheInfo->cacheTst !== null )
-            if ( $parser->hasCacheBlocks )
+            if ($parser->hasCacheBlocks && !$this->configuration->disableCache 
)
             {
                 $fetchCacheInfo = new ezcTemplateFetchCacheInformation(); 
                 $this->properties["tstTree"]->accept( $fetchCacheInfo );

Modified: trunk/Template/tests/cache_manager_test.php
===================================================================
--- trunk/Template/tests/cache_manager_test.php 2007-03-06 14:00:15 UTC (rev 
4718)
+++ trunk/Template/tests/cache_manager_test.php 2007-03-06 14:04:55 UTC (rev 
4719)
@@ -34,6 +34,7 @@
         $config = ezcTemplateConfiguration::getInstance();
         $this->tempDir = $config->compilePath =  $this->createTempDir( 
"ezcTemplate_" );
         $config->templatePath = $this->basePath . 'templates/';
+        $config->disableCache = false;
 
         $config->cacheManager = new DbCacheManager();
 

Modified: trunk/Template/tests/cache_test.php
===================================================================
--- trunk/Template/tests/cache_test.php 2007-03-06 14:00:15 UTC (rev 4718)
+++ trunk/Template/tests/cache_test.php 2007-03-06 14:04:55 UTC (rev 4719)
@@ -29,6 +29,7 @@
         $this->basePath = realpath( dirname( __FILE__ ) ) . '/';
 
         $config = ezcTemplateConfiguration::getInstance();
+        $config->disableCache = false;
         $this->tempDir = $config->compilePath =  $this->createTempDir( 
"ezcTemplate_" );
         $config->templatePath = $this->basePath . 'templates/';
     }
@@ -453,7 +454,6 @@
         $t = new ezcTemplate( );
         $t->send->user = new TestUser( "Bernard", "Black" );
         $out = $t->process( "cache_dynamic_single_quote.tpl");
-
         $this->assertEquals( "\n'Bernard'\n'Bernard' \\'\n", $out );
 
         $t->send->user = new TestUser( "Guybrush", "Threepwood", 10 );
@@ -768,6 +768,85 @@
         // $this->assertEquals( "4", $t->receive->calc);
         // $this->assertEquals( "I am rubber, you are glue.", 
$t->receive->quote);
     }
+
+    public function testDisableCaching()
+    {
+        $t = new ezcTemplate();
+        $t->send->user = new TestUser( "Bernard", "Black" );
+        $out = $t->process( "cache_dynamic.tpl");
+        $this->assertEquals( "\n[Bernard Black]\n[Bernard Black]\n", $out );
+
+        // Change the user. The first name is cached. The second name should 
change.
+        $t->send->user = new TestUser( "Guybrush", "Threepwood" );
+        $out = $t->process( "cache_dynamic.tpl");
+        $this->assertEquals( "\n[Bernard Black]\n[Guybrush Threepwood]\n", 
$out );
+
+        $t->configuration->disableCache = true;
+        $t->send->user = new TestUser( "Guybrush", "Threepwood" );
+        $out = $t->process( "cache_dynamic.tpl");
+        $this->assertEquals( "\n[Guybrush Threepwood]\n[Guybrush 
Threepwood]\n", $out );
+
+        $t->send->user = new TestUser( "Bernard", "Black" );
+        $out = $t->process( "cache_dynamic.tpl");
+        $this->assertEquals( "\n[Bernard Black]\n[Bernard Black]\n", $out );
+    }
+
+    public function testDisableCachingWithCacheBlock()
+    {
+        $t = new ezcTemplate();
+        $t->send->user = new TestUser( "Bernard", "Black" );
+        $out = $t->process( "cache_block.tpl");
+        $this->assertEquals( 
+<<<EOM
+Before: [Bernard Black]
+
+Cache block 0: [Bernard Black]
+
+Between cb 0 and 1: [Bernard Black]
+
+Cache block 1: [Bernard Black]
+
+After: [Bernard Black]
+
+EOM
+, $out);
+
+
+        $t->configuration->disableCache = true;
+        $t->send->user = new TestUser( "Roger", "Rabbit" );
+        $out = $t->process( "cache_block.tpl");
+        $this->assertEquals( 
+<<<EOM
+Before: [Roger Rabbit]
+
+Cache block 0: [Roger Rabbit]
+
+Between cb 0 and 1: [Roger Rabbit]
+
+Cache block 1: [Roger Rabbit]
+
+After: [Roger Rabbit]
+
+EOM
+, $out);
+
+        $t->send->user = new TestUser( "Bernard", "Black" );
+        $out = $t->process( "cache_block.tpl");
+        $this->assertEquals( 
+<<<EOM
+Before: [Bernard Black]
+
+Cache block 0: [Bernard Black]
+
+Between cb 0 and 1: [Bernard Black]
+
+Cache block 1: [Bernard Black]
+
+After: [Bernard Black]
+
+EOM
+, $out);
+    }
 }
 
 class TestUser

-- 
svn-components mailing list
svn-components@lists.ez.no
http://lists.ez.no/mailman/listinfo/svn-components

Reply via email to