-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClassCache.php
More file actions
126 lines (98 loc) · 3.7 KB
/
Copy pathClassCache.php
File metadata and controls
126 lines (98 loc) · 3.7 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
<?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;
/**
* @internal
*/
class ClassCache
{
private $className;
private $reflectionClass;
/**
* @var \ReflectionProperty[]
*/
private $reflectionProperties = [];
private $allPropertiesLoaded = false;
/**
* @var \ReflectionMethod[]
*/
private $reflectionMethods = [];
private $allMethodsLoaded = false;
public function __construct(string $className)
{
$this->className = $className;
$this->reflectionClass = new \ReflectionClass($className);
}
public function reflectionClass() : \ReflectionClass
{
return $this->reflectionClass;
}
public function reflectionProperty(string $propertyName) : \ReflectionProperty
{
if (! array_key_exists($propertyName, $this->reflectionProperties)) {
$this->reflectionProperties[$propertyName] = $this->reflectionClass->getProperty($propertyName);
$this->reflectionProperties[$propertyName]->setAccessible(true);
}
return $this->reflectionProperties[$propertyName];
}
/**
* @return \ReflectionProperty[]
* Contrary to `\ReflectionClass->getProperties()` the keys of the returned array are the property names
* instead of integers.
*/
public function allReflectionProperties() : array
{
if (! $this->allPropertiesLoaded) {
$allProperties = [];
foreach ($this->reflectionClass->getProperties() as $reflectionProperty) {
$propertyName = $reflectionProperty->getName();
$reflectionProperty->setAccessible(true);
if (array_key_exists($propertyName, $this->reflectionProperties)) {
$reflectionProperty = $this->reflectionProperties[$propertyName];
}
$allProperties[$propertyName] = $reflectionProperty;
}
$this->reflectionProperties = $allProperties;
$this->allPropertiesLoaded = true;
}
return $this->reflectionProperties;
}
public function reflectionMethod(string $methodName) : \ReflectionMethod
{
if (! array_key_exists($methodName, $this->reflectionMethods)) {
$this->reflectionMethods[$methodName] = $this->reflectionClass->getMethod($methodName);
$this->reflectionMethods[$methodName]->setAccessible(true);
}
return $this->reflectionMethods[$methodName];
}
/**
* @return \ReflectionMethod[]
* Contrary to `\ReflectionClass->getMethods()` the keys of the returned array are the method names
* instead of integers.
*/
public function allReflectionMethods() : array
{
if (! $this->allMethodsLoaded) {
$allMethods = [];
foreach ($this->reflectionClass->getMethods() as $reflectionMethod) {
$methodName = $reflectionMethod->getName();
$reflectionMethod->setAccessible(true);
if (array_key_exists($methodName, $this->reflectionMethods)) {
$reflectionMethod = $this->reflectionMethods[$methodName];
}
$allMethods[$methodName] = $reflectionMethod;
}
$this->reflectionMethods = $allMethods;
$this->allMethodsLoaded = true;
}
return $this->reflectionMethods;
}
}