<?php
/**
* PHP version ~5.5
*
* @category Command
* @package Fluency\Silex\Doctrine\DBAL\Tools\Console\Command
* @author Rafael Ernesto Espinosa Santiesteban <ralphlnx@gmail.com>
* @license MIT <http://www.opensource.org/licenses/mit-license.php>
* @link http://fluency.inc.com
*/
namespace Fluency\Silex\Doctrine\DBAL\Tools\Console\Command;
use Fluency\Silex\Doctrine\DBAL\Schema\ConfigurableSchema;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
/**
* Class CreateSchemaCommand
* @category Command
* @package Fluency\Silex\Doctrine\DBAL\Tools\Console\Command
* @author Rafael Ernesto Espinosa Santiesteban <ralphlnx@gmail.com>
* @license MIT <http://www.opensource.org/licenses/mit-license.php>
* @link http://fluency.inc.com
*/
class CreateSchemaCommand extends AbstractDBALCommand
{
/**
* Configures command
*
* @return void
*/
protected function configure()
{
parent::configure();
$this->setName('dbal:schema:create')
->setDescription('Creates the configured database schema')
->setHelp(
<<<EOT
The <info>dbal:schema:create</info> command creates the default
connections database:
<info>php app/console dbal:schema:create</info>
You can also optionally specify the name of a connection to create the
schema for:
<info>php app/console dbal:schema:create --connection=default</info>
EOT
);
}
/**
* Executes command
*
* @param InputInterface $input Command input
* @param OutputInterface $output Console output
*
* @throws \Exception
*
* @return void
*/
protected function execute( InputInterface $input, OutputInterface $output )
{
parent::execute($input, $output);
if ($this->getContainer()->offsetExists('dbs')) {
$this->setConnection(
$this->getContainer()['dbs'][$input->getOption('connection')]
);
}
$schema = new ConfigurableSchema();
$config = $this->getContainer()['config']['dbal_schema']
[$input->getOption('connection')];
if (isset($config['tables'])) {
$schema->createTablesFromConfig($config['tables']);
}
$schema->bundleMultipleSchemas($config);
$sql = $schema->toSql($this->getConnection()->getDatabasePlatform());
foreach ((array) $sql as $query) {
$output->write(sprintf('Executing <info>%s</info>', $query));
$this->getConnection()->executeUpdate($query);
$output->write(' OK' . "\n");
}
}
}
|