propel-bundle/Command/FixturesDumpCommand.php

96 lines
3.1 KiB
PHP
Raw Permalink Normal View History

2011-03-25 18:44:32 +01: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
*/
namespace Propel\Bundle\PropelBundle\Command;
2011-08-30 23:29:49 +02:00
use Propel\Bundle\PropelBundle\DataFixtures\Dumper\YamlDataDumper;
2011-03-25 18:44:32 +01:00
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
2011-03-25 18:44:32 +01:00
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Filesystem\Filesystem;
2011-03-25 18:44:32 +01:00
/**
* FixturesDumpCommand.
2011-03-25 18:44:32 +01:00
*
2011-04-19 13:59:19 +02:00
* @author William DURAND <william.durand1@gmail.com>
2011-03-25 18:44:32 +01:00
*/
2012-04-20 13:54:05 +02:00
class FixturesDumpCommand extends AbstractCommand
2011-03-25 18:44:32 +01:00
{
/**
* Default fixtures directory.
* @var string
*/
private $defaultFixturesDir = 'app/propel/fixtures';
2011-03-26 12:34:17 +01:00
2011-03-25 18:44:32 +01:00
/**
* @see Command
*/
protected function configure()
{
$this
->setDescription('Dump data from database into YAML fixtures file.')
2011-03-25 18:44:32 +01:00
->addOption('connection', null, InputOption::VALUE_OPTIONAL, 'Set this parameter to define a connection to use')
->addOption('dir', null, InputOption::VALUE_OPTIONAL, 'Set this parameter to define a fixture directory')
2011-03-25 18:44:32 +01:00
->setHelp(<<<EOT
The <info>propel:fixtures:dump</info> dumps data from database into YAML fixtures file.
<info>php app/console propel:fixtures:dump</info>
2011-03-25 18:44:32 +01:00
2011-03-26 18:30:43 +01:00
The <info>--connection</info> parameter allows you to change the connection to use.
The <info>--dir</info> parameter allows you to change the output directory.
2011-03-26 18:30:43 +01:00
The default connection is the active connection (propel.dbal.default_connection).
2011-03-25 18:44:32 +01:00
EOT
)
->setName('propel:fixtures:dump')
2011-03-25 18:44:32 +01:00
;
}
/**
* @see Command
*
* @throws \InvalidArgumentException When the target directory does not exist
*/
protected function execute(InputInterface $input, OutputInterface $output)
2011-04-19 13:59:19 +02:00
{
list($name, $defaultConfig) = $this->getConnection($input, $output);
$fixtureDir = $input->getOption('dir') ? $input->getOption('dir') : $this->defaultFixturesDir;
2011-03-25 18:44:32 +01:00
$path = realpath($this->getApplication()->getKernel()->getRootDir() . '/../') . '/' . $fixtureDir;
if (!file_exists($path)) {
$output->writeln("<info>The $path folder does not exists.</info>");
if ($this->askConfirmation($output, "<question>Do you want me to create it for you ?</question> [Yes]")) {
$fs = new Filesystem();
$fs->mkdir($path);
} else {
throw new \IOException(sprintf('Unable to find the %s folder', $path));
}
}
2011-09-07 10:33:20 +02:00
$filename = $path . '/fixtures_' . time() . '.yml';
2011-03-25 18:44:32 +01:00
$dumper = new YamlDataDumper($this->getApplication()->getKernel()->getRootDir());
try {
2011-09-07 10:33:20 +02:00
$dumper->dump($filename, $name);
} catch (\Exception $e) {
$this->writeSection($output, array(
'[Propel] Exception',
'',
$e->getMessage()), 'fg=white;bg=red');
2011-12-05 18:42:24 +01:00
return false;
}
2011-04-19 13:59:19 +02:00
2012-05-09 01:04:22 +02:00
$this->writeNewFile($output, $filename);
2011-03-25 18:44:32 +01:00
return true;
2011-03-25 18:44:32 +01:00
}
}