/
Reader.zep
146 lines (121 loc) · 4.09 KB
/
Reader.zep
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/**
* This file is part of the Phalcon Framework.
*
* (c) Phalcon Team <team@phalcon.io>
*
* For the full copyright and license information, please view the LICENSE.txt
* file that was distributed with this source code.
*/
namespace Phalcon\Annotations;
use ReflectionClass;
/**
* Parses docblocks returning an array with the found annotations
*/
class Reader implements ReaderInterface
{
/**
* Reads annotations from the class docblocks, its methods and/or properties
*/
public function parse(string className) -> array
{
var reflection, comment, properties, methods, property, method,
classAnnotations, line, annotationsProperties, propertyAnnotations,
annotationsMethods, methodAnnotations;
array annotations;
let annotations = [];
/**
* A ReflectionClass is used to obtain the class docblock
*/
let reflection = new ReflectionClass(className);
let comment = reflection->getDocComment();
if typeof comment == "string" {
/**
* Read annotations from class
*/
let classAnnotations = phannot_parse_annotations(
comment,
reflection->getFileName(),
reflection->getStartLine()
);
/**
* Append the class annotations to the annotations var
*/
if typeof classAnnotations == "array" {
let annotations["class"] = classAnnotations;
}
}
/**
* Get the class properties
*/
let properties = reflection->getProperties();
if count(properties) {
/**
* Line declaration for properties isn't available
*/
let line = 1;
let annotationsProperties = [];
for property in properties {
/**
* Read comment from method
*/
let comment = property->getDocComment();
if typeof comment == "string" {
/**
* Read annotations from the docblock
*/
let propertyAnnotations = phannot_parse_annotations(
comment,
reflection->getFileName(),
line
);
if typeof propertyAnnotations == "array" {
let annotationsProperties[property->name] = propertyAnnotations;
}
}
}
if count(annotationsProperties) {
let annotations["properties"] = annotationsProperties;
}
}
/**
* Get the class methods
*/
let methods = reflection->getMethods();
if count(methods) {
let annotationsMethods = [];
for method in methods {
/**
* Read comment from method
*/
let comment = method->getDocComment();
if typeof comment == "string" {
/**
* Read annotations from class
*/
let methodAnnotations = phannot_parse_annotations(
comment,
method->getFileName(),
method->getStartLine()
);
if typeof methodAnnotations == "array" {
let annotationsMethods[method->name] = methodAnnotations;
}
}
}
if count(annotationsMethods) {
let annotations["methods"] = annotationsMethods;
}
}
return annotations;
}
/**
* Parses a raw doc block returning the annotations found
*/
public static function parseDocBlock(string docBlock, file = null, line = null) -> array
{
if typeof file != "string" {
let file = "eval code";
}
return phannot_parse_annotations(docBlock, file, line);
}
}