web/lib/Zend/CodeGenerator/Php/Class.php
changeset 886 1e110b03ae96
parent 807 877f952ae2bd
child 1230 68c69c656a2c
equal deleted inserted replaced
885:2251fb41dbc7 886:1e110b03ae96
    13  * to license@zend.com so we can send you a copy immediately.
    13  * to license@zend.com so we can send you a copy immediately.
    14  *
    14  *
    15  * @category   Zend
    15  * @category   Zend
    16  * @package    Zend_CodeGenerator
    16  * @package    Zend_CodeGenerator
    17  * @subpackage PHP
    17  * @subpackage PHP
    18  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
    18  * @copyright  Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
    19  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    19  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    20  * @version    $Id: Class.php 21915 2010-04-16 20:40:15Z matthew $
    20  * @version    $Id: Class.php 24593 2012-01-05 20:35:02Z matthew $
    21  */
    21  */
    22 
    22 
    23 /**
    23 /**
    24  * @see Zend_CodeGenerator_Php_Abstract
    24  * @see Zend_CodeGenerator_Php_Abstract
    25  */
    25  */
    46 require_once 'Zend/CodeGenerator/Php/Docblock.php';
    46 require_once 'Zend/CodeGenerator/Php/Docblock.php';
    47 
    47 
    48 /**
    48 /**
    49  * @category   Zend
    49  * @category   Zend
    50  * @package    Zend_CodeGenerator
    50  * @package    Zend_CodeGenerator
    51  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
    51  * @copyright  Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
    52  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    52  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    53  */
    53  */
    54 class Zend_CodeGenerator_Php_Class extends Zend_CodeGenerator_Php_Abstract
    54 class Zend_CodeGenerator_Php_Class extends Zend_CodeGenerator_Php_Abstract
    55 {
    55 {
    56 
    56 
    81 
    81 
    82     /**
    82     /**
    83      * @var array Array of properties
    83      * @var array Array of properties
    84      */
    84      */
    85     protected $_properties = null;
    85     protected $_properties = null;
       
    86 
       
    87     /**
       
    88      * @var array Array of constants
       
    89      */
       
    90     protected $_constants = null;
    86 
    91 
    87     /**
    92     /**
    88      * @var array Array of methods
    93      * @var array Array of methods
    89      */
    94      */
    90     protected $_methods = null;
    95     protected $_methods = null;
   154             $docblock = array('shortDescription' => $docblock);
   159             $docblock = array('shortDescription' => $docblock);
   155         }
   160         }
   156 
   161 
   157         if (is_array($docblock)) {
   162         if (is_array($docblock)) {
   158             $docblock = new Zend_CodeGenerator_Php_Docblock($docblock);
   163             $docblock = new Zend_CodeGenerator_Php_Docblock($docblock);
   159         } elseif (!$docblock instanceof Zend_CodeGenerator_Php_Docblock) {
   164         } elseif ((!is_null($docblock)) && (!$docblock instanceof Zend_CodeGenerator_Php_Docblock)) {
   160             require_once 'Zend/CodeGenerator/Php/Exception.php';
   165             require_once 'Zend/CodeGenerator/Php/Exception.php';
   161             throw new Zend_CodeGenerator_Php_Exception('setDocblock() is expecting either a string, array or an instance of Zend_CodeGenerator_Php_Docblock');
   166             throw new Zend_CodeGenerator_Php_Exception('setDocblock() is expecting either a string, array or an instance of Zend_CodeGenerator_Php_Docblock');
   162         }
   167         }
   163 
   168 
   164         $this->_docblock = $docblock;
   169         $this->_docblock = $docblock;
   271      */
   276      */
   272     public function setProperties(Array $properties)
   277     public function setProperties(Array $properties)
   273     {
   278     {
   274         foreach ($properties as $property) {
   279         foreach ($properties as $property) {
   275             $this->setProperty($property);
   280             $this->setProperty($property);
       
   281         }
       
   282 
       
   283         return $this;
       
   284     }
       
   285 
       
   286     /**
       
   287      * setConstants()
       
   288      *
       
   289      * @param array $constants
       
   290      * @return Zend_CodeGenerator_Php_Class
       
   291      */
       
   292     public function setConstants(Array $constants)
       
   293     {
       
   294         foreach ($constants as $const) {
       
   295             $this->setConstant($const);
   276         }
   296         }
   277 
   297 
   278         return $this;
   298         return $this;
   279     }
   299     }
   280 
   300 
   294         } else {
   314         } else {
   295             require_once 'Zend/CodeGenerator/Php/Exception.php';
   315             require_once 'Zend/CodeGenerator/Php/Exception.php';
   296             throw new Zend_CodeGenerator_Php_Exception('setProperty() expects either an array of property options or an instance of Zend_CodeGenerator_Php_Property');
   316             throw new Zend_CodeGenerator_Php_Exception('setProperty() expects either an array of property options or an instance of Zend_CodeGenerator_Php_Property');
   297         }
   317         }
   298 
   318 
       
   319         if ($property->isConst()) {
       
   320             return $this->setConstant($property);
       
   321         }
   299         if (isset($this->_properties[$propertyName])) {
   322         if (isset($this->_properties[$propertyName])) {
   300             require_once 'Zend/CodeGenerator/Php/Exception.php';
   323             require_once 'Zend/CodeGenerator/Php/Exception.php';
   301             throw new Zend_CodeGenerator_Php_Exception('A property by name ' . $propertyName . ' already exists in this class.');
   324             throw new Zend_CodeGenerator_Php_Exception('A property by name ' . $propertyName . ' already exists in this class.');
   302         }
   325         }
   303 
   326 
   304         $this->_properties[$propertyName] = $property;
   327         $this->_properties[$propertyName] = $property;
   305         return $this;
   328         return $this;
   306     }
   329     }
   307 
   330 
   308     /**
   331     /**
       
   332      * setConstant()
       
   333      *
       
   334      * @param array|Zend_CodeGenerator_Php_Property $const
       
   335      * @return Zend_CodeGenerator_Php_Class
       
   336      */
       
   337     public function setConstant($const)
       
   338     {
       
   339         if (is_array($const)) {
       
   340             $const = new Zend_CodeGenerator_Php_Property($const);
       
   341             $constName = $const->getName();
       
   342         } elseif ($const instanceof Zend_CodeGenerator_Php_Property) {
       
   343             $constName = $const->getName();
       
   344         } else {
       
   345             require_once 'Zend/CodeGenerator/Php/Exception.php';
       
   346             throw new Zend_CodeGenerator_Php_Exception('setConstant() expects either an array of property options or an instance of Zend_CodeGenerator_Php_Property');
       
   347         }
       
   348 
       
   349         if (!$const->isConst()) {
       
   350             require_once 'Zend/CodeGenerator/Php/Exception.php';
       
   351             throw new Zend_CodeGenerator_Php_Exception('setProperty() expects argument to define a constant');
       
   352         }
       
   353         if (isset($this->_constants[$constName])) {
       
   354             require_once 'Zend/CodeGenerator/Php/Exception.php';
       
   355             throw new Zend_CodeGenerator_Php_Exception('A constant by name ' . $constName . ' already exists in this class.');
       
   356         }
       
   357 
       
   358         $this->_constants[$constName] = $const;
       
   359         return $this;
       
   360     }
       
   361 
       
   362     /**
   309      * getProperties()
   363      * getProperties()
   310      *
   364      *
   311      * @return array
   365      * @return array
   312      */
   366      */
   313     public function getProperties()
   367     public function getProperties()
   314     {
   368     {
   315         return $this->_properties;
   369         return $this->_properties;
       
   370     }
       
   371 
       
   372     /**
       
   373      * getConstants()
       
   374      *
       
   375      * @return array
       
   376      */
       
   377     public function getConstants()
       
   378     {
       
   379         return $this->_constants;
   316     }
   380     }
   317 
   381 
   318     /**
   382     /**
   319      * getProperty()
   383      * getProperty()
   320      *
   384      *
   330         }
   394         }
   331         return false;
   395         return false;
   332     }
   396     }
   333 
   397 
   334     /**
   398     /**
       
   399      * getConstant()
       
   400      *
       
   401      * @param string $constName
       
   402      * @return Zend_CodeGenerator_Php_Property
       
   403      */
       
   404     public function getConstant($constName)
       
   405     {
       
   406         foreach ($this->_constants as $const) {
       
   407             if ($const->getName() == $constName) {
       
   408                 return $const;
       
   409             }
       
   410         }
       
   411         return false;
       
   412     }
       
   413 
       
   414     /**
   335      * hasProperty()
   415      * hasProperty()
   336      *
   416      *
   337      * @param string $propertyName
   417      * @param string $propertyName
   338      * @return bool
   418      * @return bool
   339      */
   419      */
   340     public function hasProperty($propertyName)
   420     public function hasProperty($propertyName)
   341     {
   421     {
   342         return isset($this->_properties[$propertyName]);
   422         return isset($this->_properties[$propertyName]);
       
   423     }
       
   424 
       
   425     /**
       
   426      * hasConstant()
       
   427      *
       
   428      * @param string $constName
       
   429      * @return bool
       
   430      */
       
   431     public function hasConstant($constName)
       
   432     {
       
   433         return isset($this->_constants[$constName]);
   343     }
   434     }
   344 
   435 
   345     /**
   436     /**
   346      * setMethods()
   437      * setMethods()
   347      *
   438      *
   435             if ($property->isSourceDirty()) {
   526             if ($property->isSourceDirty()) {
   436                 return true;
   527                 return true;
   437             }
   528             }
   438         }
   529         }
   439 
   530 
       
   531         foreach ($this->_constants as $constant) {
       
   532             if ($constant->isSourceDirty()) {
       
   533                 return true;
       
   534             }
       
   535         }
       
   536 
   440         foreach ($this->_methods as $method) {
   537         foreach ($this->_methods as $method) {
   441             if ($method->isSourceDirty()) {
   538             if ($method->isSourceDirty()) {
   442                 return true;
   539                 return true;
   443             }
   540             }
   444         }
   541         }
   478         if (!empty($implemented)) {
   575         if (!empty($implemented)) {
   479             $output .= ' implements ' . implode(', ', $implemented);
   576             $output .= ' implements ' . implode(', ', $implemented);
   480         }
   577         }
   481 
   578 
   482         $output .= self::LINE_FEED . '{' . self::LINE_FEED . self::LINE_FEED;
   579         $output .= self::LINE_FEED . '{' . self::LINE_FEED . self::LINE_FEED;
       
   580 
       
   581         $constants = $this->getConstants();
       
   582         if (!empty($constants)) {
       
   583             foreach ($constants as $const) {
       
   584                 $output .= $const->generate() . self::LINE_FEED . self::LINE_FEED;
       
   585             }
       
   586         }
   483 
   587 
   484         $properties = $this->getProperties();
   588         $properties = $this->getProperties();
   485         if (!empty($properties)) {
   589         if (!empty($properties)) {
   486             foreach ($properties as $property) {
   590             foreach ($properties as $property) {
   487                 $output .= $property->generate() . self::LINE_FEED . self::LINE_FEED;
   591                 $output .= $property->generate() . self::LINE_FEED . self::LINE_FEED;
   505      *
   609      *
   506      */
   610      */
   507     protected function _init()
   611     protected function _init()
   508     {
   612     {
   509         $this->_properties = new Zend_CodeGenerator_Php_Member_Container(Zend_CodeGenerator_Php_Member_Container::TYPE_PROPERTY);
   613         $this->_properties = new Zend_CodeGenerator_Php_Member_Container(Zend_CodeGenerator_Php_Member_Container::TYPE_PROPERTY);
       
   614         $this->_constants = new Zend_CodeGenerator_Php_Member_Container(Zend_CodeGenerator_Php_Member_Container::TYPE_PROPERTY);
   510         $this->_methods = new Zend_CodeGenerator_Php_Member_Container(Zend_CodeGenerator_Php_Member_Container::TYPE_METHOD);
   615         $this->_methods = new Zend_CodeGenerator_Php_Member_Container(Zend_CodeGenerator_Php_Member_Container::TYPE_METHOD);
   511     }
   616     }
   512 
   617 
   513 }
   618 }