Yaffs site version 1.1
[yaffs-website] / vendor / drupal / console / src / Command / Database / AddCommand.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\Console\Command\Database\ConnectCommand.
6  */
7
8 namespace Drupal\Console\Command\Database;
9
10 use Symfony\Component\Console\Input\InputArgument;
11 use Symfony\Component\Console\Input\InputInterface;
12 use Symfony\Component\Console\Input\InputOption;
13 use Symfony\Component\Console\Output\OutputInterface;
14 use Symfony\Component\Console\Command\Command;
15 use Drupal\Console\Generator\DatabaseSettingsGenerator;
16 use Drupal\Console\Core\Command\Shared\CommandTrait;
17 use Drupal\Console\Command\Shared\ConnectTrait;
18 use Drupal\Console\Core\Style\DrupalStyle;
19
20 class AddCommand extends Command
21 {
22     use CommandTrait;
23     use ConnectTrait;
24
25
26     /**
27      * @var DatabaseSettingsGenerator
28      */
29     protected $generator;
30
31     /**
32      * FormCommand constructor.
33      *
34      * @param DatabaseSettingsGenerator $generator
35      */
36     public function __construct(
37         DatabaseSettingsGenerator $generator
38     ) {
39         $this->generator = $generator;
40         parent::__construct();
41     }
42
43     /**
44      * {@inheritdoc}
45      */
46     protected function configure()
47     {
48         $this
49             ->setName('database:add')
50             ->setDescription($this->trans('commands.database.add.description'))
51             ->addOption(
52                 'database',
53                 null,
54                 InputOption::VALUE_REQUIRED,
55                 $this->trans('commands.database.add.options.database')
56             )
57             ->addOption(
58                 'username',
59                 null,
60                 InputOption::VALUE_REQUIRED,
61                 $this->trans('commands.database.add.options.username')
62             )
63             ->addOption(
64                 'password',
65                 null,
66                 InputOption::VALUE_REQUIRED,
67                 $this->trans('commands.database.add.options.password')
68             )
69             ->addOption(
70                 'prefix',
71                 null,
72                 InputOption::VALUE_OPTIONAL,
73                 $this->trans('commands.database.add.options.prefix')
74             )
75             ->addOption(
76                 'host',
77                 null,
78                 InputOption::VALUE_OPTIONAL,
79                 $this->trans('commands.database.add.options.host')
80             )
81             ->addOption(
82                 'port',
83                 null,
84                 InputOption::VALUE_OPTIONAL,
85                 $this->trans('commands.database.add.options.port')
86             )
87             ->addOption(
88                 'driver',
89                 null,
90                 InputOption::VALUE_OPTIONAL,
91                 $this->trans('commands.database.add.options.driver')
92             )
93             ->setHelp($this->trans('commands.database.add.help'));
94     }
95     /**
96      * {@inheritdoc}
97      */
98     protected function execute(InputInterface $input, OutputInterface $output)
99     {
100         $io = new DrupalStyle($input, $output);
101         $result = $this
102             ->generator
103             ->generate($input->getOptions());
104         if (!$result) {
105             $io->error($this->trans('commands.database.add.error'));
106         }
107     }
108
109     /**
110      * {@inheritdoc}
111      */
112     protected function interact(InputInterface $input, OutputInterface $output)
113     {
114         $io = new DrupalStyle($input, $output);
115
116         $database = $input->getOption('database');
117         if (!$database) {
118             $database = $io->ask(
119                 $this->trans('commands.database.add.questions.database'),
120                 'migrate_db'
121             );
122         }
123         $input->setOption('database', $database);
124         $username = $input->getOption('username');
125         if (!$username) {
126             $username = $io->ask(
127                 $this->trans('commands.database.add.questions.username'),
128                 ''
129             );
130         }
131         $input->setOption('username', $username);
132         $password = $input->getOption('password');
133         if (!$password) {
134             $password = $io->ask(
135                 $this->trans('commands.database.add.questions.password'),
136                 ''
137             );
138         }
139         $input->setOption('password', $password);
140         $prefix = $input->getOption('prefix');
141         if (!$prefix) {
142             $prefix = $io->ask(
143                 $this->trans('commands.database.add.questions.prefix'),
144                 false
145             );
146         }
147         $input->setOption('prefix', $prefix);
148         $host = $input->getOption('host');
149         if (!$host) {
150             $host = $io->ask(
151                 $this->trans('commands.database.add.questions.host'),
152                 'localhost'
153             );
154         }
155         $input->setOption('host', $host);
156         $port = $input->getOption('port');
157         if (!$port) {
158             $port = $io->ask(
159                 $this->trans('commands.database.add.questions.port'),
160                 3306
161             );
162         }
163         $input->setOption('port', $port);
164         $driver = $input->getOption('driver');
165         if (!$driver) {
166             $driver = $io->ask(
167                 $this->trans('commands.database.add.questions.driver'),
168                 'mysql'
169             );
170         }
171         $input->setOption('driver', $driver);
172     }
173 }