Sudo.php
5.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
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
<?php
/*
* This file is part of Psy Shell.
*
* (c) 2012-2023 Justin Hileman
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Psy;
/**
* Helpers for bypassing visibility restrictions, mostly used in code generated
* by the `sudo` command.
*/
class Sudo
{
/**
* Fetch a property of an object, bypassing visibility restrictions.
*
* @param object $object
* @param string $property property name
*
* @return mixed Value of $object->property
*/
public static function fetchProperty($object, string $property)
{
$prop = self::getProperty(new \ReflectionObject($object), $property);
return $prop->getValue($object);
}
/**
* Assign the value of a property of an object, bypassing visibility restrictions.
*
* @param object $object
* @param string $property property name
* @param mixed $value
*
* @return mixed Value of $object->property
*/
public static function assignProperty($object, string $property, $value)
{
$prop = self::getProperty(new \ReflectionObject($object), $property);
$prop->setValue($object, $value);
return $value;
}
/**
* Call a method on an object, bypassing visibility restrictions.
*
* @param object $object
* @param string $method method name
* @param mixed $args...
*
* @return mixed
*/
public static function callMethod($object, string $method, ...$args)
{
$refl = new \ReflectionObject($object);
$reflMethod = $refl->getMethod($method);
$reflMethod->setAccessible(true);
return $reflMethod->invokeArgs($object, $args);
}
/**
* Fetch a property of a class, bypassing visibility restrictions.
*
* @param string|object $class class name or instance
* @param string $property property name
*
* @return mixed Value of $class::$property
*/
public static function fetchStaticProperty($class, string $property)
{
$prop = self::getProperty(new \ReflectionClass($class), $property);
$prop->setAccessible(true);
return $prop->getValue();
}
/**
* Assign the value of a static property of a class, bypassing visibility restrictions.
*
* @param string|object $class class name or instance
* @param string $property property name
* @param mixed $value
*
* @return mixed Value of $class::$property
*/
public static function assignStaticProperty($class, string $property, $value)
{
$prop = self::getProperty(new \ReflectionClass($class), $property);
$refl = $prop->getDeclaringClass();
if (\method_exists($refl, 'setStaticPropertyValue')) {
$refl->setStaticPropertyValue($property, $value);
} else {
$prop->setValue($value);
}
return $value;
}
/**
* Call a static method on a class, bypassing visibility restrictions.
*
* @param string|object $class class name or instance
* @param string $method method name
* @param mixed $args...
*
* @return mixed
*/
public static function callStatic($class, string $method, ...$args)
{
$refl = new \ReflectionClass($class);
$reflMethod = $refl->getMethod($method);
$reflMethod->setAccessible(true);
return $reflMethod->invokeArgs(null, $args);
}
/**
* Fetch a class constant, bypassing visibility restrictions.
*
* @param string|object $class class name or instance
* @param string $const constant name
*
* @return mixed
*/
public static function fetchClassConst($class, string $const)
{
$refl = new \ReflectionClass($class);
// Special case the ::class magic constant, because `getConstant` does the wrong thing here.
if ($const === 'class') {
return $refl->getName();
}
do {
if ($refl->hasConstant($const)) {
return $refl->getConstant($const);
}
$refl = $refl->getParentClass();
} while ($refl !== false);
return false;
}
/**
* Construct an instance of a class, bypassing private constructors.
*
* @param string $class class name
* @param mixed $args...
*/
public static function newInstance(string $class, ...$args)
{
$refl = new \ReflectionClass($class);
$instance = $refl->newInstanceWithoutConstructor();
$constructor = $refl->getConstructor();
$constructor->setAccessible(true);
$constructor->invokeArgs($instance, $args);
return $instance;
}
/**
* Get a ReflectionProperty from an object (or its parent classes).
*
* @throws \ReflectionException if neither the object nor any of its parents has this property
*
* @param \ReflectionClass $refl
* @param string $property property name
*
* @return \ReflectionProperty
*/
private static function getProperty(\ReflectionClass $refl, string $property): \ReflectionProperty
{
$firstException = null;
do {
try {
$prop = $refl->getProperty($property);
$prop->setAccessible(true);
return $prop;
} catch (\ReflectionException $e) {
if ($firstException === null) {
$firstException = $e;
}
$refl = $refl->getParentClass();
}
} while ($refl !== false);
throw $firstException;
}
}