This repository was archived by the owner on Jun 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnnotationParser.php
More file actions
111 lines (88 loc) · 3.41 KB
/
AnnotationParser.php
File metadata and controls
111 lines (88 loc) · 3.41 KB
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
<?php
namespace modules\phpdocannotations;
use ReflectionClass;
class AnnotationParser {
private $reflection;
private $phpDoc;
private $annotations;
public function __construct($clazz, $useString = false){
$this->annotations = [];
if ($useString)
$this->phpDoc = $clazz;
else
$this->phpDoc = (new ReflectionClass($clazz))->getDocComment();
$this->parse();
}
public function parse(){
foreach (explode("\n", $this->phpDoc) as $line) {
$line = trim($line);
if ($line != "/**" && $line != "*/") {
$parsedLine = "";
$splitLine = $line;
if (isset($splitLine[0]) && $splitLine[0] == "*") {
$splitLine[0] = " ";
}
$parsedLine = trim($splitLine);
if (isset($parsedLine[0]) && $parsedLine[0] == "@") {
$annotation = "";
$annotationClosed = false;
$parametersString = "";
foreach (str_split($parsedLine) as $char) {
if ($char == "(" && !$annotationClosed)
$annotationClosed = true;
if ($char != "@" && $char != " " && !$annotationClosed){
$annotation .= $char;
}
if ($annotationClosed) {
$parametersString .= $char;
}
}
if (!$annotationClosed)
continue;
$parametersString[0] = " ";
$parametersString[strlen($parametersString)-1] = " ";
$className = trim($annotation);
if (!class_exists($className))
continue;
$clazz = new ReflectionClass($className);
$instance = $clazz->newInstance();
foreach (explode(",", $parametersString) as $params) {
if (strpos($params, "=") !== false) {
$parts = explode("=", $params);
$parts[1] = trim($parts[1]);
if (isset($parts[1][0]) && isset($parts[1][strlen($parts[1])-1]) && $parts[1][0] == "\"" && $parts[1][strlen($parts[1])-1] == "\"") {
$parts[1] = substr_replace($parts[1], '', 0, 1);
$parts[1] = substr_replace($parts[1], '', strlen($parts[1])-1, 1);
}
if (is_numeric($parts[1]))
$parts[1] = (int) $parts[1];
$instance->{trim($parts[0])} = $parts[1];
}
}
$this->annotations[$className] = $instance;
}
}
}
}
/**
* Get the value of annotations
*/
public function getAnnotations()
{
return $this->annotations;
}
/**
* Get the a annotation
*/
public function getAnnotation($clazz)
{
return $this->annotations[$clazz];
}
/**
* Check if it has the annotation
*/
public function hasAnnotation($clazz)
{
return isset($this->annotations[$clazz]);
}
}