-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReflection.php
More file actions
178 lines (158 loc) · 5.24 KB
/
Copy pathReflection.php
File metadata and controls
178 lines (158 loc) · 5.24 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
<?php
/**
* This file is part of ScaleUpStack/Reflection.
*
* For the full copyright and license information, please view the README.md and LICENSE.md files that were distributed
* with this source code.
*
* @copyright 2019 - present ScaleUpVentures GmbH, https://www.scaleupventures.com
* @link https://github.com/scaleupstack/reflection
*/
namespace ScaleUpStack\Reflection;
/**
* @method static \ReflectionClass classByObject(object $object)
* @method static \ReflectionProperty propertyOfObject(object $object, string $propertyName)
* @method static \ReflectionProperty[] allPropertiesOfObject(object $object)
* @method static \ReflectionMethod methodOfObject(object $object, string $methodName)
* @method static \ReflectionMethod[] allMethodsOfObject(object $object)
*/
class Reflection
{
/**
* @var ClassCache[]
*/
private static $classCaches = [];
/**
* @codeCoverageIgnore
*/
private function __construct()
{
}
public static function classByName(string $className) : \ReflectionClass
{
return self::classCache($className)->reflectionClass();
}
public static function propertyOfClass(string $className, string $propertyName) : \ReflectionProperty
{
return self::classCache($className)->reflectionProperty($propertyName);
}
/**
* @return \ReflectionProperty[]
* Contrary to `\ReflectionClass->getProperties()` the keys of the returned array are the property names
* instead of integers.
*/
public static function allPropertiesOfClass(string $className) : array
{
return self::classCache($className)->allReflectionProperties();
}
public static function methodOfClass(string $className, string $methodName) : \ReflectionMethod
{
return self::classCache($className)->reflectionMethod($methodName);
}
/**
* @return \ReflectionMethod[]
* Contrary to `\ReflectionClass->getMethods()` the keys of the returned array are the method names
* instead of integers.
*/
public static function allMethodsOfClass(string $className) : array
{
return self::classCache($className)->allReflectionMethods();
}
/**
* @return mixed
*/
public static function getPropertyValue(object $object, string $propertyName)
{
return self::classCache(get_class($object))
->reflectionProperty($propertyName)
->getValue($object);
}
/**
* @param mixed $value
* @return void
*/
public static function setPropertyValue(object $object, string $propertyName, $value)
{
self::classCache(get_class($object))
->reflectionProperty($propertyName)
->setValue($object, $value);
}
/**
* @return mixed
*/
public static function getStaticPropertyValue(string $className, string $propertyName)
{
return self::classCache($className)
->reflectionProperty($propertyName)
->getValue();
}
/**
* @param mixed $value
* @return void
*/
public static function setStaticPropertyValue(string $className, string $propertyName, $value)
{
self::classCache($className)
->reflectionProperty($propertyName)
->setValue($value);
}
/**
* @return mixed
*/
public static function invokeMethod(object $object, string $methodName, array $arguments)
{
return self::classCache(get_class($object))
->reflectionMethod($methodName)
->invoke($object, ... $arguments);
}
/**
* @return mixed
*/
public static function invokeStaticMethod(string $className, string $methodName, array $arguments)
{
return self::classCache($className)
->reflectionMethod($methodName)
->invoke(null, ... $arguments);
}
/**
* Offers methods that are based on objects instead of class names.
*
* For IDE support, the methods are defined in this class' DocBlock.
*
* @param string $methodName
* @param array $arguments
*
* @return mixed
*/
public static function __callStatic(string $methodName, array $arguments)
{
$mappedMethods = [
'classByObject' => 'classByName',
'propertyOfObject' => 'propertyOfClass',
'allPropertiesOfObject' => 'allPropertiesOfClass',
'methodOfObject' => 'methodOfClass',
'allMethodsOfObject' => 'allMethodsOfClass',
];
if (! array_key_exists($methodName, $mappedMethods)) {
throw new \Error(
sprintf(
'Call to undefined method %s::%s()',
static::class,
$methodName
)
);
}
$methodName = $mappedMethods[$methodName];
$className = get_class(
array_shift($arguments)
);
return self::$methodName($className, ...$arguments);
}
private static function classCache(string $className) : ClassCache
{
if (! array_key_exists($className, self::$classCaches)) {
self::$classCaches[$className] = new ClassCache($className);
}
return self::$classCaches[$className];
}
}