/** * Disabling runs members of chains and launching chains of commands. * * @param ConsoleCommandEvent $event */ public function onConsoleCommand(ConsoleCommandEvent $event) { $command = $event->getCommand(); $commandName = $command->getName(); $application = $command->getApplication(); if (!$this->manager->isInitialized()) { $this->manager->init($application); } if ($this->manager->isMember($commandName)) { $event->disableCommand(); $event->stopPropagation(); $mainCommands = $this->manager->getMainCommands($commandName); $event->getOutput()->writeln(sprintf('<error>Error: "%s" command is a member of %s command%s chain and cannot be executed on its own.</error>', $commandName, implode(', ', array_map(function ($name) { return '"' . $name . '"'; }, $mainCommands)), count($mainCommands) > 1 ? 's' : '')); } if ($this->manager->hasChains($commandName)) { $this->manager->runChain($command, $event->getInput()); $event->disableCommand(); $event->stopPropagation(); } }
/** * Test for runChain method. */ public function testRunChain() { $application = new Application(); $mainCommand = new HelloCommand(); $mainCommand->setApplication($application); $application->add($mainCommand); $memberCommand = new HiCommand(); $memberCommand->setApplication($application); $application->add($memberCommand); $this->manager->init($application); $this->logger->expects(static::exactly(2))->method('log')->withConsecutive([200, 'Hello from Foo!'], [200, 'Hi from Bar!']); $this->manager->runChain($mainCommand, new ArrayInput(['command' => 'foo:hello', '--quiet' => true])); }