Yaffs site version 1.1
[yaffs-website] / vendor / drupal / console / src / Command / Database / DropCommand.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\Console\Command\Database\DropCommand.
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\Console\Command\Command;
14 use Drupal\Core\Database\Connection;
15 use Drupal\Console\Core\Command\Shared\CommandTrait;
16 use Drupal\Console\Command\Shared\ConnectTrait;
17 use Drupal\Console\Core\Style\DrupalStyle;
18
19 /**
20  * Class DropCommand
21  *
22  * @package Drupal\Console\Command\Database
23  */
24 class DropCommand extends Command
25 {
26     use CommandTrait;
27     use ConnectTrait;
28
29     /**
30      * @var Connection
31      */
32     protected $database;
33
34     /**
35      * DropCommand constructor.
36      *
37      * @param Connection $database
38      */
39     public function __construct(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('database:drop')
52             ->setDescription($this->trans('commands.database.drop.description'))
53             ->addArgument(
54                 'database',
55                 InputArgument::OPTIONAL,
56                 $this->trans('commands.database.drop.arguments.database'),
57                 'default'
58             )
59             ->setHelp($this->trans('commands.database.drop.help'));
60     }
61
62     /**
63      * {@inheritdoc}
64      */
65     protected function execute(InputInterface $input, OutputInterface $output)
66     {
67         $io = new DrupalStyle($input, $output);
68         $database = $input->getArgument('database');
69         $yes = $input->getOption('yes');
70
71         $databaseConnection = $this->resolveConnection($io, $database);
72
73         if (!$yes) {
74             if (!$io->confirm(
75                 sprintf(
76                     $this->trans('commands.database.drop.question.drop-tables'),
77                     $databaseConnection['database']
78                 ),
79                 true
80             )
81             ) {
82                 return 1;
83             }
84         }
85
86         $schema = $this->database->schema();
87         $tables = $schema->findTables('%');
88         $tableRows = [];
89
90         foreach ($tables as $table) {
91             if ($schema->dropTable($table)) {
92                 $tableRows['success'][] = [$table];
93             } else {
94                 $tableRows['error'][] = [$table];
95             }
96         }
97
98         $io->success(
99             sprintf(
100                 $this->trans('commands.database.drop.messages.table-drop'),
101                 count($tableRows['success'])
102             )
103         );
104
105         return 0;
106     }
107 }