1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\WebTinker; |
4
|
|
|
|
5
|
|
|
use Psy\Exception\BreakException; |
6
|
|
|
use Psy\Exception\ErrorException; |
7
|
|
|
use Psy\Exception\ThrowUpException; |
8
|
|
|
use Psy\Exception\TypeErrorException; |
9
|
|
|
use Psy\ExecutionClosure; |
10
|
|
|
use Psy\Shell; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* The custom execustion closure. |
14
|
|
|
* |
15
|
|
|
* This class is base on the Psy\ExecutionLoopClosure |
16
|
|
|
* Extracted the do while loop and removed the loop |
17
|
|
|
* To avoid infinite looping on execution |
18
|
|
|
*/ |
19
|
|
|
class CustomExecutionClosure extends ExecutionClosure |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @param Shell $__psysh__ |
23
|
|
|
*/ |
24
|
|
|
public function __construct(Shell $__psysh__) |
25
|
|
|
{ |
26
|
|
|
$this->setClosure($__psysh__, function () use ($__psysh__) { |
27
|
|
|
// Restore execution scope variables |
28
|
|
|
\extract($__psysh__->getScopeVariables(false)); |
|
|
|
|
29
|
|
|
|
30
|
|
|
try { |
31
|
|
|
$__psysh__->getInput(); |
32
|
|
|
|
33
|
|
|
try { |
34
|
|
|
// Pull in any new execution scope variables |
35
|
|
|
if ($__psysh__->getLastExecSuccess()) { |
36
|
|
|
\extract($__psysh__->getScopeVariablesDiff(\get_defined_vars())); |
|
|
|
|
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
// Buffer stdout; we'll need it later |
40
|
|
|
\ob_start([$__psysh__, 'writeStdout'], 1); |
41
|
|
|
|
42
|
|
|
// Convert all errors to exceptions |
43
|
|
|
\set_error_handler([$__psysh__, 'handleError']); |
44
|
|
|
|
45
|
|
|
// Evaluate the current code buffer |
46
|
|
|
$_ = eval($__psysh__->onExecute($__psysh__->flushCode() ?: ExecutionClosure::NOOP_INPUT)); |
47
|
|
|
} catch (\Throwable $_e) { |
48
|
|
|
// Clean up on our way out. |
49
|
|
|
if (\ob_get_level() > 0) { |
50
|
|
|
\ob_end_clean(); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
throw $_e; |
54
|
|
|
} catch (\Exception $_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 (\TypeError $_e) { |
82
|
|
|
$__psysh__->writeException(TypeErrorException::fromTypeError($_e)); |
83
|
|
|
} catch (\Error $_e) { |
84
|
|
|
$__psysh__->writeException(ErrorException::fromError($_e)); |
85
|
|
|
} catch (\Exception $_e) { |
86
|
|
|
$__psysh__->writeException($_e); |
87
|
|
|
} |
88
|
|
|
}); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|