Example #1
0
 /**
  * Runs the current command.
  *
  * If an event dispatcher has been attached to the application,
  * events are also dispatched during the life-cycle of the command.
  *
  * @param Command         $command A Command instance
  * @param InputInterface  $input   An Input instance
  * @param OutputInterface $output  An Output instance
  *
  * @return int 0 if everything went fine, or an error code
  */
 protected function doRunCommand(Command $command, InputInterface $input, OutputInterface $output)
 {
     foreach ($command->getHelperSet() as $helper) {
         if ($helper instanceof InputAwareInterface) {
             $helper->setInput($input);
         }
     }
     if (null === $this->dispatcher) {
         try {
             return $command->run($input, $output);
         } catch (\Exception $e) {
             throw $e;
         } catch (\Throwable $e) {
             throw new FatalThrowableError($e);
         }
     }
     // bind before the console.command event, so the listeners have access to input options/arguments
     try {
         $command->mergeApplicationDefinition();
         $input->bind($command->getDefinition());
     } catch (ExceptionInterface $e) {
         // ignore invalid options/arguments for now, to allow the event listeners to customize the InputDefinition
     }
     $event = new ConsoleCommandEvent($command, $input, $output);
     $this->dispatcher->dispatch(ConsoleEvents::COMMAND, $event);
     if ($event->commandShouldRun()) {
         try {
             $e = null;
             $exitCode = $command->run($input, $output);
         } catch (\Exception $x) {
             $e = $x;
         } catch (\Throwable $x) {
             $e = new FatalThrowableError($x);
         }
         if (null !== $e) {
             $event = new ConsoleExceptionEvent($command, $input, $output, $e, $e->getCode());
             $this->dispatcher->dispatch(ConsoleEvents::EXCEPTION, $event);
             if ($e !== $event->getException()) {
                 $x = $e = $event->getException();
             }
             $event = new ConsoleTerminateEvent($command, $input, $output, $e->getCode());
             $this->dispatcher->dispatch(ConsoleEvents::TERMINATE, $event);
             throw $x;
         }
     } else {
         $exitCode = ConsoleCommandEvent::RETURN_CODE_DISABLED;
     }
     $event = new ConsoleTerminateEvent($command, $input, $output, $exitCode);
     $this->dispatcher->dispatch(ConsoleEvents::TERMINATE, $event);
     return $event->getExitCode();
 }
 public function handleException($exception, array $error = null)
 {
     if (!$exception instanceof \Exception) {
         $exception = new FatalThrowableError($exception);
     }
     $type = $exception instanceof FatalErrorException ? $exception->getSeverity() : E_ERROR;
     if ($this->loggedErrors & $type) {
         $e = array('type' => $type, 'file' => $exception->getFile(), 'line' => $exception->getLine(), 'level' => error_reporting(), 'stack' => $exception->getTrace());
         if ($exception instanceof FatalErrorException) {
             if ($exception instanceof FatalThrowableError) {
                 $error = array('type' => $type, 'message' => $message = $exception->getMessage(), 'file' => $e['file'], 'line' => $e['line']);
             } else {
                 $message = 'Fatal ' . $exception->getMessage();
             }
         } elseif ($exception instanceof \ErrorException) {
             $message = 'Uncaught ' . $exception->getMessage();
             if ($exception instanceof ContextErrorException) {
                 $e['context'] = $exception->getContext();
             }
         } else {
             $message = 'Uncaught Exception: ' . $exception->getMessage();
         }
         if ($this->loggedErrors & $e['type']) {
             $this->loggers[$e['type']][0]->log($this->loggers[$e['type']][1], $message, $e);
         }
     }
     if ($exception instanceof FatalErrorException && !$exception instanceof OutOfMemoryException && $error) {
         foreach ($this->getFatalErrorHandlers() as $handler) {
             if ($e = $handler->handleError($error, $exception)) {
                 $exception = $e;
                 break;
             }
         }
     }
     if (empty($this->exceptionHandler)) {
         throw $exception;
     }
     try {
         call_user_func($this->exceptionHandler, $exception);
     } catch (\Exception $handlerException) {
     } catch (\Throwable $handlerException) {
     }
     if (isset($handlerException)) {
         $this->exceptionHandler = null;
         $this->handleException($handlerException);
     }
 }
Example #3
0
 /**
  * Runs the current command.
  *
  * If an event dispatcher has been attached to the application,
  * events are also dispatched during the life-cycle of the command.
  *
  * @param Command         $command A Command instance
  * @param InputInterface  $input   An Input instance
  * @param OutputInterface $output  An Output instance
  *
  * @return int 0 if everything went fine, or an error code
  *
  * @throws \Exception when the command being run threw an exception
  */
 protected function doRunCommand(Command $command, InputInterface $input, OutputInterface $output)
 {
     foreach ($command->getHelperSet() as $helper) {
         if ($helper instanceof InputAwareInterface) {
             $helper->setInput($input);
         }
     }
     if (null === $this->dispatcher) {
         return $command->run($input, $output);
     }
     $event = new ConsoleCommandEvent($command, $input, $output);
     $this->dispatcher->dispatch(ConsoleEvents::COMMAND, $event);
     if ($event->commandShouldRun()) {
         try {
             $e = null;
             $exitCode = $command->run($input, $output);
         } catch (\Exception $x) {
             $e = $x;
         } catch (\Throwable $x) {
             $e = new FatalThrowableError($x);
         }
         if (null !== $e) {
             $event = new ConsoleExceptionEvent($command, $input, $output, $e, $e->getCode());
             $this->dispatcher->dispatch(ConsoleEvents::EXCEPTION, $event);
             if ($e !== $event->getException()) {
                 $x = $e = $event->getException();
             }
             $event = new ConsoleTerminateEvent($command, $input, $output, $e->getCode());
             $this->dispatcher->dispatch(ConsoleEvents::TERMINATE, $event);
             throw $x;
         }
     } else {
         $exitCode = ConsoleCommandEvent::RETURN_CODE_DISABLED;
     }
     $event = new ConsoleTerminateEvent($command, $input, $output, $exitCode);
     $this->dispatcher->dispatch(ConsoleEvents::TERMINATE, $event);
     return $event->getExitCode();
 }