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