Further Drupal 8.6.4 changes. Some core files were not committed before a commit...
[yaffs-website] / vendor / drush / drush / src / Commands / core / RunserverCommands.php
1 <?php
2 namespace Drush\Commands\core;
3
4 use Drush\Drush;
5 use Drupal\Core\Url;
6 use Drush\Commands\DrushCommands;
7 use Drush\Exec\ExecTrait;
8
9 class RunserverCommands extends DrushCommands
10 {
11
12     use ExecTrait;
13
14     /**
15      * Runs PHP's built-in http server for development.
16      *
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.
20      *
21      * @command runserver
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.
26      * @bootstrap full
27      * @aliases rs,serve
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.
36      * @usage drush rs /
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.
42      */
43     public function runserver($uri = null, $options = ['default-server' => self::REQ, 'browser' => true, 'dns' => false])
44     {
45         global $base_url;
46
47         // Determine active configuration.
48         $uri = $this->uri($uri, $options);
49         if (!$uri) {
50             return false;
51         }
52
53         // Remove any leading slashes from the path, since that is what url() expects.
54         $path = ltrim($uri['path'], '/');
55
56         // $uri['addr'] is a special field set by runserver_uri()
57         $hostname = $uri['host'];
58         $addr = $uri['addr'];
59
60         drush_set_context('DRUSH_URI', 'http://' . $hostname . ':' . $uri['port']);
61
62         // We delete any registered files here, since they are not caught by Ctrl-C.
63         _drush_delete_registered_files();
64
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;
72
73
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.
77         if (!empty($path)) {
78             // Start a browser if desired. Include a 2 second delay to allow the server to come up.
79             $this->startBrowser($link, 2);
80         }
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'));
85     }
86
87     /**
88      * Determine the URI to use for this server.
89      */
90     public function uri($uri, $options)
91     {
92         $drush_default = [
93             'host' => '127.0.0.1',
94             'port' => '8888',
95             'path' => '',
96         ];
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.
105                 $uri['path'] = '';
106             }
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']);
112                 } else {
113                     $uri['addr'] = gethostbyname($uri['host']);
114                 }
115             }
116         }
117         return $uri;
118     }
119
120     /**
121      * Parse a URI or partial URI (including just a port, host IP or path).
122      *
123      * @param string $uri
124      *   String that can contain partial URI.
125      *
126      * @return array
127      *   URI array as returned by parse_url.
128      */
129     public function parseUri($uri)
130     {
131         if (empty($uri)) {
132             return [];
133         }
134         if ($uri[0] == ':') {
135             // ':port/path' shorthand, insert a placeholder hostname to allow parsing.
136             $uri = 'placeholder-hostname' . $uri;
137         }
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;
145         }
146         $uri = parse_url($uri);
147         if (empty($uri)) {
148             throw new \Exception(dt('Invalid argument - should be in the "host:port/path" format, numeric (port only) or non-numeric (path only).'));
149         }
150         if (count($uri) == 1 && isset($uri['path'])) {
151             if (is_numeric($uri['path'])) {
152                 // Port only shorthand.
153                 $uri['port'] = $uri['path'];
154                 unset($uri['path']);
155             }
156         }
157         if (isset($uri['host']) && $uri['host'] == 'placeholder-hostname') {
158             unset($uri['host']);
159         }
160         return $uri;
161     }
162 }