/** * @return callable */ private function getClosure() { $closure = function () { extract($this->shellSoul->getScopeVariables()); try { $this->shellSoul->addCode($this->code); // evaluate the current code buffer ob_start([$this->shellSoul, 'writeStdout'], version_compare(PHP_VERSION, '5.4', '>=') ? 1 : 2); set_error_handler([$this->shellSoul, 'handleError']); $_ = eval($this->shellSoul->flushCode() ?: Loop::NOOP_INPUT); restore_error_handler(); ob_end_flush(); $this->shellSoul->writeReturnValue($_); } catch (BreakException $_e) { restore_error_handler(); if (ob_get_level() > 0) { ob_end_clean(); } $this->shellSoul->writeException($_e); return; } catch (ThrowUpException $_e) { restore_error_handler(); if (ob_get_level() > 0) { ob_end_clean(); } $this->shellSoul->writeException($_e); throw $_e; } catch (\Exception $_e) { restore_error_handler(); if (ob_get_level() > 0) { ob_end_clean(); } $this->shellSoul->writeException($_e); } $this->shellSoul->setScopeVariables(get_defined_vars()); }; return $closure; }
public function testClosuresSupport() { $shell = new Shell($this->getConfig()); $code = '$test = function () {}'; $shell->addCode($code); $shell->flushCode(); $code = '$test()'; $shell->addCode($code); $shell->flushCode(); }
/** * @expectedException \Psy\Exception\ParseErrorException */ public function testCodeBufferThrowsParseExceptions() { $shell = new Shell(); $shell->addCode('this is not valid'); $shell->flushCode(); }