Chris@13: returnValue; Chris@13: Chris@13: case '_e': Chris@13: if (isset($this->lastException)) { Chris@13: return $this->lastException; Chris@13: } Chris@13: break; Chris@13: Chris@13: case '__out': Chris@13: if (isset($this->lastStdout)) { Chris@13: return $this->lastStdout; Chris@13: } Chris@13: break; Chris@13: Chris@13: case 'this': Chris@13: if (isset($this->boundObject)) { Chris@13: return $this->boundObject; Chris@13: } Chris@13: break; Chris@13: Chris@13: case '__function': Chris@13: case '__method': Chris@13: case '__class': Chris@13: case '__namespace': Chris@13: case '__file': Chris@13: case '__line': Chris@13: case '__dir': Chris@17: if (\array_key_exists($name, $this->commandScopeVariables)) { Chris@13: return $this->commandScopeVariables[$name]; Chris@13: } Chris@13: break; Chris@13: Chris@13: default: Chris@17: if (\array_key_exists($name, $this->scopeVariables)) { Chris@13: return $this->scopeVariables[$name]; Chris@13: } Chris@13: break; Chris@13: } Chris@13: Chris@13: throw new \InvalidArgumentException('Unknown variable: $' . $name); Chris@13: } Chris@13: Chris@13: /** Chris@13: * Get all defined variables. Chris@13: * Chris@13: * @return array Chris@13: */ Chris@13: public function getAll() Chris@13: { Chris@17: return \array_merge($this->scopeVariables, $this->getSpecialVariables()); Chris@13: } Chris@13: Chris@13: /** Chris@13: * Get all defined magic variables: $_, $_e, $__out, $__class, $__file, etc. Chris@13: * Chris@13: * @return array Chris@13: */ Chris@13: public function getSpecialVariables() Chris@13: { Chris@13: $vars = [ Chris@13: '_' => $this->returnValue, Chris@13: ]; Chris@13: Chris@13: if (isset($this->lastException)) { Chris@13: $vars['_e'] = $this->lastException; Chris@13: } Chris@13: Chris@13: if (isset($this->lastStdout)) { Chris@13: $vars['__out'] = $this->lastStdout; Chris@13: } Chris@13: Chris@13: if (isset($this->boundObject)) { Chris@13: $vars['this'] = $this->boundObject; Chris@13: } Chris@13: Chris@17: return \array_merge($vars, $this->commandScopeVariables); Chris@13: } Chris@13: Chris@13: /** Chris@13: * Set all scope variables. Chris@13: * Chris@13: * This method does *not* set any of the magic variables: $_, $_e, $__out, Chris@13: * $__class, $__file, etc. Chris@13: * Chris@13: * @param array $vars Chris@13: */ Chris@13: public function setAll(array $vars) Chris@13: { Chris@13: foreach (self::$specialNames as $key) { Chris@13: unset($vars[$key]); Chris@13: } Chris@13: Chris@13: foreach (self::$commandScopeNames as $key) { Chris@13: unset($vars[$key]); Chris@13: } Chris@13: Chris@13: $this->scopeVariables = $vars; Chris@13: } Chris@13: Chris@13: /** Chris@13: * Set the most recent return value. Chris@13: * Chris@13: * @param mixed $value Chris@13: */ Chris@13: public function setReturnValue($value) Chris@13: { Chris@13: $this->returnValue = $value; Chris@13: } Chris@13: Chris@13: /** Chris@13: * Get the most recent return value. Chris@13: * Chris@13: * @return mixed Chris@13: */ Chris@13: public function getReturnValue() Chris@13: { Chris@13: return $this->returnValue; Chris@13: } Chris@13: Chris@13: /** Chris@13: * Set the most recent Exception. Chris@13: * Chris@13: * @param \Exception $e Chris@13: */ Chris@13: public function setLastException(\Exception $e) Chris@13: { Chris@13: $this->lastException = $e; Chris@13: } Chris@13: Chris@13: /** Chris@13: * Get the most recent Exception. Chris@13: * Chris@13: * @throws \InvalidArgumentException If no Exception has been caught Chris@13: * Chris@13: * @return null|\Exception Chris@13: */ Chris@13: public function getLastException() Chris@13: { Chris@13: if (!isset($this->lastException)) { Chris@13: throw new \InvalidArgumentException('No most-recent exception'); Chris@13: } Chris@13: Chris@13: return $this->lastException; Chris@13: } Chris@13: Chris@13: /** Chris@13: * Set the most recent output from evaluated code. Chris@13: * Chris@13: * @param string $lastStdout Chris@13: */ Chris@13: public function setLastStdout($lastStdout) Chris@13: { Chris@13: $this->lastStdout = $lastStdout; Chris@13: } Chris@13: Chris@13: /** Chris@13: * Get the most recent output from evaluated code. Chris@13: * Chris@13: * @throws \InvalidArgumentException If no output has happened yet Chris@13: * Chris@13: * @return null|string Chris@13: */ Chris@13: public function getLastStdout() Chris@13: { Chris@13: if (!isset($this->lastStdout)) { Chris@13: throw new \InvalidArgumentException('No most-recent output'); Chris@13: } Chris@13: Chris@13: return $this->lastStdout; Chris@13: } Chris@13: Chris@13: /** Chris@13: * Set the bound object ($this variable) for the interactive shell. Chris@13: * Chris@16: * Note that this unsets the bound class, if any exists. Chris@16: * Chris@13: * @param object|null $boundObject Chris@13: */ Chris@13: public function setBoundObject($boundObject) Chris@13: { Chris@17: $this->boundObject = \is_object($boundObject) ? $boundObject : null; Chris@16: $this->boundClass = null; Chris@13: } Chris@13: Chris@13: /** Chris@13: * Get the bound object ($this variable) for the interactive shell. Chris@13: * Chris@13: * @return object|null Chris@13: */ Chris@13: public function getBoundObject() Chris@13: { Chris@13: return $this->boundObject; Chris@13: } Chris@13: Chris@13: /** Chris@16: * Set the bound class (self) for the interactive shell. Chris@16: * Chris@16: * Note that this unsets the bound object, if any exists. Chris@16: * Chris@16: * @param string|null $boundClass Chris@16: */ Chris@16: public function setBoundClass($boundClass) Chris@16: { Chris@17: $this->boundClass = (\is_string($boundClass) && $boundClass !== '') ? $boundClass : null; Chris@16: $this->boundObject = null; Chris@16: } Chris@16: Chris@16: /** Chris@16: * Get the bound class (self) for the interactive shell. Chris@16: * Chris@16: * @return string|null Chris@16: */ Chris@16: public function getBoundClass() Chris@16: { Chris@16: return $this->boundClass; Chris@16: } Chris@16: Chris@16: /** Chris@13: * Set command-scope magic variables: $__class, $__file, etc. Chris@13: * Chris@13: * @param array $commandScopeVariables Chris@13: */ Chris@13: public function setCommandScopeVariables(array $commandScopeVariables) Chris@13: { Chris@13: $vars = []; Chris@13: foreach ($commandScopeVariables as $key => $value) { Chris@13: // kind of type check Chris@17: if (\is_scalar($value) && \in_array($key, self::$commandScopeNames)) { Chris@13: $vars[$key] = $value; Chris@13: } Chris@13: } Chris@13: Chris@13: $this->commandScopeVariables = $vars; Chris@13: } Chris@13: Chris@13: /** Chris@13: * Get command-scope magic variables: $__class, $__file, etc. Chris@13: * Chris@13: * @return array Chris@13: */ Chris@13: public function getCommandScopeVariables() Chris@13: { Chris@13: return $this->commandScopeVariables; Chris@13: } Chris@13: Chris@13: /** Chris@13: * Get unused command-scope magic variables names: __class, __file, etc. Chris@13: * Chris@13: * This is used by the shell to unset old command-scope variables after a Chris@13: * new batch is set. Chris@13: * Chris@13: * @return array Array of unused variable names Chris@13: */ Chris@13: public function getUnusedCommandScopeVariableNames() Chris@13: { Chris@17: return \array_diff(self::$commandScopeNames, \array_keys($this->commandScopeVariables)); Chris@13: } Chris@13: Chris@13: /** Chris@13: * Check whether a variable name is a magic variable. Chris@13: * Chris@13: * @param string $name Chris@13: * Chris@13: * @return bool Chris@13: */ Chris@13: public static function isSpecialVariableName($name) Chris@13: { Chris@17: return \in_array($name, self::$specialNames) || \in_array($name, self::$commandScopeNames); Chris@13: } Chris@13: }