Skip to content

Instantly share code, notes, and snippets.

@mCzolko
Last active October 22, 2015 19:50
Show Gist options
  • Save mCzolko/f1b12d87d13b95ffc5a8 to your computer and use it in GitHub Desktop.
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.
<?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