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
|
|
|
|
*/
|
2016-01-23 21:41:17 +01:00
|
|
|
namespace Propel\Bundle\PropelBundle\Command;
|
2011-08-30 23:29:49 +02:00
|
|
|
|
2016-01-23 21:41:17 +01:00
|
|
|
use Propel\Bundle\PropelBundle\DataFixtures\Dumper\YamlDataDumper;
|
2011-03-25 18:44:32 +01:00
|
|
|
use Symfony\Component\Console\Input\InputInterface;
|
2016-01-23 21:41:17 +01:00
|
|
|
use Symfony\Component\Console\Input\InputOption;
|
2011-03-25 18:44:32 +01:00
|
|
|
use Symfony\Component\Console\Output\OutputInterface;
|
2013-05-14 14:34:35 +02:00
|
|
|
use Symfony\Component\Filesystem\Filesystem;
|
2011-09-06 14:56:16 +02:00
|
|
|
|
2011-03-25 18:44:32 +01:00
|
|
|
/**
|
2011-09-07 10:46:56 +02: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
|
|
|
{
|
2011-09-06 14:56:16 +02: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
|
2011-09-07 10:46:56 +02:00
|
|
|
->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')
|
2012-08-20 13:52:51 +02:00
|
|
|
->addOption('dir', null, InputOption::VALUE_OPTIONAL, 'Set this parameter to define a fixture directory')
|
2011-03-25 18:44:32 +01:00
|
|
|
->setHelp(<<<EOT
|
2011-09-07 10:46:56 +02:00
|
|
|
The <info>propel:fixtures:dump</info> dumps data from database into YAML fixtures file.
|
2011-04-19 14:32:38 +02:00
|
|
|
|
2011-09-07 10:46:56 +02:00
|
|
|
<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.
|
2012-08-20 13:52:51 +02:00
|
|
|
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
|
|
|
|
)
|
2011-09-07 10:46:56 +02:00
|
|
|
->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
|
|
|
{
|
2011-04-19 14:32:38 +02:00
|
|
|
list($name, $defaultConfig) = $this->getConnection($input, $output);
|
2012-08-20 13:52:51 +02:00
|
|
|
$fixtureDir = $input->getOption('dir') ? $input->getOption('dir') : $this->defaultFixturesDir;
|
2011-03-25 18:44:32 +01:00
|
|
|
|
2013-04-16 11:02:37 +02: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
|
|
|
|
2011-09-06 14:56:16 +02:00
|
|
|
$dumper = new YamlDataDumper($this->getApplication()->getKernel()->getRootDir());
|
2011-09-05 00:39:58 +02:00
|
|
|
|
2011-09-06 14:56:16 +02:00
|
|
|
try {
|
2011-09-07 10:33:20 +02:00
|
|
|
$dumper->dump($filename, $name);
|
2011-09-06 14:56:16 +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-06 14:56:16 +02: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
|
|
|
|
2011-09-06 14:56:16 +02:00
|
|
|
return true;
|
2011-03-25 18:44:32 +01:00
|
|
|
}
|
|
|
|
}
|