Cleaned commands

This commit is contained in:
Kévin Gomez 2013-11-24 16:36:58 +00:00
commit 64ea15cd49
15 changed files with 101 additions and 170 deletions

View file

@ -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.
*/

View file

@ -98,20 +98,4 @@ class DatabaseCreateCommand extends AbstractCommand
return $config;
}
/**
* {@inheritdoc}
*/
protected function createSubCommandInstance()
{
// useless here
}
/**
* {@inheritdoc}
*/
protected function getSubCommandArguments(InputInterface $input)
{
// useless here
}
}

View file

@ -94,20 +94,4 @@ EOT
), 'fg=white;bg=red');
}
}
/**
* {@inheritdoc}
*/
protected function createSubCommandInstance()
{
// useless here
}
/**
* {@inheritdoc}
*/
protected function getSubCommandArguments(InputInterface $input)
{
// useless here
}
}

View file

@ -19,7 +19,7 @@ use Propel\Generator\Command\DatabaseReverseCommand as BaseDatabaseReverseComman
/**
* @author Kévin Gomez <contact@kevingomez.fr>
*/
class DatabaseReverseCommand extends AbstractCommand
class DatabaseReverseCommand extends WrappedCommand
{
/**
* {@inheritdoc}

View file

@ -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
}
}

View file

@ -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
}
}

View file

@ -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

View file

@ -18,7 +18,7 @@ use Propel\Generator\Command\GraphvizGenerateCommand as BaseGraphvizGenerateComm
/**
* @author Kévin Gomez <contact@kevingomez.fr>
*/
class GraphvizGenerateCommand extends AbstractCommand
class GraphvizGenerateCommand extends WrappedCommand
{
/**
* {@inheritdoc}

View file

@ -18,7 +18,7 @@ use Propel\Generator\Command\MigrationDownCommand as BaseMigrationCommand;
/**
* @author Kévin Gomez <contact@kevingomez.fr>
*/
class MigrationDownCommand extends AbstractCommand
class MigrationDownCommand extends WrappedCommand
{
/**
* {@inheritdoc}

View file

@ -18,7 +18,7 @@ use Propel\Generator\Command\MigrationStatusCommand as BaseMigrationCommand;
/**
* @author Kévin Gomez <contact@kevingomez.fr>
*/
class MigrationStatusCommand extends AbstractCommand
class MigrationStatusCommand extends WrappedCommand
{
/**
* {@inheritdoc}

View file

@ -18,7 +18,7 @@ use Propel\Generator\Command\MigrationUpCommand as BaseMigrationCommand;
/**
* @author Kévin Gomez <contact@kevingomez.fr>
*/
class MigrationUpCommand extends AbstractCommand
class MigrationUpCommand extends WrappedCommand
{
/**
* {@inheritdoc}

View file

@ -17,7 +17,7 @@ use Symfony\Component\Console\Input\InputInterface;
/**
* @author Kévin Gomez <contact@kevingomez.fr>
*/
class ModelBuildCommand extends AbstractCommand
class ModelBuildCommand extends WrappedCommand
{
/**
* {@inheritdoc}

View file

@ -16,7 +16,7 @@ use Symfony\Component\Console\Input\InputInterface;
/**
* @author Kévin Gomez <contact@kevingomez.fr>
*/
class SqlBuildCommand extends AbstractCommand
class SqlBuildCommand extends WrappedCommand
{
/**
* {@inheritdoc}

View file

@ -16,7 +16,7 @@ use Symfony\Component\Console\Input\InputInterface;
/**
* @author Kévin Gomez <contact@kevingomez.fr>
*/
class SqlInsertCommand extends AbstractCommand
class SqlInsertCommand extends WrappedCommand
{
/**
* {@inheritdoc}

View file

@ -0,0 +1,88 @@
<?php
/**
* This file is part of the PropelBundle package.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @license MIT License
*/
namespace Propel\PropelBundle\Command;
use Propel\Generator\Command\AbstractCommand as BaseCommand;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\HttpKernel\Bundle\Bundle;
use Symfony\Component\HttpKernel\Bundle\BundleInterface;
use Symfony\Component\HttpKernel\KernelInterface;
/**
* @author Kévin Gomez <contact@kevingomez.fr>
*/
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);
}
}