Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions commands/runserver/d7-rs-router.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

// We hijack filter_init (which core filter module does not implement) as
// a convenient place to affect early changes.
if (!function_exists('filter_init')) {
// Check function_exists as a safety net in case it is added in future.
function filter_init() {
global $conf, $user;
// Inject values into the $conf array - will apply to all sites.
// This can be a useful place to apply generic development settings.
$conf_inject = unserialize(urldecode(runserver_env('RUNSERVER_CONF')));
// Merge in the injected conf, overriding existing items.
$conf = array_merge($conf, $conf_inject);
}
}

// We hijack system_watchdog (which core system module does not implement) as
// a convenient place to capture logs.
if (!function_exists('system_watchdog')) {
// Check function_exists as a safety net in case it is added in future.
function system_watchdog($log_entry = array()) {
$uid = $log_entry['user']->uid;
$message = strtr('Watchdog: !message | severity: !severity | type: !type | uid: !uid | !ip | !request_uri | !referer | !link', array(
'!message' => strip_tags(!isset($log_entry['variables']) ? $log_entry['message'] : strtr($log_entry['message'], $log_entry['variables'])),
'!severity' => $log_entry['severity'],
'!type' => $log_entry['type'],
'!ip' => $log_entry['ip'],
'!request_uri' => $log_entry['request_uri'],
'!referer' => $log_entry['referer'],
'!uid' => $uid,
'!link' => strip_tags($log_entry['link']),
));
error_log($message);
}
}

// Get a $_SERVER key, or equivalent environment variable
// if it is not set in $_SERVER.
function runserver_env($key) {
if (isset($_SERVER[$key])) {
return $_SERVER[$key];
}
else {
return getenv($key);
}
}

$url = parse_url($_SERVER["REQUEST_URI"]);
if (file_exists('.' . $url['path'])) {
// Serve the requested resource as-is.
return FALSE;
}

// Populate the "q" query key with the path, skip the leading slash.
$_GET['q'] = $_REQUEST['q'] = substr($url['path'], 1);

// We set the base_url so that Drupal generates correct URLs for runserver
// (e.g. http://127.0.0.1:8888/...), but can still select and serve a specific
// site in a multisite configuration (e.g. http://mysite.com/...).
$base_url = runserver_env('RUNSERVER_BASE_URL');

// The built in webserver incorrectly sets $_SERVER['SCRIPT_NAME'] when URLs
// contain multiple dots (such as config entity IDs) in the path. Since this is
// a virtual resource, served by index.php set the script name explicitly.
// See https://github.com/drush-ops/drush/issues/2033 for more information.
$_SERVER['SCRIPT_NAME'] = '/index.php';

// Include the main index.php and let Drupal take over.
// n.b. Drush sets the cwd to the Drupal root during bootstrap.
include 'index.php';
34 changes: 34 additions & 0 deletions commands/runserver/d8-rs-router.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,39 @@
<?php

// We hijack filter_init (which core filter module does not implement) as
// a convenient place to affect early changes.
if (!function_exists('filter_init')) {
// Check function_exists as a safety net in case it is added in future.
function filter_init() {
global $conf, $user;
// Inject values into the $conf array - will apply to all sites.
// This can be a useful place to apply generic development settings.
$conf_inject = unserialize(urldecode(runserver_env('RUNSERVER_CONF')));
// Merge in the injected conf, overriding existing items.
$conf = array_merge($conf, $conf_inject);
}
}

// We hijack system_watchdog (which core system module does not implement) as
// a convenient place to capture logs.
if (!function_exists('system_watchdog')) {
// Check function_exists as a safety net in case it is added in future.
function system_watchdog($log_entry = array()) {
$uid = $log_entry['user']->id();
$message = strtr('Watchdog: !message | severity: !severity | type: !type | uid: !uid | !ip | !request_uri | !referer | !link', array(
'!message' => strip_tags(!isset($log_entry['variables']) ? $log_entry['message'] : strtr($log_entry['message'], $log_entry['variables'])),
'!severity' => $log_entry['severity'],
'!type' => $log_entry['type'],
'!ip' => $log_entry['ip'],
'!request_uri' => $log_entry['request_uri'],
'!referer' => $log_entry['referer'],
'!uid' => $uid,
'!link' => strip_tags($log_entry['link']),
));
error_log($message);
}
}

// Get a $_SERVER key, or equivalent environment variable
// if it is not set in $_SERVER.
function runserver_env($key) {
Expand Down
5 changes: 4 additions & 1 deletion commands/runserver/runserver.drush.inc
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,12 @@ function drush_core_runserver($uri = NULL) {
drush_start_browser($browse, 2);
}
// Start the server using 'php -S'.
if (drush_drupal_major_version() >=8) {
if (drush_drupal_major_version() >= 8) {
$extra = ' "' . __DIR__ . '/d8-rs-router.php"';
}
elseif (drush_drupal_major_version() == 7) {
$extra = ' "' . __DIR__ . '/d7-rs-router.php"';
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks fine in terms of functionality. The only feedback I would have would be if it would be worth considering using the core "engines" hooks to manage this, rather than a conditional include - see https://github.com/drush-ops/drush/blob/master/includes/engines.inc for details. This would be part of the "drupal" engine type, which would provide automatic selection of the include based on the core version - similar to the "pm" engine you can see defined in https://github.com/drush-ops/drush/blob/master/commands/core/core.drush.inc#L33 and https://github.com/drush-ops/drush/tree/master/commands/core/drupal. Apart from organization and making the engines available to other commands (that do rs-type stuff), this could allow the engines to supplement the rs command docs (if they provide additional options etc).

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given that the current code is already doing a conditional, this PR isn't making the situation any worse. I'd propose putting off improving the organization to a follow-up PR after fixing the bug.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That sounds fine to me

else {
$extra = ' --define auto_prepend_file="' . __DIR__ . '/runserver-prepend.php"';
}
Expand Down