1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Wingu\OctopusCore\Reflection\Annotation; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Annotations parser class. |
7
|
|
|
*/ |
8
|
|
|
class Parser |
9
|
|
|
{ |
10
|
|
|
|
11
|
|
|
const SKIP = -1; |
12
|
|
|
|
13
|
|
|
const SCAN = 1; |
14
|
|
|
|
15
|
|
|
const NAME = 2; |
16
|
|
|
|
17
|
|
|
const COPY_LINE = 3; |
18
|
|
|
|
19
|
|
|
const COPY_ARRAY = 4; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* The original comment before parsing it. |
23
|
|
|
* |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
protected $originalComment; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* An array of found annotation definitions in the comment. |
30
|
|
|
* |
31
|
|
|
* @var \Wingu\OctopusCore\Reflection\Annotation\AnnotationDefinition[] |
32
|
|
|
*/ |
33
|
|
|
protected $foundAnnotationDefinitions = array(); |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Constructor. |
37
|
|
|
* |
38
|
|
|
* @param string $commentString The comment string to parse. |
39
|
|
|
* @throws \Wingu\OctopusCore\Reflection\Annotation\Exceptions\RuntimeException If the comment can not be parsed. |
40
|
|
|
*/ |
41
|
168 |
|
public function __construct($commentString) |
42
|
|
|
{ |
43
|
168 |
|
$this->originalComment = $commentString; |
44
|
|
|
|
45
|
168 |
|
$commentString = trim(preg_replace('/^[\/\*\# \t]+/m', '', $commentString)); |
46
|
168 |
|
$commentString = str_replace("\r\n", "\n", $commentString) . "\n"; |
47
|
168 |
|
$commentStringLen = strlen($commentString); |
48
|
|
|
|
49
|
168 |
|
$state = self::SCAN; |
50
|
168 |
|
$nesting = 0; |
51
|
168 |
|
$matches = array(); |
52
|
|
|
|
53
|
168 |
|
$name = ''; |
54
|
168 |
|
$value = ''; |
55
|
|
|
|
56
|
168 |
|
for ($i = 0; $i < $commentStringLen; $i++) { |
57
|
168 |
|
$character = $commentString[$i]; |
58
|
|
|
|
59
|
|
|
switch ($state) { |
60
|
168 |
|
case self::SCAN: |
61
|
168 |
|
if ($character === '@') { |
62
|
78 |
|
$name = ''; |
63
|
78 |
|
$value = ''; |
64
|
78 |
|
$state = self::NAME; |
65
|
26 |
|
} else { |
66
|
153 |
|
if ($character !== "\n" && $character !== ' ' && $character !== "\t") { |
67
|
90 |
|
$state = self::SKIP; |
68
|
30 |
|
} |
69
|
|
|
} |
70
|
168 |
|
break; |
71
|
|
|
|
72
|
105 |
|
case self::SKIP: |
73
|
90 |
|
if ($character === "\n") { |
74
|
90 |
|
$state = self::SCAN; |
75
|
30 |
|
} |
76
|
90 |
|
break; |
77
|
|
|
|
78
|
78 |
|
case self::NAME: |
79
|
78 |
|
$m = preg_match('/[a-zA-Z0-9\-\\\\]/', $character); |
80
|
78 |
|
if ($m !== 0 && $m !== false) { |
81
|
75 |
|
$name .= $character; |
82
|
25 |
|
} else { |
83
|
78 |
|
if ($character === ' ') { |
84
|
57 |
|
$state = self::COPY_LINE; |
85
|
19 |
|
} else { |
86
|
21 |
|
if ($character === '(') { |
87
|
12 |
|
$nesting++; |
88
|
12 |
|
$value = $character; |
89
|
12 |
|
$state = self::COPY_ARRAY; |
90
|
4 |
|
} else { |
91
|
9 |
|
if ($character === "\n") { |
92
|
6 |
|
$matches[$name][] = new AnnotationDefinition($name); |
93
|
6 |
|
$state = self::SCAN; |
94
|
2 |
|
} else { |
95
|
3 |
|
$state = self::SKIP; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
} |
100
|
78 |
|
break; |
101
|
|
|
|
102
|
69 |
|
case self::COPY_LINE: |
103
|
57 |
|
if ($character === "\n") { |
104
|
57 |
|
$matches[$name][] = new AnnotationDefinition($name, $value); |
105
|
57 |
|
$state = self::SCAN; |
106
|
19 |
|
} else { |
107
|
57 |
|
$value .= $character; |
108
|
|
|
} |
109
|
57 |
|
break; |
110
|
|
|
|
111
|
12 |
|
case self::COPY_ARRAY: |
112
|
12 |
|
if ($character === '(') { |
113
|
3 |
|
$nesting++; |
114
|
1 |
|
} |
115
|
|
|
|
116
|
12 |
|
if ($character === ')') { |
117
|
9 |
|
$nesting--; |
118
|
3 |
|
} |
119
|
|
|
|
120
|
12 |
|
$value .= $character; |
121
|
|
|
|
122
|
12 |
|
if ($nesting === 0) { |
123
|
9 |
|
$matches[$name][] = new AnnotationDefinition($name, $value); |
124
|
9 |
|
$state = self::SCAN; |
125
|
3 |
|
} |
126
|
12 |
|
break; |
127
|
|
|
} |
128
|
56 |
|
} |
129
|
|
|
|
130
|
168 |
|
if ($state !== self::SCAN) { |
131
|
3 |
|
throw new Exceptions\RuntimeException('The comment is not valid and can not be parsed.'); |
132
|
|
|
} |
133
|
|
|
|
134
|
165 |
|
$this->foundAnnotationDefinitions = $matches; |
135
|
165 |
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Get the definition of the annotations found int he comment. |
139
|
|
|
* |
140
|
|
|
* @return \Wingu\OctopusCore\Reflection\Annotation\AnnotationDefinition[] |
141
|
|
|
*/ |
142
|
165 |
|
public function getFoundAnnotationDefinitions() |
143
|
|
|
{ |
144
|
165 |
|
return $this->foundAnnotationDefinitions; |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|