4 * This file is part of Psy Shell.
6 * (c) 2012-2017 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;
16 use Psy\Exception\RuntimeException;
20 * An abstract command with helpers for inspecting the current context.
22 abstract class ReflectingCommand extends Command implements ContextAware
24 const CLASS_OR_FUNC = '/^[\\\\\w]+$/';
25 const INSTANCE = '/^\$(\w+)$/';
26 const CLASS_MEMBER = '/^([\\\\\w]+)::(\w+)$/';
27 const CLASS_STATIC = '/^([\\\\\w]+)::\$(\w+)$/';
28 const INSTANCE_MEMBER = '/^\$(\w+)(::|->)(\w+)$/';
29 const INSTANCE_STATIC = '/^\$(\w+)::\$(\w+)$/';
30 const SUPERGLOBAL = '/^\$(GLOBALS|_(?:SERVER|ENV|FILES|COOKIE|POST|GET|SESSION))$/';
33 * Context instance (for ContextAware interface).
40 * ContextAware interface.
42 * @param Context $context
44 public function setContext(Context $context)
46 $this->context = $context;
50 * Get the target for a value.
52 * @throws \InvalidArgumentException when the value specified can't be resolved
54 * @param string $valueName Function, class, variable, constant, method or property name
55 * @param bool $classOnly True if the name should only refer to a class, function or instance
57 * @return array (class or instance name, member name, kind)
59 protected function getTarget($valueName, $classOnly = false)
61 $valueName = trim($valueName);
64 case preg_match(self::SUPERGLOBAL, $valueName, $matches):
65 // @todo maybe do something interesting with these at some point?
66 if (array_key_exists($matches[1], $GLOBALS)) {
67 throw new RuntimeException('Unable to inspect a non-object');
69 throw new RuntimeException('Unknown target: ' . $valueName);
72 case preg_match(self::CLASS_OR_FUNC, $valueName, $matches):
73 return array($this->resolveName($matches[0], true), null, 0);
75 case preg_match(self::INSTANCE, $valueName, $matches):
76 return array($this->resolveInstance($matches[1]), null, 0);
78 case !$classOnly && preg_match(self::CLASS_MEMBER, $valueName, $matches):
79 return array($this->resolveName($matches[1]), $matches[2], Mirror::CONSTANT | Mirror::METHOD);
81 case !$classOnly && preg_match(self::CLASS_STATIC, $valueName, $matches):
82 return array($this->resolveName($matches[1]), $matches[2], Mirror::STATIC_PROPERTY | Mirror::PROPERTY);
84 case !$classOnly && preg_match(self::INSTANCE_MEMBER, $valueName, $matches):
85 if ($matches[2] === '->') {
86 $kind = Mirror::METHOD | Mirror::PROPERTY;
88 $kind = Mirror::CONSTANT | Mirror::METHOD;
91 return array($this->resolveInstance($matches[1]), $matches[3], $kind);
93 case !$classOnly && preg_match(self::INSTANCE_STATIC, $valueName, $matches):
94 return array($this->resolveInstance($matches[1]), $matches[2], Mirror::STATIC_PROPERTY);
97 throw new RuntimeException('Unknown target: ' . $valueName);
102 * Resolve a class or function name (with the current shell namespace).
104 * @param string $name
105 * @param bool $includeFunctions (default: false)
109 protected function resolveName($name, $includeFunctions = false)
111 if (substr($name, 0, 1) === '\\') {
115 if ($namespace = $this->getApplication()->getNamespace()) {
116 $fullName = $namespace . '\\' . $name;
118 if (class_exists($fullName) || interface_exists($fullName) || ($includeFunctions && function_exists($fullName))) {
127 * Get a Reflector and documentation for a function, class or instance, constant, method or property.
129 * @param string $valueName Function, class, variable, constant, method or property name
130 * @param bool $classOnly True if the name should only refer to a class, function or instance
132 * @return array (value, Reflector)
134 protected function getTargetAndReflector($valueName, $classOnly = false)
136 list($value, $member, $kind) = $this->getTarget($valueName, $classOnly);
138 return array($value, Mirror::get($value, $member, $kind));
142 * Return a variable instance from the current scope.
144 * @throws \InvalidArgumentException when the requested variable does not exist in the current scope
146 * @param string $name
148 * @return mixed Variable instance
150 protected function resolveInstance($name)
152 $value = $this->getScopeVariable($name);
153 if (!is_object($value)) {
154 throw new RuntimeException('Unable to inspect a non-object');
161 * Get a variable from the current shell scope.
163 * @param string $name
167 protected function getScopeVariable($name)
169 return $this->context->get($name);
173 * Get all scope variables from the current shell scope.
177 protected function getScopeVariables()
179 return $this->context->getAll();
183 * Given a Reflector instance, set command-scope variables in the shell
184 * execution context. This is used to inject magic $__class, $__method and
185 * $__file variables (as well as a handful of others).
187 * @param \Reflector $reflector
189 protected function setCommandScopeVariables(\Reflector $reflector)
193 switch (get_class($reflector)) {
194 case 'ReflectionClass':
195 case 'ReflectionObject':
196 $vars['__class'] = $reflector->name;
197 if ($reflector->inNamespace()) {
198 $vars['__namespace'] = $reflector->getNamespaceName();
202 case 'ReflectionMethod':
203 $vars['__method'] = sprintf('%s::%s', $reflector->class, $reflector->name);
204 $vars['__class'] = $reflector->class;
205 $classReflector = $reflector->getDeclaringClass();
206 if ($classReflector->inNamespace()) {
207 $vars['__namespace'] = $classReflector->getNamespaceName();
211 case 'ReflectionFunction':
212 $vars['__function'] = $reflector->name;
213 if ($reflector->inNamespace()) {
214 $vars['__namespace'] = $reflector->getNamespaceName();
218 case 'ReflectionGenerator':
219 $funcReflector = $reflector->getFunction();
220 $vars['__function'] = $funcReflector->name;
221 if ($funcReflector->inNamespace()) {
222 $vars['__namespace'] = $funcReflector->getNamespaceName();
224 if ($fileName = $reflector->getExecutingFile()) {
225 $vars['__file'] = $fileName;
226 $vars['__line'] = $reflector->getExecutingLine();
227 $vars['__dir'] = dirname($fileName);
231 case 'ReflectionProperty':
232 case 'Psy\Reflection\ReflectionConstant':
233 $classReflector = $reflector->getDeclaringClass();
234 $vars['__class'] = $classReflector->name;
235 if ($classReflector->inNamespace()) {
236 $vars['__namespace'] = $classReflector->getNamespaceName();
238 // no line for these, but this'll do
239 if ($fileName = $reflector->getDeclaringClass()->getFileName()) {
240 $vars['__file'] = $fileName;
241 $vars['__dir'] = dirname($fileName);
246 if ($reflector instanceof \ReflectionClass || $reflector instanceof \ReflectionFunctionAbstract) {
247 if ($fileName = $reflector->getFileName()) {
248 $vars['__file'] = $fileName;
249 $vars['__line'] = $reflector->getStartLine();
250 $vars['__dir'] = dirname($fileName);
254 $this->context->setCommandScopeVariables($vars);