Symfony\Component\Console\Application::add PHP Method

add() public method

If a command with the same name already exists, it will be overridden. If the command is not enabled it will not be added.
public add ( Command $command ) : Command | null
$command Symfony\Component\Console\Command\Command A Command object
return Symfony\Component\Console\Command\Command | null The registered command if enabled or null
    public function add(Command $command)
    {
        $command->setApplication($this);
        if (!$command->isEnabled()) {
            $command->setApplication(null);
            return;
        }
        if (null === $command->getDefinition()) {
            throw new LogicException(sprintf('Command class "%s" is not correctly initialized. You probably forgot to call the parent constructor.', get_class($command)));
        }
        $this->commands[$command->getName()] = $command;
        foreach ($command->getAliases() as $alias) {
            $this->commands[$alias] = $command;
        }
        return $command;
    }

Usage Example

Example #1
1
 /**
  * {@inheritdoc}
  */
 public function execute(ContainerInterface $app)
 {
     foreach ($this->commands as $command) {
         $this->console->add($app->create($command));
     }
     $this->console->run();
 }
All Usage Examples Of Symfony\Component\Console\Application::add