comparison vendor/psy/psysh/src/ExecutionLoopClosure.php @ 0:c75dbcec494b

Initial commit from drush-created site
author Chris Cannam
date Thu, 05 Jul 2018 14:24:15 +0000
parents
children a9cd425dd02b
comparison
equal deleted inserted replaced
-1:000000000000 0:c75dbcec494b
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 use Psy\Exception\BreakException;
15 use Psy\Exception\ErrorException;
16 use Psy\Exception\ThrowUpException;
17 use Psy\Exception\TypeErrorException;
18
19 /**
20 * The Psy Shell's execution loop scope.
21 *
22 * @todo Once we're on PHP 5.5, we can switch ExecutionClosure to a generator
23 * and get rid of the duplicate closure implementations :)
24 */
25 class ExecutionLoopClosure extends ExecutionClosure
26 {
27 /**
28 * @param Shell $__psysh__
29 */
30 public function __construct(Shell $__psysh__)
31 {
32 $this->setClosure($__psysh__, function () use ($__psysh__) {
33 // Restore execution scope variables
34 extract($__psysh__->getScopeVariables(false));
35
36 do {
37 $__psysh__->beforeLoop();
38
39 try {
40 $__psysh__->getInput();
41
42 try {
43 // Buffer stdout; we'll need it later
44 ob_start([$__psysh__, 'writeStdout'], 1);
45
46 // Convert all errors to exceptions
47 set_error_handler([$__psysh__, 'handleError']);
48
49 // Evaluate the current code buffer
50 $_ = eval($__psysh__->onExecute($__psysh__->flushCode() ?: ExecutionClosure::NOOP_INPUT));
51 } catch (\Throwable $_e) {
52 // Clean up on our way out.
53 restore_error_handler();
54 if (ob_get_level() > 0) {
55 ob_end_clean();
56 }
57
58 throw $_e;
59 } catch (\Exception $_e) {
60 // Clean up on our way out.
61 restore_error_handler();
62 if (ob_get_level() > 0) {
63 ob_end_clean();
64 }
65
66 throw $_e;
67 }
68
69 // Won't be needing this anymore
70 restore_error_handler();
71
72 // Flush stdout (write to shell output, plus save to magic variable)
73 ob_end_flush();
74
75 // Save execution scope variables for next time
76 $__psysh__->setScopeVariables(get_defined_vars());
77
78 $__psysh__->writeReturnValue($_);
79 } catch (BreakException $_e) {
80 $__psysh__->writeException($_e);
81
82 return;
83 } catch (ThrowUpException $_e) {
84 $__psysh__->writeException($_e);
85
86 throw $_e;
87 } catch (\TypeError $_e) {
88 $__psysh__->writeException(TypeErrorException::fromTypeError($_e));
89 } catch (\Error $_e) {
90 $__psysh__->writeException(ErrorException::fromError($_e));
91 } catch (\Exception $_e) {
92 $__psysh__->writeException($_e);
93 }
94
95 $__psysh__->afterLoop();
96 } while (true);
97 });
98 }
99 }