Last active
October 22, 2015 19:50
-
-
Save mCzolko/f1b12d87d13b95ffc5a8 to your computer and use it in GitHub Desktop.
It serves purpose decoupling logic into multiple Symfony2 commands and executing one after another.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Railhard\ImportBundle\Command; | |
use Symfony\Component\Console\Command\Command; | |
use Symfony\Component\Console\Input\InputInterface; | |
use Symfony\Component\Console\Output\OutputInterface; | |
abstract class ChainCommand extends Command | |
{ | |
/** | |
* @return array like ['doctrine:schema:drop', 'doctrine:schema:create'] | |
*/ | |
abstract protected function getCommands(); | |
/** | |
* {@inheritdoc} | |
*/ | |
final protected function execute(InputInterface $input, OutputInterface $output) | |
{ | |
/** @var Command[] $commands */ | |
$commands = []; | |
foreach ($this->getCommands() as $commandName) { | |
$command = $this->getApplication()->find($commandName); | |
if (!$command) | |
throw new \Exception($commandName .' command was not found'); | |
$commands[$commandName] = $command; | |
} | |
foreach ($commands as $commandName => $command) { | |
$returnCode = $command->run($input, $output); | |
if ($returnCode != 0) | |
throw new \Exception($commandName .' failed...'); | |
$output->writeln($commandName .' did it\'s job...'); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment