0bf8327c919a52d088249b8cf87e9057996a9f12
[yaffs-website] / vendor / psy / psysh / src / Command / ReflectingCommand.php
1 <?php
2
3 /*
4  * This file is part of Psy Shell.
5  *
6  * (c) 2012-2018 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\Command;
13
14 use Psy\CodeCleaner\NoReturnValue;
15 use Psy\Context;
16 use Psy\ContextAware;
17 use Psy\Exception\RuntimeException;
18 use Psy\Util\Mirror;
19
20 /**
21  * An abstract command with helpers for inspecting the current context.
22  */
23 abstract class ReflectingCommand extends Command implements ContextAware
24 {
25     const CLASS_OR_FUNC   = '/^[\\\\\w]+$/';
26     const CLASS_MEMBER    = '/^([\\\\\w]+)::(\w+)$/';
27     const CLASS_STATIC    = '/^([\\\\\w]+)::\$(\w+)$/';
28     const INSTANCE_MEMBER = '/^(\$\w+)(::|->)(\w+)$/';
29
30     /**
31      * Context instance (for ContextAware interface).
32      *
33      * @var Context
34      */
35     protected $context;
36
37     /**
38      * ContextAware interface.
39      *
40      * @param Context $context
41      */
42     public function setContext(Context $context)
43     {
44         $this->context = $context;
45     }
46
47     /**
48      * Get the target for a value.
49      *
50      * @throws \InvalidArgumentException when the value specified can't be resolved
51      *
52      * @param string $valueName Function, class, variable, constant, method or property name
53      *
54      * @return array (class or instance name, member name, kind)
55      */
56     protected function getTarget($valueName)
57     {
58         $valueName = trim($valueName);
59         $matches   = [];
60         switch (true) {
61             case preg_match(self::CLASS_OR_FUNC, $valueName, $matches):
62                 return [$this->resolveName($matches[0], true), null, 0];
63
64             case preg_match(self::CLASS_MEMBER, $valueName, $matches):
65                 return [$this->resolveName($matches[1]), $matches[2], Mirror::CONSTANT | Mirror::METHOD];
66
67             case preg_match(self::CLASS_STATIC, $valueName, $matches):
68                 return [$this->resolveName($matches[1]), $matches[2], Mirror::STATIC_PROPERTY | Mirror::PROPERTY];
69
70             case preg_match(self::INSTANCE_MEMBER, $valueName, $matches):
71                 if ($matches[2] === '->') {
72                     $kind = Mirror::METHOD | Mirror::PROPERTY;
73                 } else {
74                     $kind = Mirror::CONSTANT | Mirror::METHOD;
75                 }
76
77                 return [$this->resolveObject($matches[1]), $matches[3], $kind];
78
79             default:
80                 return [$this->resolveObject($valueName), null, 0];
81         }
82     }
83
84     /**
85      * Resolve a class or function name (with the current shell namespace).
86      *
87      * @param string $name
88      * @param bool   $includeFunctions (default: false)
89      *
90      * @return string
91      */
92     protected function resolveName($name, $includeFunctions = false)
93     {
94         if (substr($name, 0, 1) === '\\') {
95             return $name;
96         }
97
98         if ($namespace = $this->getApplication()->getNamespace()) {
99             $fullName = $namespace . '\\' . $name;
100
101             if (class_exists($fullName) || interface_exists($fullName) || ($includeFunctions && function_exists($fullName))) {
102                 return $fullName;
103             }
104         }
105
106         return $name;
107     }
108
109     /**
110      * Get a Reflector and documentation for a function, class or instance, constant, method or property.
111      *
112      * @param string $valueName Function, class, variable, constant, method or property name
113      *
114      * @return array (value, Reflector)
115      */
116     protected function getTargetAndReflector($valueName)
117     {
118         list($value, $member, $kind) = $this->getTarget($valueName);
119
120         return [$value, Mirror::get($value, $member, $kind)];
121     }
122
123     /**
124      * Resolve code to a value in the current scope.
125      *
126      * @throws RuntimeException when the code does not return a value in the current scope
127      *
128      * @param string $code
129      *
130      * @return mixed Variable value
131      */
132     protected function resolveCode($code)
133     {
134         try {
135             $value = $this->getApplication()->execute($code, true);
136         } catch (\Exception $e) {
137             // Swallow all exceptions?
138         }
139
140         if (!isset($value) || $value instanceof NoReturnValue) {
141             throw new RuntimeException('Unknown target: ' . $code);
142         }
143
144         return $value;
145     }
146
147     /**
148      * Resolve code to an object in the current scope.
149      *
150      * @throws RuntimeException when the code resolves to a non-object value
151      *
152      * @param string $code
153      *
154      * @return object Variable instance
155      */
156     private function resolveObject($code)
157     {
158         $value = $this->resolveCode($code);
159
160         if (!is_object($value)) {
161             throw new RuntimeException('Unable to inspect a non-object');
162         }
163
164         return $value;
165     }
166
167     /**
168      * @deprecated Use `resolveCode` instead
169      *
170      * @param string $name
171      *
172      * @return mixed Variable instance
173      */
174     protected function resolveInstance($name)
175     {
176         @trigger_error('`resolveInstance` is deprecated; use `resolveCode` instead.', E_USER_DEPRECATED);
177
178         return $this->resolveCode($name);
179     }
180
181     /**
182      * Get a variable from the current shell scope.
183      *
184      * @param string $name
185      *
186      * @return mixed
187      */
188     protected function getScopeVariable($name)
189     {
190         return $this->context->get($name);
191     }
192
193     /**
194      * Get all scope variables from the current shell scope.
195      *
196      * @return array
197      */
198     protected function getScopeVariables()
199     {
200         return $this->context->getAll();
201     }
202
203     /**
204      * Given a Reflector instance, set command-scope variables in the shell
205      * execution context. This is used to inject magic $__class, $__method and
206      * $__file variables (as well as a handful of others).
207      *
208      * @param \Reflector $reflector
209      */
210     protected function setCommandScopeVariables(\Reflector $reflector)
211     {
212         $vars = [];
213
214         switch (get_class($reflector)) {
215             case 'ReflectionClass':
216             case 'ReflectionObject':
217                 $vars['__class'] = $reflector->name;
218                 if ($reflector->inNamespace()) {
219                     $vars['__namespace'] = $reflector->getNamespaceName();
220                 }
221                 break;
222
223             case 'ReflectionMethod':
224                 $vars['__method'] = sprintf('%s::%s', $reflector->class, $reflector->name);
225                 $vars['__class'] = $reflector->class;
226                 $classReflector = $reflector->getDeclaringClass();
227                 if ($classReflector->inNamespace()) {
228                     $vars['__namespace'] = $classReflector->getNamespaceName();
229                 }
230                 break;
231
232             case 'ReflectionFunction':
233                 $vars['__function'] = $reflector->name;
234                 if ($reflector->inNamespace()) {
235                     $vars['__namespace'] = $reflector->getNamespaceName();
236                 }
237                 break;
238
239             case 'ReflectionGenerator':
240                 $funcReflector = $reflector->getFunction();
241                 $vars['__function'] = $funcReflector->name;
242                 if ($funcReflector->inNamespace()) {
243                     $vars['__namespace'] = $funcReflector->getNamespaceName();
244                 }
245                 if ($fileName = $reflector->getExecutingFile()) {
246                     $vars['__file'] = $fileName;
247                     $vars['__line'] = $reflector->getExecutingLine();
248                     $vars['__dir']  = dirname($fileName);
249                 }
250                 break;
251
252             case 'ReflectionProperty':
253             case 'Psy\Reflection\ReflectionConstant':
254                 $classReflector = $reflector->getDeclaringClass();
255                 $vars['__class'] = $classReflector->name;
256                 if ($classReflector->inNamespace()) {
257                     $vars['__namespace'] = $classReflector->getNamespaceName();
258                 }
259                 // no line for these, but this'll do
260                 if ($fileName = $reflector->getDeclaringClass()->getFileName()) {
261                     $vars['__file'] = $fileName;
262                     $vars['__dir']  = dirname($fileName);
263                 }
264                 break;
265         }
266
267         if ($reflector instanceof \ReflectionClass || $reflector instanceof \ReflectionFunctionAbstract) {
268             if ($fileName = $reflector->getFileName()) {
269                 $vars['__file'] = $fileName;
270                 $vars['__line'] = $reflector->getStartLine();
271                 $vars['__dir']  = dirname($fileName);
272             }
273         }
274
275         $this->context->setCommandScopeVariables($vars);
276     }
277 }