Repository navigation
Expand file tree
/
Copy pathReflectionManager.php
More file actions
134 lines (118 loc) · 4.58 KB
/
Copy pathReflectionManager.php
File metadata and controls
134 lines (118 loc) · 4.58 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
131
132
133
134
<?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;
use Hyperf\Di\Aop\Ast;
use InvalidArgumentException;
use ReflectionClass;
use ReflectionMethod;
use ReflectionProperty;
use Symfony\Component\Finder\Finder;
use Throwable;
use function Hyperf\Support\value;
class ReflectionManager extends MetadataCollector
{
protected static array $container = [];
public static function reflectClass(string $className): ReflectionClass
{
if (! isset(static::$container['class'][$className])) {
if (! class_exists($className) && ! interface_exists($className) && ! trait_exists($className)) {
throw new InvalidArgumentException("Class {$className} not exist");
}
static::$container['class'][$className] = new ReflectionClass($className);
}
return static::$container['class'][$className];
}
public static function reflectMethod(string $className, string $method): ReflectionMethod
{
$key = $className . '::' . $method;
if (! isset(static::$container['method'][$key])) {
// TODO check interface_exist
if (! class_exists($className) && ! trait_exists($className)) {
throw new InvalidArgumentException("Class {$className} not exist");
}
static::$container['method'][$key] = static::reflectClass($className)->getMethod($method);
}
return static::$container['method'][$key];
}
public static function reflectProperty(string $className, string $property): ReflectionProperty
{
$key = $className . '::' . $property;
if (! isset(static::$container['property'][$key])) {
if (! class_exists($className)) {
throw new InvalidArgumentException("Class {$className} not exist");
}
static::$container['property'][$key] = static::reflectClass($className)->getProperty($property);
}
return static::$container['property'][$key];
}
public static function reflectPropertyNames(string $className)
{
$key = $className;
if (! isset(static::$container['property_names'][$key])) {
if (! class_exists($className) && ! interface_exists($className) && ! trait_exists($className)) {
throw new InvalidArgumentException("Class {$className} not exist");
}
static::$container['property_names'][$key] = value(function () use ($className) {
$properties = static::reflectClass($className)->getProperties();
$result = [];
foreach ($properties as $property) {
$result[] = $property->getName();
}
return $result;
});
}
return static::$container['property_names'][$key];
}
public static function clear(?string $key = null): void
{
if ($key === null) {
static::$container = [];
}
}
public static function getPropertyDefaultValue(ReflectionProperty $property)
{
return method_exists($property, 'getDefaultValue')
? $property->getDefaultValue()
: $property->getDeclaringClass()->getDefaultProperties()[$property->getName()] ?? null;
}
public static function getAllClasses(array $paths): array
{
$finder = new Finder();
$finder->files()->in($paths)->name('*.php');
return static::getAllClassesByFinder($finder);
}
public static function getAllClassesByFinder(Finder $finder): array
{
$parser = new Ast();
$reflectionClasses = [];
foreach ($finder as $file) {
try {
$stmts = $parser->parse($file->getContents());
if (! $className = $parser->parseClassByStmts($stmts)) {
continue;
}
$reflectionClasses[$className] = static::reflectClass($className);
} catch (Throwable $e) {
echo sprintf(
"\033[31m%s\033[0m",
'[ERROR] DI Reflection Manager collecting class reflections failed. ' . PHP_EOL
. "File: {$file->getRealPath()}." . PHP_EOL
. 'Exception: ' . $e->getMessage()
) . PHP_EOL;
}
}
return $reflectionClasses;
}
public static function getContainer(): array
{
return self::$container;
}
}