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