annotate vendor/psy/psysh/src/ExecutionLoopClosure.php @ 16:c2387f117808

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