Procházet zdrojové kódy

ZF-6722, ZF-7783 - Bugfix Zend_CodeGenerator_Php_Parameter::setDefaultValue does not allow null (not in the report, but also not possible are: array(), or array() with values, or 0 or a boolean) by adding a DefaultValue Object equall to the approach in Zend_CodeGenerator_Php_Property, ZF-7779 - Added missing support for variables passed by reference, ZF-7780 - Implemented Zend_CodeGenerator_Php_Parameter::fromReflection()

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@18000 44c647ce-9c0f-0410-b52a-842ac1e357ba
beberlei před 16 roky
rodič
revize
0cb348aa5d

+ 20 - 0
documentation/manual/en/module_specs/Zend_CodeGenerator-Reference.xml

@@ -479,9 +479,29 @@ class Zend_CodeGenerator_Php_Parameter extends Zend_CodeGenerator_Php_Abstract
     public function getDefaultValue()
     public function setPosition($position)
     public function getPosition()
+    public function getPassedByReference()
+    public function setPassedByReference($passedByReference)
     public function generate()
 }
 ]]></programlisting>
+
+            <para>
+                There are several problems that might occur when trying to set null, booleans or arrays as
+                default values. For this the value holder object <classname>Zend_CodeGenerator_Php_ParameterDefaultValue</classname>
+                can be used, for example:
+            </para>
+
+            <programlisting language="php"><![CDATA[
+$parameter = new Zend_CodeGenerator_Php_Parameter();
+$parameter->setDefaultValue(new Zend_CodeGenerator_Php_Parameter_DefaultValue("null"));
+$parameter->setDefaultValue(new Zend_CodeGenerator_Php_Parameter_DefaultValue("array('foo', 'bar')"));
+]]>
+            </programlisting>
+
+            <para>
+                Internally <code>setDefaultValue()</code> also converts the values which can't be expressed in PHP into the value holder.
+            </para>
+
         </sect3>
 
         <sect3 id="zend.codegenerator.reference.concrete.php-property">

+ 77 - 6
library/Zend/CodeGenerator/Php/Parameter.php

@@ -26,6 +26,11 @@
 require_once 'Zend/CodeGenerator/Php/Abstract.php';
 
 /**
+ * @see Zend_CodeGenerator_Php_ParameterDefaultValue
+ */
+require_once 'Zend/CodeGenerator/Php/Parameter/DefaultValue.php';
+
+/**
  * @category   Zend
  * @package    Zend_CodeGenerator
  * @copyright  Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
@@ -54,6 +59,11 @@ class Zend_CodeGenerator_Php_Parameter extends Zend_CodeGenerator_Php_Abstract
     protected $_position = null;
 
     /**
+     * @var bool
+     */
+    protected $_passedByReference = false;
+
+    /**
      * fromReflection()
      *
      * @param Zend_Reflection_Parameter $reflectionParameter
@@ -61,8 +71,30 @@ class Zend_CodeGenerator_Php_Parameter extends Zend_CodeGenerator_Php_Abstract
      */
     public static function fromReflection(Zend_Reflection_Parameter $reflectionParameter)
     {
-        // @todo Research this
-        return new self();
+        $param = new Zend_CodeGenerator_Php_Parameter();
+        $param->setName($reflectionParameter->getName());
+
+        try {
+            $param->setType($reflectionParameter->getType());
+        } catch(Zend_Reflection_Exception $e) {
+            if($reflectionParameter->isArray()) {
+                $param->setType('array');
+            } else {
+                $typeClass = $reflectionParameter->getClass();
+                if($typeClass !== null) {
+                    $param->setType($typeClass->getName());
+                }
+            }
+        }
+        
+        $param->setPosition($reflectionParameter->getPosition());
+
+        if($reflectionParameter->isOptional()) {
+            $param->setDefaultValue($reflectionParameter->getDefaultValue());
+        }
+        $param->setPassedByReference($reflectionParameter->isPassedByReference());
+
+        return $param;
     }
 
     /**
@@ -110,14 +142,29 @@ class Zend_CodeGenerator_Php_Parameter extends Zend_CodeGenerator_Php_Abstract
     }
 
     /**
-     * setDefaultValue()
+     * Set the default value of the parameter.
      *
-     * @param string $defaultValue
+     * Certain variables are difficult to expres
+     *
+     * @param null|bool|string|int|float|Zend_CodeGenerator_Php_Parameter_DefaultValue $defaultValue
      * @return Zend_CodeGenerator_Php_Parameter
      */
     public function setDefaultValue($defaultValue)
     {
-        $this->_defaultValue = $defaultValue;
+        if($defaultValue === null) {
+            $this->_defaultValue = new Zend_CodeGenerator_Php_Parameter_DefaultValue("null");
+        } else if(is_array($defaultValue)) {
+            $defaultValue = str_replace(array("\r", "\n"), "", var_export($defaultValue, true));
+            $this->_defaultValue = new Zend_CodeGenerator_Php_Parameter_DefaultValue($defaultValue);
+        } else if(is_bool($defaultValue)) {
+            if($defaultValue == true) {
+                $this->_defaultValue = new Zend_CodeGenerator_Php_Parameter_DefaultValue("true");
+            } else {
+                $this->_defaultValue = new Zend_CodeGenerator_Php_Parameter_DefaultValue("false");
+            }
+        } else {
+            $this->_defaultValue = $defaultValue;
+        }
         return $this;
     }
 
@@ -154,6 +201,24 @@ class Zend_CodeGenerator_Php_Parameter extends Zend_CodeGenerator_Php_Abstract
     }
 
     /**
+     * @return bool
+     */
+    public function getPassedByReference()
+    {
+        return $this->_passedByReference;
+    }
+
+    /**
+     * @param bool $passedByReference
+     * @return Zend_CodeGenerator_Php_Parameter
+     */
+    public function setPassedByReference($passedByReference)
+    {
+        $this->_passedByReference = $passedByReference;
+        return $this;
+    }
+
+    /**
      * generate()
      *
      * @return string
@@ -166,12 +231,18 @@ class Zend_CodeGenerator_Php_Parameter extends Zend_CodeGenerator_Php_Abstract
             $output .= $this->_type . ' ';
         }
 
+        if($this->_passedByReference === true) {
+            $output .= '&';
+        }
+
         $output .= '$' . $this->_name;
 
-        if ($this->_defaultValue) {
+        if ($this->_defaultValue !== null) {
             $output .= ' = ';
             if (is_string($this->_defaultValue)) {
                 $output .= '\'' . $this->_defaultValue . '\'';
+            } else if($this->_defaultValue instanceof Zend_CodeGenerator_Php_ParameterDefaultValue) {
+                $output .= (string)$this->_defaultValue;
             } else {
                 $output .= $this->_defaultValue;
             }

+ 60 - 0
library/Zend/CodeGenerator/Php/Parameter/DefaultValue.php

@@ -0,0 +1,60 @@
+<?php
+/**
+ * Zend Framework
+ *
+ * LICENSE
+ *
+ * This source file is subject to the new BSD license that is bundled
+ * with this package in the file LICENSE.txt.
+ * It is also available through the world-wide-web at this URL:
+ * http://framework.zend.com/license/new-bsd
+ * If you did not receive a copy of the license and are unable to
+ * obtain it through the world-wide-web, please send an email
+ * to license@zend.com so we can send you a copy immediately.
+ *
+ * @category   Zend
+ * @package    Zend_CodeGenerator
+ * @subpackage Php
+ * @copyright  Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ * @version    $Id$
+ */
+
+/**
+ * A value-holder object for non-expressable parameter default values, such as null, booleans and empty array()
+ *
+ * @category   Zend
+ * @package    Zend_CodeGenerator
+ * @subpackage Php
+ * @copyright  Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ */
+class Zend_CodeGenerator_Php_Parameter_DefaultValue
+{
+    /**
+     * @var string
+     */
+    protected $_defaultValue = null;
+
+    /**
+     *
+     * @param string $defaultValue
+     * @throws Zend_CodeGenerator_Php_Exception
+     */
+    public function __construct($defaultValue)
+    {
+        if(!is_string($defaultValue)) {
+            require_once "Zend/CodeGenerator/Php/Exception.php";
+            throw new Zend_CodeGenerator_Php_Exception(
+                "Can only set a string as default value representation, ".
+                "but ".gettype($defaultValue)." was given."
+            );
+        }
+        $this->_defaultValue = $defaultValue;
+    }
+
+    public function __toString()
+    {
+        return $this->_defaultValue;
+    }
+}