Skip to content

Instantly share code, notes, and snippets.

@KeyboardCowboy
Last active December 14, 2015 15:49
Show Gist options
  • Select an option

  • Save KeyboardCowboy/5110839 to your computer and use it in GitHub Desktop.

Select an option

Save KeyboardCowboy/5110839 to your computer and use it in GitHub Desktop.
Drush auto-alias script. This script, when placed in your /etc/drush folder will scan your webroot and auto-detect Drupal installations, both standalone and multisite, and create aliases for each site. It is not complete yet. The grouping still needs a little work, but otherwise gets you off the ground.
<?php
/**
* @file
* Build a dynamic list of drush aliases by identifying Drupal directories.
*
* The script will scan your webroot for Drupal directories. Place this in
* /etc/drush/aliases.drushrc.php
*
* @param $root
* The absolute path to your webroot.
*/
// Webroot
$web = "/var/www/htdocs";
// Instantiate the alias arrays.
$aliases = $auto_aliases = array();
// The current directory will be either the actual directory if not in a drupal
// root or the drupal root directory if we are anywhere within a drupal site.
$cwd = getcwd();
/**
* Helper function to parse out site overrides.
*/
if (!function_exists('_daconfig_get_settings')) {
function _daconfig_get_settings($path) {
if (file_exists($path)) {
include($path);
}
$custom = array(
'aliases' => (isset($aliases) ? $aliases : array()),
'group_suffix' => (isset($group_suffix) ? $group_suffix : ''),
'default_uri' => (isset($default_uri) ? $default_uri : 'http://localhost/{site}'),
);
return $custom;
}
}
/**
* Helper function to recursively merge arrays.
*/
if (!function_exists('array_merge_recursive_distinct')) {
function array_merge_recursive_distinct(array &$array1, array &$array2) {
$merged = $array1;
foreach ($array2 as $key => &$value) {
if (is_array($value) && isset($merged[$key]) && is_array($merged [$key] )) {
$merged[$key] = array_merge_recursive_distinct($merged[$key], $value);
}
else {
$merged[$key] = $value;
}
}
return $merged;
}
}
/**
* For each entry, verify that it is a directory and then that it is a drupal
* instance.
*/
foreach (scandir($web) as $root) {
if (is_dir("$web/$root") && file_exists("$web/$root/sites/default/settings.php")) {
// If this drupal root is delaring its own aliases and we are currently
// working inside that root, don't add it to the global settings so the
// local settings have the right of way.
if (file_exists("$web/$root/sites/all/drush/aliases.drushrc.php") && stristr($cwd, "$web/$root")) {
continue;
}
// Load any custom settings for this site if they are declared in the file
// sites/all/drush/aliases.drushrc.inc
$custom = _daconfig_get_settings("$web/$root/sites/all/drush/autoalias.drushrc.inc");
// First, register multisite aliases.
$sites = array();
foreach (scandir("$web/$root/sites") as $site) {
if (is_dir("$web/$root/sites/$site") && $site != 'default' && file_exists("$web/$root/sites/$site/settings.php")) {
$alias = "{$root}.{$site}";
$sites[$alias]['root'] = "$web/$root";
$sites[$alias]['uri'] = str_replace('{site}', $site, $custom['default_uri']);
$sites[$alias]['path-aliases']['%site'] = "$web/$root/sites/$site";
$sites[$alias]['#group'] = array("{$root}{$custom['group_suffix']}");
}
}
// If multisite aliases were found, create a grouped alias as the base.
// Otherwise, alias the base.
if (!empty($sites)) {
// Add a default entry. We do this here instead of allowing the default
// dir when detecting multisites so single-site instances don't have
// both a default and a group alias.
$sites["{$root}.default"]['root'] = "$web/$root";
$sites["{$root}.default"]['uri'] = str_replace('{site}', $root, $custom['default_uri']);
$sites["{$root}.default"]['#group'] = array("{$root}{$custom['group_suffix']}");
$auto_aliases += $sites;
}
// Create a monosite alias
else {
$auto_aliases[$root]['root'] = "$web/$root";
$auto_aliases[$root]['uri'] = str_replace('{site}', $root, $custom['default_uri']);
}
// Merge with overrides
if (!empty($custom['aliases'])) {
$auto_aliases = array_merge_recursive_distinct($auto_aliases, $custom['aliases']);
}
}
}
// Create the groups
$groups = array();
foreach ($auto_aliases as $aa => $info) {
if (is_array($info['#group'])) {
foreach ($info['#group'] as $group) {
$groups[$group]['site-list'][] = "@{$aa}";
}
}
}
// Merge any sourced aliases with our generated aliases.
$aliases += $auto_aliases += $groups;
<?php
/**
* @file
* Drush aliases to be included into the global set. To override these with
* aliases that are only recognized within this Drupal root, place them into
* sites/all/drush/aliases.drushrc.php.
*
* Place this file in sites/all/drush/autoalias.drushrc.inc
*
* Override default auto-alias settings.
*
* $group_suffix = '';
* Add a common suffix to group multisite aliases. Base is "ROOT"
*
* Ex:
* $group_suffix = '.local'; would render the group alias as "ROOT.local"
*
* $default_uri;
* Define a custom uri pattern with the site name to generate the uri for each
* site. Defaults to http://localhost/{site}
*
* Ex:
* $default_uri = 'http://{site}.example.local'
*
* $aliases
* Define pieces of aliases and allow the auto-alias to fill in the rest.
* These settings will override anything that auto-alias may derrive.
*
* Ex:
* $aliases['root.site'] = array(
* 'uri' => 'http://different.uri.com,
* 'path-aliases' => array(
* '%custom' => '/my/custom/path',
* ),
* );
*/
$group_suffix = '';
$default_uri = '';
$aliases = array();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment