Jelajahi Sumber

ZF-10521: Zend_Cache_Frontend_Class output buffering issue when entity throws an exception

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@23051 44c647ce-9c0f-0410-b52a-842ac1e357ba
mabe 15 tahun lalu
induk
melakukan
eb47fda214

+ 2 - 1
library/Zend/Cache/Backend/Test.php

@@ -109,7 +109,8 @@ class Zend_Cache_Backend_Test extends Zend_Cache_Backend implements Zend_Cache_B
           || $id == 'e83249ea22178277d5befc2c5e2e9ace'
           || $id == '40f649b94977c0a6e76902e2a0b43587'
           || $id == '88161989b73a4cbfd0b701c446115a99'
-          || $id == '205fc79cba24f0f0018eb92c7c8b3ba4')
+          || $id == '205fc79cba24f0f0018eb92c7c8b3ba4'
+          || $id == '170720e35f38150b811f68a937fb042d')
         {
             return false;
         }

+ 10 - 5
library/Zend/Cache/Frontend/Class.php

@@ -218,11 +218,16 @@ class Zend_Cache_Frontend_Class extends Zend_Cache_Core
             // A cache is not available (or not valid for this frontend)
             ob_start();
             ob_implicit_flush(false);
-            $return = call_user_func_array(array($this->_cachedEntity, $name), $parameters);
-            $output = ob_get_contents();
-            ob_end_clean();
-            $data = array($output, $return);
-            $this->save($data, $id, $this->_tags, $this->_specificLifetime, $this->_priority);
+
+            try {
+                $return = call_user_func_array(array($this->_cachedEntity, $name), $parameters);
+                $output = ob_get_clean();
+                $data = array($output, $return);
+                $this->save($data, $id, $this->_tags, $this->_specificLifetime, $this->_priority);
+            } catch (Exception $e) {
+                ob_end_clean();
+                throw $e;
+            }
         }
 
         echo $output;

+ 30 - 2
tests/Zend/Cache/ClassFrontendTest.php

@@ -46,17 +46,25 @@ class test {
 
     private $_string = 'hello !';
 
-    public static function foobar($param1, $param2) {
+    public static function foobar($param1, $param2)
+    {
         echo "foobar_output($param1, $param2)";
         return "foobar_return($param1, $param2)";
     }
 
-    public function foobar2($param1, $param2) {
+    public function foobar2($param1, $param2)
+    {
         echo($this->_string);
         echo "foobar2_output($param1, $param2)";
         return "foobar2_return($param1, $param2)";
     }
 
+    public function throwException()
+    {
+        echo 'throw exception';
+        throw new Exception('test exception');
+    }
+
 }
 
 /**
@@ -248,5 +256,25 @@ class Zend_Cache_ClassFrontendTest extends PHPUnit_Framework_TestCase {
         );
         $test = new Zend_Cache_Frontend_Class($options);
     }
+
+    /**
+     * @ZF-10521
+     */
+    public function testOutputBufferingOnException()
+    {
+        ob_start();
+        ob_implicit_flush(false);
+
+        echo 'start';
+        try {
+            $this->_instance2->throwException();
+            $this->fail("An exception should be thrown");
+        } catch (Exception $e) {}
+        echo 'end';
+
+        $output = ob_get_clean();
+        $this->assertEquals('startend', $output);
+    }
+
 }