Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 30fdabb

Browse files
authored
Modernize runserver and startBrowser() a bit. (#3802)
* Modernize runserver and startBrowser() a bit. * Deprecate drush_which().
1 parent ba58c00 commit 30fdabb

File tree

5 files changed

+20
-11
lines changed

5 files changed

+20
-11
lines changed

examples/Commands/SyncViaHttpCommands.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
use Consolidation\AnnotatedCommand\CommandData;
55
use Drush\Commands\DrushCommands;
66
use Drush\Drush;
7+
use Drush\Exec\ExecTrait;
78
use Symfony\Component\Filesystem\Filesystem;
89

910
/**
@@ -60,7 +61,7 @@ protected function downloadFile($url, $user = false, $password = false, $destina
6061
{
6162
static $use_wget;
6263
if ($use_wget === null) {
63-
$use_wget = drush_which('wget');
64+
$use_wget = ExecTrait::programExists('wget');
6465
}
6566

6667
$destination_tmp = drush_tempnam('download_file');

includes/exec.inc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,8 @@ function _drush_shell_exec($args, $interactive = FALSE, $simulate = false) {
190190
* a command on this system.
191191
*
192192
* @return bool
193+
*
194+
* @deprecated See \Drush\Exec\ExecTrait::programExists
193195
*/
194196
function drush_which($command) {
195197
$process = Drush::process(['which', $command]);

src/Commands/core/InitCommands.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Drush\Drush;
66
use Drush\Commands\DrushCommands;
77
use Drush\Exceptions\UserAbortException;
8+
use Drush\Exec\ExecTrait;
89
use Drush\Log\LogLevel;
910
use Robo\LoadAllTasks;
1011
use Robo\Contract\IOAwareInterface;
@@ -13,6 +14,7 @@
1314
class InitCommands extends DrushCommands implements BuilderAwareInterface, IOAwareInterface
1415
{
1516
use LoadAllTasks;
17+
use ExecTrait;
1618

1719
/**
1820
* Enrich the bash startup file with bash aliases and a smart command prompt.
@@ -89,7 +91,7 @@ public function initializeDrush($options = ['edit' => false, 'add-path' => ''])
8991
// If Drush is not in the $PATH, then figure out which
9092
// path to add so that Drush can be found globally.
9193
$add_path = $options['add-path'];
92-
if ((!drush_which("drush") || $add_path) && ($add_path !== false)) {
94+
if ((!self::programExists('drush') || $add_path) && ($add_path !== false)) {
9395
$drush_path = $this->findPathToDrush();
9496
$drush_path = preg_replace("%^" . preg_quote($home) . "/%", '$HOME/', $drush_path);
9597
$pattern = "$drush_path";

src/Commands/core/RunserverCommands.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ class RunserverCommands extends DrushCommands
1212

1313
use ExecTrait;
1414

15+
protected $uri;
16+
1517
/**
1618
* Runs PHP's built-in http server for development.
1719
*
@@ -58,7 +60,7 @@ public function runserver($uri = null, $options = ['default-server' => self::REQ
5860
$hostname = $uri['host'];
5961
$addr = $uri['addr'];
6062

61-
drush_set_context('DRUSH_URI', 'http://' . $hostname . ':' . $uri['port']);
63+
$this->uri = 'http://' . $hostname . ':' . $uri['port'];
6264

6365
// We delete any registered files here, since they are not caught by Ctrl-C.
6466
_drush_delete_registered_files();
@@ -73,7 +75,7 @@ public function runserver($uri = null, $options = ['default-server' => self::REQ
7375

7476

7577
$link = Url::fromUserInput('/' . $path, ['absolute' => true])->toString();
76-
$this->logger()->notice(dt('HTTP server listening on !addr, port !port (see http://!hostname:!port/!path), serving site !site', ['!addr' => $addr, '!hostname' => $hostname, '!port' => $uri['port'], '!path' => $path, '!site' => drush_get_context('DRUSH_DRUPAL_SITE', 'default')]));
78+
$this->logger()->notice(dt('HTTP server listening on !addr, port !port (see http://!hostname:!port/!path), serving site, !site', ['!addr' => $addr, '!hostname' => $hostname, '!port' => $uri['port'], '!path' => $path, '!site' => Drush::bootstrap()->confPath()]));
7779
// Start php built-in server.
7880
if (!empty($path)) {
7981
// Start a browser if desired. Include a 2 second delay to allow the server to come up.

src/Exec/ExecTrait.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
use Consolidation\SiteProcess\Util\Shell;
55
use Drush\Drush;
6-
use Drush\Log\LogLevel;
76

87
trait ExecTrait
98
{
@@ -14,13 +13,16 @@ trait ExecTrait
1413
*
1514
* @param $uri
1615
* Optional URI or site path to open in browser. If omitted, or if a site path
17-
* is specified, the current site home page uri will be prepended if the sites
16+
* is specified, the current site home page uri will be prepended if the site's
1817
* hostname resolves.
19-
* @return
18+
* @param int $sleep
19+
* @param bool $port
20+
* @param bool $browser
21+
* @return bool TRUE if browser was opened, FALSE if browser was disabled by the user or a,
2022
* TRUE if browser was opened, FALSE if browser was disabled by the user or a,
2123
* default browser could not be found.
2224
*/
23-
public function startBrowser($uri = null, $sleep = false, $port = false, $browser = true)
25+
public function startBrowser($uri = null, $sleep = 0, $port = false, $browser = true)
2426
{
2527
if ($browser) {
2628
// We can only open a browser if we have a DISPLAY environment variable on
@@ -32,7 +34,7 @@ public function startBrowser($uri = null, $sleep = false, $port = false, $browse
3234
$host = parse_url($uri, PHP_URL_HOST);
3335
if (!$host) {
3436
// Build a URI for the current site, if we were passed a path.
35-
$site = drush_get_context('DRUSH_URI');
37+
$site = $this->uri;
3638
$host = parse_url($site, PHP_URL_HOST);
3739
$uri = $site . '/' . ltrim($uri, '/');
3840
}
@@ -49,9 +51,9 @@ public function startBrowser($uri = null, $sleep = false, $port = false, $browse
4951
}
5052
if ($browser === true) {
5153
// See if we can find an OS helper to open URLs in default browser.
52-
if (drush_which('xdg-open')) {
54+
if (self::programExists('xdg-open')) {
5355
$browser = 'xdg-open';
54-
} else if (drush_which('open')) {
56+
} else if (self::programExists('open')) {
5557
$browser = 'open';
5658
} else if (!drush_has_bash()) {
5759
$browser = 'start';

0 commit comments

Comments
 (0)