web/Zend/CodeGenerator/Php/Docblock.php
changeset 0 4eba9c11703f
equal deleted inserted replaced
-1:000000000000 0:4eba9c11703f
       
     1 <?php
       
     2 /**
       
     3  * Zend Framework
       
     4  *
       
     5  * LICENSE
       
     6  *
       
     7  * This source file is subject to the new BSD license that is bundled
       
     8  * with this package in the file LICENSE.txt.
       
     9  * It is also available through the world-wide-web at this URL:
       
    10  * http://framework.zend.com/license/new-bsd
       
    11  * If you did not receive a copy of the license and are unable to
       
    12  * obtain it through the world-wide-web, please send an email
       
    13  * to license@zend.com so we can send you a copy immediately.
       
    14  *
       
    15  * @category   Zend
       
    16  * @package    Zend_CodeGenerator
       
    17  * @subpackage PHP
       
    18  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    19  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    20  * @version    $Id: Docblock.php 20096 2010-01-06 02:05:09Z bkarwin $
       
    21  */
       
    22 
       
    23 /**
       
    24  * @see Zend_CodeGenerator_Php_Abstract
       
    25  */
       
    26 require_once 'Zend/CodeGenerator/Php/Abstract.php';
       
    27 
       
    28 /**
       
    29  * @see Zend_CodeGenerator_Php_Docblock_Tag
       
    30  */
       
    31 require_once 'Zend/CodeGenerator/Php/Docblock/Tag.php';
       
    32 
       
    33 /**
       
    34  * @category   Zend
       
    35  * @package    Zend_CodeGenerator
       
    36  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    37  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    38  */
       
    39 class Zend_CodeGenerator_Php_Docblock extends Zend_CodeGenerator_Php_Abstract
       
    40 {
       
    41     /**
       
    42      * @var string
       
    43      */
       
    44     protected $_shortDescription = null;
       
    45 
       
    46     /**
       
    47      * @var string
       
    48      */
       
    49     protected $_longDescription = null;
       
    50 
       
    51     /**
       
    52      * @var array
       
    53      */
       
    54     protected $_tags = array();
       
    55 
       
    56     /**
       
    57      * @var string
       
    58      */
       
    59     protected $_indentation = '';
       
    60 
       
    61     /**
       
    62      * fromReflection() - Build a docblock generator object from a reflection object
       
    63      *
       
    64      * @param Zend_Reflection_Docblock $reflectionDocblock
       
    65      * @return Zend_CodeGenerator_Php_Docblock
       
    66      */
       
    67     public static function fromReflection(Zend_Reflection_Docblock $reflectionDocblock)
       
    68     {
       
    69         $docblock = new self();
       
    70 
       
    71         $docblock->setSourceContent($reflectionDocblock->getContents());
       
    72         $docblock->setSourceDirty(false);
       
    73 
       
    74         $docblock->setShortDescription($reflectionDocblock->getShortDescription());
       
    75         $docblock->setLongDescription($reflectionDocblock->getLongDescription());
       
    76 
       
    77         foreach ($reflectionDocblock->getTags() as $tag) {
       
    78             $docblock->setTag(Zend_CodeGenerator_Php_Docblock_Tag::fromReflection($tag));
       
    79         }
       
    80 
       
    81         return $docblock;
       
    82     }
       
    83 
       
    84     /**
       
    85      * setShortDescription()
       
    86      *
       
    87      * @param string $shortDescription
       
    88      * @return Zend_CodeGenerator_Php_Docblock
       
    89      */
       
    90     public function setShortDescription($shortDescription)
       
    91     {
       
    92         $this->_shortDescription = $shortDescription;
       
    93         return $this;
       
    94     }
       
    95 
       
    96     /**
       
    97      * getShortDescription()
       
    98      *
       
    99      * @return string
       
   100      */
       
   101     public function getShortDescription()
       
   102     {
       
   103         return $this->_shortDescription;
       
   104     }
       
   105 
       
   106     /**
       
   107      * setLongDescription()
       
   108      *
       
   109      * @param string $longDescription
       
   110      * @return Zend_CodeGenerator_Php_Docblock
       
   111      */
       
   112     public function setLongDescription($longDescription)
       
   113     {
       
   114         $this->_longDescription = $longDescription;
       
   115         return $this;
       
   116     }
       
   117 
       
   118     /**
       
   119      * getLongDescription()
       
   120      *
       
   121      * @return string
       
   122      */
       
   123     public function getLongDescription()
       
   124     {
       
   125         return $this->_longDescription;
       
   126     }
       
   127 
       
   128     /**
       
   129      * setTags()
       
   130      *
       
   131      * @param array $tags
       
   132      * @return Zend_CodeGenerator_Php_Docblock
       
   133      */
       
   134     public function setTags(Array $tags)
       
   135     {
       
   136         foreach ($tags as $tag) {
       
   137             $this->setTag($tag);
       
   138         }
       
   139 
       
   140         return $this;
       
   141     }
       
   142 
       
   143     /**
       
   144      * setTag()
       
   145      *
       
   146      * @param array|Zend_CodeGenerator_Php_Docblock_Tag $tag
       
   147      * @return Zend_CodeGenerator_Php_Docblock
       
   148      */
       
   149     public function setTag($tag)
       
   150     {
       
   151         if (is_array($tag)) {
       
   152             $tag = new Zend_CodeGenerator_Php_Docblock_Tag($tag);
       
   153         } elseif (!$tag instanceof Zend_CodeGenerator_Php_Docblock_Tag) {
       
   154             require_once 'Zend/CodeGenerator/Php/Exception.php';
       
   155             throw new Zend_CodeGenerator_Php_Exception(
       
   156                 'setTag() expects either an array of method options or an '
       
   157                 . 'instance of Zend_CodeGenerator_Php_Docblock_Tag'
       
   158                 );
       
   159         }
       
   160 
       
   161         $this->_tags[] = $tag;
       
   162         return $this;
       
   163     }
       
   164 
       
   165     /**
       
   166      * getTags
       
   167      *
       
   168      * @return array Array of Zend_CodeGenerator_Php_Docblock_Tag
       
   169      */
       
   170     public function getTags()
       
   171     {
       
   172         return $this->_tags;
       
   173     }
       
   174 
       
   175     /**
       
   176      * generate()
       
   177      *
       
   178      * @return string
       
   179      */
       
   180     public function generate()
       
   181     {
       
   182         if (!$this->isSourceDirty()) {
       
   183             return $this->_docCommentize($this->getSourceContent());
       
   184         }
       
   185 
       
   186         $output  = '';
       
   187         if (null !== ($sd = $this->getShortDescription())) {
       
   188             $output .= $sd . self::LINE_FEED . self::LINE_FEED;
       
   189         }
       
   190         if (null !== ($ld = $this->getLongDescription())) {
       
   191             $output .= $ld . self::LINE_FEED . self::LINE_FEED;
       
   192         }
       
   193 
       
   194         foreach ($this->getTags() as $tag) {
       
   195             $output .= $tag->generate() . self::LINE_FEED;
       
   196         }
       
   197 
       
   198         return $this->_docCommentize(trim($output));
       
   199     }
       
   200 
       
   201     /**
       
   202      * _docCommentize()
       
   203      *
       
   204      * @param string $content
       
   205      * @return string
       
   206      */
       
   207     protected function _docCommentize($content)
       
   208     {
       
   209         $indent = $this->getIndentation();
       
   210         $output = $indent . '/**' . self::LINE_FEED;
       
   211         $content = wordwrap($content, 80, self::LINE_FEED);
       
   212         $lines = explode(self::LINE_FEED, $content);
       
   213         foreach ($lines as $line) {
       
   214             $output .= $indent . ' * ' . $line . self::LINE_FEED;
       
   215         }
       
   216         $output .= $indent . ' */' . self::LINE_FEED;
       
   217         return $output;
       
   218     }
       
   219 
       
   220 }