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 /**
|
Chris@13
|
15 * The Psy Shell's execution scope.
|
Chris@13
|
16 */
|
Chris@13
|
17 class ExecutionClosure
|
Chris@13
|
18 {
|
Chris@13
|
19 const NOOP_INPUT = 'return null;';
|
Chris@13
|
20
|
Chris@13
|
21 private $closure;
|
Chris@13
|
22
|
Chris@13
|
23 /**
|
Chris@13
|
24 * @param Shell $__psysh__
|
Chris@13
|
25 */
|
Chris@13
|
26 public function __construct(Shell $__psysh__)
|
Chris@13
|
27 {
|
Chris@16
|
28 $this->setClosure($__psysh__, function () use ($__psysh__) {
|
Chris@13
|
29 try {
|
Chris@13
|
30 // Restore execution scope variables
|
Chris@17
|
31 \extract($__psysh__->getScopeVariables(false));
|
Chris@13
|
32
|
Chris@13
|
33 // Buffer stdout; we'll need it later
|
Chris@17
|
34 \ob_start([$__psysh__, 'writeStdout'], 1);
|
Chris@13
|
35
|
Chris@13
|
36 // Convert all errors to exceptions
|
Chris@17
|
37 \set_error_handler([$__psysh__, 'handleError']);
|
Chris@13
|
38
|
Chris@13
|
39 // Evaluate the current code buffer
|
Chris@13
|
40 $_ = eval($__psysh__->onExecute($__psysh__->flushCode() ?: ExecutionClosure::NOOP_INPUT));
|
Chris@13
|
41 } catch (\Throwable $_e) {
|
Chris@13
|
42 // Clean up on our way out.
|
Chris@17
|
43 \restore_error_handler();
|
Chris@17
|
44 if (\ob_get_level() > 0) {
|
Chris@17
|
45 \ob_end_clean();
|
Chris@13
|
46 }
|
Chris@13
|
47
|
Chris@13
|
48 throw $_e;
|
Chris@13
|
49 } catch (\Exception $_e) {
|
Chris@13
|
50 // Clean up on our way out.
|
Chris@17
|
51 \restore_error_handler();
|
Chris@17
|
52 if (\ob_get_level() > 0) {
|
Chris@17
|
53 \ob_end_clean();
|
Chris@13
|
54 }
|
Chris@13
|
55
|
Chris@13
|
56 throw $_e;
|
Chris@13
|
57 }
|
Chris@13
|
58
|
Chris@13
|
59 // Won't be needing this anymore
|
Chris@17
|
60 \restore_error_handler();
|
Chris@13
|
61
|
Chris@13
|
62 // Flush stdout (write to shell output, plus save to magic variable)
|
Chris@17
|
63 \ob_end_flush();
|
Chris@13
|
64
|
Chris@13
|
65 // Save execution scope variables for next time
|
Chris@17
|
66 $__psysh__->setScopeVariables(\get_defined_vars());
|
Chris@13
|
67
|
Chris@13
|
68 return $_;
|
Chris@16
|
69 });
|
Chris@16
|
70 }
|
Chris@13
|
71
|
Chris@16
|
72 /**
|
Chris@16
|
73 * Set the closure instance.
|
Chris@16
|
74 *
|
Chris@16
|
75 * @param Shell $psysh
|
Chris@16
|
76 * @param \Closure $closure
|
Chris@16
|
77 */
|
Chris@16
|
78 protected function setClosure(Shell $shell, \Closure $closure)
|
Chris@16
|
79 {
|
Chris@13
|
80 if (self::shouldBindClosure()) {
|
Chris@16
|
81 $that = $shell->getBoundObject();
|
Chris@17
|
82 if (\is_object($that)) {
|
Chris@17
|
83 $closure = $closure->bindTo($that, \get_class($that));
|
Chris@13
|
84 } else {
|
Chris@16
|
85 $closure = $closure->bindTo(null, $shell->getBoundClass());
|
Chris@13
|
86 }
|
Chris@13
|
87 }
|
Chris@13
|
88
|
Chris@16
|
89 $this->closure = $closure;
|
Chris@13
|
90 }
|
Chris@13
|
91
|
Chris@13
|
92 /**
|
Chris@13
|
93 * Go go gadget closure.
|
Chris@13
|
94 *
|
Chris@13
|
95 * @return mixed
|
Chris@13
|
96 */
|
Chris@13
|
97 public function execute()
|
Chris@13
|
98 {
|
Chris@13
|
99 $closure = $this->closure;
|
Chris@13
|
100
|
Chris@13
|
101 return $closure();
|
Chris@13
|
102 }
|
Chris@13
|
103
|
Chris@13
|
104 /**
|
Chris@13
|
105 * Decide whether to bind the execution closure.
|
Chris@13
|
106 *
|
Chris@13
|
107 * @return bool
|
Chris@13
|
108 */
|
Chris@13
|
109 protected static function shouldBindClosure()
|
Chris@13
|
110 {
|
Chris@13
|
111 // skip binding on HHVM < 3.5.0
|
Chris@13
|
112 // see https://github.com/facebook/hhvm/issues/1203
|
Chris@17
|
113 if (\defined('HHVM_VERSION')) {
|
Chris@17
|
114 return \version_compare(HHVM_VERSION, '3.5.0', '>=');
|
Chris@13
|
115 }
|
Chris@13
|
116
|
Chris@13
|
117 return true;
|
Chris@13
|
118 }
|
Chris@13
|
119 }
|