Last active
December 24, 2015 02:39
-
-
Save jdillick/6731777 to your computer and use it in GitHub Desktop.
Dynamic drush aliases. Just modify $paths to include the directory to your drupal installs, and you have instant aliases.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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