Sudo.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. /*
  3. * This file is part of Psy Shell.
  4. *
  5. * (c) 2012-2020 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, $property)
  26. {
  27. $refl = new \ReflectionObject($object);
  28. $prop = $refl->getProperty($property);
  29. $prop->setAccessible(true);
  30. return $prop->getValue($object);
  31. }
  32. /**
  33. * Assign the value of a property of an object, bypassing visibility restrictions.
  34. *
  35. * @param object $object
  36. * @param string $property property name
  37. * @param mixed $value
  38. *
  39. * @return mixed Value of $object->property
  40. */
  41. public static function assignProperty($object, $property, $value)
  42. {
  43. $refl = new \ReflectionObject($object);
  44. $prop = $refl->getProperty($property);
  45. $prop->setAccessible(true);
  46. $prop->setValue($object, $value);
  47. return $value;
  48. }
  49. /**
  50. * Call a method on an object, bypassing visibility restrictions.
  51. *
  52. * @param object $object
  53. * @param string $method method name
  54. * @param mixed $args...
  55. *
  56. * @return mixed
  57. */
  58. public static function callMethod($object, $method, $args = null)
  59. {
  60. $args = \func_get_args();
  61. $object = \array_shift($args);
  62. $method = \array_shift($args);
  63. $refl = new \ReflectionObject($object);
  64. $reflMethod = $refl->getMethod($method);
  65. $reflMethod->setAccessible(true);
  66. return $reflMethod->invokeArgs($object, $args);
  67. }
  68. /**
  69. * Fetch a property of a class, bypassing visibility restrictions.
  70. *
  71. * @param string|object $class class name or instance
  72. * @param string $property property name
  73. *
  74. * @return mixed Value of $class::$property
  75. */
  76. public static function fetchStaticProperty($class, $property)
  77. {
  78. $refl = new \ReflectionClass($class);
  79. $prop = $refl->getProperty($property);
  80. $prop->setAccessible(true);
  81. return $prop->getValue();
  82. }
  83. /**
  84. * Assign the value of a static property of a class, bypassing visibility restrictions.
  85. *
  86. * @param string|object $class class name or instance
  87. * @param string $property property name
  88. * @param mixed $value
  89. *
  90. * @return mixed Value of $class::$property
  91. */
  92. public static function assignStaticProperty($class, $property, $value)
  93. {
  94. $refl = new \ReflectionClass($class);
  95. $prop = $refl->getProperty($property);
  96. $prop->setAccessible(true);
  97. $prop->setValue($value);
  98. return $value;
  99. }
  100. /**
  101. * Call a static method on a class, bypassing visibility restrictions.
  102. *
  103. * @param string|object $class class name or instance
  104. * @param string $method method name
  105. * @param mixed $args...
  106. *
  107. * @return mixed
  108. */
  109. public static function callStatic($class, $method, $args = null)
  110. {
  111. $args = \func_get_args();
  112. $class = \array_shift($args);
  113. $method = \array_shift($args);
  114. $refl = new \ReflectionClass($class);
  115. $reflMethod = $refl->getMethod($method);
  116. $reflMethod->setAccessible(true);
  117. return $reflMethod->invokeArgs(null, $args);
  118. }
  119. /**
  120. * Fetch a class constant, bypassing visibility restrictions.
  121. *
  122. * @param string|object $class class name or instance
  123. * @param string $const constant name
  124. *
  125. * @return mixed
  126. */
  127. public static function fetchClassConst($class, $const)
  128. {
  129. $refl = new \ReflectionClass($class);
  130. return $refl->getConstant($const);
  131. }
  132. }