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