2011-04-19 14:18:42 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Propel\PropelBundle\Command;
|
|
|
|
|
|
|
|
use Propel\PropelBundle\Command\PhingCommand;
|
|
|
|
use Symfony\Component\Console\Input\InputOption;
|
2011-05-19 15:49:38 +02:00
|
|
|
use Symfony\Component\Console\Input\InputArgument;
|
2011-04-19 14:18:42 +02:00
|
|
|
use Symfony\Component\Console\Input\InputInterface;
|
|
|
|
use Symfony\Component\Console\Output\OutputInterface;
|
|
|
|
use Symfony\Component\Finder\Finder;
|
|
|
|
use Symfony\Component\HttpKernel\Util\Filesystem;
|
|
|
|
|
|
|
|
/**
|
2011-05-19 15:49:38 +02:00
|
|
|
* LoadFixturesCommand
|
2011-04-19 14:18:42 +02:00
|
|
|
*
|
|
|
|
* @author William DURAND <william.durand1@gmail.com>
|
|
|
|
*/
|
|
|
|
class LoadFixturesCommand extends PhingCommand
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Default fixtures directory.
|
|
|
|
*/
|
|
|
|
private $defaultFixturesDir = 'propel/fixtures';
|
2011-05-19 15:49:38 +02:00
|
|
|
/**
|
|
|
|
* Absolute path for fixtures directory
|
|
|
|
*/
|
|
|
|
private $absoluteFixturesPath = '';
|
|
|
|
/**
|
|
|
|
* Filesystem for manipulating files
|
|
|
|
*/
|
|
|
|
private $filesystem = null;
|
|
|
|
|
2011-04-19 14:18:42 +02:00
|
|
|
/**
|
|
|
|
* @see Command
|
|
|
|
*/
|
|
|
|
protected function configure()
|
|
|
|
{
|
|
|
|
$this
|
2011-05-19 15:49:38 +02:00
|
|
|
->setDescription('Load XML fixtures')
|
2011-05-19 15:49:38 +02:00
|
|
|
->addOption('dir', 'd', InputOption::VALUE_OPTIONAL, 'The directory where XML or/and SQL fixtures files are located', $this->defaultFixturesDir)
|
2011-05-19 15:49:38 +02:00
|
|
|
->addOption('xml', '', InputOption::VALUE_NONE, 'Load XML fixtures')
|
|
|
|
->addOption('sql', '', InputOption::VALUE_NONE, 'Load SQL fixtures')
|
2011-04-19 14:18:42 +02:00
|
|
|
->addOption('connection', null, InputOption::VALUE_OPTIONAL, 'Set this parameter to define a connection to use')
|
|
|
|
->setHelp(<<<EOT
|
2011-05-19 15:49:38 +02:00
|
|
|
The <info>propel:load-fixtures</info> loads <info>XML</info> and/or <info>SQL</info> fixtures.
|
2011-04-19 14:18:42 +02:00
|
|
|
|
|
|
|
<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).
|
|
|
|
|
2011-05-19 15:49:38 +02:00
|
|
|
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.
|
2011-05-24 23:53:55 +02:00
|
|
|
|
2011-05-19 15:49:38 +02:00
|
|
|
You can mix <info>--xml</info> and <info>--sql</info> parameters to load both XML and SQL fixtures.
|
|
|
|
If none of this parameter is set, all XML and SQL files in the directory will be load.
|
2011-04-19 14:18:42 +02:00
|
|
|
|
|
|
|
XML fixtures files are the same XML files you can get with the command <info>propel:data-dump</info>:
|
|
|
|
<comment>
|
|
|
|
<?xml version="1.0" encoding="utf-8"?>
|
|
|
|
<dataset name="all">
|
|
|
|
<Object Id="..." />
|
|
|
|
</dataset>
|
|
|
|
</comment>
|
|
|
|
EOT
|
|
|
|
)
|
|
|
|
->setName('propel:load-fixtures')
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @see Command
|
|
|
|
*
|
|
|
|
* @throws \InvalidArgumentException When the target directory does not exist
|
|
|
|
*/
|
|
|
|
protected function execute(InputInterface $input, OutputInterface $output)
|
|
|
|
{
|
2011-06-22 16:47:19 +02:00
|
|
|
$this->writeSection($output, '[Propel] You are running the command: propel:load-fixtures');
|
|
|
|
|
2011-05-19 15:49:38 +02:00
|
|
|
$this->absoluteFixturesPath = $this->getApplication()->getKernel()->getRootDir() . DIRECTORY_SEPARATOR . $input->getOption('dir');
|
|
|
|
$this->filesystem = new Filesystem();
|
|
|
|
|
|
|
|
$noOptions = (!$input->getOption('xml') && !$input->getOption('sql'));
|
|
|
|
|
2011-06-22 16:47:19 +02:00
|
|
|
if ($input->getOption('xml') || $noOptions) {
|
2011-05-19 15:49:38 +02:00
|
|
|
$this->loadXmlFixtures($input, $output);
|
|
|
|
}
|
|
|
|
|
2011-06-22 16:47:19 +02:00
|
|
|
if ($input->getOption('sql') || $noOptions) {
|
2011-05-19 15:49:38 +02:00
|
|
|
$this->loadSqlFixtures($input, $output);
|
|
|
|
}
|
2011-04-19 14:18:42 +02:00
|
|
|
|
2011-05-19 15:49:38 +02:00
|
|
|
$output->writeln('<info>Fixtures successfully loaded.</info>');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Load XML fixtures
|
2011-05-24 23:53:55 +02:00
|
|
|
*
|
2011-05-19 15:49:38 +02:00
|
|
|
* @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();
|
2011-05-24 23:53:55 +02:00
|
|
|
|
2011-04-19 14:18:42 +02:00
|
|
|
// Create a "datadb.map" file
|
|
|
|
$datadbContent = '';
|
2011-05-19 15:49:38 +02:00
|
|
|
$datas = $finder->name('*.xml')->in($this->absoluteFixturesPath);
|
2011-04-19 14:18:42 +02:00
|
|
|
foreach($datas as $data) {
|
2011-04-19 14:34:02 +02:00
|
|
|
$output->writeln(sprintf('Loaded fixtures from <comment>%s</comment>.', $data));
|
2011-05-19 15:49:38 +02:00
|
|
|
|
|
|
|
$datadbContent .= $data->getFilename() . '=default' . PHP_EOL;
|
2011-04-19 14:18:42 +02:00
|
|
|
}
|
|
|
|
|
2011-05-19 15:49:38 +02:00
|
|
|
$datadbFile = $this->absoluteFixturesPath . '/datadb.map';
|
2011-04-19 14:18:42 +02:00
|
|
|
file_put_contents($datadbFile, $datadbContent);
|
|
|
|
|
|
|
|
$dest = $this->getApplication()->getKernel()->getRootDir() . '/propel/sql/';
|
|
|
|
$this->callPhing('datasql', array(
|
2011-05-19 15:49:38 +02:00
|
|
|
'propel.sql.dir' => $dest,
|
|
|
|
'propel.schema.dir' => $this->absoluteFixturesPath,
|
2011-04-19 14:18:42 +02:00
|
|
|
));
|
|
|
|
|
|
|
|
// Insert SQL
|
|
|
|
$insertCommand = new InsertSqlCommand();
|
|
|
|
$insertCommand->setApplication($this->getApplication());
|
|
|
|
|
2011-05-19 15:49:38 +02:00
|
|
|
// By-pass the '--force' required option for inserting SQL
|
|
|
|
$this->addOption('force', '', InputOption::VALUE_NONE, '');
|
2011-04-19 14:18:42 +02:00
|
|
|
$input->setOption('force', true);
|
|
|
|
|
|
|
|
$insertCommand->execute($input, $output);
|
|
|
|
|
2011-05-19 15:49:38 +02:00
|
|
|
$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>');
|
|
|
|
|
2011-04-19 14:18:42 +02:00
|
|
|
$finder = new Finder();
|
2011-05-19 15:49:38 +02:00
|
|
|
|
|
|
|
// Create a "sqldb.map" file
|
|
|
|
$sqldbContent = '';
|
|
|
|
$datas = $finder->name('*.sql')->in($this->absoluteFixturesPath);
|
2011-04-19 14:18:42 +02:00
|
|
|
foreach($datas as $data) {
|
2011-05-19 15:49:38 +02:00
|
|
|
$output->writeln(sprintf('Loaded fixtures from <comment>%s</comment>.', $data));
|
|
|
|
|
|
|
|
$sqldbContent .= $data->getFilename() . '=default' . PHP_EOL;
|
2011-04-19 14:18:42 +02:00
|
|
|
}
|
|
|
|
|
2011-05-19 15:49:38 +02:00
|
|
|
$sqldbFile = $this->absoluteFixturesPath . DIRECTORY_SEPARATOR . 'sqldb.map';
|
2011-04-19 14:18:42 +02:00
|
|
|
|
2011-05-19 15:49:38 +02:00
|
|
|
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);
|
|
|
|
}
|
2011-04-19 14:18:42 +02:00
|
|
|
}
|
|
|
|
}
|