2 namespace Drush\Commands\core;
6 use Drush\Commands\DrushCommands;
7 use Drush\Exec\ExecTrait;
9 class RunserverCommands extends DrushCommands
15 * Runs PHP's built-in http server for development.
17 * - Don't use this for production, it is neither scalable nor secure for this use.
18 * - If you run multiple servers simultaneously, you will need to assign each a unique port.
19 * - Use Ctrl-C or equivalent to stop the server when complete.
22 * @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.
23 * @option default-server A default addr:port/path to use for any values not specified as an argument.
24 * @option browser If opening a web browser, which browser to use (defaults to operating system default). Use --no-browser to avoid opening a browser.
25 * @option dns Resolve hostnames/IPs using DNS/rDNS (if possible) to determine binding IPs and/or human friendly hostnames for URLs and browser.
28 * @usage drush rs 8080
29 * Start a web server on 127.0.0.1, port 8080.
30 * @usage drush rs 10.0.0.28:80
31 * Start runserver on 10.0.0.28, port 80.
32 * @usage drush rs [::1]:80
33 * Start runserver on IPv6 localhost ::1, port 80.
34 * @usage drush rs --dns localhost:8888/user
35 * Start runserver on localhost (using rDNS to determine binding IP), port 8888, and open /user in browser.
37 * Start runserver on default IP/port (127.0.0.1, port 8888), and open / in browser.
38 * @usage drush rs --default-server=127.0.0.1:8080/ -
39 * Use a default (would be specified in your drushrc) that starts runserver on port 8080, and opens a browser to the front page. Set path to a single hyphen path in argument to prevent opening browser for this session.
40 * @usage drush rs :9000/admin
41 * 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.
43 public function runserver($uri = null, $options = ['default-server' => self::REQ, 'browser' => true, 'dns' => false])
47 // Determine active configuration.
48 $uri = $this->uri($uri, $options);
53 // Remove any leading slashes from the path, since that is what url() expects.
54 $path = ltrim($uri['path'], '/');
56 // $uri['addr'] is a special field set by runserver_uri()
57 $hostname = $uri['host'];
60 drush_set_context('DRUSH_URI', 'http://' . $hostname . ':' . $uri['port']);
62 // We delete any registered files here, since they are not caught by Ctrl-C.
63 _drush_delete_registered_files();
65 // We set the effective base_url, since we have now detected the current site,
66 // and need to ensure generated URLs point to our runserver host.
67 // We also pass in the effective base_url to our auto_prepend_script via the
68 // CGI environment. This allows Drupal to generate working URLs to this http
69 // server, whilst finding the correct multisite from the HTTP_HOST header.
70 $base_url = 'http://' . $addr . ':' . $uri['port'];
71 $env['RUNSERVER_BASE_URL'] = $base_url;
74 $link = Url::fromUserInput('/' . $path, ['absolute' => true])->toString();
75 $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')]));
76 // Start php built-in server.
78 // Start a browser if desired. Include a 2 second delay to allow the server to come up.
79 $this->startBrowser($link, 2);
81 // Start the server using 'php -S'.
82 $extra = ' "' . DRUSH_BASE_PATH . '/misc/d8-rs-router.php"';
83 $root = Drush::bootstrapManager()->getRoot();
84 drush_shell_exec_interactive('cd %s && %s -S ' . $addr . ':' . $uri['port']. $extra, $root, Drush::config()->get('php', 'php'));
88 * Determine the URI to use for this server.
90 public function uri($uri, $options)
93 'host' => '127.0.0.1',
97 $user_default = $this->parseUri($options['default-server']);
98 $site_default = $this->parseUri($uri);
99 $uri = $this->parseUri($uri);
100 if (is_array($uri)) {
101 // Populate defaults.
102 $uri = $uri + $user_default + $site_default + $drush_default;
103 if (ltrim($uri['path'], '/') == '-') {
104 // Allow a path of a single hyphen to clear a default path.
107 // Determine and set the new URI.
108 $uri['addr'] = $uri['host'];
109 if ($options['dns']) {
110 if (ip2long($uri['host'])) {
111 $uri['host'] = gethostbyaddr($uri['host']);
113 $uri['addr'] = gethostbyname($uri['host']);
121 * Parse a URI or partial URI (including just a port, host IP or path).
124 * String that can contain partial URI.
127 * URI array as returned by parse_url.
129 public function parseUri($uri)
134 if ($uri[0] == ':') {
135 // ':port/path' shorthand, insert a placeholder hostname to allow parsing.
136 $uri = 'placeholder-hostname' . $uri;
138 // FILTER_VALIDATE_IP expects '[' and ']' to be removed from IPv6 addresses.
139 // We check for colon from the right, since IPv6 addresses contain colons.
140 $to_path = trim(substr($uri, 0, strpos($uri, '/')), '[]');
141 $to_port = trim(substr($uri, 0, strrpos($uri, ':')), '[]');
142 if (filter_var(trim($uri, '[]'), FILTER_VALIDATE_IP) || filter_var($to_path, FILTER_VALIDATE_IP) || filter_var($to_port, FILTER_VALIDATE_IP)) {
143 // 'IP', 'IP/path' or 'IP:port' shorthand, insert a schema to allow parsing.
144 $uri = 'http://' . $uri;
146 $uri = parse_url($uri);
148 throw new \Exception(dt('Invalid argument - should be in the "host:port/path" format, numeric (port only) or non-numeric (path only).'));
150 if (count($uri) == 1 && isset($uri['path'])) {
151 if (is_numeric($uri['path'])) {
152 // Port only shorthand.
153 $uri['port'] = $uri['path'];
157 if (isset($uri['host']) && $uri['host'] == 'placeholder-hostname') {