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_Abstract
0025  */
0026 // require_once 'Zend/CodeGenerator/Php/Abstract.php';
0027 
0028 /**
0029  * @see Zend_CodeGenerator_Php_Docblock_Tag
0030  */
0031 // require_once 'Zend/CodeGenerator/Php/Docblock/Tag.php';
0032 
0033 /**
0034  * @category   Zend
0035  * @package    Zend_CodeGenerator
0036  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0037  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0038  */
0039 class Zend_CodeGenerator_Php_Docblock extends Zend_CodeGenerator_Php_Abstract
0040 {
0041     /**
0042      * @var string
0043      */
0044     protected $_shortDescription = null;
0045 
0046     /**
0047      * @var string
0048      */
0049     protected $_longDescription = null;
0050 
0051     /**
0052      * @var array
0053      */
0054     protected $_tags = array();
0055 
0056     /**
0057      * @var string
0058      */
0059     protected $_indentation = '';
0060 
0061     /**
0062      * fromReflection() - Build a docblock generator object from a reflection object
0063      *
0064      * @param Zend_Reflection_Docblock $reflectionDocblock
0065      * @return Zend_CodeGenerator_Php_Docblock
0066      */
0067     public static function fromReflection(Zend_Reflection_Docblock $reflectionDocblock)
0068     {
0069         $docblock = new self();
0070 
0071         $docblock->setSourceContent($reflectionDocblock->getContents());
0072         $docblock->setSourceDirty(false);
0073 
0074         $docblock->setShortDescription($reflectionDocblock->getShortDescription());
0075         $docblock->setLongDescription($reflectionDocblock->getLongDescription());
0076 
0077         foreach ($reflectionDocblock->getTags() as $tag) {
0078             $docblock->setTag(Zend_CodeGenerator_Php_Docblock_Tag::fromReflection($tag));
0079         }
0080 
0081         return $docblock;
0082     }
0083 
0084     /**
0085      * setShortDescription()
0086      *
0087      * @param string $shortDescription
0088      * @return Zend_CodeGenerator_Php_Docblock
0089      */
0090     public function setShortDescription($shortDescription)
0091     {
0092         $this->_shortDescription = $shortDescription;
0093         return $this;
0094     }
0095 
0096     /**
0097      * getShortDescription()
0098      *
0099      * @return string
0100      */
0101     public function getShortDescription()
0102     {
0103         return $this->_shortDescription;
0104     }
0105 
0106     /**
0107      * setLongDescription()
0108      *
0109      * @param string $longDescription
0110      * @return Zend_CodeGenerator_Php_Docblock
0111      */
0112     public function setLongDescription($longDescription)
0113     {
0114         $this->_longDescription = $longDescription;
0115         return $this;
0116     }
0117 
0118     /**
0119      * getLongDescription()
0120      *
0121      * @return string
0122      */
0123     public function getLongDescription()
0124     {
0125         return $this->_longDescription;
0126     }
0127 
0128     /**
0129      * setTags()
0130      *
0131      * @param array $tags
0132      * @return Zend_CodeGenerator_Php_Docblock
0133      */
0134     public function setTags(Array $tags)
0135     {
0136         foreach ($tags as $tag) {
0137             $this->setTag($tag);
0138         }
0139 
0140         return $this;
0141     }
0142 
0143     /**
0144      * setTag()
0145      *
0146      * @param array|Zend_CodeGenerator_Php_Docblock_Tag $tag
0147      * @return Zend_CodeGenerator_Php_Docblock
0148      */
0149     public function setTag($tag)
0150     {
0151         if (is_array($tag)) {
0152             $tag = new Zend_CodeGenerator_Php_Docblock_Tag($tag);
0153         } elseif (!$tag instanceof Zend_CodeGenerator_Php_Docblock_Tag) {
0154             // require_once 'Zend/CodeGenerator/Php/Exception.php';
0155             throw new Zend_CodeGenerator_Php_Exception(
0156                 'setTag() expects either an array of method options or an '
0157                 . 'instance of Zend_CodeGenerator_Php_Docblock_Tag'
0158                 );
0159         }
0160 
0161         $this->_tags[] = $tag;
0162         return $this;
0163     }
0164 
0165     /**
0166      * getTags
0167      *
0168      * @return array Array of Zend_CodeGenerator_Php_Docblock_Tag
0169      */
0170     public function getTags()
0171     {
0172         return $this->_tags;
0173     }
0174 
0175     /**
0176      * generate()
0177      *
0178      * @return string
0179      */
0180     public function generate()
0181     {
0182         if (!$this->isSourceDirty()) {
0183             return $this->_docCommentize($this->getSourceContent());
0184         }
0185 
0186         $output  = '';
0187         if (null !== ($sd = $this->getShortDescription())) {
0188             $output .= $sd . self::LINE_FEED . self::LINE_FEED;
0189         }
0190         if (null !== ($ld = $this->getLongDescription())) {
0191             $output .= $ld . self::LINE_FEED . self::LINE_FEED;
0192         }
0193 
0194         foreach ($this->getTags() as $tag) {
0195             $output .= $tag->generate() . self::LINE_FEED;
0196         }
0197 
0198         return $this->_docCommentize(trim($output));
0199     }
0200 
0201     /**
0202      * _docCommentize()
0203      *
0204      * @param string $content
0205      * @return string
0206      */
0207     protected function _docCommentize($content)
0208     {
0209         $indent  = $this->getIndentation();
0210         $output  = $indent . '/**' . self::LINE_FEED;
0211         $content = wordwrap($content, 80, self::LINE_FEED);
0212         $lines   = explode(self::LINE_FEED, $content);
0213 
0214         foreach ($lines as $line) {
0215             $output .= $indent . ' *';
0216             if ($line) {
0217                 $output .= " $line";
0218             }
0219             $output .= self::LINE_FEED;
0220         }
0221 
0222         $output = rtrim($output, ' *' . self::LINE_FEED) . self::LINE_FEED;
0223 
0224         $output .= $indent . ' */' . self::LINE_FEED;
0225         return $output;
0226     }
0227 }