annotate vendor/psy/psysh/src/Context.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;
Chris@0 13
Chris@0 14 /**
Chris@0 15 * The Shell execution context.
Chris@0 16 *
Chris@0 17 * This class encapsulates the current variables, most recent return value and
Chris@0 18 * exception, and the current namespace.
Chris@0 19 */
Chris@0 20 class Context
Chris@0 21 {
Chris@0 22 private static $specialNames = ['_', '_e', '__out', '__psysh__', 'this'];
Chris@0 23
Chris@0 24 // Whitelist a very limited number of command-scope magic variable names.
Chris@0 25 // This might be a bad idea, but future me can sort it out.
Chris@0 26 private static $commandScopeNames = [
Chris@0 27 '__function', '__method', '__class', '__namespace', '__file', '__line', '__dir',
Chris@0 28 ];
Chris@0 29
Chris@0 30 private $scopeVariables = [];
Chris@0 31 private $commandScopeVariables = [];
Chris@0 32 private $returnValue;
Chris@0 33 private $lastException;
Chris@0 34 private $lastStdout;
Chris@0 35 private $boundObject;
Chris@0 36 private $boundClass;
Chris@0 37
Chris@0 38 /**
Chris@0 39 * Get a context variable.
Chris@0 40 *
Chris@0 41 * @throws InvalidArgumentException If the variable is not found in the current context
Chris@0 42 *
Chris@0 43 * @param string $name
Chris@0 44 *
Chris@0 45 * @return mixed
Chris@0 46 */
Chris@0 47 public function get($name)
Chris@0 48 {
Chris@0 49 switch ($name) {
Chris@0 50 case '_':
Chris@0 51 return $this->returnValue;
Chris@0 52
Chris@0 53 case '_e':
Chris@0 54 if (isset($this->lastException)) {
Chris@0 55 return $this->lastException;
Chris@0 56 }
Chris@0 57 break;
Chris@0 58
Chris@0 59 case '__out':
Chris@0 60 if (isset($this->lastStdout)) {
Chris@0 61 return $this->lastStdout;
Chris@0 62 }
Chris@0 63 break;
Chris@0 64
Chris@0 65 case 'this':
Chris@0 66 if (isset($this->boundObject)) {
Chris@0 67 return $this->boundObject;
Chris@0 68 }
Chris@0 69 break;
Chris@0 70
Chris@0 71 case '__function':
Chris@0 72 case '__method':
Chris@0 73 case '__class':
Chris@0 74 case '__namespace':
Chris@0 75 case '__file':
Chris@0 76 case '__line':
Chris@0 77 case '__dir':
Chris@4 78 if (\array_key_exists($name, $this->commandScopeVariables)) {
Chris@0 79 return $this->commandScopeVariables[$name];
Chris@0 80 }
Chris@0 81 break;
Chris@0 82
Chris@0 83 default:
Chris@4 84 if (\array_key_exists($name, $this->scopeVariables)) {
Chris@0 85 return $this->scopeVariables[$name];
Chris@0 86 }
Chris@0 87 break;
Chris@0 88 }
Chris@0 89
Chris@0 90 throw new \InvalidArgumentException('Unknown variable: $' . $name);
Chris@0 91 }
Chris@0 92
Chris@0 93 /**
Chris@0 94 * Get all defined variables.
Chris@0 95 *
Chris@0 96 * @return array
Chris@0 97 */
Chris@0 98 public function getAll()
Chris@0 99 {
Chris@4 100 return \array_merge($this->scopeVariables, $this->getSpecialVariables());
Chris@0 101 }
Chris@0 102
Chris@0 103 /**
Chris@0 104 * Get all defined magic variables: $_, $_e, $__out, $__class, $__file, etc.
Chris@0 105 *
Chris@0 106 * @return array
Chris@0 107 */
Chris@0 108 public function getSpecialVariables()
Chris@0 109 {
Chris@0 110 $vars = [
Chris@0 111 '_' => $this->returnValue,
Chris@0 112 ];
Chris@0 113
Chris@0 114 if (isset($this->lastException)) {
Chris@0 115 $vars['_e'] = $this->lastException;
Chris@0 116 }
Chris@0 117
Chris@0 118 if (isset($this->lastStdout)) {
Chris@0 119 $vars['__out'] = $this->lastStdout;
Chris@0 120 }
Chris@0 121
Chris@0 122 if (isset($this->boundObject)) {
Chris@0 123 $vars['this'] = $this->boundObject;
Chris@0 124 }
Chris@0 125
Chris@4 126 return \array_merge($vars, $this->commandScopeVariables);
Chris@0 127 }
Chris@0 128
Chris@0 129 /**
Chris@0 130 * Set all scope variables.
Chris@0 131 *
Chris@0 132 * This method does *not* set any of the magic variables: $_, $_e, $__out,
Chris@0 133 * $__class, $__file, etc.
Chris@0 134 *
Chris@0 135 * @param array $vars
Chris@0 136 */
Chris@0 137 public function setAll(array $vars)
Chris@0 138 {
Chris@0 139 foreach (self::$specialNames as $key) {
Chris@0 140 unset($vars[$key]);
Chris@0 141 }
Chris@0 142
Chris@0 143 foreach (self::$commandScopeNames as $key) {
Chris@0 144 unset($vars[$key]);
Chris@0 145 }
Chris@0 146
Chris@0 147 $this->scopeVariables = $vars;
Chris@0 148 }
Chris@0 149
Chris@0 150 /**
Chris@0 151 * Set the most recent return value.
Chris@0 152 *
Chris@0 153 * @param mixed $value
Chris@0 154 */
Chris@0 155 public function setReturnValue($value)
Chris@0 156 {
Chris@0 157 $this->returnValue = $value;
Chris@0 158 }
Chris@0 159
Chris@0 160 /**
Chris@0 161 * Get the most recent return value.
Chris@0 162 *
Chris@0 163 * @return mixed
Chris@0 164 */
Chris@0 165 public function getReturnValue()
Chris@0 166 {
Chris@0 167 return $this->returnValue;
Chris@0 168 }
Chris@0 169
Chris@0 170 /**
Chris@0 171 * Set the most recent Exception.
Chris@0 172 *
Chris@0 173 * @param \Exception $e
Chris@0 174 */
Chris@0 175 public function setLastException(\Exception $e)
Chris@0 176 {
Chris@0 177 $this->lastException = $e;
Chris@0 178 }
Chris@0 179
Chris@0 180 /**
Chris@0 181 * Get the most recent Exception.
Chris@0 182 *
Chris@0 183 * @throws \InvalidArgumentException If no Exception has been caught
Chris@0 184 *
Chris@0 185 * @return null|\Exception
Chris@0 186 */
Chris@0 187 public function getLastException()
Chris@0 188 {
Chris@0 189 if (!isset($this->lastException)) {
Chris@0 190 throw new \InvalidArgumentException('No most-recent exception');
Chris@0 191 }
Chris@0 192
Chris@0 193 return $this->lastException;
Chris@0 194 }
Chris@0 195
Chris@0 196 /**
Chris@0 197 * Set the most recent output from evaluated code.
Chris@0 198 *
Chris@0 199 * @param string $lastStdout
Chris@0 200 */
Chris@0 201 public function setLastStdout($lastStdout)
Chris@0 202 {
Chris@0 203 $this->lastStdout = $lastStdout;
Chris@0 204 }
Chris@0 205
Chris@0 206 /**
Chris@0 207 * Get the most recent output from evaluated code.
Chris@0 208 *
Chris@0 209 * @throws \InvalidArgumentException If no output has happened yet
Chris@0 210 *
Chris@0 211 * @return null|string
Chris@0 212 */
Chris@0 213 public function getLastStdout()
Chris@0 214 {
Chris@0 215 if (!isset($this->lastStdout)) {
Chris@0 216 throw new \InvalidArgumentException('No most-recent output');
Chris@0 217 }
Chris@0 218
Chris@0 219 return $this->lastStdout;
Chris@0 220 }
Chris@0 221
Chris@0 222 /**
Chris@0 223 * Set the bound object ($this variable) for the interactive shell.
Chris@0 224 *
Chris@0 225 * Note that this unsets the bound class, if any exists.
Chris@0 226 *
Chris@0 227 * @param object|null $boundObject
Chris@0 228 */
Chris@0 229 public function setBoundObject($boundObject)
Chris@0 230 {
Chris@4 231 $this->boundObject = \is_object($boundObject) ? $boundObject : null;
Chris@0 232 $this->boundClass = null;
Chris@0 233 }
Chris@0 234
Chris@0 235 /**
Chris@0 236 * Get the bound object ($this variable) for the interactive shell.
Chris@0 237 *
Chris@0 238 * @return object|null
Chris@0 239 */
Chris@0 240 public function getBoundObject()
Chris@0 241 {
Chris@0 242 return $this->boundObject;
Chris@0 243 }
Chris@0 244
Chris@0 245 /**
Chris@0 246 * Set the bound class (self) for the interactive shell.
Chris@0 247 *
Chris@0 248 * Note that this unsets the bound object, if any exists.
Chris@0 249 *
Chris@0 250 * @param string|null $boundClass
Chris@0 251 */
Chris@0 252 public function setBoundClass($boundClass)
Chris@0 253 {
Chris@4 254 $this->boundClass = (\is_string($boundClass) && $boundClass !== '') ? $boundClass : null;
Chris@0 255 $this->boundObject = null;
Chris@0 256 }
Chris@0 257
Chris@0 258 /**
Chris@0 259 * Get the bound class (self) for the interactive shell.
Chris@0 260 *
Chris@0 261 * @return string|null
Chris@0 262 */
Chris@0 263 public function getBoundClass()
Chris@0 264 {
Chris@0 265 return $this->boundClass;
Chris@0 266 }
Chris@0 267
Chris@0 268 /**
Chris@0 269 * Set command-scope magic variables: $__class, $__file, etc.
Chris@0 270 *
Chris@0 271 * @param array $commandScopeVariables
Chris@0 272 */
Chris@0 273 public function setCommandScopeVariables(array $commandScopeVariables)
Chris@0 274 {
Chris@0 275 $vars = [];
Chris@0 276 foreach ($commandScopeVariables as $key => $value) {
Chris@0 277 // kind of type check
Chris@4 278 if (\is_scalar($value) && \in_array($key, self::$commandScopeNames)) {
Chris@0 279 $vars[$key] = $value;
Chris@0 280 }
Chris@0 281 }
Chris@0 282
Chris@0 283 $this->commandScopeVariables = $vars;
Chris@0 284 }
Chris@0 285
Chris@0 286 /**
Chris@0 287 * Get command-scope magic variables: $__class, $__file, etc.
Chris@0 288 *
Chris@0 289 * @return array
Chris@0 290 */
Chris@0 291 public function getCommandScopeVariables()
Chris@0 292 {
Chris@0 293 return $this->commandScopeVariables;
Chris@0 294 }
Chris@0 295
Chris@0 296 /**
Chris@0 297 * Get unused command-scope magic variables names: __class, __file, etc.
Chris@0 298 *
Chris@0 299 * This is used by the shell to unset old command-scope variables after a
Chris@0 300 * new batch is set.
Chris@0 301 *
Chris@0 302 * @return array Array of unused variable names
Chris@0 303 */
Chris@0 304 public function getUnusedCommandScopeVariableNames()
Chris@0 305 {
Chris@4 306 return \array_diff(self::$commandScopeNames, \array_keys($this->commandScopeVariables));
Chris@0 307 }
Chris@0 308
Chris@0 309 /**
Chris@0 310 * Check whether a variable name is a magic variable.
Chris@0 311 *
Chris@0 312 * @param string $name
Chris@0 313 *
Chris@0 314 * @return bool
Chris@0 315 */
Chris@0 316 public static function isSpecialVariableName($name)
Chris@0 317 {
Chris@4 318 return \in_array($name, self::$specialNames) || \in_array($name, self::$commandScopeNames);
Chris@0 319 }
Chris@0 320 }