annotate vendor/psy/psysh/src/ExecutionClosure.php @ 13:5fb285c0d0e3

Update Drupal core to 8.4.7 via Composer. Security update; I *think* we've been lucky to get away with this so far, as we don't support self-registration which seems to be used by the so-called "drupalgeddon 2" attack that 8.4.5 was vulnerable to.
author Chris Cannam
date Mon, 23 Apr 2018 09:33:26 +0100
parents
children c2387f117808
rev   line source
Chris@13 1 <?php
Chris@13 2
Chris@13 3 /*
Chris@13 4 * This file is part of Psy Shell.
Chris@13 5 *
Chris@13 6 * (c) 2012-2018 Justin Hileman
Chris@13 7 *
Chris@13 8 * For the full copyright and license information, please view the LICENSE
Chris@13 9 * file that was distributed with this source code.
Chris@13 10 */
Chris@13 11
Chris@13 12 namespace Psy;
Chris@13 13
Chris@13 14 /**
Chris@13 15 * The Psy Shell's execution scope.
Chris@13 16 */
Chris@13 17 class ExecutionClosure
Chris@13 18 {
Chris@13 19 const NOOP_INPUT = 'return null;';
Chris@13 20
Chris@13 21 private $closure;
Chris@13 22
Chris@13 23 /**
Chris@13 24 * @param Shell $__psysh__
Chris@13 25 */
Chris@13 26 public function __construct(Shell $__psysh__)
Chris@13 27 {
Chris@13 28 $exec = function () use ($__psysh__) {
Chris@13 29 try {
Chris@13 30 // Restore execution scope variables
Chris@13 31 extract($__psysh__->getScopeVariables(false));
Chris@13 32
Chris@13 33 // Buffer stdout; we'll need it later
Chris@13 34 ob_start([$__psysh__, 'writeStdout'], 1);
Chris@13 35
Chris@13 36 // Convert all errors to exceptions
Chris@13 37 set_error_handler([$__psysh__, 'handleError']);
Chris@13 38
Chris@13 39 // Evaluate the current code buffer
Chris@13 40 $_ = eval($__psysh__->onExecute($__psysh__->flushCode() ?: ExecutionClosure::NOOP_INPUT));
Chris@13 41 } catch (\Throwable $_e) {
Chris@13 42 // Clean up on our way out.
Chris@13 43 restore_error_handler();
Chris@13 44 if (ob_get_level() > 0) {
Chris@13 45 ob_end_clean();
Chris@13 46 }
Chris@13 47
Chris@13 48 throw $_e;
Chris@13 49 } catch (\Exception $_e) {
Chris@13 50 // Clean up on our way out.
Chris@13 51 restore_error_handler();
Chris@13 52 if (ob_get_level() > 0) {
Chris@13 53 ob_end_clean();
Chris@13 54 }
Chris@13 55
Chris@13 56 throw $_e;
Chris@13 57 }
Chris@13 58
Chris@13 59 // Won't be needing this anymore
Chris@13 60 restore_error_handler();
Chris@13 61
Chris@13 62 // Flush stdout (write to shell output, plus save to magic variable)
Chris@13 63 ob_end_flush();
Chris@13 64
Chris@13 65 // Save execution scope variables for next time
Chris@13 66 $__psysh__->setScopeVariables(get_defined_vars());
Chris@13 67
Chris@13 68 return $_;
Chris@13 69 };
Chris@13 70
Chris@13 71 if (self::shouldBindClosure()) {
Chris@13 72 $that = $__psysh__->getBoundObject();
Chris@13 73 if (is_object($that)) {
Chris@13 74 $this->closure = $exec->bindTo($that, get_class($that));
Chris@13 75 } else {
Chris@13 76 $this->closure = $exec->bindTo(null, null);
Chris@13 77 }
Chris@13 78
Chris@13 79 return;
Chris@13 80 }
Chris@13 81
Chris@13 82 $this->closure = $exec;
Chris@13 83 }
Chris@13 84
Chris@13 85 /**
Chris@13 86 * Go go gadget closure.
Chris@13 87 *
Chris@13 88 * @return mixed
Chris@13 89 */
Chris@13 90 public function execute()
Chris@13 91 {
Chris@13 92 $closure = $this->closure;
Chris@13 93
Chris@13 94 return $closure();
Chris@13 95 }
Chris@13 96
Chris@13 97 /**
Chris@13 98 * Decide whether to bind the execution closure.
Chris@13 99 *
Chris@13 100 * @return bool
Chris@13 101 */
Chris@13 102 protected static function shouldBindClosure()
Chris@13 103 {
Chris@13 104 // skip binding on HHVM < 3.5.0
Chris@13 105 // see https://github.com/facebook/hhvm/issues/1203
Chris@13 106 if (defined('HHVM_VERSION')) {
Chris@13 107 return version_compare(HHVM_VERSION, '3.5.0', '>=');
Chris@13 108 }
Chris@13 109
Chris@13 110 return true;
Chris@13 111 }
Chris@13 112 }