Updated from some -dev modules to alpha, beta or full releases
[yaffs-website] / vendor / psy / psysh / src / ExecutionLoop.php
1 <?php
2
3 /*
4  * This file is part of Psy Shell.
5  *
6  * (c) 2012-2018 Justin Hileman
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Psy;
13
14 use Psy\Exception\ErrorException;
15
16 /**
17  * The Psy Shell execution loop.
18  */
19 class ExecutionLoop
20 {
21     /**
22      * Run the execution loop.
23      *
24      * @throws ThrowUpException if thrown by the `throw-up` command
25      *
26      * @param Shell $shell
27      */
28     public function run(Shell $shell)
29     {
30         $this->loadIncludes($shell);
31
32         $closure = new ExecutionLoopClosure($shell);
33         $closure->execute();
34     }
35
36     /**
37      * Load user-defined includes.
38      *
39      * @param Shell $shell
40      */
41     protected function loadIncludes(Shell $shell)
42     {
43         // Load user-defined includes
44         $load = function (Shell $__psysh__) {
45             set_error_handler([$__psysh__, 'handleError']);
46             foreach ($__psysh__->getIncludes() as $__psysh_include__) {
47                 try {
48                     include $__psysh_include__;
49                 } catch (\Error $_e) {
50                     $__psysh__->writeException(ErrorException::fromError($_e));
51                 } catch (\Exception $_e) {
52                     $__psysh__->writeException($_e);
53                 }
54             }
55             restore_error_handler();
56             unset($__psysh_include__);
57
58             // Override any new local variables with pre-defined scope variables
59             extract($__psysh__->getScopeVariables(false));
60
61             // ... then add the whole mess of variables back.
62             $__psysh__->setScopeVariables(get_defined_vars());
63         };
64
65         $load($shell);
66     }
67 }