2013-11-04 23:01:44 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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-02-11 19:14:03 +01:00
|
|
|
namespace Propel\Bundle\PropelBundle\Command;
|
2013-11-04 23:01:44 +01:00
|
|
|
|
|
|
|
use Propel\Runtime\Connection\ConnectionManagerSingle;
|
|
|
|
use Propel\Runtime\Propel;
|
|
|
|
use Symfony\Component\Console\Input\InputInterface;
|
|
|
|
use Symfony\Component\Console\Output\OutputInterface;
|
|
|
|
use Symfony\Component\Console\Input\InputOption;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* DatabaseCreateCommand class.
|
|
|
|
* Useful to create a database.
|
|
|
|
*
|
|
|
|
* @author William DURAND
|
|
|
|
*/
|
|
|
|
class DatabaseCreateCommand extends AbstractCommand
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @see Command
|
|
|
|
*/
|
|
|
|
protected function configure()
|
|
|
|
{
|
|
|
|
$this
|
|
|
|
->setName('propel:database:create')
|
|
|
|
->setDescription('Create a given database or the default one.')
|
|
|
|
|
|
|
|
->addOption('connection', null, InputOption::VALUE_OPTIONAL, 'Set this parameter to define a connection to use')
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @see Command
|
|
|
|
*
|
|
|
|
* @throws \InvalidArgumentException When the target directory does not exist
|
|
|
|
*/
|
|
|
|
protected function execute(InputInterface $input, OutputInterface $output)
|
|
|
|
{
|
2014-09-09 22:59:13 +02:00
|
|
|
$connectionName = $input->getOption('connection') ?: $this->getDefaultConnection();
|
2013-11-04 23:01:44 +01:00
|
|
|
$config = $this->getConnectionData($connectionName);
|
2014-09-09 22:59:13 +02:00
|
|
|
$dbName = $this->parseDbName($config['dsn']);
|
2013-11-04 23:01:44 +01:00
|
|
|
|
|
|
|
if (null === $dbName) {
|
|
|
|
return $output->writeln('<error>No database name found.</error>');
|
|
|
|
} else {
|
|
|
|
$query = 'CREATE DATABASE '. $dbName .';';
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
$manager = new ConnectionManagerSingle();
|
2014-09-09 22:59:13 +02:00
|
|
|
$manager->setConfiguration($this->getTemporaryConfiguration($config));
|
2013-11-04 23:01:44 +01:00
|
|
|
|
|
|
|
$serviceContainer = Propel::getServiceContainer();
|
|
|
|
$serviceContainer->setAdapterClass($connectionName, $config['adapter']);
|
|
|
|
$serviceContainer->setConnectionManager($connectionName, $manager);
|
|
|
|
|
|
|
|
$connection = Propel::getConnection($connectionName);
|
|
|
|
|
|
|
|
$statement = $connection->prepare($query);
|
|
|
|
$statement->execute();
|
|
|
|
|
|
|
|
$output->writeln(sprintf('<info>Database <comment>%s</comment> has been created.</info>', $dbName));
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
$this->writeSection($output, array(
|
|
|
|
'[Propel] Exception caught',
|
|
|
|
'',
|
|
|
|
$e->getMessage()
|
|
|
|
), 'fg=white;bg=red');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a temporary configuration to connect to the database in order
|
|
|
|
* to create a given database. This idea comes from Doctrine1.
|
|
|
|
*
|
|
|
|
* @see https://github.com/doctrine/doctrine1/blob/master/lib/Doctrine/Connection.php#L1491
|
|
|
|
*
|
|
|
|
* @param array $config A Propel connection configuration.
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
private function getTemporaryConfiguration($config)
|
|
|
|
{
|
|
|
|
$dbName = $this->parseDbName($config['dsn']);
|
|
|
|
|
|
|
|
$config['dsn'] = preg_replace(
|
2016-05-18 22:06:14 +02:00
|
|
|
'#;?dbname='.$dbName.'#',
|
2013-11-04 23:01:44 +01:00
|
|
|
'',
|
|
|
|
$config['dsn']
|
|
|
|
);
|
|
|
|
|
|
|
|
return $config;
|
|
|
|
}
|
|
|
|
}
|