diff --git a/Command/AbstractCommand.php b/Command/AbstractCommand.php index d151975..2ae8727 100644 --- a/Command/AbstractCommand.php +++ b/Command/AbstractCommand.php @@ -39,57 +39,17 @@ abstract class AbstractCommand extends ContainerAwareCommand protected $bundle = null; /** - * * @var InputInterface */ protected $input; use FormattingHelpers; - /** - * {@inheritdoc} - */ - protected function configure() - { - $this - ->addOption('platform', null, InputOption::VALUE_REQUIRED, 'The platform', BaseCommand::DEFAULT_PLATFORM) - ; - } - - /** - * Creates the instance of the Propel sub-command to execute. - * - * @return \Symfony\Component\Console\Command\Command - */ - abstract protected function createSubCommandInstance(); - - /** - * Returns all the arguments and options needed by the Propel sub-command. - * - * @return array - */ - abstract protected function getSubCommandArguments(InputInterface $input); - - /** - * {@inheritdoc} - */ - protected function execute(InputInterface $input, OutputInterface $output) - { - $params = $this->getSubCommandArguments($input); - $command = $this->createSubCommandInstance(); - - $this->setupBuildTimeFiles(); - - return $this->runCommand($command, $params, $input, $output); - } - /** * {@inheritdoc} */ protected function initialize(InputInterface $input, OutputInterface $output) { - parent::initialize($input, $output); - $kernel = $this->getApplication()->getKernel(); $this->input = $input; @@ -105,27 +65,6 @@ abstract class AbstractCommand extends ContainerAwareCommand } } - protected function runCommand(Command $command, array $parameters, InputInterface $input, OutputInterface $output) - { - // add the command's name to the parameters - array_unshift($parameters, $this->getName()); - - // merge the default parameters - $parameters = array_merge(array( - '--input-dir' => $this->cacheDir, - '--verbose' => $input->getOption('verbose'), - ), $parameters); - - if ($input->hasOption('platform')) { - $parameters['--platform'] = $input->getOption('platform'); - } - - $command->setApplication($this->getApplication()); - - // and run the sub-command - return $command->run(new ArrayInput($parameters), $output); - } - /** * Create all the files needed by Propel's commands. */ diff --git a/Command/DatabaseCreateCommand.php b/Command/DatabaseCreateCommand.php index d4caf60..5c6b6bd 100644 --- a/Command/DatabaseCreateCommand.php +++ b/Command/DatabaseCreateCommand.php @@ -98,20 +98,4 @@ class DatabaseCreateCommand extends AbstractCommand return $config; } - - /** - * {@inheritdoc} - */ - protected function createSubCommandInstance() - { - // useless here - } - - /** - * {@inheritdoc} - */ - protected function getSubCommandArguments(InputInterface $input) - { - // useless here - } } diff --git a/Command/DatabaseDropCommand.php b/Command/DatabaseDropCommand.php index 885ba62..5cdf734 100644 --- a/Command/DatabaseDropCommand.php +++ b/Command/DatabaseDropCommand.php @@ -94,20 +94,4 @@ EOT ), 'fg=white;bg=red'); } } - - /** - * {@inheritdoc} - */ - protected function createSubCommandInstance() - { - // useless here - } - - /** - * {@inheritdoc} - */ - protected function getSubCommandArguments(InputInterface $input) - { - // useless here - } } diff --git a/Command/DatabaseReverseCommand.php b/Command/DatabaseReverseCommand.php index 55babb9..e2d1b48 100644 --- a/Command/DatabaseReverseCommand.php +++ b/Command/DatabaseReverseCommand.php @@ -19,7 +19,7 @@ use Propel\Generator\Command\DatabaseReverseCommand as BaseDatabaseReverseComman /** * @author Kévin Gomez */ -class DatabaseReverseCommand extends AbstractCommand +class DatabaseReverseCommand extends WrappedCommand { /** * {@inheritdoc} diff --git a/Command/FixturesDumpCommand.php b/Command/FixturesDumpCommand.php index 1154cdc..c6b9575 100644 --- a/Command/FixturesDumpCommand.php +++ b/Command/FixturesDumpCommand.php @@ -60,7 +60,6 @@ EOT protected function execute(InputInterface $input, OutputInterface $output) { $fixtureDir = $input->getOption('dir') ?: $this->defaultFixturesDir; - $path = realpath($this->getApplication()->getKernel()->getRootDir() . '/../') . '/' . $fixtureDir; if (!file_exists($path)) { @@ -75,7 +74,6 @@ EOT } $filename = $path . '/fixtures_' . time() . '.yml'; - $dumper = $this->getContainer()->get('propel.dumper.yaml'); try { @@ -93,20 +91,4 @@ EOT return true; } - - /** - * {@inheritdoc} - */ - protected function createSubCommandInstance() - { - // useless here - } - - /** - * {@inheritdoc} - */ - protected function getSubCommandArguments(InputInterface $input) - { - // useless here - } } diff --git a/Command/FixturesLoadCommand.php b/Command/FixturesLoadCommand.php index d7c58a5..9159222 100644 --- a/Command/FixturesLoadCommand.php +++ b/Command/FixturesLoadCommand.php @@ -314,20 +314,4 @@ EOT { return $bundle->getPath() . DIRECTORY_SEPARATOR . 'Resources' . DIRECTORY_SEPARATOR . 'fixtures'; } - - /** - * {@inheritdoc} - */ - protected function createSubCommandInstance() - { - // useless here - } - - /** - * {@inheritdoc} - */ - protected function getSubCommandArguments(InputInterface $input) - { - // useless here - } } diff --git a/Command/FormGenerateCommand.php b/Command/FormGenerateCommand.php index 55b6d0a..ec0f13e 100644 --- a/Command/FormGenerateCommand.php +++ b/Command/FormGenerateCommand.php @@ -10,13 +10,13 @@ namespace Propel\PropelBundle\Command; +use Propel\Generator\Config\GeneratorConfig; +use Propel\Generator\Command\ModelBuildCommand; +use Propel\Generator\Command\AbstractCommand as BaseCommand; use Propel\Generator\Model\Database; use Propel\Generator\Model\Table; -use Propel\Runtime\Propel; use Propel\Generator\Manager\ModelManager; -use Propel\Generator\Config\GeneratorConfig; - -use Propel\Generator\Command\ModelBuildCommand; +use Propel\Runtime\Propel; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; @@ -37,13 +37,12 @@ class FormGenerateCommand extends AbstractCommand */ protected function configure() { - parent::configure(); - $this ->setName('propel:form:generate') ->setDescription('Generate Form types stubs based on the schema.xml') ->addOption('force', 'f', InputOption::VALUE_NONE, 'Overwrite existing Form types') + ->addOption('platform', null, InputOption::VALUE_REQUIRED, 'The platform', BaseCommand::DEFAULT_PLATFORM) ->addArgument('bundle', InputArgument::REQUIRED, 'The bundle to use to generate Form types') ->addArgument('models', InputArgument::IS_ARRAY, 'Model classes to generate Form Types from') @@ -142,35 +141,6 @@ EOT return str_replace('##BUILD_CODE##', $buildCode, $formTypeContent); } - /** - * {@inheritdoc} - */ - protected function createSubCommandInstance() - { - // useless here - } - - /** - * {@inheritdoc} - */ - protected function getSubCommandArguments(InputInterface $input) - { - // useless here - } - - protected function getDatabasesFromSchema(\SplFileInfo $file) - { - $databaseXml = simplexml_load_file($file); - $databaseName = (string) $databaseXml['name']; - - if (empty($databaseName)) { - throw new \RuntimeException(sprintf('Impossible to determine database name for schema "%s". Please define the "name" attribute', $file->getRealPath())); - } - - $serviceContainer = Propel::getServiceContainer(); - return $serviceContainer->getDatabaseMap($databaseName); - } - /** * @param \SplFileInfo $file * @return string diff --git a/Command/GraphvizGenerateCommand.php b/Command/GraphvizGenerateCommand.php index ca87390..693f346 100644 --- a/Command/GraphvizGenerateCommand.php +++ b/Command/GraphvizGenerateCommand.php @@ -18,7 +18,7 @@ use Propel\Generator\Command\GraphvizGenerateCommand as BaseGraphvizGenerateComm /** * @author Kévin Gomez */ -class GraphvizGenerateCommand extends AbstractCommand +class GraphvizGenerateCommand extends WrappedCommand { /** * {@inheritdoc} diff --git a/Command/MigrationDownCommand.php b/Command/MigrationDownCommand.php index 41dc2f2..6b57652 100644 --- a/Command/MigrationDownCommand.php +++ b/Command/MigrationDownCommand.php @@ -18,7 +18,7 @@ use Propel\Generator\Command\MigrationDownCommand as BaseMigrationCommand; /** * @author Kévin Gomez */ -class MigrationDownCommand extends AbstractCommand +class MigrationDownCommand extends WrappedCommand { /** * {@inheritdoc} diff --git a/Command/MigrationStatusCommand.php b/Command/MigrationStatusCommand.php index 64f76a9..b494a53 100644 --- a/Command/MigrationStatusCommand.php +++ b/Command/MigrationStatusCommand.php @@ -18,7 +18,7 @@ use Propel\Generator\Command\MigrationStatusCommand as BaseMigrationCommand; /** * @author Kévin Gomez */ -class MigrationStatusCommand extends AbstractCommand +class MigrationStatusCommand extends WrappedCommand { /** * {@inheritdoc} diff --git a/Command/MigrationUpCommand.php b/Command/MigrationUpCommand.php index d142a33..43401b5 100644 --- a/Command/MigrationUpCommand.php +++ b/Command/MigrationUpCommand.php @@ -18,7 +18,7 @@ use Propel\Generator\Command\MigrationUpCommand as BaseMigrationCommand; /** * @author Kévin Gomez */ -class MigrationUpCommand extends AbstractCommand +class MigrationUpCommand extends WrappedCommand { /** * {@inheritdoc} diff --git a/Command/ModelBuildCommand.php b/Command/ModelBuildCommand.php index dec3b53..d005eb7 100644 --- a/Command/ModelBuildCommand.php +++ b/Command/ModelBuildCommand.php @@ -17,7 +17,7 @@ use Symfony\Component\Console\Input\InputInterface; /** * @author Kévin Gomez */ -class ModelBuildCommand extends AbstractCommand +class ModelBuildCommand extends WrappedCommand { /** * {@inheritdoc} diff --git a/Command/SqlBuildCommand.php b/Command/SqlBuildCommand.php index be59a44..e4a35ee 100644 --- a/Command/SqlBuildCommand.php +++ b/Command/SqlBuildCommand.php @@ -16,7 +16,7 @@ use Symfony\Component\Console\Input\InputInterface; /** * @author Kévin Gomez */ -class SqlBuildCommand extends AbstractCommand +class SqlBuildCommand extends WrappedCommand { /** * {@inheritdoc} diff --git a/Command/SqlInsertCommand.php b/Command/SqlInsertCommand.php index 58029cb..2a5abe7 100644 --- a/Command/SqlInsertCommand.php +++ b/Command/SqlInsertCommand.php @@ -16,7 +16,7 @@ use Symfony\Component\Console\Input\InputInterface; /** * @author Kévin Gomez */ -class SqlInsertCommand extends AbstractCommand +class SqlInsertCommand extends WrappedCommand { /** * {@inheritdoc} diff --git a/Command/WrappedCommand.php b/Command/WrappedCommand.php new file mode 100644 index 0000000..6f5a34a --- /dev/null +++ b/Command/WrappedCommand.php @@ -0,0 +1,88 @@ + + */ +abstract class WrappedCommand extends AbstractCommand +{ + /** + * Creates the instance of the Propel sub-command to execute. + * + * @return \Symfony\Component\Console\Command\Command + */ + abstract protected function createSubCommandInstance(); + + /** + * Returns all the arguments and options needed by the Propel sub-command. + * + * @return array + */ + abstract protected function getSubCommandArguments(InputInterface $input); + + /** + * {@inheritdoc} + */ + protected function configure() + { + $this + ->addOption('platform', null, InputOption::VALUE_REQUIRED, 'The platform', BaseCommand::DEFAULT_PLATFORM) + ; + } + + /** + * {@inheritdoc} + */ + protected function execute(InputInterface $input, OutputInterface $output) + { + $params = $this->getSubCommandArguments($input); + $command = $this->createSubCommandInstance(); + + $this->setupBuildTimeFiles(); + + return $this->runCommand($command, $params, $input, $output); + } + + protected function runCommand(Command $command, array $parameters, InputInterface $input, OutputInterface $output) + { + // add the command's name to the parameters + array_unshift($parameters, $this->getName()); + + // merge the default parameters + $parameters = array_merge(array( + '--input-dir' => $this->cacheDir, + '--verbose' => $input->getOption('verbose'), + ), $parameters); + + if ($input->hasOption('platform')) { + $parameters['--platform'] = $input->getOption('platform'); + } + + $command->setApplication($this->getApplication()); + + // and run the sub-command + return $command->run(new ArrayInput($parameters), $output); + } +}