Yaffs site version 1.1
[yaffs-website] / vendor / drupal / console / src / Command / Database / ClientCommand.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\Console\Command\Database\ClientCommand.
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\Output\OutputInterface;
13 use Symfony\Component\Process\ProcessBuilder;
14 use Symfony\Component\Console\Command\Command;
15 use Drupal\Console\Core\Command\Shared\CommandTrait;
16 use Drupal\Console\Command\Shared\ConnectTrait;
17 use Drupal\Console\Core\Style\DrupalStyle;
18
19 class ClientCommand extends Command
20 {
21     use ConnectTrait;
22     use CommandTrait;
23
24     /**
25      * {@inheritdoc}
26      */
27     protected function configure()
28     {
29         $this
30             ->setName('database:client')
31             ->setDescription($this->trans('commands.database.client.description'))
32             ->addArgument(
33                 'database',
34                 InputArgument::OPTIONAL,
35                 $this->trans('commands.database.client.arguments.database'),
36                 'default'
37             )
38             ->setHelp($this->trans('commands.database.client.help'));
39     }
40
41     /**
42      * {@inheritdoc}
43      */
44     protected function execute(InputInterface $input, OutputInterface $output)
45     {
46         $io = new DrupalStyle($input, $output);
47
48         $database = $input->getArgument('database');
49         $learning = $input->getOption('learning');
50
51         $databaseConnection = $this->resolveConnection($io, $database);
52
53         $connection = sprintf(
54             '%s -A --database=%s --user=%s --password=%s --host=%s --port=%s',
55             $databaseConnection['driver'],
56             $databaseConnection['database'],
57             $databaseConnection['username'],
58             $databaseConnection['password'],
59             $databaseConnection['host'],
60             $databaseConnection['port']
61         );
62
63         if ($learning) {
64             $io->commentBlock(
65                 sprintf(
66                     $this->trans('commands.database.client.messages.connection'),
67                     $connection
68                 )
69             );
70         }
71
72         $processBuilder = new ProcessBuilder([]);
73         $processBuilder->setArguments(explode(' ', $connection));
74         $process = $processBuilder->getProcess();
75         $process->setTty('true');
76         $process->run();
77
78         if (!$process->isSuccessful()) {
79             throw new \RuntimeException($process->getErrorOutput());
80         }
81
82         return 0;
83     }
84 }