15b579f94f1c7055540ae9ff0569d6d6f8c7042d
[yaffs-website] / vendor / drush / drush / src / Exec / ExecTrait.php
1 <?php
2 namespace Drush\Exec;
3
4 use Drush\Drush;
5
6 trait ExecTrait
7 {
8     /**
9      * Starts a background browser/tab for the current site or a specified URL.
10      *
11      * Uses a non-blocking proc_open call, so Drush execution will continue.
12      *
13      * @param $uri
14      *   Optional URI or site path to open in browser. If omitted, or if a site path
15      *   is specified, the current site home page uri will be prepended if the sites
16      *   hostname resolves.
17      * @return
18      *   TRUE if browser was opened, FALSE if browser was disabled by the user or a,
19      *   default browser could not be found.
20      */
21     public function startBrowser($uri = null, $sleep = false, $port = false, $browser = true)
22     {
23         if ($browser) {
24             // We can only open a browser if we have a DISPLAY environment variable on
25             // POSIX or are running Windows or OS X.
26             if (!Drush::simulate() && !getenv('DISPLAY') && !drush_is_windows() && !drush_is_osx()) {
27                 $this->logger()->info(dt('No graphical display appears to be available, not starting browser.'));
28                 return false;
29             }
30             $host = parse_url($uri, PHP_URL_HOST);
31             if (!$host) {
32                 // Build a URI for the current site, if we were passed a path.
33                 $site = drush_get_context('DRUSH_URI');
34                 $host = parse_url($site, PHP_URL_HOST);
35                 $uri = $site . '/' . ltrim($uri, '/');
36             }
37             // Validate that the host part of the URL resolves, so we don't attempt to
38             // open the browser for http://default or similar invalid hosts.
39             $hosterror = (gethostbynamel($host) === false);
40             $iperror = (ip2long($host) && gethostbyaddr($host) == $host);
41             if (!Drush::simulate() && ($hosterror || $iperror)) {
42                 $this->logger()->warning(dt('!host does not appear to be a resolvable hostname or IP, not starting browser. You may need to use the --uri option in your command or site alias to indicate the correct URL of this site.', ['!host' => $host]));
43                 return false;
44             }
45             if ($port) {
46                 $uri = str_replace($host, "localhost:$port", $uri);
47             }
48             if ($browser === true) {
49                 // See if we can find an OS helper to open URLs in default browser.
50                 if (drush_shell_exec('which xdg-open')) {
51                     $browser = 'xdg-open';
52                 } else if (drush_shell_exec('which open')) {
53                     $browser = 'open';
54                 } else if (!drush_has_bash()) {
55                     $browser = 'start';
56                 } else {
57                     // Can't find a valid browser.
58                     $browser = false;
59                 }
60             }
61             $prefix = '';
62             if ($sleep) {
63                 $prefix = 'sleep ' . $sleep . ' && ';
64             }
65             if ($browser) {
66                 $this->logger()->success(dt('Opening browser !browser at !uri', ['!browser' => $browser, '!uri' => $uri]));
67                 if (!Drush::simulate()) {
68                     $pipes = [];
69                     proc_close(proc_open($prefix . $browser . ' ' . drush_escapeshellarg($uri) . ' 2> ' . drush_bit_bucket() . ' &', [], $pipes));
70                 }
71                 return true;
72             }
73         }
74         return false;
75     }
76 }