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