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