Subversion-Projekte lars-tiefland.laravel_shop

Revision

Revision 148 | Details | Vergleich mit vorheriger | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
/*
4
 * This file is part of Psy Shell.
5
 *
200 lars 6
 * (c) 2012-2023 Justin Hileman
148 lars 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
        $this->setClosure($__psysh__, 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() ?: self::NOOP_INPUT));
41
            } catch (\Throwable $_e) {
42
                // Clean up on our way out.
43
                if (\ob_get_level() > 0) {
44
                    \ob_end_clean();
45
                }
46
 
47
                throw $_e;
48
            } finally {
49
                // Won't be needing this anymore
50
                \restore_error_handler();
51
            }
52
 
53
            // Flush stdout (write to shell output, plus save to magic variable)
54
            \ob_end_flush();
55
 
56
            // Save execution scope variables for next time
57
            $__psysh__->setScopeVariables(\get_defined_vars());
58
 
59
            return $_;
60
        });
61
    }
62
 
63
    /**
64
     * Set the closure instance.
65
     *
66
     * @param Shell    $shell
67
     * @param \Closure $closure
68
     */
69
    protected function setClosure(Shell $shell, \Closure $closure)
70
    {
71
        $that = $shell->getBoundObject();
72
 
73
        if (\is_object($that)) {
74
            $this->closure = $closure->bindTo($that, \get_class($that));
75
        } else {
76
            $this->closure = $closure->bindTo(null, $shell->getBoundClass());
77
        }
78
    }
79
 
80
    /**
81
     * Go go gadget closure.
82
     *
83
     * @return mixed
84
     */
85
    public function execute()
86
    {
87
        $closure = $this->closure;
88
 
89
        return $closure();
90
    }
91
}