Skip to content

Commit

Permalink
Fix #2948. Port drush_start_browser() to a trait
Browse files Browse the repository at this point in the history
  • Loading branch information
weitzman committed Sep 25, 2017
1 parent 44e8220 commit 94b4e5d
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 13 deletions.
16 changes: 10 additions & 6 deletions examples/Commands/XkcdCommands.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
<?php
namespace Drush\Commands;

use Drush\Exec\ExecTrait;

/**
* Run these commands using the --include option - e.g. `drush --include=/path/to/drush/examples xkcd`
*/

class XkcdCommands extends DrushCommands {

use ExecTrait;

/**
* Retrieve and display xkcd cartoons.
*
Expand All @@ -24,25 +28,25 @@ class XkcdCommands extends DrushCommands {
* Retrieve and display a random cartoon in Firefox.
* @aliases @xkcd
*/
public function fetch($search = NULL, $options = ['image-viewer' => 'open', 'google-custom-search-api-key' => NULL]) {
public function fetch($search = NULL, $options = ['image-viewer' => 'open', 'google-custom-search-api-key' => 'AIzaSyDpE01VDNNT73s6CEeJRdSg5jukoG244ek']) {
if (empty($search)) {
drush_start_browser('http://xkcd.com');
$this->startBrowser('http://xkcd.com');
}
elseif (is_numeric($search)) {
drush_start_browser('http://xkcd.com/' . $search);
$this->startBrowser('http://xkcd.com/' . $search);
}
elseif ($search == 'random') {
$xkcd_response = @json_decode(file_get_contents('http://xkcd.com/info.0.json'));
if (!empty($xkcd_response->num)) {
drush_start_browser('http://xkcd.com/' . rand(1, $xkcd_response->num));
$this->startBrowser('http://xkcd.com/' . rand(1, $xkcd_response->num));
}
}
else {
// This uses an API key with a limited number of searches per.
$search_response = @json_decode(file_get_contents('https://www.googleapis.com/customsearch/v1?key=' . drush_get_option('google-custom-search-api-key', 'AIzaSyDpE01VDNNT73s6CEeJRdSg5jukoG244ek') . '&cx=012652707207066138651:zudjtuwe28q&q=' . $search));
$search_response = @json_decode(file_get_contents('https://www.googleapis.com/customsearch/v1?key=' . $options['google-custom-search-api-key'] . '&cx=012652707207066138651:zudjtuwe28q&q=' . $search));
if (!empty($search_response->items)) {
foreach ($search_response->items as $item) {
drush_start_browser($item->link);
$this->startBrowser($item->link);
}
}
else {
Expand Down
4 changes: 3 additions & 1 deletion src/Commands/core/BrowseCommands.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@

use Drupal\Core\Url;
use Drush\Commands\DrushCommands;
use Drush\Exec\ExecTrait;
use Drush\SiteAlias\SiteAliasManagerAwareInterface;
use Drush\SiteAlias\SiteAliasManagerAwareTrait;

class BrowseCommands extends DrushCommands implements SiteAliasManagerAwareInterface
{
use ExecTrait;
use SiteAliasManagerAwareTrait;

/**
Expand Down Expand Up @@ -49,7 +51,7 @@ public function browse($path = '', $options = ['browser' => null, 'redirect-port
$link = Url::fromUserInput('/' . $path, ['absolute' => true])->toString();
}

drush_start_browser($link, false, $options['redirect-port']);
$this->startBrowser($link, false, $options['redirect-port']);
return $link;
}
}
4 changes: 3 additions & 1 deletion src/Commands/core/LoginCommands.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
use Drupal\user\Entity\User;
use Drush\Commands\DrushCommands;
use Drush\Drush;
use Drush\Exec\ExecTrait;
use Drush\SiteAlias\SiteAliasManagerAwareInterface;
use Drush\SiteAlias\SiteAliasManagerAwareTrait;

class LoginCommands extends DrushCommands implements SiteAliasManagerAwareInterface
{

use SiteAliasManagerAwareTrait;
use ExecTrait;

/**
* Display a one time login link for user ID 1, or another user.
Expand Down Expand Up @@ -60,7 +62,7 @@ public function login($path = '', $options = ['name' => '1', 'browser' => true,
}
}
$port = $options['redirect-port'];
drush_start_browser($link, false, $port, $options['browser']);
$this->startBrowser($link, false, $port, $options['browser']);
// Use an array for backwards compat.
drush_backend_set_result([$link]);
return $link;
Expand Down
13 changes: 8 additions & 5 deletions src/Commands/core/RunserverCommands.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@

use Drush\Drush;
use Drupal\Core\Url;
use Drupal\user\Entity\User;
use Drush\Commands\DrushCommands;
use Drush\Exec\ExecTrait;

class RunserverCommands extends DrushCommands
{

use ExecTrait;

/**
* Runs PHP's built-in http server for development.
*
Expand All @@ -18,7 +21,7 @@ class RunserverCommands extends DrushCommands
* @command runserver
* @param $uri Host IP address and port number to bind to and path to open in web browser. Format is addr:port/path. Only opens a browser if a path is specified.
* @option default-server A default addr:port/path to use for any values not specified as an argument.
* @option browser If opening a web browser, which browser to user (defaults to operating system default). Use --no-browser to avoid opening a browser.
* @option browser If opening a web browser, which browser to use (defaults to operating system default). Use --no-browser to avoid opening a browser.
* @option dns Resolve hostnames/IPs using DNS/rDNS (if possible) to determine binding IPs and/or human friendly hostnames for URLs and browser.
* @bootstrap full
* @aliases rs
Expand All @@ -37,7 +40,7 @@ class RunserverCommands extends DrushCommands
* @usage drush rs :9000/admin
* Start runserver on 127.0.0.1, port 9000, and open /admin in browser. Note that you need a colon when you specify port and path, but no IP.
*/
public function runserver($uri = null, $options = ['default-server' => null, 'user' => 1, 'browser' => true, 'dns' => false])
public function runserver($uri = null, $options = ['default-server' => null, 'browser' => true, 'dns' => false])
{
global $base_url;

Expand Down Expand Up @@ -69,11 +72,11 @@ public function runserver($uri = null, $options = ['default-server' => null, 'us


$link = Url::fromUserInput('/' . $path, ['absolute' => true])->toString();
drush_print(dt('HTTP server listening on !addr, port !port (see http://!hostname:!port/!path), serving site !site!user...', array('!addr' => $addr, '!hostname' => $hostname, '!port' => $uri['port'], '!path' => $path, '!site' => drush_get_context('DRUSH_DRUPAL_SITE', 'default'), '!user' => $user_message)));
drush_print(dt('HTTP server listening on !addr, port !port (see http://!hostname:!port/!path), serving site !site', array('!addr' => $addr, '!hostname' => $hostname, '!port' => $uri['port'], '!path' => $path, '!site' => drush_get_context('DRUSH_DRUPAL_SITE', 'default'))));
// Start php built-in server.
if (!empty($path)) {
// Start a browser if desired. Include a 2 second delay to allow the server to come up.
drush_start_browser($link, 2);
$this->startBrowser($link, 2);
}
// Start the server using 'php -S'.
$extra = ' "' . DRUSH_BASE_PATH . '/misc/d8-rs-router.php"';
Expand Down
77 changes: 77 additions & 0 deletions src/Exec/ExecTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php
namespace Drush\Exec;

trait ExecTrait
{
/**
* Starts a background browser/tab for the current site or a specified URL.
*
* Uses a non-blocking proc_open call, so Drush execution will continue.
*
* @param $uri
* Optional URI or site path to open in browser. If omitted, or if a site path
* is specified, the current site home page uri will be prepended if the sites
* hostname resolves.
* @return
* TRUE if browser was opened, FALSE if browser was disabled by the user or a,
* default browser could not be found.
*/
function startBrowser($uri = NULL, $sleep = FALSE, $port = FALSE, $browser = true) {
if ($browser) {
// We can only open a browser if we have a DISPLAY environment variable on
// POSIX or are running Windows or OS X.
if (!\Drush\Drush::simulate() && !getenv('DISPLAY') && !drush_is_windows() && !drush_is_osx()) {
drush_log(dt('No graphical display appears to be available, not starting browser.'), LogLevel::INFO);
return FALSE;
}
$host = parse_url($uri, PHP_URL_HOST);
if (!$host) {
// Build a URI for the current site, if we were passed a path.
$site = drush_get_context('DRUSH_URI');
$host = parse_url($site, PHP_URL_HOST);
$uri = $site . '/' . ltrim($uri, '/');
}
// Validate that the host part of the URL resolves, so we don't attempt to
// open the browser for http://default or similar invalid hosts.
$hosterror = (gethostbynamel($host) === FALSE);
$iperror = (ip2long($host) && gethostbyaddr($host) == $host);
if (!\Drush\Drush::simulate() && ($hosterror || $iperror)) {
drush_log(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.', array('!host' => $host)), LogLevel::WARNING);
return FALSE;
}
if ($port) {
$uri = str_replace($host, "localhost:$port", $uri);
}
if ($browser === TRUE) {
// See if we can find an OS helper to open URLs in default browser.
if (drush_shell_exec('which xdg-open')) {
$browser = 'xdg-open';
}
else if (drush_shell_exec('which open')) {
$browser = 'open';
}
else if (!drush_has_bash()) {
$browser = 'start';
}
else {
// Can't find a valid browser.
$browser = FALSE;
}
}
$prefix = '';
if ($sleep) {
$prefix = 'sleep ' . $sleep . ' && ';
}
if ($browser) {
drush_log(dt('Opening browser !browser at !uri', array('!browser' => $browser, '!uri' => $uri)));
if (!\Drush\Drush::simulate()) {
$pipes = array();
proc_close(proc_open($prefix . $browser . ' ' . drush_escapeshellarg($uri) . ' 2> ' . drush_bit_bucket() . ' &', array(), $pipes));
}
return TRUE;
}
}
return FALSE;
}

}

0 comments on commit 94b4e5d

Please sign in to comment.