Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* database:add command * Switch order of default and custom Much more frequent to want $databases['custom']['default'] than the other way around - for exampale with the migrate drush commands.
- Loading branch information
1 parent
2daeb3f
commit a76d51c
Showing
5 changed files
with
241 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }}', | ||
]; |