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