Yaffs site version 1.1
[yaffs-website] / vendor / drupal / console / src / Command / Database / DumpCommand.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\Console\Command\Database\DumpCommand.
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\Console\Command\Command;
15 use Drupal\Console\Core\Command\Shared\CommandTrait;
16 use Drupal\Console\Command\Shared\ConnectTrait;
17 use Drupal\Console\Core\Utils\ShellProcess;
18 use Drupal\Console\Core\Style\DrupalStyle;
19
20 class DumpCommand extends Command
21 {
22     use CommandTrait;
23     use ConnectTrait;
24
25
26     protected $appRoot;
27     /**
28      * @var ShellProcess
29      */
30     protected $shellProcess;
31
32     /**
33      * DumpCommand constructor.
34      *
35      * @param $appRoot
36      * @param ShellProcess $shellProcess
37      */
38     public function __construct(
39         $appRoot,
40         ShellProcess $shellProcess
41     ) {
42         $this->appRoot = $appRoot;
43         $this->shellProcess = $shellProcess;
44         parent::__construct();
45     }
46
47     /**
48      * {@inheritdoc}
49      */
50     protected function configure()
51     {
52         $this
53             ->setName('database:dump')
54             ->setDescription($this->trans('commands.database.dump.description'))
55             ->addArgument(
56                 'database',
57                 InputArgument::OPTIONAL,
58                 $this->trans('commands.database.dump.arguments.database'),
59                 'default'
60             )
61             ->addOption(
62                 'file',
63                 null,
64                 InputOption::VALUE_OPTIONAL,
65                 $this->trans('commands.database.dump.options.file')
66             )
67             ->addOption(
68                 'gz',
69                 null,
70                 InputOption::VALUE_NONE,
71                 $this->trans('commands.database.dump.options.gz')
72             )
73             ->setHelp($this->trans('commands.database.dump.help'));
74     }
75
76     /**
77      * {@inheritdoc}
78      */
79     protected function execute(InputInterface $input, OutputInterface $output)
80     {
81         $io = new DrupalStyle($input, $output);
82
83         $database = $input->getArgument('database');
84         $file = $input->getOption('file');
85         $learning = $input->getOption('learning');
86         $gz = $input->getOption('gz');
87
88         $databaseConnection = $this->resolveConnection($io, $database);
89
90         if (!$file) {
91             $date = new \DateTime();
92             $file = sprintf(
93                 '%s/%s-%s.sql',
94                 $this->appRoot,
95                 $databaseConnection['database'],
96                 $date->format('Y-m-d-h-i-s')
97             );
98         }
99
100         $command = null;
101
102         if ($databaseConnection['driver'] == 'mysql') {
103             $command = sprintf(
104                 'mysqldump --user="%s" --password="%s" --host="%s" --port="%s" "%s" > "%s"',
105                 $databaseConnection['username'],
106                 $databaseConnection['password'],
107                 $databaseConnection['host'],
108                 $databaseConnection['port'],
109                 $databaseConnection['database'],
110                 $file
111             );
112         } elseif ($databaseConnection['driver'] == 'pgsql') {
113             $command = sprintf(
114                 'PGPASSWORD="%s" pg_dumpall -w -U "%s" -h "%s" -p "%s" -l "%s" -f "%s"',
115                 $databaseConnection['password'],
116                 $databaseConnection['username'],
117                 $databaseConnection['host'],
118                 $databaseConnection['port'],
119                 $databaseConnection['database'],
120                 $file
121             );
122         }
123
124         if ($learning) {
125             $io->commentBlock($command);
126         }
127
128         if ($this->shellProcess->exec($command, $this->appRoot)) {
129             $resultFile = $file;
130             if ($gz) {
131                 if (substr($file, -3) != '.gz') {
132                     $resultFile = $file . ".gz";
133                 }
134                 file_put_contents(
135                     $resultFile,
136                     gzencode(
137                         file_get_contents(
138                             $file
139                         )
140                     )
141                 );
142                 if ($resultFile != $file) {
143                     unlink($file);
144                 }
145             }
146
147             $io->success(
148                 sprintf(
149                     '%s %s',
150                     $this->trans('commands.database.dump.messages.success'),
151                     $resultFile
152                 )
153             );
154         }
155
156         return 0;
157     }
158 }