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

Breadcrumb

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

class TraceableCommand

@internal

@author Jules Pietri <jules@heahprod.com>

Hierarchy

  • class \Symfony\Component\Console\Command\Command
    • class \Symfony\Component\Console\Command\TraceableCommand extends \Symfony\Component\Console\Command\Command implements \Symfony\Component\Console\Command\SignalableCommandInterface

Expanded class hierarchy of TraceableCommand

1 file declares its use of TraceableCommand
CliRequest.php in vendor/symfony/console/Debug/CliRequest.php

File

vendor/symfony/console/Command/TraceableCommand.php, line 30

Namespace

Symfony\Component\Console\Command
View source
final class TraceableCommand extends Command implements SignalableCommandInterface {
    public readonly Command $command;
    public int $exitCode;
    public ?int $interruptedBySignal = null;
    public bool $ignoreValidation;
    public bool $isInteractive = false;
    public string $duration = 'n/a';
    public string $maxMemoryUsage = 'n/a';
    public InputInterface $input;
    public OutputInterface $output;
    
    /** @var array<string, mixed> */
    public array $arguments;
    
    /** @var array<string, mixed> */
    public array $options;
    
    /** @var array<string, mixed> */
    public array $interactiveInputs = [];
    public array $handledSignals = [];
    public function __construct(Command $command, Stopwatch $stopwatch) {
        if ($command instanceof LazyCommand) {
            $command = $command->getCommand();
        }
        $this->command = $command;
        // prevent call to self::getDefaultDescription()
        $this->setDescription($command->getDescription());
        parent::__construct($command->getName());
        // init below enables calling {@see parent::run()}
        [
            $code,
            $processTitle,
            $ignoreValidationErrors,
        ] = \Closure::bind(function () {
            return [
                $this->code,
                $this->processTitle,
                $this->ignoreValidationErrors,
            ];
        }, $command, Command::class)();
        if (\is_callable($code)) {
            $this->setCode($code);
        }
        if ($processTitle) {
            parent::setProcessTitle($processTitle);
        }
        if ($ignoreValidationErrors) {
            parent::ignoreValidationErrors();
        }
        $this->ignoreValidation = $ignoreValidationErrors;
    }
    public function __call(string $name, array $arguments) : mixed {
        return $this->command
            ->{$name}(...$arguments);
    }
    public function getSubscribedSignals() : array {
        return $this->command instanceof SignalableCommandInterface ? $this->command
            ->getSubscribedSignals() : [];
    }
    public function handleSignal(int $signal, int|false $previousExitCode = 0) : int|false {
        if (!$this->command instanceof SignalableCommandInterface) {
            return false;
        }
        $event = $this->stopwatch
            ->start($this->getName() . '.handle_signal');
        $exit = $this->command
            ->handleSignal($signal, $previousExitCode);
        $event->stop();
        if (!isset($this->handledSignals[$signal])) {
            $this->handledSignals[$signal] = [
                'handled' => 0,
                'duration' => 0,
                'memory' => 0,
            ];
        }
        ++$this->handledSignals[$signal]['handled'];
        $this->handledSignals[$signal]['duration'] += $event->getDuration();
        $this->handledSignals[$signal]['memory'] = max($this->handledSignals[$signal]['memory'], $event->getMemory() >> 20);
        return $exit;
    }
    
    /**
     * {@inheritdoc}
     *
     * Calling parent method is required to be used in {@see parent::run()}.
     */
    public function ignoreValidationErrors() : void {
        $this->ignoreValidation = true;
        $this->command
            ->ignoreValidationErrors();
        parent::ignoreValidationErrors();
    }
    public function setApplication(?Application $application = null) : void {
        $this->command
            ->setApplication($application);
    }
    public function getApplication() : ?Application {
        return $this->command
            ->getApplication();
    }
    public function setHelperSet(HelperSet $helperSet) : void {
        $this->command
            ->setHelperSet($helperSet);
    }
    public function getHelperSet() : ?HelperSet {
        return $this->command
            ->getHelperSet();
    }
    public function isEnabled() : bool {
        return $this->command
            ->isEnabled();
    }
    public function complete(CompletionInput $input, CompletionSuggestions $suggestions) : void {
        $this->command
            ->complete($input, $suggestions);
    }
    
    /**
     * {@inheritdoc}
     *
     * Calling parent method is required to be used in {@see parent::run()}.
     */
    public function setCode(callable $code) : static {
        $this->command
            ->setCode($code);
        return parent::setCode(function (InputInterface $input, OutputInterface $output) use ($code) : int {
            $event = $this->stopwatch
                ->start($this->getName() . '.code');
            $this->exitCode = $code($input, $output);
            $event->stop();
            return $this->exitCode;
        });
    }
    
    /**
     * @internal
     */
    public function mergeApplicationDefinition(bool $mergeArgs = true) : void {
        $this->command
            ->mergeApplicationDefinition($mergeArgs);
    }
    public function setDefinition(array|InputDefinition $definition) : static {
        $this->command
            ->setDefinition($definition);
        return $this;
    }
    public function getDefinition() : InputDefinition {
        return $this->command
            ->getDefinition();
    }
    public function getNativeDefinition() : InputDefinition {
        return $this->command
            ->getNativeDefinition();
    }
    public function addArgument(string $name, ?int $mode = null, string $description = '', mixed $default = null, array|\Closure $suggestedValues = []) : static {
        $this->command
            ->addArgument($name, $mode, $description, $default, $suggestedValues);
        return $this;
    }
    public function addOption(string $name, string|array|null $shortcut = null, ?int $mode = null, string $description = '', mixed $default = null, array|\Closure $suggestedValues = []) : static {
        $this->command
            ->addOption($name, $shortcut, $mode, $description, $default, $suggestedValues);
        return $this;
    }
    
    /**
     * {@inheritdoc}
     *
     * Calling parent method is required to be used in {@see parent::run()}.
     */
    public function setProcessTitle(string $title) : static {
        $this->command
            ->setProcessTitle($title);
        return parent::setProcessTitle($title);
    }
    public function setHelp(string $help) : static {
        $this->command
            ->setHelp($help);
        return $this;
    }
    public function getHelp() : string {
        return $this->command
            ->getHelp();
    }
    public function getProcessedHelp() : string {
        return $this->command
            ->getProcessedHelp();
    }
    public function getSynopsis(bool $short = false) : string {
        return $this->command
            ->getSynopsis($short);
    }
    public function addUsage(string $usage) : static {
        $this->command
            ->addUsage($usage);
        return $this;
    }
    public function getUsages() : array {
        return $this->command
            ->getUsages();
    }
    public function getHelper(string $name) : HelperInterface {
        return $this->command
            ->getHelper($name);
    }
    public function run(InputInterface $input, OutputInterface $output) : int {
        $this->input = $input;
        $this->output = $output;
        $this->arguments = $input->getArguments();
        $this->options = $input->getOptions();
        $event = $this->stopwatch
            ->start($this->getName(), 'command');
        try {
            $this->exitCode = parent::run($input, $output);
        } finally {
            $event->stop();
            if ($output instanceof ConsoleOutputInterface && $output->isDebug()) {
                $output->getErrorOutput()
                    ->writeln((string) $event);
            }
            $this->duration = $event->getDuration() . ' ms';
            $this->maxMemoryUsage = ($event->getMemory() >> 20) . ' MiB';
            if ($this->isInteractive) {
                $this->extractInteractiveInputs($input->getArguments(), $input->getOptions());
            }
        }
        return $this->exitCode;
    }
    protected function initialize(InputInterface $input, OutputInterface $output) : void {
        $event = $this->stopwatch
            ->start($this->getName() . '.init', 'command');
        $this->command
            ->initialize($input, $output);
        $event->stop();
    }
    protected function interact(InputInterface $input, OutputInterface $output) : void {
        if (!($this->isInteractive = Command::class !== (new \ReflectionMethod($this->command, 'interact'))
            ->getDeclaringClass()
            ->getName())) {
            return;
        }
        $event = $this->stopwatch
            ->start($this->getName() . '.interact', 'command');
        $this->command
            ->interact($input, $output);
        $event->stop();
    }
    protected function execute(InputInterface $input, OutputInterface $output) : int {
        $event = $this->stopwatch
            ->start($this->getName() . '.execute', 'command');
        $exitCode = $this->command
            ->execute($input, $output);
        $event->stop();
        return $exitCode;
    }
    private function extractInteractiveInputs(array $arguments, array $options) : void {
        foreach ($arguments as $argName => $argValue) {
            if (\array_key_exists($argName, $this->arguments) && $this->arguments[$argName] === $argValue) {
                continue;
            }
            $this->interactiveInputs[$argName] = $argValue;
        }
        foreach ($options as $optName => $optValue) {
            if (\array_key_exists($optName, $this->options) && $this->options[$optName] === $optValue) {
                continue;
            }
            $this->interactiveInputs['--' . $optName] = $optValue;
        }
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
Command::$aliases private property 1
Command::$application private property
Command::$code private property
Command::$definition private property
Command::$description private property 1
Command::$fullDefinition private property
Command::$help private property
Command::$helperSet private property
Command::$hidden private property
Command::$ignoreValidationErrors private property 2
Command::$name private property
Command::$processTitle private property
Command::$synopsis private property
Command::$usages private property
Command::configure protected function Configures the current command. 50
Command::FAILURE public constant
Command::getAliases public function Returns the aliases for the command.
Command::getDefaultDescription public static function
Command::getDefaultName public static function
Command::getDescription public function Returns the description for the command.
Command::getName public function Returns the command name.
Command::INVALID public constant
Command::isHidden public function
Command::setAliases public function Sets the aliases for the command.
Command::setDescription public function Sets the description for the command.
Command::setHidden public function
Command::setName public function Sets the name of the command.
Command::SUCCESS public constant
Command::validateName private function Validates a command name.
TraceableCommand::$arguments public property @var array&lt;string, mixed&gt;
TraceableCommand::$command public property
TraceableCommand::$duration public property
TraceableCommand::$exitCode public property
TraceableCommand::$handledSignals public property
TraceableCommand::$ignoreValidation public property
TraceableCommand::$input public property
TraceableCommand::$interactiveInputs public property @var array&lt;string, mixed&gt;
TraceableCommand::$interruptedBySignal public property
TraceableCommand::$isInteractive public property
TraceableCommand::$maxMemoryUsage public property
TraceableCommand::$options public property @var array&lt;string, mixed&gt;
TraceableCommand::$output public property
TraceableCommand::addArgument public function Adds an argument. Overrides Command::addArgument
TraceableCommand::addOption public function Adds an option. Overrides Command::addOption
TraceableCommand::addUsage public function Add a command usage example, it&#039;ll be prefixed with the command name. Overrides Command::addUsage
TraceableCommand::complete public function Supplies suggestions when resolving possible completion options for input (e.g. option or argument). Overrides Command::complete
TraceableCommand::execute protected function Executes the current command. Overrides Command::execute
TraceableCommand::extractInteractiveInputs private function
TraceableCommand::getApplication public function Gets the application instance for this command. Overrides Command::getApplication
TraceableCommand::getDefinition public function Gets the InputDefinition attached to this Command. Overrides Command::getDefinition
TraceableCommand::getHelp public function Returns the help for the command. Overrides Command::getHelp
TraceableCommand::getHelper public function Gets a helper instance by name. Overrides Command::getHelper
TraceableCommand::getHelperSet public function Gets the helper set. Overrides Command::getHelperSet
TraceableCommand::getNativeDefinition public function Gets the InputDefinition to be used to create representations of this Command. Overrides Command::getNativeDefinition
TraceableCommand::getProcessedHelp public function Returns the processed help for the command replacing the %command.name% and
%command.full_name% patterns with the real values dynamically.
Overrides Command::getProcessedHelp
TraceableCommand::getSubscribedSignals public function Returns the list of signals to subscribe. Overrides SignalableCommandInterface::getSubscribedSignals
TraceableCommand::getSynopsis public function Returns the synopsis for the command. Overrides Command::getSynopsis
TraceableCommand::getUsages public function Returns alternative usages of the command. Overrides Command::getUsages
TraceableCommand::handleSignal public function The method will be called when the application is signaled. Overrides SignalableCommandInterface::handleSignal
TraceableCommand::ignoreValidationErrors public function Calling parent method is required to be used in { Overrides Command::ignoreValidationErrors
TraceableCommand::initialize protected function Initializes the command after the input has been bound and before the input
is validated.
Overrides Command::initialize
TraceableCommand::interact protected function Interacts with the user. Overrides Command::interact
TraceableCommand::isEnabled public function Checks whether the command is enabled or not in the current environment. Overrides Command::isEnabled
TraceableCommand::mergeApplicationDefinition public function @internal Overrides Command::mergeApplicationDefinition
TraceableCommand::run public function Runs the command. Overrides Command::run
TraceableCommand::setApplication public function Overrides Command::setApplication
TraceableCommand::setCode public function Calling parent method is required to be used in { Overrides Command::setCode
TraceableCommand::setDefinition public function Sets an array of argument and option instances. Overrides Command::setDefinition
TraceableCommand::setHelp public function Sets the help for the command. Overrides Command::setHelp
TraceableCommand::setHelperSet public function Overrides Command::setHelperSet
TraceableCommand::setProcessTitle public function Calling parent method is required to be used in { Overrides Command::setProcessTitle
TraceableCommand::__call public function
TraceableCommand::__construct public function Overrides Command::__construct

API Navigation

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