Skip to content

Commit

Permalink
Improve handling of DRUSH_OPTIONS_URI
Browse files Browse the repository at this point in the history
  • Loading branch information
back-2-95 committed Feb 25, 2020
1 parent 0805398 commit 21faa47
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions src/DrupalEnvDetector.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,20 +184,29 @@ private function setGlobalDefaults() {
*/
private function setTrustedHostPatterns() {
$this->settings['trusted_host_patterns'] = [];
$hosts = [];

// Drupal route(s).
$routes = (getenv('DRUPAL_ROUTES')) ? explode(',', getenv('DRUPAL_ROUTES')) : [];

foreach ($routes as $route) {
$host = str_replace('.', '\.', parse_url($route)['host']);
$this->settings['trusted_host_patterns'][] = '^' . $host . '$';
$hosts[] = $host = parse_url($route)['host'];
$trusted_host = str_replace('.', '\.', $host);
$this->settings['trusted_host_patterns'][] = '^' . $trusted_host . '$';
}

if (getenv('DRUSH_OPTIONS_URI') && !in_array(getenv('DRUSH_OPTIONS_URI'), $routes)) {
$host = str_replace('.', '\.', parse_url(getenv('DRUSH_OPTIONS_URI'))['host']);
$drush_options_uri = getenv('DRUSH_OPTIONS_URI');

if ($drush_options_uri && !in_array($drush_options_uri, $routes)) {
$host = str_replace('.', '\.', parse_url($drush_options_uri)['host']);
$this->settings['trusted_host_patterns'][] = '^' . $host . '$';
}

// If not explicitly set, use first host as DRUSH_OPTIONS_URI
if (!$drush_options_uri) {
putenv('DRUSH_OPTIONS_URI=http://' . $hosts[0]);
}

if (method_exists($this->omen, 'getTrustedHostPatterns')) {
$patterns = $this->omen->getTrustedHostPatterns();
$this->settings['trusted_host_patterns'] = array_merge($this->settings['trusted_host_patterns'], $patterns);
Expand Down

0 comments on commit 21faa47

Please sign in to comment.