Przeglądaj źródła

ZF-11703 - Zend_CodeGenerator: Fixes corruption of Controllers source code when generating Actions on a file which omits a file level docblock

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@24456 44c647ce-9c0f-0410-b52a-842ac1e357ba
padraic 14 lat temu
rodzic
commit
73cf25eeeb

+ 1 - 1
library/Zend/CodeGenerator/Php/Class.php

@@ -161,7 +161,7 @@ class Zend_CodeGenerator_Php_Class extends Zend_CodeGenerator_Php_Abstract
 
         if (is_array($docblock)) {
             $docblock = new Zend_CodeGenerator_Php_Docblock($docblock);
-        } elseif (!$docblock instanceof Zend_CodeGenerator_Php_Docblock) {
+        } elseif ((!is_null($docblock)) && (!$docblock instanceof Zend_CodeGenerator_Php_Docblock)) {
             require_once 'Zend/CodeGenerator/Php/Exception.php';
             throw new Zend_CodeGenerator_Php_Exception('setDocblock() is expecting either a string, array or an instance of Zend_CodeGenerator_Php_Docblock');
         }

+ 4 - 1
library/Zend/CodeGenerator/Php/File.php

@@ -394,7 +394,7 @@ class Zend_CodeGenerator_Php_File extends Zend_CodeGenerator_Php_Abstract
 
         // if there are markers, put the body into the output
         $body = $this->getBody();
-        if (preg_match('#/\* Zend_CodeGenerator_Php_File-(.*?)Marker:#', $body)) {
+        if (preg_match('#/\* Zend_CodeGenerator_Php_File-(.*?)Marker#', $body)) {
             $output .= $body;
             $body    = '';
         }
@@ -428,6 +428,9 @@ class Zend_CodeGenerator_Php_File extends Zend_CodeGenerator_Php_Abstract
         $classes = $this->getClasses();
         if (!empty($classes)) {
             foreach ($classes as $class) {
+                if($this->getDocblock() == $class->getDocblock()) {
+                    $class->setDocblock(null);
+                }                   
                 $regex = str_replace('?', $class->getName(), self::$_markerClass);
                 $regex = preg_quote($regex, '#');
                 if (preg_match('#'.$regex.'#', $output)) {

+ 82 - 0
tests/Zend/CodeGenerator/Php/FileTest.php

@@ -296,4 +296,86 @@ EOS;
         $this->assertEquals(';', $lines[2]{$targetLength-1});
     }
 
+    /**
+    * @group ZF-11703
+    */
+    public function testNewMethodKeepDocBlock(){
+        $codeGenFile = Zend_CodeGenerator_Php_File::fromReflectedFileName(dirname(__FILE__).'/_files/zf-11703.php', true, true);
+        $target = <<<EOS
+<?php
+/**
+ * For manipulating files.
+ *
+ */
+
+class Foo
+{
+
+    public function bar()
+    {
+        // action body
+    }
+
+    public function bar2()
+    {
+        // action body
+    }
+
+
 }
+
+
+EOS;
+
+        $codeGenFile->getClass()->setMethod(array(
+            'name' => 'bar2',
+            'body' => '// action body'
+            ));
+
+        $this->assertEquals($target, $codeGenFile->generate());
+    }
+    
+    /**
+    * @group ZF-11703
+    */
+    public function testNewMethodKeepTwoDocBlock(){
+        $codeGenFile = Zend_CodeGenerator_Php_File::fromReflectedFileName(dirname(__FILE__).'/_files/zf-11703_1.php', true, true);
+        $target = <<<EOS
+<?php
+/**
+ * For manipulating files.
+ *
+ */
+
+
+/**
+ * Class Foo1
+ *
+ */
+class Foo1
+{
+
+    public function bar()
+    {
+        // action body
+    }
+
+    public function bar2()
+    {
+        // action body
+    }
+
+
+}
+
+
+EOS;
+
+        $codeGenFile->getClass()->setMethod(array(
+            'name' => 'bar2',
+            'body' => '// action body'
+            ));
+
+        $this->assertEquals($target, $codeGenFile->generate());
+    }
+}

+ 13 - 0
tests/Zend/CodeGenerator/Php/_files/zf-11703.php

@@ -0,0 +1,13 @@
+<?php
+/**
+ * For manipulating files.
+ */
+
+class Foo
+{
+
+    public function bar()
+    {
+        // action body
+    }
+}

+ 16 - 0
tests/Zend/CodeGenerator/Php/_files/zf-11703_1.php

@@ -0,0 +1,16 @@
+<?php
+/**
+ * For manipulating files.
+ */
+
+/**
+ * Class Foo1
+ */
+class Foo1
+{
+
+    public function bar()
+    {
+        // action body
+    }
+}