propel-bundle/Command/LoadFixturesCommand.php

263 lines
8.8 KiB
PHP
Raw Normal View History

2011-04-19 14:18:42 +02:00
<?php
2011-08-30 23:29:49 +02:00
/**
* 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
*/
2011-04-19 14:18:42 +02:00
namespace Propel\PropelBundle\Command;
use Propel\PropelBundle\Command\PhingCommand;
use Symfony\Component\Console\Input\InputOption;
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;
/**
* LoadFixturesCommand
2011-04-19 14:18:42 +02:00
*
* @author William DURAND <william.durand1@gmail.com>
*/
class LoadFixturesCommand extends PhingCommand
{
/**
* Default fixtures directory.
*/
2011-08-23 15:59:14 +02:00
private $defaultFixturesDir = 'app/propel/fixtures';
/**
* 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
->setDescription('Load XML 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')
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
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).
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
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');
$this->filesystem = new Filesystem();
2011-08-23 15:59:14 +02:00
$this->absoluteFixturesPath = realpath($this->getApplication()->getKernel()->getRootDir() . '/../' . $input->getOption('dir'));
if ($input->getOption('verbose')) {
$this->additionalPhingArgs[] = 'verbose';
}
if (!$this->absoluteFixturesPath && !file_exists($this->absoluteFixturesPath)) {
return $output->writeln('<info>[Propel] The fixtures directory does not exist.</info>');
}
$noOptions = (!$input->getOption('xml') && !$input->getOption('sql'));
2011-06-22 16:47:19 +02:00
if ($input->getOption('xml') || $noOptions) {
2011-08-23 15:59:14 +02:00
if (-1 === $this->loadXmlFixtures($input, $output)) {
$output->writeln('<info>[Propel] No XML fixtures found.</info>');
}
}
2011-06-22 16:47:19 +02:00
if ($input->getOption('sql') || $noOptions) {
2011-08-23 15:59:14 +02:00
if (-1 === $this->loadSqlFixtures($input, $output)) {
$output->writeln('<info>[Propel] No SQL fixtures found.</info>');
}
}
}
/**
* Load XML fixtures
2011-05-24 23:53:55 +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)
{
$finder = new Finder();
2011-08-23 15:59:14 +02:00
$tmpdir = $this->getApplication()->getKernel()->getRootDir() . '/cache/propel';
2011-08-18 15:23:35 +02:00
$datas = $finder->name('*.xml')->in($this->absoluteFixturesPath);
2011-05-24 23:53:55 +02:00
2011-08-23 15:59:14 +02:00
$this->prepareCache($tmpdir);
2011-08-18 15:29:12 +02:00
2011-08-23 15:59:14 +02:00
list($name, $defaultConfig) = $this->getConnection($input, $output);
2011-07-30 19:26:48 +02:00
2011-08-23 15:59:14 +02:00
if (!$this->createDbFiles($name, $datas, $tmpdir, $output)) {
return -1;
}
2011-08-23 15:59:14 +02:00
// Convert XML to SQL
2011-08-30 16:10:52 +02:00
$ret = $this->callPhing('datasql', array(
2011-08-23 15:59:14 +02:00
'propel.sql.dir' => $tmpdir,
'propel.schema.dir' => $tmpdir,
2011-04-19 14:18:42 +02:00
));
2011-08-30 16:10:52 +02:00
if ($ret === false) {
2011-08-31 16:12:56 +02:00
$this->writeTaskError($output, 'datasql');
2011-08-30 16:10:52 +02:00
return -2;
}
2011-08-23 15:59:14 +02:00
if (!$this->insertSql($defaultConfig, $tmpdir . '/fixtures', $tmpdir, $output)) {
return -1;
}
2011-08-23 15:59:14 +02:00
$this->filesystem->remove($tmpdir);
return 0;
}
/**
* 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)
{
2011-04-19 14:18:42 +02:00
$finder = new Finder();
2011-08-23 15:59:14 +02:00
$tmpdir = $this->getApplication()->getKernel()->getRootDir() . '/cache/propel';
2011-08-18 15:23:35 +02:00
$datas = $finder->name('*.sql')->in($this->absoluteFixturesPath);
2011-08-23 15:59:14 +02:00
$this->prepareCache($tmpdir);
2011-08-18 15:29:12 +02:00
list($name, $defaultConfig) = $this->getConnection($input, $output);
// Create a "sqldb.map" file
$sqldbContent = '';
2011-08-18 15:23:35 +02:00
foreach($datas as $data) {
2011-08-23 15:59:14 +02:00
$output->writeln(sprintf('<info>[Propel] Loading SQL fixtures from</info> <comment>%s</comment>', $data));
2011-08-18 15:23:35 +02:00
$sqldbContent .= $data->getFilename() . '=' . $name . PHP_EOL;
2011-08-23 15:59:14 +02:00
$this->filesystem->copy($data, $tmpdir . '/fixtures/' . $data->getFilename(), true);
2011-08-18 15:23:35 +02:00
}
2011-08-18 15:23:35 +02:00
if ('' === $sqldbContent) {
return -1;
}
2011-08-23 15:59:14 +02:00
$sqldbFile = $tmpdir . '/fixtures/sqldb.map';
file_put_contents($sqldbFile, $sqldbContent);
2011-08-23 15:59:14 +02:00
if (!$this->insertSql($defaultConfig, $tmpdir . '/fixtures', $tmpdir, $output)) {
return -1;
}
2011-08-23 15:59:14 +02:00
$this->filesystem->remove($tmpdir);
return 0;
}
2011-08-23 15:59:14 +02:00
protected function prepareCache($tmpdir)
{
// Recreate a propel directory in cache
$this->filesystem->remove($tmpdir);
$this->filesystem->mkdir($tmpdir);
$fixturesdir = $tmpdir . '/fixtures/';
$this->filesystem->remove($fixturesdir);
$this->filesystem->mkdir($fixturesdir);
}
protected function createDbFiles($name, $datas, $tmpdir, $output)
{
// Create a "datadb.map" file
$dbContent = '';
foreach($datas as $data) {
$output->writeln(sprintf('<info>[Propel] Loading XML fixtures from</info> <comment>%s</comment>', $data));
$dbContent .= 'fixtures/' . $data->getFilename() . '=' . $name . PHP_EOL;
$this->filesystem->copy($data, $tmpdir . '/fixtures/' . $data->getFilename(), true);
}
// No XML found
if ('' === $dbContent) {
return false;
}
// Write datadb.map and sqldb.map files
$datadbFile = $tmpdir . '/datadb.map';
$sqldbFile = $tmpdir . '/fixtures/sqldb.map';
file_put_contents($datadbFile, $dbContent);
$dbContent = preg_replace('#fixtures/#', '', $dbContent);
$dbContent = preg_replace('#\.xml=#', '.sql=', $dbContent);
file_put_contents($sqldbFile, $dbContent);
return true;
}
/**
2011-08-23 15:59:14 +02:00
* Insert SQL
*/
2011-08-23 15:59:14 +02:00
protected function insertSql($config, $sqlDir, $schemaDir, $output)
{
2011-08-23 15:59:14 +02:00
// Insert SQL
$ret = $this->callPhing('insert-sql', array(
'propel.database.url' => $config['connection']['dsn'],
'propel.database.database' => $config['adapter'],
'propel.database.user' => $config['connection']['user'],
'propel.database.password' => $config['connection']['password'],
'propel.schema.dir' => $schemaDir,
'propel.sql.dir' => $sqlDir,
));
2011-08-23 15:59:14 +02:00
if (true === $ret) {
$output->writeln('<info>[Propel] All SQL statements have been executed.</info>');
} else {
2011-08-31 16:12:56 +02:00
$this->writeTaskError($output, 'insert-sql', false);
2011-08-23 15:59:14 +02:00
return false;
}
2011-08-23 15:59:14 +02:00
return true;
2011-04-19 14:18:42 +02:00
}
}