Sudo.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <?php
  2. /*
  3. * This file is part of Psy Shell.
  4. *
  5. * (c) 2012-2023 Justin Hileman
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Psy;
  11. /**
  12. * Helpers for bypassing visibility restrictions, mostly used in code generated
  13. * by the `sudo` command.
  14. */
  15. class Sudo
  16. {
  17. /**
  18. * Fetch a property of an object, bypassing visibility restrictions.
  19. *
  20. * @param object $object
  21. * @param string $property property name
  22. *
  23. * @return mixed Value of $object->property
  24. */
  25. public static function fetchProperty($object, string $property)
  26. {
  27. $prop = self::getProperty(new \ReflectionObject($object), $property);
  28. return $prop->getValue($object);
  29. }
  30. /**
  31. * Assign the value of a property of an object, bypassing visibility restrictions.
  32. *
  33. * @param object $object
  34. * @param string $property property name
  35. * @param mixed $value
  36. *
  37. * @return mixed Value of $object->property
  38. */
  39. public static function assignProperty($object, string $property, $value)
  40. {
  41. $prop = self::getProperty(new \ReflectionObject($object), $property);
  42. $prop->setValue($object, $value);
  43. return $value;
  44. }
  45. /**
  46. * Call a method on an object, bypassing visibility restrictions.
  47. *
  48. * @param object $object
  49. * @param string $method method name
  50. * @param mixed $args...
  51. *
  52. * @return mixed
  53. */
  54. public static function callMethod($object, string $method, ...$args)
  55. {
  56. $refl = new \ReflectionObject($object);
  57. $reflMethod = $refl->getMethod($method);
  58. $reflMethod->setAccessible(true);
  59. return $reflMethod->invokeArgs($object, $args);
  60. }
  61. /**
  62. * Fetch a property of a class, bypassing visibility restrictions.
  63. *
  64. * @param string|object $class class name or instance
  65. * @param string $property property name
  66. *
  67. * @return mixed Value of $class::$property
  68. */
  69. public static function fetchStaticProperty($class, string $property)
  70. {
  71. $prop = self::getProperty(new \ReflectionClass($class), $property);
  72. $prop->setAccessible(true);
  73. return $prop->getValue();
  74. }
  75. /**
  76. * Assign the value of a static property of a class, bypassing visibility restrictions.
  77. *
  78. * @param string|object $class class name or instance
  79. * @param string $property property name
  80. * @param mixed $value
  81. *
  82. * @return mixed Value of $class::$property
  83. */
  84. public static function assignStaticProperty($class, string $property, $value)
  85. {
  86. $prop = self::getProperty(new \ReflectionClass($class), $property);
  87. $refl = $prop->getDeclaringClass();
  88. if (\method_exists($refl, 'setStaticPropertyValue')) {
  89. $refl->setStaticPropertyValue($property, $value);
  90. } else {
  91. $prop->setValue($value);
  92. }
  93. return $value;
  94. }
  95. /**
  96. * Call a static method on a class, bypassing visibility restrictions.
  97. *
  98. * @param string|object $class class name or instance
  99. * @param string $method method name
  100. * @param mixed $args...
  101. *
  102. * @return mixed
  103. */
  104. public static function callStatic($class, string $method, ...$args)
  105. {
  106. $refl = new \ReflectionClass($class);
  107. $reflMethod = $refl->getMethod($method);
  108. $reflMethod->setAccessible(true);
  109. return $reflMethod->invokeArgs(null, $args);
  110. }
  111. /**
  112. * Fetch a class constant, bypassing visibility restrictions.
  113. *
  114. * @param string|object $class class name or instance
  115. * @param string $const constant name
  116. *
  117. * @return mixed
  118. */
  119. public static function fetchClassConst($class, string $const)
  120. {
  121. $refl = new \ReflectionClass($class);
  122. do {
  123. if ($refl->hasConstant($const)) {
  124. return $refl->getConstant($const);
  125. }
  126. $refl = $refl->getParentClass();
  127. } while ($refl !== false);
  128. return false;
  129. }
  130. /**
  131. * Construct an instance of a class, bypassing private constructors.
  132. *
  133. * @param string $class class name
  134. * @param mixed $args...
  135. */
  136. public static function newInstance(string $class, ...$args)
  137. {
  138. $refl = new \ReflectionClass($class);
  139. $instance = $refl->newInstanceWithoutConstructor();
  140. $constructor = $refl->getConstructor();
  141. $constructor->setAccessible(true);
  142. $constructor->invokeArgs($instance, $args);
  143. return $instance;
  144. }
  145. /**
  146. * Get a ReflectionProperty from an object (or its parent classes).
  147. *
  148. * @throws \ReflectionException if neither the object nor any of its parents has this property
  149. *
  150. * @param \ReflectionClass $refl
  151. * @param string $property property name
  152. *
  153. * @return \ReflectionProperty
  154. */
  155. private static function getProperty(\ReflectionClass $refl, string $property): \ReflectionProperty
  156. {
  157. $firstException = null;
  158. do {
  159. try {
  160. $prop = $refl->getProperty($property);
  161. $prop->setAccessible(true);
  162. return $prop;
  163. } catch (\ReflectionException $e) {
  164. if ($firstException === null) {
  165. $firstException = $e;
  166. }
  167. $refl = $refl->getParentClass();
  168. }
  169. } while ($refl !== false);
  170. throw $firstException;
  171. }
  172. }