Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. Application.php

function Application::doRunCommand

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.

Return value

int 0 if everything went fine, or an error code

1 call to Application::doRunCommand()
Application::doRun in vendor/symfony/console/Application.php
Runs the current application.

File

vendor/symfony/console/Application.php, line 1000

Class

Application
An Application is the container for a collection of commands.

Namespace

Symfony\Component\Console

Code

protected function doRunCommand(Command $command, InputInterface $input, OutputInterface $output) : int {
    foreach ($command->getHelperSet() as $helper) {
        if ($helper instanceof InputAwareInterface) {
            $helper->setInput($input);
        }
    }
    $commandSignals = $command instanceof SignalableCommandInterface ? $command->getSubscribedSignals() : [];
    if ($commandSignals || $this->dispatcher && $this->signalsToDispatchEvent) {
        $signalRegistry = $this->getSignalRegistry();
        if (Terminal::hasSttyAvailable()) {
            $sttyMode = shell_exec('stty -g');
            foreach ([
                \SIGINT,
                \SIGQUIT,
                \SIGTERM,
            ] as $signal) {
                $signalRegistry->register($signal, static fn() => shell_exec('stty ' . $sttyMode));
            }
        }
        if ($this->dispatcher) {
            // We register application signals, so that we can dispatch the event
            foreach ($this->signalsToDispatchEvent as $signal) {
                $signalEvent = new ConsoleSignalEvent($command, $input, $output, $signal);
                $alarmEvent = \SIGALRM === $signal ? new ConsoleAlarmEvent($command, $input, $output) : null;
                $signalRegistry->register($signal, function ($signal) use ($signalEvent, $alarmEvent, $command, $commandSignals, $input, $output) {
                    $this->dispatcher
                        ->dispatch($signalEvent, ConsoleEvents::SIGNAL);
                    $exitCode = $signalEvent->getExitCode();
                    if (null !== $alarmEvent) {
                        if (false !== $exitCode) {
                            $alarmEvent->setExitCode($exitCode);
                        }
                        else {
                            $alarmEvent->abortExit();
                        }
                        $this->dispatcher
                            ->dispatch($alarmEvent);
                        $exitCode = $alarmEvent->getExitCode();
                    }
                    // If the command is signalable, we call the handleSignal() method
                    if (\in_array($signal, $commandSignals, true)) {
                        $exitCode = $command->handleSignal($signal, $exitCode);
                    }
                    if (\SIGALRM === $signal) {
                        $this->scheduleAlarm();
                    }
                    if (false !== $exitCode) {
                        $event = new ConsoleTerminateEvent($command, $input, $output, $exitCode, $signal);
                        $this->dispatcher
                            ->dispatch($event, ConsoleEvents::TERMINATE);
                        exit($event->getExitCode());
                    }
                });
            }
            // then we register command signals, but not if already handled after the dispatcher
            $commandSignals = array_diff($commandSignals, $this->signalsToDispatchEvent);
        }
        foreach ($commandSignals as $signal) {
            $signalRegistry->register($signal, function (int $signal) use ($command) : void {
                if (\SIGALRM === $signal) {
                    $this->scheduleAlarm();
                }
                if (false !== ($exitCode = $command->handleSignal($signal))) {
                    exit($exitCode);
                }
            });
        }
    }
    if (null === $this->dispatcher) {
        return $command->run($input, $output);
    }
    // bind before the console.command event, so the listeners have access to input options/arguments
    try {
        $command->mergeApplicationDefinition();
        $input->bind($command->getDefinition());
    } catch (ExceptionInterface) {
        // ignore invalid options/arguments for now, to allow the event listeners to customize the InputDefinition
    }
    $event = new ConsoleCommandEvent($command, $input, $output);
    $e = null;
    try {
        $this->dispatcher
            ->dispatch($event, ConsoleEvents::COMMAND);
        if ($event->commandShouldRun()) {
            $exitCode = $command->run($input, $output);
        }
        else {
            $exitCode = ConsoleCommandEvent::RETURN_CODE_DISABLED;
        }
    } catch (\Throwable $e) {
        $event = new ConsoleErrorEvent($input, $output, $e, $command);
        $this->dispatcher
            ->dispatch($event, ConsoleEvents::ERROR);
        $e = $event->getError();
        if (0 === ($exitCode = $event->getExitCode())) {
            $e = null;
        }
    }
    $event = new ConsoleTerminateEvent($command, $input, $output, $exitCode);
    $this->dispatcher
        ->dispatch($event, ConsoleEvents::TERMINATE);
    if (null !== $e) {
        throw $e;
    }
    return $event->getExitCode();
}

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal