File indexing completed on 2025-01-19 05:20:59

0001 <?php
0002 /**
0003  * Zend Framework
0004  *
0005  * LICENSE
0006  *
0007  * This source file is subject to the new BSD license that is bundled
0008  * with this package in the file LICENSE.txt.
0009  * It is also available through the world-wide-web at this URL:
0010  * http://framework.zend.com/license/new-bsd
0011  * If you did not receive a copy of the license and are unable to
0012  * obtain it through the world-wide-web, please send an email
0013  * to license@zend.com so we can send you a copy immediately.
0014  *
0015  * @category   Zend
0016  * @package    Zend_CodeGenerator
0017  * @subpackage PHP
0018  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0019  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0020  * @version    $Id$
0021  */
0022 
0023 /**
0024  * @see Zend_CodeGenerator_Php_Member_Abstract
0025  */
0026 // require_once 'Zend/CodeGenerator/Php/Member/Abstract.php';
0027 
0028 /**
0029  * @see Zend_CodeGenerator_Php_Docblock
0030  */
0031 // require_once 'Zend/CodeGenerator/Php/Docblock.php';
0032 
0033 /**
0034  * @see Zend_CodeGenerator_Php_Parameter
0035  */
0036 // require_once 'Zend/CodeGenerator/Php/Parameter.php';
0037 
0038 /**
0039  * @category   Zend
0040  * @package    Zend_CodeGenerator
0041  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0042  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0043  */
0044 class Zend_CodeGenerator_Php_Method extends Zend_CodeGenerator_Php_Member_Abstract
0045 {
0046     /**
0047      * @var Zend_CodeGenerator_Php_Docblock
0048      */
0049     protected $_docblock = null;
0050 
0051     /**
0052      * @var bool
0053      */
0054     protected $_isFinal = false;
0055 
0056     /**
0057      * @var array
0058      */
0059     protected $_parameters = array();
0060 
0061     /**
0062      * @var string
0063      */
0064     protected $_body = null;
0065 
0066     /**
0067      * fromReflection()
0068      *
0069      * @param Zend_Reflection_Method $reflectionMethod
0070      * @return Zend_CodeGenerator_Php_Method
0071      */
0072     public static function fromReflection(Zend_Reflection_Method $reflectionMethod)
0073     {
0074         $method = new self();
0075 
0076         $method->setSourceContent($reflectionMethod->getContents(false));
0077         $method->setSourceDirty(false);
0078 
0079         if ($reflectionMethod->getDocComment() != '') {
0080             $method->setDocblock(Zend_CodeGenerator_Php_Docblock::fromReflection($reflectionMethod->getDocblock()));
0081         }
0082 
0083         $method->setFinal($reflectionMethod->isFinal());
0084 
0085         if ($reflectionMethod->isPrivate()) {
0086             $method->setVisibility(self::VISIBILITY_PRIVATE);
0087         } elseif ($reflectionMethod->isProtected()) {
0088             $method->setVisibility(self::VISIBILITY_PROTECTED);
0089         } else {
0090             $method->setVisibility(self::VISIBILITY_PUBLIC);
0091         }
0092 
0093         $method->setStatic($reflectionMethod->isStatic());
0094 
0095         $method->setName($reflectionMethod->getName());
0096 
0097         foreach ($reflectionMethod->getParameters() as $reflectionParameter) {
0098             $method->setParameter(Zend_CodeGenerator_Php_Parameter::fromReflection($reflectionParameter));
0099         }
0100 
0101         $method->setBody($reflectionMethod->getBody());
0102 
0103         return $method;
0104     }
0105 
0106     /**
0107      * setFinal()
0108      *
0109      * @param bool $isFinal
0110      */
0111     public function setFinal($isFinal)
0112     {
0113         $this->_isFinal = ($isFinal) ? true : false;
0114     }
0115 
0116     /**
0117      * setParameters()
0118      *
0119      * @param array $parameters
0120      * @return Zend_CodeGenerator_Php_Method
0121      */
0122     public function setParameters(Array $parameters)
0123     {
0124         foreach ($parameters as $parameter) {
0125             $this->setParameter($parameter);
0126         }
0127         return $this;
0128     }
0129 
0130     /**
0131      * setParameter()
0132      *
0133      * @param Zend_CodeGenerator_Php_Parameter|array $parameter
0134      * @return Zend_CodeGenerator_Php_Method
0135      */
0136     public function setParameter($parameter)
0137     {
0138         if (is_array($parameter)) {
0139             $parameter = new Zend_CodeGenerator_Php_Parameter($parameter);
0140             $parameterName = $parameter->getName();
0141         } elseif ($parameter instanceof Zend_CodeGenerator_Php_Parameter) {
0142             $parameterName = $parameter->getName();
0143         } else {
0144             // require_once 'Zend/CodeGenerator/Php/Exception.php';
0145             throw new Zend_CodeGenerator_Php_Exception('setParameter() expects either an array of method options or an instance of Zend_CodeGenerator_Php_Parameter');
0146         }
0147 
0148         $this->_parameters[$parameterName] = $parameter;
0149         return $this;
0150     }
0151 
0152     /**
0153      * getParameters()
0154      *
0155      * @return array Array of Zend_CodeGenerator_Php_Parameter
0156      */
0157     public function getParameters()
0158     {
0159         return $this->_parameters;
0160     }
0161 
0162     /**
0163      * setBody()
0164      *
0165      * @param string $body
0166      * @return Zend_CodeGenerator_Php_Method
0167      */
0168     public function setBody($body)
0169     {
0170         $this->_body = $body;
0171         return $this;
0172     }
0173 
0174     /**
0175      * getBody()
0176      *
0177      * @return string
0178      */
0179     public function getBody()
0180     {
0181         return $this->_body;
0182     }
0183 
0184     /**
0185      * generate()
0186      *
0187      * @return string
0188      */
0189     public function generate()
0190     {
0191         $output = '';
0192 
0193         $indent = $this->getIndentation();
0194 
0195         if (($docblock = $this->getDocblock()) !== null) {
0196             $docblock->setIndentation($indent);
0197             $output .= $docblock->generate();
0198         }
0199 
0200         $output .= $indent;
0201 
0202         if ($this->isAbstract()) {
0203             $output .= 'abstract ';
0204         } else {
0205             $output .= (($this->isFinal()) ? 'final ' : '');
0206         }
0207 
0208         $output .= $this->getVisibility()
0209             . (($this->isStatic()) ? ' static' : '')
0210             . ' function ' . $this->getName() . '(';
0211 
0212         $parameters = $this->getParameters();
0213         if (!empty($parameters)) {
0214             foreach ($parameters as $parameter) {
0215                 $parameterOuput[] = $parameter->generate();
0216             }
0217 
0218             $output .= implode(', ', $parameterOuput);
0219         }
0220 
0221         $output .= ')' . self::LINE_FEED . $indent . '{' . self::LINE_FEED;
0222 
0223         if ($this->_body && $this->isSourceDirty()) {
0224             $output .= '        '
0225                     .  str_replace(self::LINE_FEED, self::LINE_FEED . $indent . $indent, trim($this->_body))
0226                     .  self::LINE_FEED;
0227         } elseif ($this->_body) {
0228             $output .= $this->_body . self::LINE_FEED;
0229         }
0230 
0231         $output .= $indent . '}' . self::LINE_FEED;
0232 
0233         return $output;
0234     }
0235 
0236 }