-
Notifications
You must be signed in to change notification settings - Fork 20
/
AnnotationUtility.php
107 lines (89 loc) · 2.67 KB
/
AnnotationUtility.php
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
<?php
declare(strict_types=1);
namespace SwaggerBake\Lib\Utility;
use Doctrine\Common\Annotations\AnnotationReader;
use Exception;
use ReflectionClass;
/**
* Class AnnotationUtility
*
* @package SwaggerBake\Lib\Utility
*/
class AnnotationUtility
{
/**
* Gets class annotations from namespace argument
*
* @uses AnnotationReader
* @uses ReflectionClass
* @param string $namespace Fully qualified namespace of the class
* @return array
*/
public static function getClassAnnotationsFromFqns(string $namespace): array
{
try {
$reflectionClass = new ReflectionClass($namespace);
} catch (Exception $e) {
return [];
}
$reader = new AnnotationReader();
$annotations = $reader->getClassAnnotations($reflectionClass);
if (!is_array($annotations)) {
return [];
}
return $annotations;
}
/**
* Gets class annotations from instance
*
* @uses AnnotationReader
* @uses ReflectionClass
* @param object $instance PHP object
* @return array
*/
public static function getClassAnnotationsFromInstance(object $instance): array
{
try {
$reflectionClass = new ReflectionClass(get_class($instance));
} catch (Exception $e) {
return [];
}
$reader = new AnnotationReader();
$annotations = $reader->getClassAnnotations($reflectionClass);
if (!is_array($annotations)) {
return [];
}
return $annotations;
}
/**
* Returns an array of Lib/Annotation objects that can be applied to methods
*
* @uses AnnotationReader
* @uses ReflectionClass
* @param string $namespace Fully qualified namespace
* @param string $method Method name
* @return array
*/
public static function getMethodAnnotations(string $namespace, string $method): array
{
$return = [];
try {
$reflectionClass = new ReflectionClass($namespace);
$reflectedMethods = $reflectionClass->getMethods();
} catch (Exception $e) {
return $return;
}
$argMethodAnnotations = array_filter($reflectedMethods, function ($refMethod) use ($method) {
return $refMethod->name == $method;
});
$reader = new AnnotationReader();
foreach ($argMethodAnnotations as $methodAnnotation) {
$annotations = $reader->getMethodAnnotations($methodAnnotation);
if (empty($annotations)) {
continue;
}
$return = array_merge($return, $annotations);
}
return $return;
}
}