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