4 * This file is part of Psy Shell.
6 * (c) 2012-2018 Justin Hileman
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
14 use Psy\Exception\BreakException;
15 use Psy\Exception\ErrorException;
16 use Psy\Exception\ThrowUpException;
17 use Psy\Exception\TypeErrorException;
20 * The Psy Shell execution loop.
25 * Run the execution loop.
27 * @throws ThrowUpException if thrown by the `throw-up` command
31 public function run(Shell $shell)
33 $this->loadIncludes($shell);
35 $closure = new ExecutionClosure($shell);
42 $_ = $closure->execute();
43 $shell->writeReturnValue($_);
44 } catch (BreakException $_e) {
45 $shell->writeException($_e);
48 } catch (ThrowUpException $_e) {
49 $shell->writeException($_e);
52 } catch (\TypeError $_e) {
53 $shell->writeException(TypeErrorException::fromTypeError($_e));
54 } catch (\Error $_e) {
55 $shell->writeException(ErrorException::fromError($_e));
56 } catch (\Exception $_e) {
57 $shell->writeException($_e);
65 * Load user-defined includes.
69 protected function loadIncludes(Shell $shell)
71 // Load user-defined includes
72 $load = function (Shell $__psysh__) {
73 set_error_handler([$__psysh__, 'handleError']);
74 foreach ($__psysh__->getIncludes() as $__psysh_include__) {
76 include $__psysh_include__;
77 } catch (\Error $_e) {
78 $__psysh__->writeException(ErrorException::fromError($_e));
79 } catch (\Exception $_e) {
80 $__psysh__->writeException($_e);
83 restore_error_handler();
84 unset($__psysh_include__);
86 // Override any new local variables with pre-defined scope variables
87 extract($__psysh__->getScopeVariables(false));
89 // ... then add the whole mess of variables back.
90 $__psysh__->setScopeVariables(get_defined_vars());