Added the zend framework 2 library, the path is specified in line no.26 in zend_modul...
[openemr.git] / interface / modules / zend_modules / library / Zend / Code / Scanner / DocBlockScanner.php
blob2e594cbfe295f511850de18269daba07a632497e
1 <?php
2 /**
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
8 */
10 namespace Zend\Code\Scanner;
12 use Zend\Code\Annotation\AnnotationManager;
13 use Zend\Code\NameInformation;
15 class DocBlockScanner implements ScannerInterface
17 /**
18 * @var bool
20 protected $isScanned = false;
22 /**
23 * @var string
25 protected $docComment = null;
27 /**
28 * @var NameInformation
30 protected $nameInformation = null;
32 /**
33 * @var AnnotationManager
35 protected $annotationManager = null;
37 /**
38 * @var string
40 protected $shortDescription = null;
42 /**
43 * @var string
45 protected $longDescription = '';
47 /**
48 * @var array
50 protected $tags = array();
52 /**
53 * @var array
55 protected $annotations = array();
57 /**
58 * @param string $docComment
59 * @param null|NameInformation $nameInformation
61 public function __construct($docComment, NameInformation $nameInformation = null)
63 $this->docComment = $docComment;
64 $this->nameInformation = $nameInformation;
67 /**
68 * @return string
70 public function getShortDescription()
72 $this->scan();
74 return $this->shortDescription;
77 /**
78 * @return string
80 public function getLongDescription()
82 $this->scan();
84 return $this->longDescription;
87 /**
88 * @return array
90 public function getTags()
92 $this->scan();
94 return $this->tags;
97 /**
98 * @return array
100 public function getAnnotations()
102 $this->scan();
104 return $this->annotations;
108 * @return void
110 protected function scan()
112 if ($this->isScanned) {
113 return;
116 $mode = 1;
118 $tokens = $this->tokenize();
119 $tagIndex = null;
120 reset($tokens);
122 SCANNER_TOP:
123 $token = current($tokens);
125 switch ($token[0]) {
126 case 'DOCBLOCK_NEWLINE':
127 if ($this->shortDescription != '' && $tagIndex === null) {
128 $mode = 2;
129 } else {
130 $this->longDescription .= $token[1];
132 goto SCANNER_CONTINUE;
134 case 'DOCBLOCK_WHITESPACE':
135 case 'DOCBLOCK_TEXT':
136 if ($tagIndex !== null) {
137 $this->tags[$tagIndex]['value'] .= ($this->tags[$tagIndex]['value'] == '') ? $token[1] : ' ' . $token[1];
138 goto SCANNER_CONTINUE;
139 } elseif ($mode <= 2) {
140 if ($mode == 1) {
141 $this->shortDescription .= $token[1];
142 } else {
143 $this->longDescription .= $token[1];
145 goto SCANNER_CONTINUE;
147 case 'DOCBLOCK_TAG':
148 array_push($this->tags, array('name' => $token[1],
149 'value' => ''));
150 end($this->tags);
151 $tagIndex = key($this->tags);
152 $mode = 3;
153 goto SCANNER_CONTINUE;
155 case 'DOCBLOCK_COMMENTEND':
156 goto SCANNER_END;
160 SCANNER_CONTINUE:
161 if (next($tokens) === false) {
162 goto SCANNER_END;
164 goto SCANNER_TOP;
166 SCANNER_END:
168 $this->shortDescription = trim($this->shortDescription);
169 $this->longDescription = trim($this->longDescription);
170 $this->isScanned = true;
174 * @return array
176 protected function tokenize()
178 static $CONTEXT_INSIDE_DOCBLOCK = 0x01;
179 static $CONTEXT_INSIDE_ASTERISK = 0x02;
181 $context = 0x00;
182 $stream = $this->docComment;
183 $streamIndex = null;
184 $tokens = array();
185 $tokenIndex = null;
186 $currentChar = null;
187 $currentWord = null;
188 $currentLine = null;
190 $MACRO_STREAM_ADVANCE_CHAR = function ($positionsForward = 1) use (&$stream, &$streamIndex, &$currentChar, &$currentWord, &$currentLine) {
191 $positionsForward = ($positionsForward > 0) ? $positionsForward : 1;
192 $streamIndex = ($streamIndex === null) ? 0 : $streamIndex + $positionsForward;
193 if (!isset($stream[$streamIndex])) {
194 $currentChar = false;
196 return false;
198 $currentChar = $stream[$streamIndex];
199 $matches = array();
200 $currentLine = (preg_match('#(.*?)\r?\n#', $stream, $matches, null,
201 $streamIndex) === 1) ? $matches[1] : substr($stream, $streamIndex);
202 if ($currentChar === ' ') {
203 $currentWord = (preg_match('#( +)#', $currentLine, $matches) === 1) ? $matches[1] : $currentLine;
204 } else {
205 $currentWord = (($matches = strpos($currentLine, ' ')) !== false) ? substr($currentLine, 0, $matches) : $currentLine;
208 return $currentChar;
210 $MACRO_STREAM_ADVANCE_WORD = function () use (&$currentWord, &$MACRO_STREAM_ADVANCE_CHAR) {
211 return $MACRO_STREAM_ADVANCE_CHAR(strlen($currentWord));
213 $MACRO_STREAM_ADVANCE_LINE = function () use (&$currentLine, &$MACRO_STREAM_ADVANCE_CHAR) {
214 return $MACRO_STREAM_ADVANCE_CHAR(strlen($currentLine));
216 $MACRO_TOKEN_ADVANCE = function () use (&$tokenIndex, &$tokens) {
217 $tokenIndex = ($tokenIndex === null) ? 0 : $tokenIndex + 1;
218 $tokens[$tokenIndex] = array('DOCBLOCK_UNKNOWN', '');
220 $MACRO_TOKEN_SET_TYPE = function ($type) use (&$tokenIndex, &$tokens) {
221 $tokens[$tokenIndex][0] = $type;
223 $MACRO_TOKEN_APPEND_CHAR = function () use (&$currentChar, &$tokens, &$tokenIndex) {
224 $tokens[$tokenIndex][1] .= $currentChar;
226 $MACRO_TOKEN_APPEND_WORD = function () use (&$currentWord, &$tokens, &$tokenIndex) {
227 $tokens[$tokenIndex][1] .= $currentWord;
229 $MACRO_TOKEN_APPEND_WORD_PARTIAL = function ($length) use (&$currentWord, &$tokens, &$tokenIndex) {
230 $tokens[$tokenIndex][1] .= substr($currentWord, 0, $length);
232 $MACRO_TOKEN_APPEND_LINE = function () use (&$currentLine, &$tokens, &$tokenIndex) {
233 $tokens[$tokenIndex][1] .= $currentLine;
236 $MACRO_STREAM_ADVANCE_CHAR();
237 $MACRO_TOKEN_ADVANCE();
239 TOKENIZER_TOP:
241 if ($context === 0x00 && $currentChar === '/' && $currentWord === '/**') {
242 $MACRO_TOKEN_SET_TYPE('DOCBLOCK_COMMENTSTART');
243 $MACRO_TOKEN_APPEND_WORD();
244 $MACRO_TOKEN_ADVANCE();
245 $context |= $CONTEXT_INSIDE_DOCBLOCK;
246 $context |= $CONTEXT_INSIDE_ASTERISK;
247 if ($MACRO_STREAM_ADVANCE_WORD() === false) {
248 goto TOKENIZER_END;
250 goto TOKENIZER_TOP;
253 if ($context & $CONTEXT_INSIDE_DOCBLOCK && $currentWord === '*/') {
254 $MACRO_TOKEN_SET_TYPE('DOCBLOCK_COMMENTEND');
255 $MACRO_TOKEN_APPEND_WORD();
256 $MACRO_TOKEN_ADVANCE();
257 $context &= ~$CONTEXT_INSIDE_DOCBLOCK;
258 if ($MACRO_STREAM_ADVANCE_WORD() === false) {
259 goto TOKENIZER_END;
261 goto TOKENIZER_TOP;
264 if ($currentChar === ' ' || $currentChar === "\t") {
265 $MACRO_TOKEN_SET_TYPE(($context & $CONTEXT_INSIDE_ASTERISK) ? 'DOCBLOCK_WHITESPACE' : 'DOCBLOCK_WHITESPACE_INDENT');
266 $MACRO_TOKEN_APPEND_WORD();
267 $MACRO_TOKEN_ADVANCE();
268 if ($MACRO_STREAM_ADVANCE_WORD() === false) {
269 goto TOKENIZER_END;
271 goto TOKENIZER_TOP;
274 if ($currentChar === '*') {
275 if (($context & $CONTEXT_INSIDE_DOCBLOCK) && ($context & $CONTEXT_INSIDE_ASTERISK)) {
276 $MACRO_TOKEN_SET_TYPE('DOCBLOCK_TEXT');
277 } else {
278 $MACRO_TOKEN_SET_TYPE('DOCBLOCK_ASTERISK');
279 $context |= $CONTEXT_INSIDE_ASTERISK;
281 $MACRO_TOKEN_APPEND_CHAR();
282 $MACRO_TOKEN_ADVANCE();
283 if ($MACRO_STREAM_ADVANCE_CHAR() === false) {
284 goto TOKENIZER_END;
286 goto TOKENIZER_TOP;
289 if ($currentChar === '@') {
290 $MACRO_TOKEN_SET_TYPE('DOCBLOCK_TAG');
291 $MACRO_TOKEN_APPEND_WORD();
292 $MACRO_TOKEN_ADVANCE();
293 if ($MACRO_STREAM_ADVANCE_WORD() === false) {
294 goto TOKENIZER_END;
296 goto TOKENIZER_TOP;
299 if ($currentChar === "\n") {
300 $MACRO_TOKEN_SET_TYPE('DOCBLOCK_NEWLINE');
301 $MACRO_TOKEN_APPEND_CHAR();
302 $MACRO_TOKEN_ADVANCE();
303 $context &= ~$CONTEXT_INSIDE_ASTERISK;
304 if ($MACRO_STREAM_ADVANCE_CHAR() === false) {
305 goto TOKENIZER_END;
307 goto TOKENIZER_TOP;
310 $MACRO_TOKEN_SET_TYPE('DOCBLOCK_TEXT');
311 $MACRO_TOKEN_APPEND_LINE();
312 $MACRO_TOKEN_ADVANCE();
313 if ($MACRO_STREAM_ADVANCE_LINE() === false) {
314 goto TOKENIZER_END;
316 goto TOKENIZER_TOP;
318 TOKENIZER_END:
320 array_pop($tokens);
322 return $tokens;