4 * This file is part of Psy Shell.
6 * (c) 2012-2017 Justin Hileman
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
15 * The Shell execution context.
17 * This class encapsulates the current variables, most recent return value and
18 * exception, and the current namespace.
22 private static $specialNames = array('_', '_e', '__psysh__', 'this');
24 // Whitelist a very limited number of command-scope magic variable names.
25 // This might be a bad idea, but future me can sort it out.
26 private static $commandScopeNames = array(
27 '__function', '__method', '__class', '__namespace', '__file', '__line', '__dir',
30 private $scopeVariables = array();
31 private $commandScopeVariables = array();
32 private $lastException;
37 * Get a context variable.
39 * @throws InvalidArgumentException If the variable is not found in the current context
45 public function get($name)
49 return $this->returnValue;
52 if (isset($this->lastException)) {
53 return $this->lastException;
58 if (isset($this->boundObject)) {
59 return $this->boundObject;
70 if (array_key_exists($name, $this->commandScopeVariables)) {
71 return $this->commandScopeVariables[$name];
76 if (array_key_exists($name, $this->scopeVariables)) {
77 return $this->scopeVariables[$name];
82 throw new \InvalidArgumentException('Unknown variable: $' . $name);
86 * Get all defined variables.
90 public function getAll()
92 return array_merge($this->scopeVariables, $this->getSpecialVariables());
96 * Get all defined magic variables: $_, $_e, $__class, $__file, etc.
100 public function getSpecialVariables()
103 '_' => $this->returnValue,
106 if (isset($this->lastException)) {
107 $vars['_e'] = $this->lastException;
110 if (isset($this->boundObject)) {
111 $vars['this'] = $this->boundObject;
114 return array_merge($vars, $this->commandScopeVariables);
118 * Set all scope variables.
120 * This method does *not* set any of the magic variables: $_, $_e, $__class, $__file, etc.
124 public function setAll(array $vars)
126 foreach (self::$specialNames as $key) {
130 foreach (self::$commandScopeNames as $key) {
134 $this->scopeVariables = $vars;
138 * Set the most recent return value.
140 * @param mixed $value
142 public function setReturnValue($value)
144 $this->returnValue = $value;
148 * Get the most recent return value.
152 public function getReturnValue()
154 return $this->returnValue;
158 * Set the most recent Exception.
160 * @param \Exception $e
162 public function setLastException(\Exception $e)
164 $this->lastException = $e;
168 * Get the most recent Exception.
170 * @throws InvalidArgumentException If no Exception has been caught
172 * @return null|Exception
174 public function getLastException()
176 if (!isset($this->lastException)) {
177 throw new \InvalidArgumentException('No most-recent exception');
180 return $this->lastException;
184 * Set the bound object ($this variable) for the interactive shell.
186 * @param object|null $boundObject
188 public function setBoundObject($boundObject)
190 $this->boundObject = is_object($boundObject) ? $boundObject : null;
194 * Get the bound object ($this variable) for the interactive shell.
196 * @return object|null
198 public function getBoundObject()
200 return $this->boundObject;
204 * Set command-scope magic variables: $__class, $__file, etc.
206 * @param array $commandScopeVariables
208 public function setCommandScopeVariables(array $commandScopeVariables)
211 foreach ($commandScopeVariables as $key => $value) {
212 // kind of type check
213 if (is_scalar($value) && in_array($key, self::$commandScopeNames)) {
214 $vars[$key] = $value;
218 $this->commandScopeVariables = $vars;
222 * Get command-scope magic variables: $__class, $__file, etc.
226 public function getCommandScopeVariables()
228 return $this->commandScopeVariables;
232 * Get unused command-scope magic variables names: __class, __file, etc.
234 * This is used by the shell to unset old command-scope variables after a
237 * @return array Array of unused variable names
239 public function getUnusedCommandScopeVariableNames()
241 return array_diff(self::$commandScopeNames, array_keys($this->commandScopeVariables));
245 * Check whether a variable name is a magic variable.
247 * @param string $name
251 public static function isSpecialVariableName($name)
253 return in_array($name, self::$specialNames) || in_array($name, self::$commandScopeNames);