Selaa lähdekoodia

ZF-11513
Zend_CodeGenerator
Add support for classes which have class constant and instance variable with same name


git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@24418 44c647ce-9c0f-0410-b52a-842ac1e357ba

adamlundrigan 14 vuotta sitten
vanhempi
commit
969a217b8f

+ 105 - 0
library/Zend/CodeGenerator/Php/Class.php

@@ -85,6 +85,11 @@ class Zend_CodeGenerator_Php_Class extends Zend_CodeGenerator_Php_Abstract
     protected $_properties = null;
 
     /**
+     * @var array Array of constants
+     */
+    protected $_constants = null;
+
+    /**
      * @var array Array of methods
      */
     protected $_methods = null;
@@ -279,6 +284,21 @@ class Zend_CodeGenerator_Php_Class extends Zend_CodeGenerator_Php_Abstract
     }
 
     /**
+     * setConstants()
+     *
+     * @param array $constants
+     * @return Zend_CodeGenerator_Php_Class
+     */
+    public function setConstants(Array $constants)
+    {
+        foreach ($constants as $const) {
+            $this->setConstant($const);
+        }
+
+        return $this;
+    }
+
+    /**
      * setProperty()
      *
      * @param array|Zend_CodeGenerator_Php_Property $property
@@ -296,6 +316,9 @@ class Zend_CodeGenerator_Php_Class extends Zend_CodeGenerator_Php_Abstract
             throw new Zend_CodeGenerator_Php_Exception('setProperty() expects either an array of property options or an instance of Zend_CodeGenerator_Php_Property');
         }
 
+        if ($property->isConst()) {
+            return $this->setConstant($property);
+        }
         if (isset($this->_properties[$propertyName])) {
             require_once 'Zend/CodeGenerator/Php/Exception.php';
             throw new Zend_CodeGenerator_Php_Exception('A property by name ' . $propertyName . ' already exists in this class.');
@@ -306,6 +329,37 @@ class Zend_CodeGenerator_Php_Class extends Zend_CodeGenerator_Php_Abstract
     }
 
     /**
+     * setConstant()
+     *
+     * @param array|Zend_CodeGenerator_Php_Property $const
+     * @return Zend_CodeGenerator_Php_Class
+     */
+    public function setConstant($const)
+    {
+        if (is_array($const)) {
+            $const = new Zend_CodeGenerator_Php_Property($const);
+            $constName = $const->getName();
+        } elseif ($const instanceof Zend_CodeGenerator_Php_Property) {
+            $constName = $const->getName();
+        } else {
+            require_once 'Zend/CodeGenerator/Php/Exception.php';
+            throw new Zend_CodeGenerator_Php_Exception('setConstant() expects either an array of property options or an instance of Zend_CodeGenerator_Php_Property');
+        }
+
+        if (!$const->isConst()) {
+            require_once 'Zend/CodeGenerator/Php/Exception.php';
+            throw new Zend_CodeGenerator_Php_Exception('setProperty() expects argument to define a constant');
+        }
+        if (isset($this->_constants[$constName])) {
+            require_once 'Zend/CodeGenerator/Php/Exception.php';
+            throw new Zend_CodeGenerator_Php_Exception('A constant by name ' . $constName . ' already exists in this class.');
+        }
+
+        $this->_constants[$constName] = $const;
+        return $this;
+    }
+
+    /**
      * getProperties()
      *
      * @return array
@@ -316,6 +370,16 @@ class Zend_CodeGenerator_Php_Class extends Zend_CodeGenerator_Php_Abstract
     }
 
     /**
+     * getConstants()
+     *
+     * @return array
+     */
+    public function getConstants()
+    {
+        return $this->_constants;
+    }
+
+    /**
      * getProperty()
      *
      * @param string $propertyName
@@ -332,6 +396,22 @@ class Zend_CodeGenerator_Php_Class extends Zend_CodeGenerator_Php_Abstract
     }
 
     /**
+     * getConstant()
+     *
+     * @param string $constName
+     * @return Zend_CodeGenerator_Php_Property
+     */
+    public function getConstant($constName)
+    {
+        foreach ($this->_constants as $const) {
+            if ($const->getName() == $constName) {
+                return $const;
+            }
+        }
+        return false;
+    }
+
+    /**
      * hasProperty()
      *
      * @param string $propertyName
@@ -343,6 +423,17 @@ class Zend_CodeGenerator_Php_Class extends Zend_CodeGenerator_Php_Abstract
     }
 
     /**
+     * hasConstant()
+     *
+     * @param string $constName
+     * @return bool
+     */
+    public function hasConstant($constName)
+    {
+        return isset($this->_constants[$constName]);
+    }
+
+    /**
      * setMethods()
      *
      * @param array $methods
@@ -437,6 +528,12 @@ class Zend_CodeGenerator_Php_Class extends Zend_CodeGenerator_Php_Abstract
             }
         }
 
+        foreach ($this->_constants as $constant) {
+            if ($constant->isSourceDirty()) {
+                return true;
+            }
+        }
+
         foreach ($this->_methods as $method) {
             if ($method->isSourceDirty()) {
                 return true;
@@ -481,6 +578,13 @@ class Zend_CodeGenerator_Php_Class extends Zend_CodeGenerator_Php_Abstract
 
         $output .= self::LINE_FEED . '{' . self::LINE_FEED . self::LINE_FEED;
 
+        $constants = $this->getConstants();
+        if (!empty($constants)) {
+            foreach ($constants as $const) {
+                $output .= $const->generate() . self::LINE_FEED . self::LINE_FEED;
+            }
+        }
+
         $properties = $this->getProperties();
         if (!empty($properties)) {
             foreach ($properties as $property) {
@@ -507,6 +611,7 @@ class Zend_CodeGenerator_Php_Class extends Zend_CodeGenerator_Php_Abstract
     protected function _init()
     {
         $this->_properties = new Zend_CodeGenerator_Php_Member_Container(Zend_CodeGenerator_Php_Member_Container::TYPE_PROPERTY);
+        $this->_constants = new Zend_CodeGenerator_Php_Member_Container(Zend_CodeGenerator_Php_Member_Container::TYPE_PROPERTY);
         $this->_methods = new Zend_CodeGenerator_Php_Member_Container(Zend_CodeGenerator_Php_Member_Container::TYPE_METHOD);
     }
 

+ 30 - 0
tests/Zend/CodeGenerator/Php/ClassTest.php

@@ -309,4 +309,34 @@ class MyClass extends ParentClass
 CODE;
         $this->assertEquals( $expected, $codeGenClass->generate() );
     }
+
+    /**
+     * @group ZF-11513
+     */
+    public function testAllowsClassConstantToHaveSameNameAsClassProperty()
+    {
+        $const = new Zend_CodeGenerator_Php_Property();
+        $const->setName('name')->setDefaultValue('constant')->setConst(true);
+
+        $property = new Zend_CodeGenerator_Php_Property();
+        $property->setName('name')->setDefaultValue('property');
+
+        $codeGenClass = new Zend_CodeGenerator_Php_Class();
+        $codeGenClass->setName('My_Class')->setProperties(array($const, $property));
+
+        $expected = <<<CODE
+class My_Class
+{
+
+    const name = 'constant';
+
+    public \$name = 'property';
+
+
+}
+
+CODE;
+        $this->assertEquals( $expected, $codeGenClass->generate() );
+    }
+
 }