3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
10 namespace Zend\Code\Generator
;
12 use Zend\Code\Generator\DocBlock\Tag
as DocBlockTag
;
13 use Zend\Code\Reflection\DocBlockReflection
;
15 class DocBlockGenerator
extends AbstractGenerator
20 protected $shortDescription = null;
25 protected $longDescription = null;
30 protected $tags = array();
35 protected $indentation = '';
40 protected $wordwrap = true;
43 * Build a DocBlock generator object from a reflection object
45 * @param DocBlockReflection $reflectionDocBlock
46 * @return DocBlockGenerator
48 public static function fromReflection(DocBlockReflection
$reflectionDocBlock)
50 $docBlock = new static();
52 $docBlock->setSourceContent($reflectionDocBlock->getContents());
53 $docBlock->setSourceDirty(false);
55 $docBlock->setShortDescription($reflectionDocBlock->getShortDescription());
56 $docBlock->setLongDescription($reflectionDocBlock->getLongDescription());
58 foreach ($reflectionDocBlock->getTags() as $tag) {
59 $docBlock->setTag(DocBlockTag
::fromReflection($tag));
68 * @configkey shortdescription string The short description for this doc block
69 * @configkey longdescription string The long description for this doc block
70 * @configkey tags array
72 * @throws Exception\InvalidArgumentException
74 * @return DocBlockGenerator
76 public static function fromArray(array $array)
78 $docBlock = new static();
80 foreach ($array as $name => $value) {
82 switch (strtolower(str_replace(array('.', '-', '_'), '', $name))) {
83 case 'shortdescription':
84 $docBlock->setShortDescription($value);
85 case 'longdescription':
86 $docBlock->setLongDescription($value);
89 $docBlock->setTags($value);
98 * @param string $shortDescription
99 * @param string $longDescription
102 public function __construct($shortDescription = null, $longDescription = null, array $tags = array())
104 if ($shortDescription) {
105 $this->setShortDescription($shortDescription);
107 if ($longDescription) {
108 $this->setLongDescription($longDescription);
110 if (is_array($tags) && $tags) {
111 $this->setTags($tags);
116 * @param string $shortDescription
117 * @return DocBlockGenerator
119 public function setShortDescription($shortDescription)
121 $this->shortDescription
= $shortDescription;
128 public function getShortDescription()
130 return $this->shortDescription
;
134 * @param string $longDescription
135 * @return DocBlockGenerator
137 public function setLongDescription($longDescription)
139 $this->longDescription
= $longDescription;
146 public function getLongDescription()
148 return $this->longDescription
;
153 * @return DocBlockGenerator
155 public function setTags(array $tags)
157 foreach ($tags as $tag) {
165 * @param array|DocBlockTag $tag
166 * @throws Exception\InvalidArgumentException
167 * @return DocBlockGenerator
169 public function setTag($tag)
171 if (is_array($tag)) {
172 $tag = new DocBlockTag($tag);
173 } elseif (!$tag instanceof DocBlockTag
) {
174 throw new Exception\
InvalidArgumentException(sprintf(
175 '%s expects either an array of method options or an instance of %s\DocBlock\Tag',
181 $this->tags
[] = $tag;
186 * @return DocBlockTag[]
188 public function getTags()
197 * @return \Zend\Code\Generator\DocBlockGenerator
199 public function setWordWrap($value)
201 $this->wordwrap
= (bool) $value;
210 public function getWordWrap()
212 return $this->wordwrap
;
218 public function generate()
220 if (!$this->isSourceDirty()) {
221 return $this->docCommentize(trim($this->getSourceContent()));
225 if (null !== ($sd = $this->getShortDescription())) {
226 $output .= $sd . self
::LINE_FEED
. self
::LINE_FEED
;
228 if (null !== ($ld = $this->getLongDescription())) {
229 $output .= $ld . self
::LINE_FEED
. self
::LINE_FEED
;
232 foreach ($this->getTags() as $tag) {
233 $output .= $tag->generate() . self
::LINE_FEED
;
236 return $this->docCommentize(trim($output));
240 * @param string $content
243 protected function docCommentize($content)
245 $indent = $this->getIndentation();
246 $output = $indent . '/**' . self
::LINE_FEED
;
247 $content = $this->getWordWrap() == true ?
wordwrap($content, 80, self
::LINE_FEED
) : $content;
248 $lines = explode(self
::LINE_FEED
, $content);
249 foreach ($lines as $line) {
250 $output .= $indent . ' *';
254 $output .= self
::LINE_FEED
;
256 $output .= $indent . ' */' . self
::LINE_FEED
;