Security update for Core, with self-updated composer
[yaffs-website] / vendor / drupal / console / src / Command / Debug / DatabaseTableCommand.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\Console\Command\Debug\DatabaseTableCommand.
6  */
7
8 namespace Drupal\Console\Command\Debug;
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 Drupal\Console\Core\Command\Command;
15 use RedBeanPHP\R;
16 use Drupal\Core\Database\Connection;
17 use Drupal\Console\Command\Shared\ConnectTrait;
18
19 /**
20  * Class DatabaseTableCommand
21  *
22  * @package Drupal\Console\Command\Database
23  */
24 class DatabaseTableCommand extends Command
25 {
26     use ConnectTrait;
27
28     /**
29      * @var Connection
30      */
31     protected $database;
32
33     /**
34      * DatabaseTableCommand constructor.
35      *
36      * @param Connection $database
37      */
38     public function __construct(
39         Connection $database
40     ) {
41         $this->database = $database;
42         parent::__construct();
43     }
44
45     /**
46      * {@inheritdoc}
47      */
48     protected function configure()
49     {
50         $this
51             ->setName('debug:database:table')
52             ->setDescription($this->trans('commands.debug.database.table.description'))
53             ->addOption(
54                 'database',
55                 null,
56                 InputOption::VALUE_OPTIONAL,
57                 $this->trans('commands.debug.database.table.options.database'),
58                 'default'
59             )
60             ->addArgument(
61                 'table',
62                 InputArgument::OPTIONAL,
63                 $this->trans('commands.debug.database.table.arguments.table'),
64                 null
65             )
66             ->setHelp($this->trans('commands.debug.database.table.help'))
67             ->setAliases(['ddt']);
68     }
69
70     /**
71      * {@inheritdoc}
72      */
73     protected function execute(InputInterface $input, OutputInterface $output)
74     {
75         $database = $input->getOption('database');
76         $table = $input->getArgument('table');
77         $databaseConnection = $this->resolveConnection($database);
78         if ($table) {
79             $result = $this->database
80                 ->query('DESCRIBE '. $table .';')
81                 ->fetchAll();
82             if (!$result) {
83                 throw new \Exception(
84                     sprintf(
85                         $this->trans('commands.debug.database.table.messages.no-connection'),
86                         $database
87                     )
88                 );
89             }
90
91             $tableHeader = [
92                 $this->trans('commands.debug.database.table.messages.column'),
93                 $this->trans('commands.debug.database.table.messages.type')
94             ];
95             $tableRows = [];
96             foreach ($result as $record) {
97                 $column = json_decode(json_encode($record), true);
98                 $tableRows[] = [
99                     'column' => $column['Field'],
100                     'type' => $column['Type'],
101                 ];
102             }
103
104             $this->getIo()->table($tableHeader, $tableRows);
105
106             return 0;
107         }
108
109         $schema = $this->database->schema();
110         $tables = $schema->findTables('%');
111
112         $this->getIo()->comment(
113             sprintf(
114                 $this->trans('commands.debug.database.table.messages.table-show'),
115                 $databaseConnection['database']
116             )
117         );
118
119         $this->getIo()->table(
120             [$this->trans('commands.debug.database.table.messages.table')],
121             $tables
122         );
123
124         return 0;
125     }
126 }