Chris@16
|
1 <?php
|
Chris@16
|
2
|
Chris@16
|
3 /*
|
Chris@16
|
4 * This file is part of Psy Shell.
|
Chris@16
|
5 *
|
Chris@16
|
6 * (c) 2012-2018 Justin Hileman
|
Chris@16
|
7 *
|
Chris@16
|
8 * For the full copyright and license information, please view the LICENSE
|
Chris@16
|
9 * file that was distributed with this source code.
|
Chris@16
|
10 */
|
Chris@16
|
11
|
Chris@16
|
12 namespace Psy;
|
Chris@16
|
13
|
Chris@16
|
14 use Psy\Exception\BreakException;
|
Chris@16
|
15 use Psy\Exception\ErrorException;
|
Chris@16
|
16 use Psy\Exception\ThrowUpException;
|
Chris@16
|
17 use Psy\Exception\TypeErrorException;
|
Chris@16
|
18
|
Chris@16
|
19 /**
|
Chris@16
|
20 * The Psy Shell's execution loop scope.
|
Chris@16
|
21 *
|
Chris@16
|
22 * @todo Once we're on PHP 5.5, we can switch ExecutionClosure to a generator
|
Chris@16
|
23 * and get rid of the duplicate closure implementations :)
|
Chris@16
|
24 */
|
Chris@16
|
25 class ExecutionLoopClosure extends ExecutionClosure
|
Chris@16
|
26 {
|
Chris@16
|
27 /**
|
Chris@16
|
28 * @param Shell $__psysh__
|
Chris@16
|
29 */
|
Chris@16
|
30 public function __construct(Shell $__psysh__)
|
Chris@16
|
31 {
|
Chris@16
|
32 $this->setClosure($__psysh__, function () use ($__psysh__) {
|
Chris@16
|
33 // Restore execution scope variables
|
Chris@17
|
34 \extract($__psysh__->getScopeVariables(false));
|
Chris@16
|
35
|
Chris@16
|
36 do {
|
Chris@16
|
37 $__psysh__->beforeLoop();
|
Chris@16
|
38
|
Chris@16
|
39 try {
|
Chris@16
|
40 $__psysh__->getInput();
|
Chris@16
|
41
|
Chris@16
|
42 try {
|
Chris@17
|
43 // Pull in any new execution scope variables
|
Chris@17
|
44 if ($__psysh__->getLastExecSuccess()) {
|
Chris@17
|
45 \extract($__psysh__->getScopeVariablesDiff(\get_defined_vars()));
|
Chris@17
|
46 }
|
Chris@17
|
47
|
Chris@16
|
48 // Buffer stdout; we'll need it later
|
Chris@17
|
49 \ob_start([$__psysh__, 'writeStdout'], 1);
|
Chris@16
|
50
|
Chris@16
|
51 // Convert all errors to exceptions
|
Chris@17
|
52 \set_error_handler([$__psysh__, 'handleError']);
|
Chris@16
|
53
|
Chris@16
|
54 // Evaluate the current code buffer
|
Chris@16
|
55 $_ = eval($__psysh__->onExecute($__psysh__->flushCode() ?: ExecutionClosure::NOOP_INPUT));
|
Chris@16
|
56 } catch (\Throwable $_e) {
|
Chris@16
|
57 // Clean up on our way out.
|
Chris@17
|
58 \restore_error_handler();
|
Chris@17
|
59 if (\ob_get_level() > 0) {
|
Chris@17
|
60 \ob_end_clean();
|
Chris@16
|
61 }
|
Chris@16
|
62
|
Chris@16
|
63 throw $_e;
|
Chris@16
|
64 } catch (\Exception $_e) {
|
Chris@16
|
65 // Clean up on our way out.
|
Chris@17
|
66 \restore_error_handler();
|
Chris@17
|
67 if (\ob_get_level() > 0) {
|
Chris@17
|
68 \ob_end_clean();
|
Chris@16
|
69 }
|
Chris@16
|
70
|
Chris@16
|
71 throw $_e;
|
Chris@16
|
72 }
|
Chris@16
|
73
|
Chris@16
|
74 // Won't be needing this anymore
|
Chris@17
|
75 \restore_error_handler();
|
Chris@16
|
76
|
Chris@16
|
77 // Flush stdout (write to shell output, plus save to magic variable)
|
Chris@17
|
78 \ob_end_flush();
|
Chris@16
|
79
|
Chris@16
|
80 // Save execution scope variables for next time
|
Chris@17
|
81 $__psysh__->setScopeVariables(\get_defined_vars());
|
Chris@16
|
82
|
Chris@16
|
83 $__psysh__->writeReturnValue($_);
|
Chris@16
|
84 } catch (BreakException $_e) {
|
Chris@16
|
85 $__psysh__->writeException($_e);
|
Chris@16
|
86
|
Chris@16
|
87 return;
|
Chris@16
|
88 } catch (ThrowUpException $_e) {
|
Chris@16
|
89 $__psysh__->writeException($_e);
|
Chris@16
|
90
|
Chris@16
|
91 throw $_e;
|
Chris@16
|
92 } catch (\TypeError $_e) {
|
Chris@16
|
93 $__psysh__->writeException(TypeErrorException::fromTypeError($_e));
|
Chris@16
|
94 } catch (\Error $_e) {
|
Chris@16
|
95 $__psysh__->writeException(ErrorException::fromError($_e));
|
Chris@16
|
96 } catch (\Exception $_e) {
|
Chris@16
|
97 $__psysh__->writeException($_e);
|
Chris@16
|
98 }
|
Chris@16
|
99
|
Chris@16
|
100 $__psysh__->afterLoop();
|
Chris@16
|
101 } while (true);
|
Chris@16
|
102 });
|
Chris@16
|
103 }
|
Chris@16
|
104 }
|