Repository navigation
Expand file tree
/
Copy pathAnnotationCollector.php
More file actions
130 lines (111 loc) · 3.93 KB
/
Copy pathAnnotationCollector.php
File metadata and controls
130 lines (111 loc) · 3.93 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://hyperf.wiki
* @contact [email protected]
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespace Hyperf\Di\Annotation;
use Hyperf\Di\MetadataCollector;
class AnnotationCollector extends MetadataCollector
{
protected static array $container = [];
public static function collectClass(string $class, string $annotation, $value): void
{
static::$container[$class]['_c'][$annotation] = $value;
}
public static function collectClassConstant(string $class, string $constant, string $annotation, $value): void
{
static::$container[$class]['_cc'][$constant][$annotation] = $value;
}
public static function collectProperty(string $class, string $property, string $annotation, $value): void
{
static::$container[$class]['_p'][$property][$annotation] = $value;
}
public static function collectMethod(string $class, string $method, string $annotation, $value): void
{
static::$container[$class]['_m'][$method][$annotation] = $value;
}
public static function clear(?string $key = null): void
{
if ($key) {
unset(static::$container[$key]);
} else {
static::$container = [];
}
}
public static function getClassConstantsByAnnotation(string $annotation): array
{
$result = [];
foreach (static::$container as $class => $metadata) {
foreach ($metadata['_cc'] ?? [] as $constant => $_metadata) {
if ($value = $_metadata[$annotation] ?? null) {
$result[] = ['class' => $class, 'constant' => $constant, 'annotation' => $value];
}
}
}
return $result;
}
public static function getClassesByAnnotation(string $annotation): array
{
$result = [];
foreach (static::$container as $class => $metadata) {
if (! isset($metadata['_c'][$annotation])) {
continue;
}
$result[$class] = $metadata['_c'][$annotation];
}
return $result;
}
public static function getMethodsByAnnotation(string $annotation): array
{
$result = [];
foreach (static::$container as $class => $metadata) {
foreach ($metadata['_m'] ?? [] as $method => $_metadata) {
if ($value = $_metadata[$annotation] ?? null) {
$result[] = ['class' => $class, 'method' => $method, 'annotation' => $value];
}
}
}
return $result;
}
public static function getPropertiesByAnnotation(string $annotation): array
{
$properties = [];
foreach (static::$container as $class => $metadata) {
foreach ($metadata['_p'] ?? [] as $property => $_metadata) {
if ($value = $_metadata[$annotation] ?? null) {
$properties[] = ['class' => $class, 'property' => $property, 'annotation' => $value];
}
}
}
return $properties;
}
public static function getClassAnnotation(string $class, string $annotation)
{
return static::get($class . '._c.' . $annotation);
}
public static function getClassAnnotations(string $class)
{
return static::get($class . '._c');
}
public static function getClassConstantAnnotation(string $class, string $constant)
{
return static::get($class . '._cc.' . $constant);
}
public static function getClassMethodAnnotation(string $class, string $method)
{
return static::get($class . '._m.' . $method);
}
public static function getClassPropertyAnnotation(string $class, string $property)
{
return static::get($class . '._p.' . $property);
}
public static function getContainer(): array
{
return static::$container;
}
}