Updated to Drupal 8.5. Core Media not yet in use.
[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\BreakException;
15 use Psy\Exception\ErrorException;
16 use Psy\Exception\ThrowUpException;
17 use Psy\Exception\TypeErrorException;
18
19 /**
20  * The Psy Shell execution loop.
21  */
22 class ExecutionLoop
23 {
24     /**
25      * Run the execution loop.
26      *
27      * @throws ThrowUpException if thrown by the `throw-up` command
28      *
29      * @param Shell $shell
30      */
31     public function run(Shell $shell)
32     {
33         $this->loadIncludes($shell);
34
35         $closure = new ExecutionClosure($shell);
36
37         do {
38             $shell->beforeLoop();
39
40             try {
41                 $shell->getInput();
42                 $_ = $closure->execute();
43                 $shell->writeReturnValue($_);
44             } catch (BreakException $_e) {
45                 $shell->writeException($_e);
46
47                 return;
48             } catch (ThrowUpException $_e) {
49                 $shell->writeException($_e);
50
51                 throw $_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);
58             }
59
60             $shell->afterLoop();
61         } while (true);
62     }
63
64     /**
65      * Load user-defined includes.
66      *
67      * @param Shell $shell
68      */
69     protected function loadIncludes(Shell $shell)
70     {
71         // Load user-defined includes
72         $load = function (Shell $__psysh__) {
73             set_error_handler([$__psysh__, 'handleError']);
74             foreach ($__psysh__->getIncludes() as $__psysh_include__) {
75                 try {
76                     include $__psysh_include__;
77                 } catch (\Error $_e) {
78                     $__psysh__->writeException(ErrorException::fromError($_e));
79                 } catch (\Exception $_e) {
80                     $__psysh__->writeException($_e);
81                 }
82             }
83             restore_error_handler();
84             unset($__psysh_include__);
85
86             // Override any new local variables with pre-defined scope variables
87             extract($__psysh__->getScopeVariables(false));
88
89             // ... then add the whole mess of variables back.
90             $__psysh__->setScopeVariables(get_defined_vars());
91         };
92
93         $load($shell);
94     }
95 }