vendor/symfony/src/Symfony/Bundle/DoctrineBundle/Command/CreateDatabaseDoctrineCommand.php
changeset 0 7f95f8617b0b
equal deleted inserted replaced
-1:000000000000 0:7f95f8617b0b
       
     1 <?php
       
     2 
       
     3 /*
       
     4  * This file is part of the Symfony package.
       
     5  *
       
     6  * (c) Fabien Potencier <fabien@symfony.com>
       
     7  *
       
     8  * For the full copyright and license information, please view the LICENSE
       
     9  * file that was distributed with this source code.
       
    10  */
       
    11 
       
    12 namespace Symfony\Bundle\DoctrineBundle\Command;
       
    13 
       
    14 use Symfony\Component\Console\Input\InputOption;
       
    15 use Symfony\Component\Console\Input\InputInterface;
       
    16 use Symfony\Component\Console\Output\OutputInterface;
       
    17 use Doctrine\DBAL\DriverManager;
       
    18 
       
    19 /**
       
    20  * Database tool allows you to easily drop and create your configured databases.
       
    21  *
       
    22  * @author Fabien Potencier <fabien@symfony.com>
       
    23  * @author Jonathan H. Wage <jonwage@gmail.com>
       
    24  */
       
    25 class CreateDatabaseDoctrineCommand extends DoctrineCommand
       
    26 {
       
    27     protected function configure()
       
    28     {
       
    29         $this
       
    30             ->setName('doctrine:database:create')
       
    31             ->setDescription('Create the configured databases')
       
    32             ->addOption('connection', null, InputOption::VALUE_OPTIONAL, 'The connection to use for this command')
       
    33             ->setHelp(<<<EOT
       
    34 The <info>doctrine:database:create</info> command creates the default
       
    35 connections database:
       
    36 
       
    37 <info>php app/console doctrine:database:create</info>
       
    38 
       
    39 You can also optionally specify the name of a connection to create the
       
    40 database for:
       
    41 
       
    42 <info>php app/console doctrine:database:create --connection=default</info>
       
    43 EOT
       
    44         );
       
    45     }
       
    46 
       
    47     protected function execute(InputInterface $input, OutputInterface $output)
       
    48     {
       
    49         $connection = $this->getDoctrineConnection($input->getOption('connection'));
       
    50 
       
    51         $params = $connection->getParams();
       
    52         $name = isset($params['path']) ? $params['path'] : $params['dbname'];
       
    53 
       
    54         unset($params['dbname']);
       
    55 
       
    56         $tmpConnection = DriverManager::getConnection($params);
       
    57 
       
    58         try {
       
    59             $tmpConnection->getSchemaManager()->createDatabase($name);
       
    60             $output->writeln(sprintf('<info>Created database for connection named <comment>%s</comment></info>', $name));
       
    61         } catch (\Exception $e) {
       
    62             $output->writeln(sprintf('<error>Could not create database for connection named <comment>%s</comment></error>', $name));
       
    63             $output->writeln(sprintf('<error>%s</error>', $e->getMessage()));
       
    64         }
       
    65 
       
    66         $tmpConnection->close();
       
    67     }
       
    68 }