Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / vendor / drush / drush / examples / Commands / SyncViaHttpCommands.php
1 <?php
2 namespace Drush\Commands;
3
4 use Consolidation\AnnotatedCommand\CommandData;
5 use Drush\Commands\DrushCommands;
6 use Drush\Drush;
7 use Symfony\Component\Filesystem\Filesystem;
8
9 /**
10  * Load this commandfile using the --include option - e.g. `drush --include=/path/to/drush/examples`
11  */
12
13 class SyncViaHttpCommands extends DrushCommands
14 {
15
16   /**
17    * When a hook extends a command with additional options, it must
18    * implement declare those option(s) in a @hook option like this one.  Doing so will add
19    * the option to the help text for the modified command, and will also
20    * allow the new option to be specified on the command line.  Without
21    * this, Drush will fail with an error when a user attempts to use
22    * an unknown option.
23    *
24    * @hook option sql-sync
25    * @option http-sync Copy the database via http instead of rsync.  Value is the url that the existing database dump can be found at.
26    * @option http-sync-user Username for the protected directory containing the sql dump.
27    * @option http-sync-password Password for the same directory.
28    */
29     public function optionsetSqlSync()
30     {
31     }
32
33   /**
34    * During the pre hook, determine if the http-sync option has been
35    * specified.  If it has been, then disable the normal ssh + rsync
36    * dump-and-transfer that sql-sync usually does, and transfer the
37    * database dump via an http download.
38    *
39    * @hook pre-command sql-sync
40    */
41     public function preSqlSync(CommandData $commandData)
42     {
43         $sql_dump_download_url = $commandData->input()->getOption('http-sync');
44         if (!empty($sql_dump_download_url)) {
45             $user = $commandData->input()->getOption('http-sync-user');
46             $password = $commandData->input()->getOption('http-sync-password');
47             $source_dump_file = $this->downloadFile($sql_dump_download_url, $user, $password);
48             $commandData->input()->setOption('target-dump', $source_dump_file);
49             $commandData->input()->setOption('no-dump', true);
50             $commandData->input()->setOption('no-sync', true);
51         }
52     }
53
54   /**
55    * Downloads a file.
56    *
57    * Optionally uses user authentication, using either wget or curl, as available.
58    */
59     protected function downloadFile($url, $user = false, $password = false, $destination = false, $overwrite = true)
60     {
61         static $use_wget;
62         if ($use_wget === null) {
63             $use_wget = drush_shell_exec('which wget');
64         }
65
66         $destination_tmp = drush_tempnam('download_file');
67         if ($use_wget) {
68             if ($user && $password) {
69                 drush_shell_exec("wget -q --timeout=30 --user=%s --password=%s -O %s %s", $user, $password, $destination_tmp, $url);
70             } else {
71                 drush_shell_exec("wget -q --timeout=30 -O %s %s", $destination_tmp, $url);
72             }
73         } else {
74             if ($user && $password) {
75                 drush_shell_exec("curl -s -L --connect-timeout 30 --user %s:%s -o %s %s", $user, $password, $destination_tmp, $url);
76             } else {
77                 drush_shell_exec("curl -s -L --connect-timeout 30 -o %s %s", $destination_tmp, $url);
78             }
79         }
80         if (!Drush::simulate()) {
81             if (!drush_file_not_empty($destination_tmp) && $file = @file_get_contents($url)) {
82                 @file_put_contents($destination_tmp, $file);
83             }
84             if (!drush_file_not_empty($destination_tmp)) {
85                 // Download failed.
86                 throw new \Exception(dt("The URL !url could not be downloaded.", ['!url' => $url]));
87             }
88         }
89         if ($destination) {
90             $fs = new Filesystem();
91             $fs->rename($destination_tmp, $destination, $overwrite);
92             return $destination;
93         }
94         return $destination_tmp;
95     }
96 }