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.
16 * @package Zend_CodeGenerator
18 * @copyright Copyright (c) 2005-2009 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 16971 2009-07-22 18:05:45Z mikaelkael $
24 * @see Zend_CodeGenerator_Php_Abstract
26 require_once 'Zend/CodeGenerator/Php/Abstract.php';
29 * @see Zend_CodeGenerator_Php_Docblock_Tag
31 require_once 'Zend/CodeGenerator/Php/Docblock/Tag.php';
35 * @package Zend_CodeGenerator
36 * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
37 * @license http://framework.zend.com/license/new-bsd New BSD License
39 class Zend_CodeGenerator_Php_Docblock
extends Zend_CodeGenerator_Php_Abstract
44 protected $_shortDescription = null;
49 protected $_longDescription = null;
54 protected $_tags = array();
59 protected $_indentation = '';
62 * fromReflection() - Build a docblock generator object from a reflection object
64 * @param Zend_Reflection_Docblock $reflectionDocblock
65 * @return Zend_CodeGenerator_Php_Docblock
67 public static function fromReflection(Zend_Reflection_Docblock
$reflectionDocblock)
69 $docblock = new self();
71 $docblock->setSourceContent($reflectionDocblock->getContents());
72 $docblock->setSourceDirty(false);
74 $docblock->setShortDescription($reflectionDocblock->getShortDescription());
75 $docblock->setLongDescription($reflectionDocblock->getLongDescription());
77 foreach ($reflectionDocblock->getTags() as $tag) {
78 $docblock->setTag(Zend_CodeGenerator_Php_Docblock_Tag
::fromReflection($tag));
85 * setShortDescription()
87 * @param string $shortDescription
88 * @return Zend_CodeGenerator_Php_Docblock
90 public function setShortDescription($shortDescription)
92 $this->_shortDescription
= $shortDescription;
97 * getShortDescription()
101 public function getShortDescription()
103 return $this->_shortDescription
;
107 * setLongDescription()
109 * @param string $longDescription
110 * @return Zend_CodeGenerator_Php_Docblock
112 public function setLongDescription($longDescription)
114 $this->_longDescription
= $longDescription;
119 * getLongDescription()
123 public function getLongDescription()
125 return $this->_longDescription
;
132 * @return Zend_CodeGenerator_Php_Docblock
134 public function setTags(Array $tags)
136 foreach ($tags as $tag) {
146 * @param array|Zend_CodeGenerator_Php_Docblock_Tag $tag
147 * @return Zend_CodeGenerator_Php_Docblock
149 public function setTag($tag)
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'
161 $this->_tags
[] = $tag;
168 * @return array Array of Zend_CodeGenerator_Php_Docblock_Tag
170 public function getTags()
180 public function generate()
182 if (!$this->isSourceDirty()) {
183 return $this->_docCommentize($this->getSourceContent());
187 if (null !== ($sd = $this->getShortDescription())) {
188 $output .= $sd . self
::LINE_FEED
. self
::LINE_FEED
;
190 if (null !== ($ld = $this->getLongDescription())) {
191 $output .= $ld . self
::LINE_FEED
. self
::LINE_FEED
;
194 foreach ($this->getTags() as $tag) {
195 $output .= $tag->generate() . self
::LINE_FEED
;
198 return $this->_docCommentize(trim($output));
204 * @param string $content
207 protected function _docCommentize($content)
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
;
216 $output .= $indent . ' */' . self
::LINE_FEED
;