|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PhpBoot\Annotation; |
|
4
|
|
|
use Doctrine\Common\Cache\ApcCache; |
|
5
|
|
|
use Doctrine\Common\Cache\Cache; |
|
6
|
|
|
use PhpBoot\Cache\CheckableCache; |
|
7
|
|
|
use PhpBoot\Cache\ClassModifiedChecker; |
|
8
|
|
|
use phpDocumentor\Reflection\DocBlock\DescriptionFactory; |
|
9
|
|
|
use phpDocumentor\Reflection\DocBlock\StandardTagFactory; |
|
10
|
|
|
use phpDocumentor\Reflection\DocBlock\Tag; |
|
11
|
|
|
use phpDocumentor\Reflection\DocBlock\Tags\Formatter; |
|
12
|
|
|
use phpDocumentor\Reflection\DocBlockFactory; |
|
13
|
|
|
use phpDocumentor\Reflection\FqsenResolver; |
|
14
|
|
|
use phpDocumentor\Reflection\TypeResolver; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* AnnotationEnabledTest |
|
18
|
|
|
*/ |
|
19
|
|
|
class AnnotationEnabledTest |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* testMethod |
|
23
|
|
|
*/ |
|
24
|
|
|
public function testMethod() |
|
25
|
|
|
{ |
|
26
|
|
|
|
|
27
|
|
|
} |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
class AnnotationTagsOutput implements Formatter |
|
|
|
|
|
|
31
|
|
|
{ |
|
32
|
|
|
/** |
|
33
|
|
|
* Formats a tag into a string representation according to a specific format, such as Markdown. |
|
34
|
|
|
* |
|
35
|
|
|
* @param Tag $tag |
|
36
|
|
|
* |
|
37
|
|
|
* @return string |
|
38
|
|
|
*/ |
|
39
|
4 |
|
public function format(Tag $tag) |
|
40
|
|
|
{ |
|
41
|
4 |
|
$this->tags[] = $tag; |
|
42
|
4 |
|
return strval($tag); |
|
43
|
|
|
} |
|
44
|
|
|
public $tags = []; |
|
45
|
|
|
} |
|
46
|
|
|
class AnnotationReader implements \ArrayAccess |
|
|
|
|
|
|
47
|
|
|
{ |
|
48
|
17 |
|
static public function createDocBlockFactory(){ |
|
|
|
|
|
|
49
|
17 |
|
$fqsenResolver = new FqsenResolver(); |
|
50
|
17 |
|
$tagFactory = new StandardTagFactory($fqsenResolver,[]); |
|
51
|
17 |
|
$descriptionFactory = new DescriptionFactory($tagFactory); |
|
52
|
17 |
|
$tagFactory->addService($descriptionFactory); |
|
53
|
17 |
|
$tagFactory->addService(new TypeResolver($fqsenResolver)); |
|
54
|
17 |
|
$docBlockFactory = new DocBlockFactory($descriptionFactory, $tagFactory); |
|
55
|
17 |
|
return $docBlockFactory; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
17 |
|
static public function assertAnnotationEnabled() |
|
|
|
|
|
|
59
|
|
|
{ |
|
60
|
17 |
|
$rfl = new \ReflectionClass(AnnotationEnabledTest::class); |
|
61
|
17 |
|
!empty($rfl->getDocComment()) or \PhpBoot\abort('Annotation dose not work! If opcache is enable, please set opcache.save_comments=1 and opcache.load_comments=1'); |
|
|
|
|
|
|
62
|
17 |
|
} |
|
63
|
|
|
/** |
|
64
|
|
|
* load from class with local cache |
|
65
|
|
|
* TODO 增加 filter 能力 |
|
66
|
|
|
* @param string $className |
|
67
|
|
|
* @param Cache $localCache |
|
68
|
|
|
* @return object |
|
69
|
|
|
*/ |
|
70
|
17 |
|
static public function read($className, Cache $localCache = null) |
|
|
|
|
|
|
71
|
|
|
{ |
|
72
|
17 |
|
self::assertAnnotationEnabled(); |
|
73
|
17 |
|
$rfl = new \ReflectionClass($className) or \PhpBoot\abort("load class $className failed"); |
|
|
|
|
|
|
74
|
17 |
|
$fileName = $rfl->getFileName(); |
|
75
|
17 |
|
$key = str_replace('\\','.',self::class).md5($fileName.$className); |
|
76
|
17 |
|
$oldData = null; |
|
77
|
17 |
|
$cache = new CheckableCache($localCache?:new ApcCache()); |
|
|
|
|
|
|
78
|
17 |
|
$res = $cache->get('ann:'.$key, null, $oldData, false); |
|
79
|
17 |
|
if($res === null){ |
|
80
|
|
|
try{ |
|
81
|
17 |
|
$meta = self::readWithoutCache($className); |
|
82
|
17 |
|
$cache->set($key, $meta, 0, $fileName?new ClassModifiedChecker($className):null); |
|
83
|
17 |
|
return $meta; |
|
84
|
1 |
|
}catch (\Exception $e){ |
|
85
|
|
|
$cache->set($key, $e->getMessage(), 0, $fileName?new ClassModifiedChecker($className):null); |
|
86
|
|
|
throw $e; |
|
87
|
1 |
|
} |
|
88
|
|
|
}elseif(is_string($res)){ |
|
89
|
|
|
\PhpBoot\abort($res); |
|
90
|
|
|
}else{ |
|
91
|
|
|
return $res; |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
/** |
|
95
|
|
|
* @param $className |
|
96
|
|
|
* @return self |
|
97
|
|
|
*/ |
|
98
|
17 |
|
static function readWithoutCache($className) |
|
99
|
|
|
{ |
|
100
|
17 |
|
$reader = new self(); |
|
101
|
|
|
|
|
102
|
17 |
|
$rfl = new \ReflectionClass($className); |
|
103
|
17 |
|
$reader->class = self::readAnnotationBlock($rfl->getDocComment()); |
|
104
|
17 |
|
$reader->class->name = $className; |
|
105
|
|
|
|
|
106
|
|
|
//method annotations |
|
107
|
17 |
|
foreach ($rfl->getMethods() as $i){ |
|
108
|
13 |
|
$block = self::readAnnotationBlock($i->getDocComment()); |
|
109
|
13 |
|
$block->name = $i->getName(); |
|
|
|
|
|
|
110
|
13 |
|
$reader->methods[$i->getName()]=$block; |
|
|
|
|
|
|
111
|
17 |
|
} |
|
112
|
|
|
//property annotations |
|
113
|
17 |
View Code Duplication |
foreach ($rfl->getProperties() as $i){ |
|
|
|
|
|
|
114
|
16 |
|
if ($i->isStatic()) { |
|
115
|
12 |
|
continue; |
|
116
|
|
|
} |
|
117
|
16 |
|
$block = self::readAnnotationBlock($i->getDocComment()); |
|
118
|
16 |
|
$block->name = $i->getName(); |
|
119
|
16 |
|
$reader->properties[$i->getName()]=$block; |
|
120
|
17 |
|
} |
|
121
|
17 |
|
while ($rfl = $rfl->getParentClass()) { |
|
122
|
|
View Code Duplication |
foreach ($rfl->getProperties(\ReflectionProperty::IS_PRIVATE) as $i) { |
|
|
|
|
|
|
123
|
|
|
if ($i->isStatic()) { |
|
124
|
|
|
continue; |
|
125
|
|
|
} |
|
126
|
|
|
$block = self::readAnnotationBlock($i->getDocComment()); |
|
127
|
|
|
$block->name = $i->getName(); |
|
128
|
|
|
$reader->properties[$i->getName()]=$block; |
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
17 |
|
return $reader; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
17 |
|
static private function readAnnotationBlock($doc) |
|
|
|
|
|
|
135
|
|
|
{ |
|
136
|
17 |
|
$annBlock = new AnnotationBlock(); |
|
137
|
17 |
|
if(empty($doc)){ |
|
138
|
16 |
|
return $annBlock; |
|
139
|
|
|
} |
|
140
|
17 |
|
$docFactory = AnnotationReader::createDocBlockFactory(); |
|
141
|
17 |
|
$docBlock = $docFactory->create($doc); |
|
142
|
17 |
|
$annBlock->summary = $docBlock->getSummary(); |
|
143
|
17 |
|
$annBlock->description = strval($docBlock->getDescription()); |
|
144
|
17 |
|
$annBlock->children = []; |
|
145
|
17 |
|
$tags = $docBlock->getTags(); |
|
146
|
17 |
|
foreach ($tags as $tag) { |
|
147
|
17 |
|
$annTag = new AnnotationTag(); |
|
148
|
17 |
|
$desc = $tag->getDescription(); |
|
149
|
17 |
|
$annTag->parent = $annBlock; |
|
150
|
17 |
|
$annTag->description = strval($desc); |
|
151
|
17 |
|
$annTag->name = $tag->getName(); |
|
152
|
17 |
|
$annTag->children=[]; |
|
153
|
17 |
|
if($desc){ |
|
154
|
17 |
|
$output = new AnnotationTagsOutput(); |
|
155
|
17 |
|
$desc->render($output); |
|
156
|
17 |
|
foreach ($output->tags as $child) { |
|
157
|
4 |
|
$childTag = new AnnotationTag(); |
|
158
|
4 |
|
$childTag->parent = $annTag; |
|
|
|
|
|
|
159
|
4 |
|
$childTag->name = $child->getName(); |
|
160
|
4 |
|
$childTag->description = strval($child->getDescription()); |
|
161
|
4 |
|
$annTag->children[] = $childTag; |
|
162
|
17 |
|
} |
|
163
|
17 |
|
} |
|
164
|
17 |
|
$annBlock->children[] = $annTag; |
|
165
|
17 |
|
} |
|
166
|
17 |
|
return $annBlock; |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* @var AnnotationBlock |
|
171
|
|
|
*/ |
|
172
|
|
|
public $class; |
|
173
|
|
|
/** |
|
174
|
|
|
* @var AnnotationBlock[] |
|
175
|
|
|
*/ |
|
176
|
|
|
|
|
177
|
|
|
public $methods=[]; |
|
178
|
|
|
/** |
|
179
|
|
|
* @var AnnotationBlock[] |
|
180
|
|
|
*/ |
|
181
|
|
|
public $properties=[]; |
|
182
|
|
|
|
|
183
|
17 |
|
public function offsetExists($offset) |
|
184
|
|
|
{ |
|
185
|
17 |
|
return isset($this->$offset); |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
17 |
|
public function offsetGet($offset) |
|
189
|
|
|
{ |
|
190
|
17 |
|
return $this->$offset; |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
public function offsetSet($offset, $value) |
|
194
|
|
|
{ |
|
195
|
|
|
$this->$offset = $value; |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
public function offsetUnset($offset) |
|
199
|
|
|
{ |
|
200
|
|
|
unset($this->$offset); |
|
201
|
|
|
} |
|
202
|
|
|
} |
Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.