Chris@13
|
1 <?php
|
Chris@13
|
2
|
Chris@13
|
3 /*
|
Chris@13
|
4 * This file is part of Psy Shell.
|
Chris@13
|
5 *
|
Chris@13
|
6 * (c) 2012-2018 Justin Hileman
|
Chris@13
|
7 *
|
Chris@13
|
8 * For the full copyright and license information, please view the LICENSE
|
Chris@13
|
9 * file that was distributed with this source code.
|
Chris@13
|
10 */
|
Chris@13
|
11
|
Chris@13
|
12 namespace Psy;
|
Chris@13
|
13
|
Chris@13
|
14 /**
|
Chris@13
|
15 * Helpers for bypassing visibility restrictions, mostly used in code generated
|
Chris@13
|
16 * by the `sudo` command.
|
Chris@13
|
17 */
|
Chris@13
|
18 class Sudo
|
Chris@13
|
19 {
|
Chris@13
|
20 /**
|
Chris@13
|
21 * Fetch a property of an object, bypassing visibility restrictions.
|
Chris@13
|
22 *
|
Chris@13
|
23 * @param object $object
|
Chris@13
|
24 * @param string $property property name
|
Chris@13
|
25 *
|
Chris@13
|
26 * @return mixed Value of $object->property
|
Chris@13
|
27 */
|
Chris@13
|
28 public static function fetchProperty($object, $property)
|
Chris@13
|
29 {
|
Chris@13
|
30 $refl = new \ReflectionObject($object);
|
Chris@13
|
31 $prop = $refl->getProperty($property);
|
Chris@13
|
32 $prop->setAccessible(true);
|
Chris@13
|
33
|
Chris@13
|
34 return $prop->getValue($object);
|
Chris@13
|
35 }
|
Chris@13
|
36
|
Chris@13
|
37 /**
|
Chris@13
|
38 * Assign the value of a property of an object, bypassing visibility restrictions.
|
Chris@13
|
39 *
|
Chris@13
|
40 * @param object $object
|
Chris@13
|
41 * @param string $property property name
|
Chris@13
|
42 * @param mixed $value
|
Chris@13
|
43 *
|
Chris@13
|
44 * @return mixed Value of $object->property
|
Chris@13
|
45 */
|
Chris@13
|
46 public static function assignProperty($object, $property, $value)
|
Chris@13
|
47 {
|
Chris@13
|
48 $refl = new \ReflectionObject($object);
|
Chris@13
|
49 $prop = $refl->getProperty($property);
|
Chris@13
|
50 $prop->setAccessible(true);
|
Chris@13
|
51 $prop->setValue($object, $value);
|
Chris@13
|
52
|
Chris@13
|
53 return $value;
|
Chris@13
|
54 }
|
Chris@13
|
55
|
Chris@13
|
56 /**
|
Chris@13
|
57 * Call a method on an object, bypassing visibility restrictions.
|
Chris@13
|
58 *
|
Chris@13
|
59 * @param object $object
|
Chris@13
|
60 * @param string $method method name
|
Chris@13
|
61 * @param mixed $args...
|
Chris@13
|
62 *
|
Chris@13
|
63 * @return mixed
|
Chris@13
|
64 */
|
Chris@13
|
65 public static function callMethod($object, $method, $args = null)
|
Chris@13
|
66 {
|
Chris@17
|
67 $args = \func_get_args();
|
Chris@17
|
68 $object = \array_shift($args);
|
Chris@17
|
69 $method = \array_shift($args);
|
Chris@13
|
70
|
Chris@13
|
71 $refl = new \ReflectionObject($object);
|
Chris@13
|
72 $reflMethod = $refl->getMethod($method);
|
Chris@13
|
73 $reflMethod->setAccessible(true);
|
Chris@13
|
74
|
Chris@13
|
75 return $reflMethod->invokeArgs($object, $args);
|
Chris@13
|
76 }
|
Chris@13
|
77
|
Chris@13
|
78 /**
|
Chris@13
|
79 * Fetch a property of a class, bypassing visibility restrictions.
|
Chris@13
|
80 *
|
Chris@13
|
81 * @param string|object $class class name or instance
|
Chris@13
|
82 * @param string $property property name
|
Chris@13
|
83 *
|
Chris@13
|
84 * @return mixed Value of $class::$property
|
Chris@13
|
85 */
|
Chris@13
|
86 public static function fetchStaticProperty($class, $property)
|
Chris@13
|
87 {
|
Chris@13
|
88 $refl = new \ReflectionClass($class);
|
Chris@13
|
89 $prop = $refl->getProperty($property);
|
Chris@13
|
90 $prop->setAccessible(true);
|
Chris@13
|
91
|
Chris@13
|
92 return $prop->getValue();
|
Chris@13
|
93 }
|
Chris@13
|
94
|
Chris@13
|
95 /**
|
Chris@13
|
96 * Assign the value of a static property of a class, bypassing visibility restrictions.
|
Chris@13
|
97 *
|
Chris@13
|
98 * @param string|object $class class name or instance
|
Chris@13
|
99 * @param string $property property name
|
Chris@13
|
100 * @param mixed $value
|
Chris@13
|
101 *
|
Chris@13
|
102 * @return mixed Value of $class::$property
|
Chris@13
|
103 */
|
Chris@13
|
104 public static function assignStaticProperty($class, $property, $value)
|
Chris@13
|
105 {
|
Chris@13
|
106 $refl = new \ReflectionClass($class);
|
Chris@13
|
107 $prop = $refl->getProperty($property);
|
Chris@13
|
108 $prop->setAccessible(true);
|
Chris@13
|
109 $prop->setValue($value);
|
Chris@13
|
110
|
Chris@13
|
111 return $value;
|
Chris@13
|
112 }
|
Chris@13
|
113
|
Chris@13
|
114 /**
|
Chris@13
|
115 * Call a static method on a class, bypassing visibility restrictions.
|
Chris@13
|
116 *
|
Chris@13
|
117 * @param string|object $class class name or instance
|
Chris@13
|
118 * @param string $method method name
|
Chris@13
|
119 * @param mixed $args...
|
Chris@13
|
120 *
|
Chris@13
|
121 * @return mixed
|
Chris@13
|
122 */
|
Chris@13
|
123 public static function callStatic($class, $method, $args = null)
|
Chris@13
|
124 {
|
Chris@17
|
125 $args = \func_get_args();
|
Chris@17
|
126 $class = \array_shift($args);
|
Chris@17
|
127 $method = \array_shift($args);
|
Chris@13
|
128
|
Chris@13
|
129 $refl = new \ReflectionClass($class);
|
Chris@13
|
130 $reflMethod = $refl->getMethod($method);
|
Chris@13
|
131 $reflMethod->setAccessible(true);
|
Chris@13
|
132
|
Chris@13
|
133 return $reflMethod->invokeArgs(null, $args);
|
Chris@13
|
134 }
|
Chris@13
|
135
|
Chris@13
|
136 /**
|
Chris@13
|
137 * Fetch a class constant, bypassing visibility restrictions.
|
Chris@13
|
138 *
|
Chris@13
|
139 * @param string|object $class class name or instance
|
Chris@13
|
140 * @param string $const constant name
|
Chris@13
|
141 *
|
Chris@13
|
142 * @return mixed
|
Chris@13
|
143 */
|
Chris@13
|
144 public static function fetchClassConst($class, $const)
|
Chris@13
|
145 {
|
Chris@13
|
146 $refl = new \ReflectionClass($class);
|
Chris@13
|
147
|
Chris@13
|
148 return $refl->getConstant($const);
|
Chris@13
|
149 }
|
Chris@13
|
150 }
|