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