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