propel-bundle/Command/SqlInsertCommand.php

124 lines
4 KiB
PHP
Raw Normal View History

<?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;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
/**
2012-04-07 21:58:45 +02:00
* SqlInsertCommand.
*
2011-04-19 14:18:07 +02:00
* @author William DURAND <william.durand1@gmail.com>
*/
2012-04-20 13:54:05 +02:00
class SqlInsertCommand extends AbstractCommand
{
/**
* @see Command
*/
protected function configure()
{
$this
->setDescription('Insert SQL for current model')
->addOption('force', null, InputOption::VALUE_NONE, 'Set this parameter to execute this action.')
2011-04-06 15:05:32 +02:00
->addOption('connection', null, InputOption::VALUE_OPTIONAL, 'Set this parameter to define a connection to use')
->setHelp(<<<EOT
2012-04-07 21:58:45 +02:00
The <info>%command.name%</info> command connects to the database and executes all SQL statements found in <comment>app/propel/sql/*schema.sql</comment>.
2012-04-07 21:58:45 +02:00
<info>php %command.full_name%</info>
2011-03-26 18:30:43 +01:00
The <info>--force</info> parameter has to be used to actually insert SQL.
2011-04-06 15:05:32 +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).
EOT
)
2012-04-07 21:58:45 +02:00
->setName('propel:sql:insert')
;
}
/**
* @see Command
*
* @throws \InvalidArgumentException When the target directory does not exist
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
2011-09-05 00:43:23 +02:00
if ($input->getOption('force')) {
2011-09-22 18:47:53 +02:00
$connections = $this->getConnections();
$sqlDir = $this->getSqlDir();
2011-04-06 15:05:32 +02:00
2011-09-22 18:47:53 +02:00
$manager = new \PropelSqlManager();
$manager->setWorkingDirectory($sqlDir);
$manager->setConnections($connections);
2011-04-19 17:39:10 +02:00
2011-09-22 18:47:53 +02:00
if ($input->getOption('connection')) {
list($name, $config) = $this->getConnection($input, $output);
2012-04-07 21:58:45 +02:00
$this->doSqlInsert($manager, $output, $name);
} else {
2011-09-22 18:47:53 +02:00
foreach ($connections as $name => $config) {
$output->writeln(sprintf('Use connection named <comment>%s</comment> in <comment>%s</comment> environment.',
$name, $this->getApplication()->getKernel()->getEnvironment()));
2012-04-07 21:58:45 +02:00
$this->doSqlInsert($manager, $output, $name);
2011-09-22 18:47:53 +02:00
}
}
} else {
$output->writeln('<error>You have to use --force to execute all SQL statements.</error>');
}
}
2011-09-22 18:47:53 +02:00
protected function getSqlDir()
{
return sprintf('%s/propel/sql', $this->getApplication()->getKernel()->getCacheDir());
}
2011-09-22 18:47:53 +02:00
/**
* @param \PropelSqlManager $manager
2012-05-24 12:44:48 +02:00
* @param OutputInterface $output
* @param string $connectionName
2011-09-22 18:47:53 +02:00
*/
2012-04-07 21:58:45 +02:00
protected function doSqlInsert(\PropelSqlManager $manager, OutputInterface $output, $connectionName)
2011-09-22 18:47:53 +02:00
{
try {
$statusCode = $manager->insertSql($connectionName);
} catch (\Exception $e) {
return $this->writeSection(
$output,
array('[Propel] Exception', '', $e),
'fg=white;bg=red'
);
}
if (true === $statusCode) {
2012-06-24 01:09:07 +02:00
$output->writeln('<info>All SQL statements have been inserted.</info>');
2011-09-22 18:47:53 +02:00
} else {
2012-06-24 01:09:07 +02:00
$output->writeln('<comment>No SQL statements found.</comment>');
2011-09-22 18:47:53 +02:00
}
}
/**
* @return array
*/
protected function getConnections()
{
$propelConfiguration = $this->getContainer()->get('propel.configuration');
$connections = array();
foreach ($propelConfiguration['datasources'] as $name => $config) {
if (is_scalar($config)) {
continue;
}
2011-09-22 18:47:53 +02:00
$connections[$name] = $config['connection'];
}
return $connections;
}
}