annotate vendor/psy/psysh/src/Psy/Command/ReflectingCommand.php @ 12:7a779792577d

Update Drupal core to v8.4.5 (via Composer)
author Chris Cannam
date Fri, 23 Feb 2018 15:52:07 +0000
parents 4c8ae668cc8c
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-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 }
Chris@0 69
Chris@12 70 throw new RuntimeException('Unknown target: ' . $valueName);
Chris@0 71 case preg_match(self::CLASS_OR_FUNC, $valueName, $matches):
Chris@0 72 return array($this->resolveName($matches[0], true), null, 0);
Chris@0 73
Chris@0 74 case preg_match(self::INSTANCE, $valueName, $matches):
Chris@0 75 return array($this->resolveInstance($matches[1]), null, 0);
Chris@0 76
Chris@0 77 case !$classOnly && preg_match(self::CLASS_MEMBER, $valueName, $matches):
Chris@0 78 return array($this->resolveName($matches[1]), $matches[2], Mirror::CONSTANT | Mirror::METHOD);
Chris@0 79
Chris@0 80 case !$classOnly && preg_match(self::CLASS_STATIC, $valueName, $matches):
Chris@0 81 return array($this->resolveName($matches[1]), $matches[2], Mirror::STATIC_PROPERTY | Mirror::PROPERTY);
Chris@0 82
Chris@0 83 case !$classOnly && preg_match(self::INSTANCE_MEMBER, $valueName, $matches):
Chris@0 84 if ($matches[2] === '->') {
Chris@0 85 $kind = Mirror::METHOD | Mirror::PROPERTY;
Chris@0 86 } else {
Chris@0 87 $kind = Mirror::CONSTANT | Mirror::METHOD;
Chris@0 88 }
Chris@0 89
Chris@0 90 return array($this->resolveInstance($matches[1]), $matches[3], $kind);
Chris@0 91
Chris@0 92 case !$classOnly && preg_match(self::INSTANCE_STATIC, $valueName, $matches):
Chris@0 93 return array($this->resolveInstance($matches[1]), $matches[2], Mirror::STATIC_PROPERTY);
Chris@0 94
Chris@0 95 default:
Chris@0 96 throw new RuntimeException('Unknown target: ' . $valueName);
Chris@0 97 }
Chris@0 98 }
Chris@0 99
Chris@0 100 /**
Chris@0 101 * Resolve a class or function name (with the current shell namespace).
Chris@0 102 *
Chris@0 103 * @param string $name
Chris@0 104 * @param bool $includeFunctions (default: false)
Chris@0 105 *
Chris@0 106 * @return string
Chris@0 107 */
Chris@0 108 protected function resolveName($name, $includeFunctions = false)
Chris@0 109 {
Chris@0 110 if (substr($name, 0, 1) === '\\') {
Chris@0 111 return $name;
Chris@0 112 }
Chris@0 113
Chris@0 114 if ($namespace = $this->getApplication()->getNamespace()) {
Chris@0 115 $fullName = $namespace . '\\' . $name;
Chris@0 116
Chris@0 117 if (class_exists($fullName) || interface_exists($fullName) || ($includeFunctions && function_exists($fullName))) {
Chris@0 118 return $fullName;
Chris@0 119 }
Chris@0 120 }
Chris@0 121
Chris@0 122 return $name;
Chris@0 123 }
Chris@0 124
Chris@0 125 /**
Chris@0 126 * Get a Reflector and documentation for a function, class or instance, constant, method or property.
Chris@0 127 *
Chris@0 128 * @param string $valueName Function, class, variable, constant, method or property name
Chris@0 129 * @param bool $classOnly True if the name should only refer to a class, function or instance
Chris@0 130 *
Chris@0 131 * @return array (value, Reflector)
Chris@0 132 */
Chris@0 133 protected function getTargetAndReflector($valueName, $classOnly = false)
Chris@0 134 {
Chris@0 135 list($value, $member, $kind) = $this->getTarget($valueName, $classOnly);
Chris@0 136
Chris@0 137 return array($value, Mirror::get($value, $member, $kind));
Chris@0 138 }
Chris@0 139
Chris@0 140 /**
Chris@0 141 * Return a variable instance from the current scope.
Chris@0 142 *
Chris@0 143 * @throws \InvalidArgumentException when the requested variable does not exist in the current scope
Chris@0 144 *
Chris@0 145 * @param string $name
Chris@0 146 *
Chris@0 147 * @return mixed Variable instance
Chris@0 148 */
Chris@0 149 protected function resolveInstance($name)
Chris@0 150 {
Chris@0 151 $value = $this->getScopeVariable($name);
Chris@0 152 if (!is_object($value)) {
Chris@0 153 throw new RuntimeException('Unable to inspect a non-object');
Chris@0 154 }
Chris@0 155
Chris@0 156 return $value;
Chris@0 157 }
Chris@0 158
Chris@0 159 /**
Chris@0 160 * Get a variable from the current shell scope.
Chris@0 161 *
Chris@0 162 * @param string $name
Chris@0 163 *
Chris@0 164 * @return mixed
Chris@0 165 */
Chris@0 166 protected function getScopeVariable($name)
Chris@0 167 {
Chris@0 168 return $this->context->get($name);
Chris@0 169 }
Chris@0 170
Chris@0 171 /**
Chris@0 172 * Get all scope variables from the current shell scope.
Chris@0 173 *
Chris@0 174 * @return array
Chris@0 175 */
Chris@0 176 protected function getScopeVariables()
Chris@0 177 {
Chris@0 178 return $this->context->getAll();
Chris@0 179 }
Chris@0 180
Chris@0 181 /**
Chris@0 182 * Given a Reflector instance, set command-scope variables in the shell
Chris@0 183 * execution context. This is used to inject magic $__class, $__method and
Chris@0 184 * $__file variables (as well as a handful of others).
Chris@0 185 *
Chris@0 186 * @param \Reflector $reflector
Chris@0 187 */
Chris@0 188 protected function setCommandScopeVariables(\Reflector $reflector)
Chris@0 189 {
Chris@0 190 $vars = array();
Chris@0 191
Chris@0 192 switch (get_class($reflector)) {
Chris@0 193 case 'ReflectionClass':
Chris@0 194 case 'ReflectionObject':
Chris@0 195 $vars['__class'] = $reflector->name;
Chris@0 196 if ($reflector->inNamespace()) {
Chris@0 197 $vars['__namespace'] = $reflector->getNamespaceName();
Chris@0 198 }
Chris@0 199 break;
Chris@0 200
Chris@0 201 case 'ReflectionMethod':
Chris@0 202 $vars['__method'] = sprintf('%s::%s', $reflector->class, $reflector->name);
Chris@0 203 $vars['__class'] = $reflector->class;
Chris@0 204 $classReflector = $reflector->getDeclaringClass();
Chris@0 205 if ($classReflector->inNamespace()) {
Chris@0 206 $vars['__namespace'] = $classReflector->getNamespaceName();
Chris@0 207 }
Chris@0 208 break;
Chris@0 209
Chris@0 210 case 'ReflectionFunction':
Chris@0 211 $vars['__function'] = $reflector->name;
Chris@0 212 if ($reflector->inNamespace()) {
Chris@0 213 $vars['__namespace'] = $reflector->getNamespaceName();
Chris@0 214 }
Chris@0 215 break;
Chris@0 216
Chris@0 217 case 'ReflectionGenerator':
Chris@0 218 $funcReflector = $reflector->getFunction();
Chris@0 219 $vars['__function'] = $funcReflector->name;
Chris@0 220 if ($funcReflector->inNamespace()) {
Chris@0 221 $vars['__namespace'] = $funcReflector->getNamespaceName();
Chris@0 222 }
Chris@0 223 if ($fileName = $reflector->getExecutingFile()) {
Chris@0 224 $vars['__file'] = $fileName;
Chris@0 225 $vars['__line'] = $reflector->getExecutingLine();
Chris@0 226 $vars['__dir'] = dirname($fileName);
Chris@0 227 }
Chris@0 228 break;
Chris@0 229
Chris@0 230 case 'ReflectionProperty':
Chris@0 231 case 'Psy\Reflection\ReflectionConstant':
Chris@0 232 $classReflector = $reflector->getDeclaringClass();
Chris@0 233 $vars['__class'] = $classReflector->name;
Chris@0 234 if ($classReflector->inNamespace()) {
Chris@0 235 $vars['__namespace'] = $classReflector->getNamespaceName();
Chris@0 236 }
Chris@0 237 // no line for these, but this'll do
Chris@0 238 if ($fileName = $reflector->getDeclaringClass()->getFileName()) {
Chris@0 239 $vars['__file'] = $fileName;
Chris@0 240 $vars['__dir'] = dirname($fileName);
Chris@0 241 }
Chris@0 242 break;
Chris@0 243 }
Chris@0 244
Chris@0 245 if ($reflector instanceof \ReflectionClass || $reflector instanceof \ReflectionFunctionAbstract) {
Chris@0 246 if ($fileName = $reflector->getFileName()) {
Chris@0 247 $vars['__file'] = $fileName;
Chris@0 248 $vars['__line'] = $reflector->getStartLine();
Chris@0 249 $vars['__dir'] = dirname($fileName);
Chris@0 250 }
Chris@0 251 }
Chris@0 252
Chris@0 253 $this->context->setCommandScopeVariables($vars);
Chris@0 254 }
Chris@0 255 }