Example #1
0
 public function testRenderingExceptions()
 {
     $shell = new Shell($this->getConfig());
     $output = $this->getOutput();
     $stream = $output->getStream();
     $e = new ParseErrorException('message', 13);
     $shell->addCode('code');
     $this->assertTrue($shell->hasCode());
     $this->assertNotEmpty($shell->getCodeBuffer());
     $shell->renderException($e, $output);
     $this->assertSame($e, $shell->getScopeVariable('_e'));
     $this->assertFalse($shell->hasCode());
     $this->assertEmpty($shell->getCodeBuffer());
     rewind($stream);
     $streamContents = stream_get_contents($stream);
     $this->assertContains('PHP Parse error', $streamContents);
     $this->assertContains('message', $streamContents);
     $this->assertContains('line 13', $streamContents);
 }
Example #2
0
 /**
  * Run the execution loop.
  *
  * @throws ThrowUpException if thrown by the `throw-up` command.
  *
  * @param Shell $shell
  */
 public function run(Shell $shell)
 {
     $loop = function ($__psysh__) {
         // Load user-defined includes
         set_error_handler(array($__psysh__, 'handleError'));
         try {
             foreach ($__psysh__->getIncludes() as $__psysh_include__) {
                 include $__psysh_include__;
             }
         } catch (\Exception $_e) {
             $__psysh__->writeException($_e);
         }
         restore_error_handler();
         unset($__psysh_include__);
         extract($__psysh__->getScopeVariables());
         do {
             $__psysh__->beforeLoop();
             $__psysh__->setScopeVariables(get_defined_vars());
             try {
                 // read a line, see if we should eval
                 $__psysh__->getInput();
                 // evaluate the current code buffer
                 ob_start(array($__psysh__, 'writeStdout'), version_compare(PHP_VERSION, '5.4', '>=') ? 1 : 2);
                 set_error_handler(array($__psysh__, 'handleError'));
                 $_ = eval($__psysh__->flushCode());
                 restore_error_handler();
                 ob_end_flush();
                 $__psysh__->writeReturnValue($_);
             } catch (BreakException $_e) {
                 restore_error_handler();
                 if (ob_get_level() > 0) {
                     ob_end_clean();
                 }
                 $__psysh__->writeException($_e);
                 return;
             } catch (ThrowUpException $_e) {
                 restore_error_handler();
                 if (ob_get_level() > 0) {
                     ob_end_clean();
                 }
                 $__psysh__->writeException($_e);
                 throw $_e;
             } catch (\Exception $_e) {
                 restore_error_handler();
                 if (ob_get_level() > 0) {
                     ob_end_clean();
                 }
                 $__psysh__->writeException($_e);
             }
             // a bit of housekeeping
             unset($__psysh_out__);
             $__psysh__->afterLoop();
         } while (true);
     };
     // bind the closure to $this from the shell scope variables...
     if (self::bindLoop()) {
         $that = null;
         try {
             $that = $shell->getScopeVariable('this');
         } catch (\InvalidArgumentException $e) {
             // well, it was worth a shot
         }
         if (is_object($that)) {
             $loop = $loop->bindTo($that, get_class($that));
         } else {
             $loop = $loop->bindTo(null, null);
         }
     }
     $loop($shell);
 }