annotate vendor/psy/psysh/src/Psy/Command/ReflectingCommand.php @ 0:4c8ae668cc8c

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