Merge branch maxailloud

This commit is contained in:
William DURAND 2011-05-25 00:04:27 +02:00
commit 465fad8872
4 changed files with 131 additions and 25 deletions

View file

@ -29,9 +29,9 @@ class BuildCommand extends PhingCommand
$this
->setDescription('Hub for Propel build commands (model, sql)')
->setDefinition(array(
new InputOption('--classes', '', InputOption::VALUE_NONE, 'Build only classes'),
new InputOption('--sql', '', InputOption::VALUE_NONE, 'Build only code'),
new InputOption('--insert-sql', '', InputOption::VALUE_NONE, 'Build all and insert SQL'),
new InputOption('classes', '', InputOption::VALUE_NONE, 'Build only classes'),
new InputOption('sql', '', InputOption::VALUE_NONE, 'Build only code'),
new InputOption('insert-sql', '', InputOption::VALUE_NONE, 'Build all and insert SQL'),
new InputOption('connection', null, InputOption::VALUE_OPTIONAL, 'Set this parameter to define a connection to use')
))
->setName('propel:build');
@ -64,7 +64,7 @@ class BuildCommand extends PhingCommand
$insertCommand->setApplication($this->getApplication());
// By-pass the '--force' required option
$this->addOption('--force', '', InputOption::VALUE_NONE, '');
$this->addOption('force', '', InputOption::VALUE_NONE, '');
$input->setOption('force', true);
$insertCommand->execute($input, $output);

View file

@ -4,6 +4,7 @@ namespace Propel\PropelBundle\Command;
use Propel\PropelBundle\Command\PhingCommand;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Finder\Finder;
@ -21,24 +22,42 @@ class LoadFixturesCommand extends PhingCommand
*/
private $defaultFixturesDir = 'propel/fixtures';
/**
* Absolute path for fixtures directory
*/
private $absoluteFixturesPath = '';
/**
* Filesystem for manipulating files
*/
private $filesystem = null;
/**
* @see Command
*/
protected function configure()
{
$this
->setDescription('Load XML fixtures')
->addOption('dir', 'd', InputOption::VALUE_REQUIRED, 'The directory where XML fixtures files are located')
->setDescription('Load fixtures')
->addOption('dir', 'd', InputOption::VALUE_OPTIONAL, 'The directory where XML or/and SQL fixtures files are located', $this->defaultFixturesDir)
->addOption('xml', '', InputOption::VALUE_NONE, 'Load xml fixtures')
->addOption('sql', '', InputOption::VALUE_NONE, 'Load sql fixtures')
->addOption('connection', null, InputOption::VALUE_OPTIONAL, 'Set this parameter to define a connection to use')
->setHelp(<<<EOT
The <info>propel:load-fixtures</info> loads XML fixtures.
The <info>propel:load-fixtures</info> loads <info>XML</info> and/or <info>SQL</info> fixtures.
<info>php app/console propel:load-fixtures</info>
The <info>--connection</info> parameter allows you to change the connection to use.
The default connection is the active connection (propel.dbal.default_connection).
The <info>--dir</info> parameter allows you to change the directory that contains XML fixtures files (default: <info>app/propel/fixtures</info>).
The <info>--dir</info> parameter allows you to change the directory that contains <info>XML</info> or/and <info>SQL</info> fixtures files <comment>(default: app/propel/fixtures)</comment>.
The <info>--xml</info> parameter allows you to load only <info>XML</info> fixtures.
The <info>--sql</info> parameter allows you to load only <info>SQL</info> fixtures.
You can mix <info>--xml</info> parameter and <info>--sql</info> parameter to load XML and SQL fixtures.
If none of this parameter are set all files, XML and SQL, in the directory will be load.
XML fixtures files are the same XML files you can get with the command <info>propel:data-dump</info>:
<comment>
@ -60,47 +79,125 @@ EOT
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$this->absoluteFixturesPath = $this->getApplication()->getKernel()->getRootDir() . DIRECTORY_SEPARATOR . $input->getOption('dir');
$this->filesystem = new Filesystem();
$noOptions = (!$input->getOption('xml') && !$input->getOption('sql'));
if ($input->getOption('xml') || $noOptions)
{
$this->loadXmlFixtures($input, $output);
}
if ($input->getOption('sql') || $noOptions)
{
$this->loadSqlFixtures($input, $output);
}
$output->writeln('<info>Fixtures successfully loaded.</info>');
}
/**
* Load XML fixtures
*
* @param \Symfony\Component\Console\Input\InputInterface $input
* @param \Symfony\Component\Console\Output\OutputInterface $output
* @return void
*/
protected function loadXmlFixtures(InputInterface $input, OutputInterface $output)
{
$output->writeln('<info>Loading XML Fixtures.</info>');
$finder = new Finder();
$filesystem = new Filesystem();
$dir = $input->getOption('dir') ?: $this->defaultFixturesDir;
$fixturesDir = $this->getApplication()->getKernel()->getRootDir() . '/' . $dir;
// Create a "datadb.map" file
$datadbContent = '';
$datas = $finder->name('*.xml')->in($fixturesDir);
$datas = $finder->name('*.xml')->in($this->absoluteFixturesPath);
foreach($datas as $data) {
$output->writeln(sprintf('Loaded fixtures from <comment>%s</comment>.', $data));
$datadbContent .= $data->getFilename() . '=default' . "\n";
$datadbContent .= $data->getFilename() . '=default' . PHP_EOL;
}
$datadbFile = $fixturesDir . '/datadb.map';
$datadbFile = $this->absoluteFixturesPath . '/datadb.map';
file_put_contents($datadbFile, $datadbContent);
$dest = $this->getApplication()->getKernel()->getRootDir() . '/propel/sql/';
$this->callPhing('datasql', array(
'propel.sql.dir' => $dest,
'propel.schema.dir' => $fixturesDir,
'propel.sql.dir' => $dest,
'propel.schema.dir' => $this->absoluteFixturesPath,
));
// Insert SQL
$insertCommand = new InsertSqlCommand();
$insertCommand->setApplication($this->getApplication());
// By-pass the '--force' required option
$this->addOption('--force', '', InputOption::VALUE_NONE, '');
// By-pass the '--force' required option for inserting SQL
$this->addOption('force', '', InputOption::VALUE_NONE, '');
$input->setOption('force', true);
$insertCommand->execute($input, $output);
// Delete temporary files
$this->removeTemporaryFiles();
$this->filesystem->remove($datadbFile);
}
/**
* Load SQL fixtures
*
* @param \Symfony\Component\Console\Input\InputInterface $input
* @param \Symfony\Component\Console\Output\OutputInterface $output
* @return void
*/
protected function loadSqlFixtures(InputInterface $input, OutputInterface $output)
{
$output->writeln('<info>Loading SQL Fixtures.</info>');
$finder = new Finder();
$datas = $finder->name('*_schema.xml')->name('build*')->in($fixturesDir);
// Create a "sqldb.map" file
$sqldbContent = '';
$datas = $finder->name('*.sql')->in($this->absoluteFixturesPath);
foreach($datas as $data) {
$filesystem->remove($data);
$output->writeln(sprintf('Loaded fixtures from <comment>%s</comment>.', $data));
$sqldbContent .= $data->getFilename() . '=default' . PHP_EOL;
}
$filesystem->remove($datadbFile);
$sqldbFile = $this->absoluteFixturesPath . DIRECTORY_SEPARATOR . 'sqldb.map';
$output->writeln('<info>Fixtures successfully loaded.</info>');
file_put_contents($sqldbFile, $sqldbContent);
list($name, $defaultConfig) = $this->getConnection($input, $output);
$this->callPhing('insert-sql', array(
'propel.database.url' => $defaultConfig['connection']['dsn'],
'propel.database.database' => $defaultConfig['adapter'],
'propel.database.user' => $defaultConfig['connection']['user'],
'propel.database.password' => $defaultConfig['connection']['password'],
'propel.sql.dir' => $this->absoluteFixturesPath,
'propel.schema.dir' => $this->absoluteFixturesPath,
));
$this->removeTemporaryFiles();
$this->filesystem->remove($this->absoluteFixturesPath . DIRECTORY_SEPARATOR . 'sqldb.map');
}
/**
* Remove all temporary files
*
* @return void
*/
protected function removeTemporaryFiles()
{
$finder = new Finder();
// Delete temporary files
$datas = $finder->name('*_schema.xml')->name('build*')->in($this->absoluteFixturesPath);
foreach($datas as $data) {
$this->filesystem->remove($data);
}
}
}

View file

@ -195,6 +195,9 @@ EOT
<user>%username%</user>
<password>%password%</password>
</connection>
<settings>
<setting id="charset">%charset%</setting>
</settings>
</datasource>
EOT
@ -204,6 +207,7 @@ EOT
'%dsn%' => $datasource['connection']['dsn'],
'%username%' => $datasource['connection']['user'],
'%password%' => $datasource['connection']['password'],
'%charset%' => $container->getParameter('propel.charset'),
));
}
@ -310,7 +314,7 @@ EOT;
protected function checkConfiguration()
{
$parameters = $this->container->get('propel.configuration')->getParameters();
if (0 === count($parameters['datasources'])) {
if (!isset($parameters['datasources']) ||0 === count($parameters['datasources'])) {
throw new \RuntimeException('Propel should be configured (no database configuration found).');
}
}

View file

@ -220,13 +220,18 @@ SQL will be write in `app/propel/sql/`.
You can load your own fixtures by using the following command:
> php app/console propel:load-fixtures [-d|--dir="..."] [--connection[="..."]]
> php app/console propel:load-fixtures [-d|--dir[="..."]] [--xml] [--sql] [--connection[="..."]]
As usual, `--connection` allows to specify a connection.
The `--dir` option allows to specify a directory containing the fixtures (default is: `app/propel/fixtures/`).
Note that the `--dir` expects a relative path from the root dir (which is `app/`).
The --xml parameter allows you to load only XML fixtures.
The --sql parameter allows you to load only SQL fixtures.
You can mix --xml parameter and --sql parameter to load XML and SQL fixtures.
If none of this parameter are set all files, XML and SQL, in the directory will be load.
A valid _XML fixtures file_ is:
<?xml version="1.0" encoding="utf-8"?>