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