|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace BultonFr\Annotation; |
|
4
|
|
|
|
|
5
|
|
|
use Exception; |
|
6
|
|
|
use ReflectionClass; |
|
7
|
|
|
use BultonFr\Annotation\Parsers\AbstractParser; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Manage all differents parser used to class a full class. |
|
11
|
|
|
* |
|
12
|
|
|
* @package BultonFr\Annotation |
|
13
|
|
|
*/ |
|
14
|
|
|
class ParserManager |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @const EXCEP_SAVE_COMMENTS Exception code if the opcode cache system not |
|
18
|
|
|
* keep comments (so annotation not exists in opcache) |
|
19
|
|
|
* |
|
20
|
|
|
* @see README.md for code format |
|
21
|
|
|
*/ |
|
22
|
|
|
const EXCEP_SAVE_COMMENTS = 101001; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @const EXCEP_NS_ALREADY_EXIST Exception code if a new imported ns |
|
26
|
|
|
* already exist. |
|
27
|
|
|
* |
|
28
|
|
|
* @see README.md for code format |
|
29
|
|
|
*/ |
|
30
|
|
|
const EXCEP_NS_ALREADY_EXIST = 101002; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Reader class instance which have instanciate this class |
|
34
|
|
|
* |
|
35
|
|
|
* @var \BultonFr\Annotation\Reader |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $reader; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Reflection class instance for the class to parse |
|
41
|
|
|
* |
|
42
|
|
|
* @var \ReflectionClass |
|
43
|
|
|
*/ |
|
44
|
|
|
protected $reflection; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* List of things parsed into the class |
|
48
|
|
|
* |
|
49
|
|
|
* @var AbstractParser[<string>] |
|
|
|
|
|
|
50
|
|
|
*/ |
|
51
|
|
|
protected $parserList = []; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* List of namespaces imported with AddNS annotation |
|
55
|
|
|
* |
|
56
|
|
|
* @var string[<string>] |
|
|
|
|
|
|
57
|
|
|
*/ |
|
58
|
|
|
protected $importedNS = []; |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Construct |
|
62
|
|
|
* |
|
63
|
|
|
* @param Reader $reader The main reader system |
|
64
|
|
|
*/ |
|
65
|
|
|
public function __construct(Reader $reader) |
|
66
|
|
|
{ |
|
67
|
1 |
|
$this->reader = $reader; |
|
68
|
|
|
|
|
69
|
1 |
|
$this->checkLib(); |
|
70
|
1 |
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Get reader class instance which have instanciate this class |
|
74
|
|
|
* |
|
75
|
|
|
* @return \BultonFr\Annotation\Reader |
|
76
|
|
|
*/ |
|
77
|
|
|
public function getReader(): Reader |
|
78
|
|
|
{ |
|
79
|
1 |
|
return $this->reader; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Get reflection class instance for the class to parse |
|
84
|
|
|
* |
|
85
|
|
|
* @return \ReflectionClass |
|
86
|
|
|
*/ |
|
87
|
|
|
public function getReflection(): ReflectionClass |
|
88
|
|
|
{ |
|
89
|
1 |
|
return $this->reflection; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Get list of things parsed into the class |
|
94
|
|
|
* |
|
95
|
|
|
* @return AbstractParser[<string>] |
|
|
|
|
|
|
96
|
|
|
*/ |
|
97
|
|
|
public function getParserList(): array |
|
98
|
|
|
{ |
|
99
|
1 |
|
return $this->parserList; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Get the value of importedNS |
|
104
|
|
|
* |
|
105
|
|
|
* @return string[<string>] |
|
|
|
|
|
|
106
|
|
|
*/ |
|
107
|
|
|
public function getImportedNS(): array |
|
108
|
|
|
{ |
|
109
|
1 |
|
return $this->importedNS; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Check if Opcache is configured to save comments. If not, we cannot use |
|
114
|
|
|
* the annotation system. |
|
115
|
|
|
* |
|
116
|
|
|
* @return void |
|
117
|
|
|
* |
|
118
|
|
|
* @throws Exception If opcache is not configured to save comments |
|
119
|
|
|
*/ |
|
120
|
|
|
protected function checkLib() |
|
121
|
|
|
{ |
|
122
|
|
|
if ( |
|
123
|
1 |
|
extension_loaded('Zend OPcache') && |
|
124
|
1 |
|
(int) ini_get('opcache.save_comments') === 0 |
|
125
|
|
|
) { |
|
126
|
|
|
throw new Exception( |
|
127
|
|
|
'Zend OPcache should have save_comments enabled', |
|
128
|
|
|
self::EXCEP_SAVE_COMMENTS |
|
129
|
|
|
); |
|
130
|
|
|
} |
|
131
|
1 |
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* Obtain the reflection object for the asked class |
|
135
|
|
|
* And call methods to create the parser list and execute all parser. |
|
136
|
|
|
* |
|
137
|
|
|
* @return void |
|
138
|
|
|
*/ |
|
139
|
|
|
public function run() |
|
140
|
|
|
{ |
|
141
|
1 |
|
$this->createReflectionObject(); |
|
142
|
1 |
|
$this->createParserList(); |
|
143
|
1 |
|
$this->execAllParser(); |
|
144
|
1 |
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* Create the ReflectionClass object |
|
148
|
|
|
* |
|
149
|
|
|
* @return void |
|
150
|
|
|
*/ |
|
151
|
|
|
protected function createReflectionObject() |
|
152
|
|
|
{ |
|
153
|
1 |
|
$this->reflection = new ReflectionClass($this->reader->getClassName()); |
|
154
|
1 |
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* Generate the parser list which contain the class which parse a part |
|
158
|
|
|
* of the class. |
|
159
|
|
|
* |
|
160
|
|
|
* @return void |
|
161
|
|
|
*/ |
|
162
|
|
|
protected function createParserList() |
|
163
|
|
|
{ |
|
164
|
1 |
|
$this->parserList = [ |
|
165
|
1 |
|
'class' => new Parsers\ClassParser($this, $this->reflection), |
|
166
|
1 |
|
'methods' => new Parsers\MethodsParser($this, $this->reflection), |
|
167
|
1 |
|
'properties' => new Parsers\PropertiesParser($this, $this->reflection) |
|
168
|
|
|
]; |
|
169
|
1 |
|
} |
|
170
|
|
|
|
|
171
|
|
|
/** |
|
172
|
|
|
* Loop on all parser and execute it |
|
173
|
|
|
* |
|
174
|
|
|
* @return void |
|
175
|
|
|
*/ |
|
176
|
|
|
protected function execAllParser() |
|
177
|
|
|
{ |
|
178
|
1 |
|
foreach ($this->parserList as $parser) { |
|
179
|
1 |
|
$parser->run(); |
|
180
|
|
|
} |
|
181
|
1 |
|
} |
|
182
|
|
|
|
|
183
|
|
|
/** |
|
184
|
|
|
* Add a new imported namespace (obtain by AddNS or manually) |
|
185
|
|
|
* |
|
186
|
|
|
* @param string $name The class path (with ns) of the class which will be |
|
187
|
|
|
* instancied when the annotation defined by $alias will be found. |
|
188
|
|
|
* @param string $alias The annotation key |
|
189
|
|
|
* |
|
190
|
|
|
* @return void |
|
191
|
|
|
*/ |
|
192
|
|
|
public function addImportedNS(string $name, string $alias) |
|
193
|
|
|
{ |
|
194
|
|
|
if ( |
|
195
|
1 |
|
array_key_exists($alias, $this->importedNS) && |
|
196
|
1 |
|
$this->importedNS[$alias] !== $name |
|
197
|
|
|
) { |
|
198
|
1 |
|
throw new Exception( |
|
199
|
1 |
|
'A ns '.$alias.' is already imported with a different value.', |
|
200
|
1 |
|
static::EXCEP_NS_ALREADY_EXIST |
|
201
|
|
|
); |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
1 |
|
$this->importedNS[$alias] = $name; |
|
205
|
1 |
|
} |
|
206
|
|
|
} |
|
207
|
|
|
|