9 * Starts a background browser/tab for the current site or a specified URL.
11 * Uses a non-blocking proc_open call, so Drush execution will continue.
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
18 * TRUE if browser was opened, FALSE if browser was disabled by the user or a,
19 * default browser could not be found.
21 public function startBrowser($uri = null, $sleep = false, $port = false, $browser = true)
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.'));
30 $host = parse_url($uri, PHP_URL_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, '/');
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]));
46 $uri = str_replace($host, "localhost:$port", $uri);
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')) {
54 } else if (!drush_has_bash()) {
57 // Can't find a valid browser.
63 $prefix = 'sleep ' . $sleep . ' && ';
66 $this->logger()->success(dt('Opening browser !browser at !uri', ['!browser' => $browser, '!uri' => $uri]));
67 if (!Drush::simulate()) {
69 proc_close(proc_open($prefix . $browser . ' ' . drush_escapeshellarg($uri) . ' 2> ' . drush_bit_bucket() . ' &', [], $pipes));