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 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;
|
2011-12-25 14:21:54 +01:00
|
|
|
use Symfony\Component\Filesystem\Filesystem;
|
2011-04-19 14:18:42 +02:00
|
|
|
use Symfony\Component\Finder\Finder;
|
2012-04-06 12:04:43 +02:00
|
|
|
use Symfony\Component\HttpKernel\Bundle\BundleInterface;
|
2011-04-19 14:18:42 +02:00
|
|
|
|
2011-09-05 22:18:18 +02:00
|
|
|
use Propel\PropelBundle\Command\AbstractPropelCommand;
|
2011-09-06 13:30:09 +02:00
|
|
|
use Propel\PropelBundle\DataFixtures\Loader\YamlDataLoader;
|
|
|
|
use Propel\PropelBundle\DataFixtures\Loader\XmlDataLoader;
|
2011-09-02 15:51:38 +02:00
|
|
|
|
2011-04-19 14:18:42 +02:00
|
|
|
/**
|
2011-09-07 10:46:56 +02:00
|
|
|
* FixturesLoadCommand
|
2011-04-19 14:18:42 +02:00
|
|
|
*
|
|
|
|
* @author William DURAND <william.durand1@gmail.com>
|
|
|
|
*/
|
2011-09-07 10:46:56 +02:00
|
|
|
class FixturesLoadCommand extends AbstractPropelCommand
|
2011-04-19 14:18:42 +02:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Default fixtures directory.
|
2011-09-04 23:16:11 +02:00
|
|
|
* @var string
|
2011-04-19 14:18:42 +02:00
|
|
|
*/
|
2011-08-23 15:59:14 +02:00
|
|
|
private $defaultFixturesDir = 'app/propel/fixtures';
|
2011-05-19 15:49:38 +02:00
|
|
|
/**
|
|
|
|
* Absolute path for fixtures directory
|
2011-09-04 23:16:11 +02:00
|
|
|
* @var string
|
2011-05-19 15:49:38 +02:00
|
|
|
*/
|
|
|
|
private $absoluteFixturesPath = '';
|
|
|
|
/**
|
|
|
|
* Filesystem for manipulating files
|
2011-12-25 14:21:54 +01:00
|
|
|
* @var \Symfony\Component\Filesystem\Filesystem
|
2011-05-19 15:49:38 +02:00
|
|
|
*/
|
|
|
|
private $filesystem = null;
|
2012-04-06 11:30:32 +02:00
|
|
|
/**
|
|
|
|
* Bundle the fixtures are being loaded from
|
|
|
|
* @var Symfony\Component\HttpKernel\Bundle\BundleInterface
|
|
|
|
*/
|
|
|
|
private $bundle;
|
2011-04-19 14:18:42 +02:00
|
|
|
/**
|
|
|
|
* @see Command
|
|
|
|
*/
|
|
|
|
protected function configure()
|
|
|
|
{
|
|
|
|
$this
|
2011-09-04 23:16:11 +02:00
|
|
|
->setDescription('Load XML, SQL and/or YAML fixtures')
|
2012-04-04 23:28:06 +02:00
|
|
|
->addArgument('bundle', InputArgument::OPTIONAL, 'The bundle to load fixtures from')
|
2011-09-04 23:16:11 +02:00
|
|
|
->addOption(
|
|
|
|
'dir', 'd', InputOption::VALUE_OPTIONAL,
|
|
|
|
'The directory where XML, SQL and/or YAML 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-09-02 15:51:38 +02:00
|
|
|
->addOption('yml', '', InputOption::VALUE_NONE, 'Load YAML 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-09-07 10:46:56 +02:00
|
|
|
The <info>propel:fixtures:load</info> loads <info>XML</info>, <info>SQL</info> and/or <info>YAML</info> fixtures.
|
2011-04-19 14:18:42 +02:00
|
|
|
|
2011-09-07 10:46:56 +02:00
|
|
|
<info>php app/console propel:fixtures:load</info>
|
2011-04-19 14:18:42 +02:00
|
|
|
|
|
|
|
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-09-02 15:51:38 +02:00
|
|
|
The <info>--yml</info> parameter allows you to load only <info>YAML</info> fixtures.
|
2011-05-24 23:53:55 +02:00
|
|
|
|
2011-09-02 15:51:38 +02:00
|
|
|
You can mix <info>--xml</info>, <info>--sql</info> and <info>--yml</info> parameters to load XML, YAML and SQL fixtures at the same time.
|
|
|
|
If none of this parameter is set, all XML, YAML 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>
|
2011-09-02 17:21:39 +02:00
|
|
|
<Fixtures>
|
|
|
|
<Object Namespace="Awesome">
|
|
|
|
<o1 Title="My title" MyFoo="bar" />
|
|
|
|
</Object>
|
|
|
|
<Related Namespace="Awesome">
|
|
|
|
<r1 ObjectId="o1" Description="Hello world !" />
|
|
|
|
</Related>
|
|
|
|
</Fixtures>
|
2011-04-19 14:18:42 +02:00
|
|
|
</comment>
|
2011-09-02 15:51:38 +02:00
|
|
|
|
|
|
|
YAML fixtures are:
|
|
|
|
<comment>
|
2011-09-02 17:21:39 +02:00
|
|
|
\Awesome\Object:
|
|
|
|
o1:
|
|
|
|
Title: My title
|
|
|
|
MyFoo: bar
|
|
|
|
|
|
|
|
\Awesome\Related:
|
|
|
|
r1:
|
|
|
|
ObjectId: o1
|
|
|
|
Description: Hello world !
|
2011-09-02 15:51:38 +02:00
|
|
|
</comment>
|
2011-04-19 14:18:42 +02:00
|
|
|
EOT
|
|
|
|
)
|
2011-09-07 10:46:56 +02:00
|
|
|
->setName('propel:fixtures:load')
|
2011-04-19 14:18:42 +02:00
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @see Command
|
|
|
|
*
|
|
|
|
* @throws \InvalidArgumentException When the target directory does not exist
|
|
|
|
*/
|
|
|
|
protected function execute(InputInterface $input, OutputInterface $output)
|
|
|
|
{
|
2011-09-07 10:46:56 +02:00
|
|
|
$this->writeSection($output, '[Propel] You are running the command: propel:fixtures:load');
|
2011-06-22 16:47:19 +02:00
|
|
|
|
2011-05-19 15:49:38 +02:00
|
|
|
$this->filesystem = new Filesystem();
|
2012-04-04 23:28:06 +02:00
|
|
|
|
2012-04-06 11:30:32 +02:00
|
|
|
if ('@' === substr($input->getArgument('bundle'), 0, 1)) {
|
|
|
|
$this->bundle = $this
|
2012-04-04 23:28:06 +02:00
|
|
|
->getContainer()
|
|
|
|
->get('kernel')
|
2012-04-06 11:30:32 +02:00
|
|
|
->getBundle(substr($input->getArgument('bundle'), 1));
|
2012-04-06 12:04:43 +02:00
|
|
|
$this->absoluteFixturesPath = $this->getFixturesPath($this->bundle);
|
2012-04-04 23:28:06 +02:00
|
|
|
} else {
|
|
|
|
$this->absoluteFixturesPath = realpath($this->getApplication()->getKernel()->getRootDir() . '/../' . $input->getOption('dir'));
|
|
|
|
}
|
2011-05-19 15:49:38 +02:00
|
|
|
|
2011-08-30 18:30:27 +02:00
|
|
|
if ($input->getOption('verbose')) {
|
|
|
|
$this->additionalPhingArgs[] = 'verbose';
|
|
|
|
}
|
|
|
|
|
2011-08-30 11:54:40 +02:00
|
|
|
if (!$this->absoluteFixturesPath && !file_exists($this->absoluteFixturesPath)) {
|
2011-09-07 10:52:01 +02:00
|
|
|
return $this->writeSection($output, array(
|
2011-10-24 12:19:42 +02:00
|
|
|
'The fixtures directory "' . $this->absoluteFixturesPath . '" does not exist.'
|
2011-09-07 10:52:01 +02:00
|
|
|
), 'fg=white;bg=red');
|
2011-08-30 11:54:40 +02:00
|
|
|
}
|
|
|
|
|
2011-09-02 15:51:38 +02:00
|
|
|
$noOptions = (!$input->getOption('xml') && !$input->getOption('sql') && !$input->getOption('yml'));
|
2011-05-19 15:49:38 +02:00
|
|
|
|
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)) {
|
2011-09-05 22:18:18 +02:00
|
|
|
$output->writeln('<info>No SQL fixtures found.</info>');
|
2011-08-18 14:01:21 +02:00
|
|
|
}
|
2011-05-19 15:49:38 +02:00
|
|
|
}
|
2011-09-02 15:51:38 +02:00
|
|
|
|
2011-09-02 17:21:39 +02:00
|
|
|
if ($input->getOption('xml') || $noOptions) {
|
|
|
|
if (-1 === $this->loadFixtures($input, $output, 'xml')) {
|
2011-09-05 22:18:18 +02:00
|
|
|
$output->writeln('<info>No XML fixtures found.</info>');
|
2011-09-02 17:21:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-02 15:51:38 +02:00
|
|
|
if ($input->getOption('yml') || $noOptions) {
|
2011-09-02 17:21:39 +02:00
|
|
|
if (-1 === $this->loadFixtures($input, $output, 'yml')) {
|
2011-09-05 22:18:18 +02:00
|
|
|
$output->writeln('<info>No YAML fixtures found.</info>');
|
2011-09-02 15:51:38 +02:00
|
|
|
}
|
|
|
|
}
|
2011-05-19 15:49:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-09-02 17:21:39 +02:00
|
|
|
* Load 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
|
|
|
|
*/
|
2011-09-02 17:21:39 +02:00
|
|
|
protected function loadFixtures(InputInterface $input, OutputInterface $output, $type = null)
|
2011-05-19 15:49:38 +02:00
|
|
|
{
|
2011-09-02 17:21:39 +02:00
|
|
|
if (null === $type) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-02-07 16:15:54 +01:00
|
|
|
$datas = $this->getFixtureFiles($type);
|
2011-07-30 19:26:48 +02:00
|
|
|
|
2011-09-02 15:51:38 +02:00
|
|
|
if (count(iterator_to_array($datas)) === 0) {
|
2011-08-18 14:01:21 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2011-09-02 15:51:38 +02:00
|
|
|
list($name, $defaultConfig) = $this->getConnection($input, $output);
|
2011-04-19 14:18:42 +02:00
|
|
|
|
2011-09-02 17:21:39 +02:00
|
|
|
if ('yml' === $type) {
|
|
|
|
$loader = new YamlDataLoader($this->getApplication()->getKernel()->getRootDir());
|
|
|
|
} elseif ('xml' === $type) {
|
|
|
|
$loader = new XmlDataLoader($this->getApplication()->getKernel()->getRootDir());
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
2011-08-30 16:10:52 +02:00
|
|
|
|
2011-09-02 15:51:38 +02:00
|
|
|
try {
|
2011-09-02 15:58:26 +02:00
|
|
|
$nb = $loader->load($datas, $name);
|
2011-09-02 15:51:38 +02:00
|
|
|
} catch (\Exception $e) {
|
|
|
|
$this->writeSection($output, array(
|
|
|
|
'[Propel] Exception',
|
|
|
|
'',
|
|
|
|
$e->getMessage()), 'fg=white;bg=red');
|
2011-12-05 18:42:24 +01:00
|
|
|
|
2011-09-02 15:51:38 +02:00
|
|
|
return false;
|
2011-08-23 15:59:14 +02:00
|
|
|
}
|
2011-08-18 14:01:21 +02:00
|
|
|
|
2011-09-02 15:58:26 +02:00
|
|
|
$this->writeSection(
|
|
|
|
$output,
|
2011-09-02 17:21:39 +02:00
|
|
|
sprintf('<comment>%s</comment> %s fixtures file%s loaded.', $nb, strtoupper($type), $nb > 1 ? 's' : ''),
|
2011-09-02 15:58:26 +02:00
|
|
|
'fg=white;bg=black'
|
|
|
|
);
|
|
|
|
|
2011-09-02 15:51:38 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-05-19 15:49:38 +02:00
|
|
|
/**
|
|
|
|
* 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-08-23 15:59:14 +02:00
|
|
|
$tmpdir = $this->getApplication()->getKernel()->getRootDir() . '/cache/propel';
|
2012-02-07 16:15:54 +01:00
|
|
|
$datas = $this->getFixtureFiles('sql');
|
2011-05-19 15:49:38 +02:00
|
|
|
|
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);
|
|
|
|
|
2011-05-19 15:49:38 +02:00
|
|
|
// Create a "sqldb.map" file
|
|
|
|
$sqldbContent = '';
|
2011-08-18 15:23:35 +02:00
|
|
|
foreach($datas as $data) {
|
2011-09-05 22:18:18 +02:00
|
|
|
$output->writeln(sprintf('<info>Loading SQL fixtures from</info> <comment>%s</comment>.', $data));
|
2011-08-23 15:59:14 +02:00
|
|
|
|
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 14:01:21 +02:00
|
|
|
|
2011-08-18 15:23:35 +02:00
|
|
|
if ('' === $sqldbContent) {
|
2011-08-18 14:01:21 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2011-08-23 15:59:14 +02:00
|
|
|
$sqldbFile = $tmpdir . '/fixtures/sqldb.map';
|
2011-05-19 15:49:38 +02:00
|
|
|
file_put_contents($sqldbFile, $sqldbContent);
|
|
|
|
|
2011-08-23 15:59:14 +02:00
|
|
|
if (!$this->insertSql($defaultConfig, $tmpdir . '/fixtures', $tmpdir, $output)) {
|
|
|
|
return -1;
|
|
|
|
}
|
2011-05-19 15:49:38 +02:00
|
|
|
|
2011-08-23 15:59:14 +02:00
|
|
|
$this->filesystem->remove($tmpdir);
|
2011-08-18 14:01:21 +02:00
|
|
|
|
|
|
|
return 0;
|
2011-05-19 15:49:38 +02:00
|
|
|
}
|
|
|
|
|
2011-09-02 15:51:38 +02:00
|
|
|
/**
|
|
|
|
* Prepare the cache directory
|
|
|
|
*
|
|
|
|
* @param string $tmpdir The temporary directory path.
|
|
|
|
*/
|
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);
|
|
|
|
}
|
|
|
|
|
2011-05-19 15:49:38 +02:00
|
|
|
/**
|
2011-08-23 15:59:14 +02:00
|
|
|
* Insert SQL
|
2011-05-19 15:49:38 +02:00
|
|
|
*/
|
2011-08-23 15:59:14 +02:00
|
|
|
protected function insertSql($config, $sqlDir, $schemaDir, $output)
|
2011-05-19 15:49:38 +02:00
|
|
|
{
|
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-05-19 15:49:38 +02:00
|
|
|
|
2011-08-23 15:59:14 +02:00
|
|
|
if (true === $ret) {
|
2011-09-05 22:18:18 +02:00
|
|
|
$this->writeSection($output, array(
|
|
|
|
'', 'All SQL statements have been executed.'
|
|
|
|
), 'fg=green;bg=black');
|
2011-08-23 15:59:14 +02:00
|
|
|
} else {
|
2011-08-31 16:12:56 +02:00
|
|
|
$this->writeTaskError($output, 'insert-sql', false);
|
2011-12-05 18:42:24 +01:00
|
|
|
|
2011-08-23 15:59:14 +02:00
|
|
|
return false;
|
2011-05-19 15:49:38 +02:00
|
|
|
}
|
2011-12-05 18:42:24 +01:00
|
|
|
|
2011-08-23 15:59:14 +02:00
|
|
|
return true;
|
2011-04-19 14:18:42 +02:00
|
|
|
}
|
2012-02-07 16:15:54 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the fixtures files to load.
|
|
|
|
*
|
|
|
|
* @param string $type The extension of the files.
|
|
|
|
* @param string $in The directory in which we search the files. If null,
|
|
|
|
* we'll use the absoluteFixturesPath property.
|
|
|
|
*
|
|
|
|
* @return \Iterator An iterator through the files.
|
|
|
|
*/
|
|
|
|
protected function getFixtureFiles($type = 'sql', $in = null)
|
|
|
|
{
|
|
|
|
$finder = new Finder();
|
|
|
|
$finder->sortByName()->name('*.' . $type);
|
|
|
|
|
2012-04-06 11:30:32 +02:00
|
|
|
$files = $finder->in(null !== $in ? $in : $this->absoluteFixturesPath);
|
|
|
|
|
|
|
|
if (null === $this->bundle) {
|
|
|
|
return $files;
|
|
|
|
}
|
|
|
|
|
|
|
|
$finalFixtureFiles = array();
|
|
|
|
|
|
|
|
foreach ($files as $file) {
|
2012-04-06 12:04:43 +02:00
|
|
|
$fixtureFilePath = str_replace($this->getFixturesPath($this->bundle) . DIRECTORY_SEPARATOR, '', $file->getRealPath());
|
2012-04-06 11:30:32 +02:00
|
|
|
$logicalName = sprintf('@%s/Resources/fixtures/%s', $this->bundle->getName(), $fixtureFilePath);
|
|
|
|
$finalFixtureFiles[] = new \SplFileInfo($this->getFileLocator()->locate($logicalName));
|
|
|
|
}
|
|
|
|
|
|
|
|
return new \ArrayIterator($finalFixtureFiles);
|
2012-02-07 16:15:54 +01:00
|
|
|
}
|
2012-04-06 12:04:43 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the path the command will look into to find fixture files
|
|
|
|
*
|
|
|
|
* @return String
|
|
|
|
*/
|
|
|
|
protected function getFixturesPath(BundleInterface $bundle)
|
|
|
|
{
|
|
|
|
return $bundle->getPath().DIRECTORY_SEPARATOR.'Resources'.DIRECTORY_SEPARATOR.'fixtures';
|
|
|
|
}
|
2011-04-19 14:18:42 +02:00
|
|
|
}
|