annotate vendor/psy/psysh/src/Command/ReflectingCommand.php @ 4:a9cd425dd02b

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