Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

database:add command #3037

Merged
merged 2 commits into from
Dec 27, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions config/services/drupal-console/database.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
services:
console.database_add:
class: Drupal\Console\Command\Database\AddCommand
arguments: ['@console.database_settings_generator']
tags:
- { name: drupal.command }
console.database_client:
class: Drupal\Console\Command\Database\ClientCommand
tags:
Expand Down
5 changes: 5 additions & 0 deletions config/services/drupal-console/generator.yml
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,11 @@ services:
arguments: ['@console.extension_manager']
tags:
- { name: drupal.generator }
console.database_settings_generator:
class: Drupal\Console\Generator\DatabaseSettingsGenerator
arguments: ['@kernel']
tags:
- { name: drupal.generator }
console.entitycontent_generator:
class: Drupal\Console\Generator\EntityContentGenerator
arguments: ['@console.extension_manager', '@console.site', '@console.renderer']
Expand Down
172 changes: 172 additions & 0 deletions src/Command/Database/AddCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
<?php

/**
* @file
* Contains \Drupal\Console\Command\Database\ConnectCommand.
*/

namespace Drupal\Console\Command\Database;

use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Command\Command;
use Drupal\Console\Generator\DatabaseSettingsGenerator;
use Drupal\Console\Command\Shared\CommandTrait;
use Drupal\Console\Command\Shared\ConnectTrait;
use Drupal\Console\Style\DrupalStyle;

class AddCommand extends Command
{
use CommandTrait;
use ConnectTrait;


/**
* @var DatabaseSettingsGenerator
*/
protected $generator;

/**
* FormCommand constructor.
* @param DatabaseSettingsGenerator $generator
*/
public function __construct(
DatabaseSettingsGenerator $generator
) {
$this->generator = $generator;
parent::__construct();
}

/**
* {@inheritdoc}
*/
protected function configure()
{
$this
->setName('database:add')
->setDescription($this->trans('commands.database.add.description'))
->addOption(
'database',
'',
InputOption::VALUE_REQUIRED,
$this->trans('commands.database.add.options.database')
)
->addOption(
'username',
'',
InputOption::VALUE_REQUIRED,
$this->trans('commands.database.add.options.username')
)
->addOption(
'password',
'',
InputOption::VALUE_REQUIRED,
$this->trans('commands.database.add.options.password')
)
->addOption(
'prefix',
'',
InputOption::VALUE_OPTIONAL,
$this->trans('commands.database.add.options.prefix')
)
->addOption(
'host',
'',
InputOption::VALUE_OPTIONAL,
$this->trans('commands.database.add.options.host')
)
->addOption(
'port',
'',
InputOption::VALUE_OPTIONAL,
$this->trans('commands.database.add.options.port')
)
->addOption(
'driver',
'',
InputOption::VALUE_OPTIONAL,
$this->trans('commands.database.add.options.driver')
)
->setHelp($this->trans('commands.database.add.help'));
}
/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$io = new DrupalStyle($input, $output);
$result = $this
->generator
->generate($input->getOptions());
if (!$result) {
$io->error($this->trans('commands.database.add.error'));
}
}

/**
* {@inheritdoc}
*/
protected function interact(InputInterface $input, OutputInterface $output)
{
$io = new DrupalStyle($input, $output);

$database = $input->getOption('database');
if (!$database) {
$database = $io->ask(
$this->trans('commands.database.add.questions.database'),
'migrate_db'
);
}
$input->setOption('database', $database);
$username = $input->getOption('username');
if (!$username) {
$username = $io->ask(
$this->trans('commands.database.add.questions.username'),
''
);
}
$input->setOption('username', $username);
$password = $input->getOption('password');
if (!$password) {
$password = $io->ask(
$this->trans('commands.database.add.questions.password'),
''
);
}
$input->setOption('password', $password);
$prefix = $input->getOption('prefix');
if (!$prefix) {
$prefix = $io->ask(
$this->trans('commands.database.add.questions.prefix'),
FALSE
);
}
$input->setOption('prefix', $prefix);
$host = $input->getOption('host');
if (!$host) {
$host = $io->ask(
$this->trans('commands.database.add.questions.host'),
'localhost'
);
}
$input->setOption('host', $host);
$port = $input->getOption('port');
if (!$port) {
$port = $io->ask(
$this->trans('commands.database.add.questions.port'),
3306
);
}
$input->setOption('port', $port);
$driver = $input->getOption('driver');
if (!$driver) {
$driver = $io->ask(
$this->trans('commands.database.add.questions.driver'),
'mysql'
);
}
$input->setOption('driver', $driver);
}
}
49 changes: 49 additions & 0 deletions src/Generator/DatabaseSettingsGenerator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

/**
* @file
* Contains \Drupal\Console\Generator\DatabaseSettingsGenerator.
*/

namespace Drupal\Console\Generator;

use Drupal\Core\DrupalKernelInterface;

class DatabaseSettingsGenerator extends Generator
{

/**
* @var DrupalKernelInterface
*/
protected $kernel;

/**
* DatabaseSettingsGenerator constructor.
* @param DrupalKernelInterface $kernel
*/
public function __construct(
DrupalKernelInterface $kernel
) {
$this->kernel = $kernel;
}


/**
* Generator Plugin Block.
*
* @param $parameters
*/
public function generate($parameters)
{
$settingsFile = $this->kernel->getSitePath().'/settings.php';
if (!is_writable($settingsFile)) {
return false;
}
return $this->renderFile(
'database/add.php.twig',
$settingsFile,
$parameters,
FILE_APPEND
);
}
}
10 changes: 10 additions & 0 deletions templates/database/add.php.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

$databases['{{ database }}']['default'] = [
'database' => '{{ database }}',
'username' => '{{ username }}',
'password' => '{{ password }}',
'host' => '{{ host }}',
'port' => '{{ port }}',
'driver' => '{{ driver }}',
'prefix' => '{{ prefix }}',
];