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