annotate vendor/psy/psysh/src/ExecutionLoopClosure.php @ 4:a9cd425dd02b

Update, including to Drupal core 8.6.10
author Chris Cannam
date Thu, 28 Feb 2019 13:11:55 +0000
parents c75dbcec494b
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 /*
Chris@0 4 * This file is part of Psy Shell.
Chris@0 5 *
Chris@0 6 * (c) 2012-2018 Justin Hileman
Chris@0 7 *
Chris@0 8 * For the full copyright and license information, please view the LICENSE
Chris@0 9 * file that was distributed with this source code.
Chris@0 10 */
Chris@0 11
Chris@0 12 namespace Psy;
Chris@0 13
Chris@0 14 use Psy\Exception\BreakException;
Chris@0 15 use Psy\Exception\ErrorException;
Chris@0 16 use Psy\Exception\ThrowUpException;
Chris@0 17 use Psy\Exception\TypeErrorException;
Chris@0 18
Chris@0 19 /**
Chris@0 20 * The Psy Shell's execution loop scope.
Chris@0 21 *
Chris@0 22 * @todo Once we're on PHP 5.5, we can switch ExecutionClosure to a generator
Chris@0 23 * and get rid of the duplicate closure implementations :)
Chris@0 24 */
Chris@0 25 class ExecutionLoopClosure extends ExecutionClosure
Chris@0 26 {
Chris@0 27 /**
Chris@0 28 * @param Shell $__psysh__
Chris@0 29 */
Chris@0 30 public function __construct(Shell $__psysh__)
Chris@0 31 {
Chris@0 32 $this->setClosure($__psysh__, function () use ($__psysh__) {
Chris@0 33 // Restore execution scope variables
Chris@4 34 \extract($__psysh__->getScopeVariables(false));
Chris@0 35
Chris@0 36 do {
Chris@0 37 $__psysh__->beforeLoop();
Chris@0 38
Chris@0 39 try {
Chris@0 40 $__psysh__->getInput();
Chris@0 41
Chris@0 42 try {
Chris@4 43 // Pull in any new execution scope variables
Chris@4 44 if ($__psysh__->getLastExecSuccess()) {
Chris@4 45 \extract($__psysh__->getScopeVariablesDiff(\get_defined_vars()));
Chris@4 46 }
Chris@4 47
Chris@0 48 // Buffer stdout; we'll need it later
Chris@4 49 \ob_start([$__psysh__, 'writeStdout'], 1);
Chris@0 50
Chris@0 51 // Convert all errors to exceptions
Chris@4 52 \set_error_handler([$__psysh__, 'handleError']);
Chris@0 53
Chris@0 54 // Evaluate the current code buffer
Chris@0 55 $_ = eval($__psysh__->onExecute($__psysh__->flushCode() ?: ExecutionClosure::NOOP_INPUT));
Chris@0 56 } catch (\Throwable $_e) {
Chris@0 57 // Clean up on our way out.
Chris@4 58 \restore_error_handler();
Chris@4 59 if (\ob_get_level() > 0) {
Chris@4 60 \ob_end_clean();
Chris@0 61 }
Chris@0 62
Chris@0 63 throw $_e;
Chris@0 64 } catch (\Exception $_e) {
Chris@0 65 // Clean up on our way out.
Chris@4 66 \restore_error_handler();
Chris@4 67 if (\ob_get_level() > 0) {
Chris@4 68 \ob_end_clean();
Chris@0 69 }
Chris@0 70
Chris@0 71 throw $_e;
Chris@0 72 }
Chris@0 73
Chris@0 74 // Won't be needing this anymore
Chris@4 75 \restore_error_handler();
Chris@0 76
Chris@0 77 // Flush stdout (write to shell output, plus save to magic variable)
Chris@4 78 \ob_end_flush();
Chris@0 79
Chris@0 80 // Save execution scope variables for next time
Chris@4 81 $__psysh__->setScopeVariables(\get_defined_vars());
Chris@0 82
Chris@0 83 $__psysh__->writeReturnValue($_);
Chris@0 84 } catch (BreakException $_e) {
Chris@0 85 $__psysh__->writeException($_e);
Chris@0 86
Chris@0 87 return;
Chris@0 88 } catch (ThrowUpException $_e) {
Chris@0 89 $__psysh__->writeException($_e);
Chris@0 90
Chris@0 91 throw $_e;
Chris@0 92 } catch (\TypeError $_e) {
Chris@0 93 $__psysh__->writeException(TypeErrorException::fromTypeError($_e));
Chris@0 94 } catch (\Error $_e) {
Chris@0 95 $__psysh__->writeException(ErrorException::fromError($_e));
Chris@0 96 } catch (\Exception $_e) {
Chris@0 97 $__psysh__->writeException($_e);
Chris@0 98 }
Chris@0 99
Chris@0 100 $__psysh__->afterLoop();
Chris@0 101 } while (true);
Chris@0 102 });
Chris@0 103 }
Chris@0 104 }