Sudo.php
4.04 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
<?php
/*
* This file is part of Psy Shell.
*
* (c) 2012-2018 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, $property)
{
$refl = new \ReflectionObject($object);
$prop = $refl->getProperty($property);
$prop->setAccessible(true);
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, $property, $value)
{
$refl = new \ReflectionObject($object);
$prop = $refl->getProperty($property);
$prop->setAccessible(true);
$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, $method, $args = null)
{
$args = \func_get_args();
$object = \array_shift($args);
$method = \array_shift($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, $property)
{
$refl = new \ReflectionClass($class);
$prop = $refl->getProperty($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, $property, $value)
{
$refl = new \ReflectionClass($class);
$prop = $refl->getProperty($property);
$prop->setAccessible(true);
$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, $method, $args = null)
{
$args = \func_get_args();
$class = \array_shift($args);
$method = \array_shift($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, $const)
{
$refl = new \ReflectionClass($class);
return $refl->getConstant($const);
}
}