yka-vq
Last Updated: February 25, 2016
·
514
· yorirou
6070c18b2f3de0155705b277bbd43f2b

Automatically determine domain with drush

This is a Drupal-specific continuation of my previous protip: https://coderwall.com/p/r4nbzw

Add the following snippet to ~/.drush/drushrc.php

function determineDomain() {
  static $SITES_DIR = '/Users/<username>/Sites/';
  $cwd = getcwd();
  if (stripos($cwd, $SITES_DIR) === 0) {
    $relpath = substr($cwd, strlen($SITES_DIR));
    $slashpos = strpos($relpath, '/');
    if ($slashpos === FALSE) {
      $domain = $relpath;
    }
    else {
      $domain = substr($relpath, 0, $slashpos);
    }

    if (stripos($relpath, "$domain/sites/")) {
      return FALSE;
    }

    return "$domain.127.0.0.1.xip.io";
  }

  return FALSE;
}

if ($domain = determineDomain()) {
  $options['l'] = $domain;
}

What this does is that it populates the -l command line switch if you are in a /Users/<username>/Sites/<sitename> or any of its subdirectories except sites/<sitedomain>. This fixes some commands, for example user-login, which can't determine the correct domain automatically.

Say Thanks
Respond