Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Dynamic drush aliases. Just modify $paths to include the directory to your drupal installs, and you have instant aliases.
<?php
// the current environment
$env = $_ENV['ENVTYPE'] ? $_ENV['ENVTYPE'] : '';
// paths to search for drupal sites
$paths = array(
'/var/www/multisites/',
'/var/www/schoolyard/',
);
$new_aliases = array();
foreach ( $paths as $path ) {
$dh = opendir($path);
while ( $file = readdir($dh) ) {
$fullname = "$path$file";
// skip files, current directory, and parent directory
if ( strpos($file, '.') === 0 || ! is_dir($fullname) ) continue;
// look for sites or webroot/sites directories in path
$has_webroot = file_exists($fullname . '/webroot/sites/');
$has_sites = file_exists($fullname . '/sites/');
if ( $has_webroot || $has_sites ) {
$sites = $fullname . ($has_sites ? '/sites/' : '/webroot/sites/');
$sh = opendir($sites);
// look for domain.tld directory in sites
while ( $site = readdir($sh) ) {
$sitepath = $sites . $site;
// skip ./, ../, all/, and default/ directories
if ( is_dir($sitepath) && ! in_array($site, array('default', '.', '..', 'all')) ) {
// construct the site alias from the environment and domain
$new_aliases[$file][$site] = array(
'root' => $fullname . ($has_webroot ? '/webroot/' : '/'),
'uri' => (strpos($fullname, 'schoolyard') ? "http://$site" : "http://$env.$site"),
);
}
}
}
}
}
foreach ( $new_aliases as $file => $sites ) {
// single site drupal install
if ( count($sites) == 1 ) {
$aliases[$file] = reset($sites);
}
// multisite drupal install
else {
foreach ( $sites as $name => $site ) {
list($prefix, $suffix) = explode('.', $name);
$aliases[$file . "." . $prefix] = $site;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.