2019-07-17 20:16:19 +00:00
< ? 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
2019-07-17 20:31:04 +00:00
* @ version $Id : Method . php 18951 2009 - 11 - 12 16 : 26 : 19 Z alexander $
2019-07-17 20:16:19 +00:00
*/
/**
* @ see Zend_CodeGenerator_Php_Member_Abstract
*/
require_once 'Zend/CodeGenerator/Php/Member/Abstract.php' ;
/**
* @ see Zend_CodeGenerator_Php_Docblock
*/
require_once 'Zend/CodeGenerator/Php/Docblock.php' ;
/**
* @ see Zend_CodeGenerator_Php_Parameter
*/
require_once 'Zend/CodeGenerator/Php/Parameter.php' ;
/**
* @ category Zend
* @ package Zend_CodeGenerator
* @ copyright Copyright ( c ) 2005 - 2009 Zend Technologies USA Inc . ( http :// www . zend . com )
* @ license http :// framework . zend . com / license / new - bsd New BSD License
*/
2019-07-17 20:31:04 +00:00
class Zend_CodeGenerator_Php_Method extends Zend_CodeGenerator_Php_Member_Abstract
2019-07-17 20:16:19 +00:00
{
/**
* @ var Zend_CodeGenerator_Php_Docblock
*/
protected $_docblock = null ;
2019-07-17 20:31:04 +00:00
2019-07-17 20:16:19 +00:00
/**
* @ var bool
*/
protected $_isFinal = false ;
2019-07-17 20:31:04 +00:00
2019-07-17 20:16:19 +00:00
/**
* @ var array
*/
protected $_parameters = array ();
2019-07-17 20:31:04 +00:00
2019-07-17 20:16:19 +00:00
/**
* @ var string
*/
protected $_body = null ;
2019-07-17 20:31:04 +00:00
2019-07-17 20:16:19 +00:00
/**
* fromReflection ()
*
* @ param Zend_Reflection_Method $reflectionMethod
* @ return Zend_CodeGenerator_Php_Method
*/
public static function fromReflection ( Zend_Reflection_Method $reflectionMethod )
{
$method = new self ();
2019-07-17 20:31:04 +00:00
2019-07-17 20:16:19 +00:00
$method -> setSourceContent ( $reflectionMethod -> getContents ( false ));
$method -> setSourceDirty ( false );
2019-07-17 20:31:04 +00:00
2019-07-17 20:16:19 +00:00
if ( $reflectionMethod -> getDocComment () != '' ) {
$method -> setDocblock ( Zend_CodeGenerator_Php_Docblock :: fromReflection ( $reflectionMethod -> getDocblock ()));
}
2019-07-17 20:31:04 +00:00
2019-07-17 20:16:19 +00:00
$method -> setFinal ( $reflectionMethod -> isFinal ());
2019-07-17 20:31:04 +00:00
2019-07-17 20:16:19 +00:00
if ( $reflectionMethod -> isPrivate ()) {
$method -> setVisibility ( self :: VISIBILITY_PRIVATE );
} elseif ( $reflectionMethod -> isProtected ()) {
$method -> setVisibility ( self :: VISIBILITY_PROTECTED );
} else {
$method -> setVisibility ( self :: VISIBILITY_PUBLIC );
}
2019-07-17 20:31:04 +00:00
2019-07-17 20:16:19 +00:00
$method -> setStatic ( $reflectionMethod -> isStatic ());
$method -> setName ( $reflectionMethod -> getName ());
2019-07-17 20:31:04 +00:00
2019-07-17 20:16:19 +00:00
foreach ( $reflectionMethod -> getParameters () as $reflectionParameter ) {
$method -> setParameter ( Zend_CodeGenerator_Php_Parameter :: fromReflection ( $reflectionParameter ));
}
2019-07-17 20:31:04 +00:00
2019-07-17 20:16:19 +00:00
$method -> setBody ( $reflectionMethod -> getBody ());
return $method ;
}
2019-07-17 20:31:04 +00:00
2019-07-17 20:16:19 +00:00
/**
* setFinal ()
*
* @ param bool $isFinal
*/
public function setFinal ( $isFinal )
{
$this -> _isFinal = ( $isFinal ) ? true : false ;
}
2019-07-17 20:31:04 +00:00
2019-07-17 20:16:19 +00:00
/**
* setParameters ()
*
* @ param array $parameters
* @ return Zend_CodeGenerator_Php_Method
*/
public function setParameters ( Array $parameters )
{
foreach ( $parameters as $parameter ) {
$this -> setParameter ( $parameter );
}
return $this ;
}
2019-07-17 20:31:04 +00:00
2019-07-17 20:16:19 +00:00
/**
* setParameter ()
*
* @ param Zend_CodeGenerator_Php_Parameter | array $parameter
* @ return Zend_CodeGenerator_Php_Method
*/
public function setParameter ( $parameter )
{
if ( is_array ( $parameter )) {
$parameter = new Zend_CodeGenerator_Php_Parameter ( $parameter );
$parameterName = $parameter -> getName ();
} elseif ( $parameter instanceof Zend_CodeGenerator_Php_Parameter ) {
$parameterName = $parameter -> getName ();
} else {
require_once 'Zend/CodeGenerator/Php/Exception.php' ;
throw new Zend_CodeGenerator_Php_Exception ( 'setParameter() expects either an array of method options or an instance of Zend_CodeGenerator_Php_Parameter' );
}
2019-07-17 20:31:04 +00:00
2019-07-17 20:16:19 +00:00
$this -> _parameters [ $parameterName ] = $parameter ;
return $this ;
}
2019-07-17 20:31:04 +00:00
2019-07-17 20:16:19 +00:00
/**
* getParameters ()
*
* @ return array Array of Zend_CodeGenerator_Php_Parameter
*/
public function getParameters ()
{
return $this -> _parameters ;
}
/**
* setBody ()
*
* @ param string $body
* @ return Zend_CodeGenerator_Php_Method
*/
public function setBody ( $body )
{
$this -> _body = $body ;
return $this ;
}
2019-07-17 20:31:04 +00:00
2019-07-17 20:16:19 +00:00
/**
* getBody ()
*
* @ return string
*/
public function getBody ()
{
return $this -> _body ;
}
2019-07-17 20:31:04 +00:00
2019-07-17 20:16:19 +00:00
/**
* generate ()
*
* @ return string
*/
public function generate ()
{
2019-07-17 20:19:00 +00:00
$output = '' ;
2019-07-17 20:31:04 +00:00
2019-07-17 20:19:00 +00:00
$indent = $this -> getIndentation ();
2019-07-17 20:31:04 +00:00
2019-07-17 20:19:00 +00:00
if (( $docblock = $this -> getDocblock ()) !== null ) {
$docblock -> setIndentation ( $indent );
2019-07-17 20:16:19 +00:00
$output .= $docblock -> generate ();
}
2019-07-17 20:31:04 +00:00
2019-07-17 20:19:00 +00:00
$output .= $indent ;
2019-07-17 20:31:04 +00:00
2019-07-17 20:16:19 +00:00
if ( $this -> isAbstract ()) {
$output .= 'abstract ' ;
2019-07-17 20:19:00 +00:00
} else {
$output .= (( $this -> isFinal ()) ? 'final ' : '' );
2019-07-17 20:16:19 +00:00
}
2019-07-17 20:31:04 +00:00
2019-07-17 20:19:00 +00:00
$output .= $this -> getVisibility ()
. (( $this -> isStatic ()) ? ' static' : '' )
. ' function ' . $this -> getName () . '(' ;
2019-07-17 20:16:19 +00:00
$parameters = $this -> getParameters ();
if ( ! empty ( $parameters )) {
foreach ( $parameters as $parameter ) {
$parameterOuput [] = $parameter -> generate ();
}
2019-07-17 20:31:04 +00:00
2019-07-17 20:16:19 +00:00
$output .= implode ( ', ' , $parameterOuput );
}
2019-07-17 20:31:04 +00:00
2019-07-17 20:19:00 +00:00
$output .= ')' . self :: LINE_FEED . $indent . '{' . self :: LINE_FEED ;
2019-07-17 20:16:19 +00:00
if ( $this -> _body ) {
2019-07-17 20:31:04 +00:00
$output .= ' '
. str_replace ( self :: LINE_FEED , self :: LINE_FEED . $indent . $indent , trim ( $this -> _body ))
2019-07-17 20:19:00 +00:00
. self :: LINE_FEED ;
2019-07-17 20:16:19 +00:00
}
2019-07-17 20:31:04 +00:00
2019-07-17 20:19:00 +00:00
$output .= $indent . '}' . self :: LINE_FEED ;
2019-07-17 20:31:04 +00:00
2019-07-17 20:16:19 +00:00
return $output ;
}
2019-07-17 20:31:04 +00:00
2019-07-17 20:16:19 +00:00
}