4 * This file is part of Psy Shell.
6 * (c) 2012-2018 Justin Hileman
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Psy\Command;
14 use Psy\CodeCleaner\NoReturnValue;
17 use Psy\Exception\RuntimeException;
21 * An abstract command with helpers for inspecting the current context.
23 abstract class ReflectingCommand extends Command implements ContextAware
25 const CLASS_OR_FUNC = '/^[\\\\\w]+$/';
26 const CLASS_MEMBER = '/^([\\\\\w]+)::(\w+)$/';
27 const CLASS_STATIC = '/^([\\\\\w]+)::\$(\w+)$/';
28 const INSTANCE_MEMBER = '/^(\$\w+)(::|->)(\w+)$/';
31 * Context instance (for ContextAware interface).
38 * ContextAware interface.
40 * @param Context $context
42 public function setContext(Context $context)
44 $this->context = $context;
48 * Get the target for a value.
50 * @throws \InvalidArgumentException when the value specified can't be resolved
52 * @param string $valueName Function, class, variable, constant, method or property name
54 * @return array (class or instance name, member name, kind)
56 protected function getTarget($valueName)
58 $valueName = trim($valueName);
61 case preg_match(self::CLASS_OR_FUNC, $valueName, $matches):
62 return [$this->resolveName($matches[0], true), null, 0];
64 case preg_match(self::CLASS_MEMBER, $valueName, $matches):
65 return [$this->resolveName($matches[1]), $matches[2], Mirror::CONSTANT | Mirror::METHOD];
67 case preg_match(self::CLASS_STATIC, $valueName, $matches):
68 return [$this->resolveName($matches[1]), $matches[2], Mirror::STATIC_PROPERTY | Mirror::PROPERTY];
70 case preg_match(self::INSTANCE_MEMBER, $valueName, $matches):
71 if ($matches[2] === '->') {
72 $kind = Mirror::METHOD | Mirror::PROPERTY;
74 $kind = Mirror::CONSTANT | Mirror::METHOD;
77 return [$this->resolveObject($matches[1]), $matches[3], $kind];
80 return [$this->resolveObject($valueName), null, 0];
85 * Resolve a class or function name (with the current shell namespace).
88 * @param bool $includeFunctions (default: false)
92 protected function resolveName($name, $includeFunctions = false)
94 if (substr($name, 0, 1) === '\\') {
98 if ($namespace = $this->getApplication()->getNamespace()) {
99 $fullName = $namespace . '\\' . $name;
101 if (class_exists($fullName) || interface_exists($fullName) || ($includeFunctions && function_exists($fullName))) {
110 * Get a Reflector and documentation for a function, class or instance, constant, method or property.
112 * @param string $valueName Function, class, variable, constant, method or property name
114 * @return array (value, Reflector)
116 protected function getTargetAndReflector($valueName)
118 list($value, $member, $kind) = $this->getTarget($valueName);
120 return [$value, Mirror::get($value, $member, $kind)];
124 * Resolve code to a value in the current scope.
126 * @throws RuntimeException when the code does not return a value in the current scope
128 * @param string $code
130 * @return mixed Variable value
132 protected function resolveCode($code)
135 $value = $this->getApplication()->execute($code, true);
136 } catch (\Exception $e) {
137 // Swallow all exceptions?
140 if (!isset($value) || $value instanceof NoReturnValue) {
141 throw new RuntimeException('Unknown target: ' . $code);
148 * Resolve code to an object in the current scope.
150 * @throws RuntimeException when the code resolves to a non-object value
152 * @param string $code
154 * @return object Variable instance
156 private function resolveObject($code)
158 $value = $this->resolveCode($code);
160 if (!is_object($value)) {
161 throw new RuntimeException('Unable to inspect a non-object');
168 * @deprecated Use `resolveCode` instead
170 * @param string $name
172 * @return mixed Variable instance
174 protected function resolveInstance($name)
176 @trigger_error('`resolveInstance` is deprecated; use `resolveCode` instead.', E_USER_DEPRECATED);
178 return $this->resolveCode($name);
182 * Get a variable from the current shell scope.
184 * @param string $name
188 protected function getScopeVariable($name)
190 return $this->context->get($name);
194 * Get all scope variables from the current shell scope.
198 protected function getScopeVariables()
200 return $this->context->getAll();
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).
208 * @param \Reflector $reflector
210 protected function setCommandScopeVariables(\Reflector $reflector)
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();
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();
232 case 'ReflectionFunction':
233 $vars['__function'] = $reflector->name;
234 if ($reflector->inNamespace()) {
235 $vars['__namespace'] = $reflector->getNamespaceName();
239 case 'ReflectionGenerator':
240 $funcReflector = $reflector->getFunction();
241 $vars['__function'] = $funcReflector->name;
242 if ($funcReflector->inNamespace()) {
243 $vars['__namespace'] = $funcReflector->getNamespaceName();
245 if ($fileName = $reflector->getExecutingFile()) {
246 $vars['__file'] = $fileName;
247 $vars['__line'] = $reflector->getExecutingLine();
248 $vars['__dir'] = dirname($fileName);
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();
259 // no line for these, but this'll do
260 if ($fileName = $reflector->getDeclaringClass()->getFileName()) {
261 $vars['__file'] = $fileName;
262 $vars['__dir'] = dirname($fileName);
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);
275 $this->context->setCommandScopeVariables($vars);