Yaffs site version 1.1
[yaffs-website] / vendor / drupal / console / src / Command / Database / RestoreCommand.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\Console\Command\Database\RestoreCommand.
6  */
7
8 namespace Drupal\Console\Command\Database;
9
10 use Symfony\Component\Console\Input\InputArgument;
11 use Symfony\Component\Console\Input\InputOption;
12 use Symfony\Component\Console\Input\InputInterface;
13 use Symfony\Component\Console\Output\OutputInterface;
14 use Symfony\Component\Process\ProcessBuilder;
15 use Symfony\Component\Console\Command\Command;
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 RestoreCommand extends Command
21 {
22     use CommandTrait;
23     use ConnectTrait;
24
25     /**
26      * @var string
27      */
28     protected $appRoot;
29
30     /**
31      * RestoreCommand constructor.
32      *
33      * @param string $appRoot
34      */
35     public function __construct($appRoot)
36     {
37         $this->appRoot = $appRoot;
38         parent::__construct();
39     }
40
41     /**
42      * {@inheritdoc}
43      */
44     protected function configure()
45     {
46         $this
47             ->setName('database:restore')
48             ->setDescription($this->trans('commands.database.restore.description'))
49             ->addArgument(
50                 'database',
51                 InputArgument::OPTIONAL,
52                 $this->trans('commands.database.restore.arguments.database'),
53                 'default'
54             )
55             ->addOption(
56                 'file',
57                 null,
58                 InputOption::VALUE_REQUIRED,
59                 $this->trans('commands.database.restore.options.file')
60             )
61             ->setHelp($this->trans('commands.database.restore.help'));
62     }
63
64     /**
65      * {@inheritdoc}
66      */
67     protected function execute(InputInterface $input, OutputInterface $output)
68     {
69         $io = new DrupalStyle($input, $output);
70
71         $database = $input->getArgument('database');
72         $file = $input->getOption('file');
73         $learning = $input->getOption('learning');
74
75         $databaseConnection = $this->resolveConnection($io, $database);
76
77         if (!$file) {
78             $io->error(
79                 $this->trans('commands.database.restore.messages.no-file')
80             );
81             return 1;
82         }
83         if ($databaseConnection['driver'] == 'mysql') {
84             $command = sprintf(
85                 'mysql --user=%s --password=%s --host=%s --port=%s %s < %s',
86                 $databaseConnection['username'],
87                 $databaseConnection['password'],
88                 $databaseConnection['host'],
89                 $databaseConnection['port'],
90                 $databaseConnection['database'],
91                 $file
92             );
93         } elseif ($databaseConnection['driver'] == 'pgsql') {
94             $command = sprintf(
95                 'PGPASSWORD="%s" psql -w -U %s -h %s -p %s -d %s -f %s',
96                 $databaseConnection['password'],
97                 $databaseConnection['username'],
98                 $databaseConnection['host'],
99                 $databaseConnection['port'],
100                 $databaseConnection['database'],
101                 $file
102             );
103         }
104
105         if ($learning) {
106             $io->commentBlock($command);
107         }
108
109         $processBuilder = new ProcessBuilder(['-v']);
110         $process = $processBuilder->getProcess();
111         $process->setWorkingDirectory($this->appRoot);
112         $process->setTty('true');
113         $process->setCommandLine($command);
114         $process->run();
115
116         if (!$process->isSuccessful()) {
117             throw new \RuntimeException($process->getErrorOutput());
118         }
119
120         $io->success(
121             sprintf(
122                 '%s %s',
123                 $this->trans('commands.database.restore.messages.success'),
124                 $file
125             )
126         );
127
128         return 0;
129     }
130 }