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 : Class . php 18951 2009 - 11 - 12 16 : 26 : 19 Z alexander $
2019-07-17 20:16:19 +00:00
*/
/**
* @ see Zend_CodeGenerator_Php_Abstract
*/
require_once 'Zend/CodeGenerator/Php/Abstract.php' ;
/**
* @ see Zend_CodeGenerator_Php_Member_Container
*/
require_once 'Zend/CodeGenerator/Php/Member/Container.php' ;
/**
* @ see Zend_CodeGenerator_Php_Method
*/
require_once 'Zend/CodeGenerator/Php/Method.php' ;
/**
* @ see Zend_CodeGenerator_Php_Property
*/
require_once 'Zend/CodeGenerator/Php/Property.php' ;
/**
* @ see Zend_CodeGenerator_Php_Docblock
*/
require_once 'Zend/CodeGenerator/Php/Docblock.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
*/
class Zend_CodeGenerator_Php_Class extends Zend_CodeGenerator_Php_Abstract
{
2019-07-17 20:31:04 +00:00
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 string
*/
protected $_name = null ;
2019-07-17 20:31:04 +00:00
2019-07-17 20:16:19 +00:00
/**
* @ var bool
*/
protected $_isAbstract = false ;
2019-07-17 20:31:04 +00:00
2019-07-17 20:16:19 +00:00
/**
* @ var string
*/
protected $_extendedClass = null ;
2019-07-17 20:31:04 +00:00
2019-07-17 20:16:19 +00:00
/**
* @ var array Array of string names
*/
protected $_implementedInterfaces = array ();
2019-07-17 20:31:04 +00:00
2019-07-17 20:16:19 +00:00
/**
* @ var array Array of properties
*/
protected $_properties = null ;
2019-07-17 20:31:04 +00:00
2019-07-17 20:16:19 +00:00
/**
* @ var array Array of methods
*/
protected $_methods = null ;
/**
* fromReflection () - build a Code Generation PHP Object from a Class Reflection
*
* @ param Zend_Reflection_Class $reflectionClass
* @ return Zend_CodeGenerator_Php_Class
*/
public static function fromReflection ( Zend_Reflection_Class $reflectionClass )
{
$class = new self ();
2019-07-17 20:31:04 +00:00
2019-07-17 20:16:19 +00:00
$class -> setSourceContent ( $class -> getSourceContent ());
$class -> setSourceDirty ( false );
2019-07-17 20:31:04 +00:00
2019-07-17 20:16:19 +00:00
if ( $reflectionClass -> getDocComment () != '' ) {
$class -> setDocblock ( Zend_CodeGenerator_Php_Docblock :: fromReflection ( $reflectionClass -> getDocblock ()));
}
2019-07-17 20:31:04 +00:00
2019-07-17 20:16:19 +00:00
$class -> setAbstract ( $reflectionClass -> isAbstract ());
$class -> setName ( $reflectionClass -> getName ());
2019-07-17 20:31:04 +00:00
2019-07-17 20:16:19 +00:00
if ( $parentClass = $reflectionClass -> getParentClass ()) {
$class -> setExtendedClass ( $parentClass -> getName ());
2019-07-17 20:31:04 +00:00
$interfaces = array_diff ( $reflectionClass -> getInterfaces (), $parentClass -> getInterfaces ());
2019-07-17 20:16:19 +00:00
} else {
$interfaces = $reflectionClass -> getInterfaces ();
}
2019-07-17 20:31:04 +00:00
$interfaceNames = array ();
foreach ( $interfaces AS $interface ) {
$interfaceNames [] = $interface -> getName ();
}
$class -> setImplementedInterfaces ( $interfaceNames );
2019-07-17 20:16:19 +00:00
$properties = array ();
foreach ( $reflectionClass -> getProperties () as $reflectionProperty ) {
if ( $reflectionProperty -> getDeclaringClass () -> getName () == $class -> getName ()) {
$properties [] = Zend_CodeGenerator_Php_Property :: fromReflection ( $reflectionProperty );
}
}
$class -> setProperties ( $properties );
2019-07-17 20:31:04 +00:00
2019-07-17 20:16:19 +00:00
$methods = array ();
foreach ( $reflectionClass -> getMethods () as $reflectionMethod ) {
if ( $reflectionMethod -> getDeclaringClass () -> getName () == $class -> getName ()) {
$methods [] = Zend_CodeGenerator_Php_Method :: fromReflection ( $reflectionMethod );
}
}
$class -> setMethods ( $methods );
2019-07-17 20:31:04 +00:00
2019-07-17 20:16:19 +00:00
return $class ;
}
2019-07-17 20:31:04 +00:00
2019-07-17 20:16:19 +00:00
/**
* setDocblock () Set the docblock
*
* @ param Zend_CodeGenerator_Php_Docblock | array | string $docblock
* @ return Zend_CodeGenerator_Php_File
*/
2019-07-17 20:31:04 +00:00
public function setDocblock ( $docblock )
2019-07-17 20:16:19 +00:00
{
if ( is_string ( $docblock )) {
$docblock = array ( 'shortDescription' => $docblock );
}
2019-07-17 20:31:04 +00:00
2019-07-17 20:16:19 +00:00
if ( is_array ( $docblock )) {
$docblock = new Zend_CodeGenerator_Php_Docblock ( $docblock );
} elseif ( ! $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' );
}
2019-07-17 20:31:04 +00:00
2019-07-17 20:16:19 +00:00
$this -> _docblock = $docblock ;
return $this ;
}
2019-07-17 20:31:04 +00:00
2019-07-17 20:16:19 +00:00
/**
* getDocblock ()
*
* @ return Zend_CodeGenerator_Php_Docblock
*/
public function getDocblock ()
{
return $this -> _docblock ;
}
2019-07-17 20:31:04 +00:00
2019-07-17 20:16:19 +00:00
/**
* setName ()
*
* @ param string $name
* @ return Zend_CodeGenerator_Php_Class
*/
public function setName ( $name )
{
$this -> _name = $name ;
return $this ;
}
2019-07-17 20:31:04 +00:00
2019-07-17 20:16:19 +00:00
/**
* getName ()
*
* @ return string
*/
public function getName ()
{
return $this -> _name ;
}
/**
* setAbstract ()
*
* @ param bool $isAbstract
* @ return Zend_CodeGenerator_Php_Class
*/
public function setAbstract ( $isAbstract )
{
$this -> _isAbstract = ( $isAbstract ) ? true : false ;
return $this ;
}
2019-07-17 20:31:04 +00:00
2019-07-17 20:16:19 +00:00
/**
* isAbstract ()
*
* @ return bool
*/
public function isAbstract ()
{
return $this -> _isAbstract ;
}
2019-07-17 20:31:04 +00:00
2019-07-17 20:16:19 +00:00
/**
* setExtendedClass ()
*
* @ param string $extendedClass
* @ return Zend_CodeGenerator_Php_Class
*/
public function setExtendedClass ( $extendedClass )
{
$this -> _extendedClass = $extendedClass ;
return $this ;
}
2019-07-17 20:31:04 +00:00
2019-07-17 20:16:19 +00:00
/**
* getExtendedClass ()
*
* @ return string
*/
public function getExtendedClass ()
{
return $this -> _extendedClass ;
}
2019-07-17 20:31:04 +00:00
2019-07-17 20:16:19 +00:00
/**
* setImplementedInterfaces ()
*
* @ param array $implementedInterfaces
* @ return Zend_CodeGenerator_Php_Class
*/
public function setImplementedInterfaces ( Array $implementedInterfaces )
{
$this -> _implementedInterfaces = $implementedInterfaces ;
return $this ;
}
2019-07-17 20:31:04 +00:00
2019-07-17 20:16:19 +00:00
/**
* getImplementedInterfaces
*
* @ return array
*/
public function getImplementedInterfaces ()
{
return $this -> _implementedInterfaces ;
}
2019-07-17 20:31:04 +00:00
2019-07-17 20:16:19 +00:00
/**
* setProperties ()
*
* @ param array $properties
* @ return Zend_CodeGenerator_Php_Class
*/
public function setProperties ( Array $properties )
{
foreach ( $properties as $property ) {
$this -> setProperty ( $property );
}
2019-07-17 20:31:04 +00:00
2019-07-17 20:16:19 +00:00
return $this ;
}
2019-07-17 20:31:04 +00:00
2019-07-17 20:16:19 +00:00
/**
* setProperty ()
*
* @ param array | Zend_CodeGenerator_Php_Property $property
* @ return Zend_CodeGenerator_Php_Class
*/
public function setProperty ( $property )
{
if ( is_array ( $property )) {
$property = new Zend_CodeGenerator_Php_Property ( $property );
$propertyName = $property -> getName ();
} elseif ( $property instanceof Zend_CodeGenerator_Php_Property ) {
$propertyName = $property -> getName ();
} else {
require_once 'Zend/CodeGenerator/Php/Exception.php' ;
throw new Zend_CodeGenerator_Php_Exception ( 'setProperty() expects either an array of property options or an instance of Zend_CodeGenerator_Php_Property' );
}
2019-07-17 20:31:04 +00:00
2019-07-17 20:16:19 +00:00
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.' );
}
2019-07-17 20:31:04 +00:00
$this -> _properties [ $propertyName ] = $property ;
2019-07-17 20:16:19 +00:00
return $this ;
}
2019-07-17 20:31:04 +00:00
2019-07-17 20:16:19 +00:00
/**
* getProperties ()
*
* @ return array
*/
public function getProperties ()
{
return $this -> _properties ;
}
2019-07-17 20:31:04 +00:00
2019-07-17 20:16:19 +00:00
/**
* getProperty ()
*
* @ param string $propertyName
* @ return Zend_CodeGenerator_Php_Property
*/
public function getProperty ( $propertyName )
{
foreach ( $this -> _properties as $property ) {
if ( $property -> getName () == $propertyName ) {
return $property ;
}
}
return false ;
}
2019-07-17 20:31:04 +00:00
2019-07-17 20:19:00 +00:00
/**
* hasProperty ()
*
* @ param string $propertyName
* @ return bool
*/
public function hasProperty ( $propertyName )
{
return isset ( $this -> _properties [ $propertyName ]);
}
2019-07-17 20:31:04 +00:00
2019-07-17 20:16:19 +00:00
/**
* setMethods ()
*
* @ param array $methods
* @ return Zend_CodeGenerator_Php_Class
*/
public function setMethods ( Array $methods )
{
foreach ( $methods as $method ) {
$this -> setMethod ( $method );
}
return $this ;
}
2019-07-17 20:31:04 +00:00
2019-07-17 20:16:19 +00:00
/**
* setMethod ()
*
* @ param array | Zend_CodeGenerator_Php_Method $method
* @ return Zend_CodeGenerator_Php_Class
*/
public function setMethod ( $method )
{
if ( is_array ( $method )) {
$method = new Zend_CodeGenerator_Php_Method ( $method );
$methodName = $method -> getName ();
} elseif ( $method instanceof Zend_CodeGenerator_Php_Method ) {
$methodName = $method -> getName ();
} else {
require_once 'Zend/CodeGenerator/Php/Exception.php' ;
throw new Zend_CodeGenerator_Php_Exception ( 'setMethod() expects either an array of method options or an instance of Zend_CodeGenerator_Php_Method' );
}
2019-07-17 20:31:04 +00:00
2019-07-17 20:16:19 +00:00
if ( isset ( $this -> _methods [ $methodName ])) {
require_once 'Zend/CodeGenerator/Php/Exception.php' ;
throw new Zend_CodeGenerator_Php_Exception ( 'A method by name ' . $methodName . ' already exists in this class.' );
}
2019-07-17 20:31:04 +00:00
$this -> _methods [ $methodName ] = $method ;
2019-07-17 20:16:19 +00:00
return $this ;
}
2019-07-17 20:31:04 +00:00
2019-07-17 20:16:19 +00:00
/**
* getMethods ()
*
* @ return array
*/
public function getMethods ()
{
return $this -> _methods ;
}
2019-07-17 20:31:04 +00:00
2019-07-17 20:16:19 +00:00
/**
* getMethod ()
*
* @ param string $methodName
* @ return Zend_CodeGenerator_Php_Method
*/
public function getMethod ( $methodName )
{
foreach ( $this -> _methods as $method ) {
if ( $method -> getName () == $methodName ) {
return $method ;
}
}
return false ;
}
2019-07-17 20:31:04 +00:00
2019-07-17 20:16:19 +00:00
/**
* hasMethod ()
*
* @ param string $methodName
* @ return bool
*/
public function hasMethod ( $methodName )
{
return isset ( $this -> _methods [ $methodName ]);
}
2019-07-17 20:31:04 +00:00
2019-07-17 20:16:19 +00:00
/**
* isSourceDirty ()
*
* @ return bool
*/
public function isSourceDirty ()
{
if (( $docblock = $this -> getDocblock ()) && $docblock -> isSourceDirty ()) {
return true ;
}
2019-07-17 20:31:04 +00:00
2019-07-17 20:16:19 +00:00
foreach ( $this -> _properties as $property ) {
if ( $property -> isSourceDirty ()) {
return true ;
}
}
2019-07-17 20:31:04 +00:00
2019-07-17 20:16:19 +00:00
foreach ( $this -> _methods as $method ) {
if ( $method -> isSourceDirty ()) {
return true ;
}
}
2019-07-17 20:31:04 +00:00
2019-07-17 20:16:19 +00:00
return parent :: isSourceDirty ();
}
2019-07-17 20:31:04 +00:00
2019-07-17 20:16:19 +00:00
/**
* generate ()
*
* @ return string
*/
public function generate ()
{
if ( ! $this -> isSourceDirty ()) {
return $this -> getSourceContent ();
}
2019-07-17 20:31:04 +00:00
$output = '' ;
2019-07-17 20:16:19 +00:00
if ( null !== ( $docblock = $this -> getDocblock ())) {
$docblock -> setIndentation ( '' );
$output .= $docblock -> generate ();
}
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:31:04 +00:00
2019-07-17 20:16:19 +00:00
$output .= 'class ' . $this -> getName ();
2019-07-17 20:31:04 +00:00
2019-07-17 20:16:19 +00:00
if ( null !== ( $ec = $this -> _extendedClass )) {
$output .= ' extends ' . $ec ;
}
2019-07-17 20:31:04 +00:00
2019-07-17 20:16:19 +00:00
$implemented = $this -> getImplementedInterfaces ();
if ( ! empty ( $implemented )) {
$output .= ' implements ' . implode ( ', ' , $implemented );
}
2019-07-17 20:31:04 +00:00
2019-07-17 20:19:00 +00:00
$output .= self :: LINE_FEED . '{' . self :: LINE_FEED . self :: LINE_FEED ;
2019-07-17 20:31:04 +00:00
2019-07-17 20:16:19 +00:00
$properties = $this -> getProperties ();
if ( ! empty ( $properties )) {
foreach ( $properties as $property ) {
2019-07-17 20:19:00 +00:00
$output .= $property -> generate () . self :: LINE_FEED . self :: LINE_FEED ;
2019-07-17 20:16:19 +00:00
}
}
2019-07-17 20:31:04 +00:00
2019-07-17 20:16:19 +00:00
$methods = $this -> getMethods ();
if ( ! empty ( $methods )) {
foreach ( $methods as $method ) {
2019-07-17 20:19:00 +00:00
$output .= $method -> generate () . 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 .= self :: LINE_FEED . '}' . 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
/**
* _init () - is called at construction time
*
*/
protected function _init ()
{
$this -> _properties = 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 );
}
}