See the following little function for merging aliases at includes/sitealias.inc:

// array_merge_recursive is too much; we only want to run
// array_merge on the common top-level keys of the array.
function _sitealias_array_merge($a, $b) {
  $result = $a;

  foreach($b as $key => $value) {
    if (array_key_exists($key, $result)) {
      $result[$key] = array_merge($result[$key], $b[$key]);
    }
    else {
      $result[$key] = $value;
    }
  }

  return $result;
}

The following statement assumes that all keys that exist in both site aliases contain arrays:

if (array_key_exists($key, $result)) {
   $result[$key] = array_merge($result[$key], $b[$key]);
}

This makes that if $site_alias_b redefines a key in $site_alias_a whose contents are not an array you get the following warnings when that site alias is loaded:

array_merge(): Argument #1 is not an array sitealias.inc:673                                                                               [warning]

A patch is being posted in the following comment to address this.

Comments

juampynr’s picture

Status: Active » Needs review
StatusFileSize
new1.64 KB

Attaching patch. I also documented the function.

moshe weitzman’s picture

Assigned: Unassigned » greg.1.anderson
greg.1.anderson’s picture

Status: Needs review » Needs work

This looks good, but it could use a test.

greg.1.anderson’s picture

Issue summary: View changes

Updated issue summary.

juampynr’s picture

Status: Needs work » Needs review

Here you are. You can test it by doing the following:

$ phpunit siteAliasUnitTest
PHPUnit 3.6.10 by Sebastian Bergmann.
Configuration read from /usr/share/drush/tests/phpunit.xml
.
Time: 0 seconds, Memory: 5.25Mb
OK (1 test, 1 assertion)

The above won't throw any warnings and the test will pass. Try now undoing the changes that this patch does at phpunit includes/sitealias.inc and run the test again:

$ git checkout ../includes/sitealias.inc
$ phpunit siteAliasUnitTest
PHPUnit 3.6.10 by Sebastian Bergmann.

Configuration read from /usr/share/drush/tests/phpunit.xml

array_merge(): Argument #1 is not an array sitealias.inc:672         [warning]
array_merge(): Argument #1 is not an array sitealias.inc:672         [warning]
array_merge(): Argument #1 is not an array sitealias.inc:672         [warning]
array_merge(): Argument #1 is not an array sitealias.inc:672         [warning]
F

Time: 0 seconds, Memory: 5.50Mb
There was 1 failure:
1) saUnitCase::testArrayMerge
Failed asserting that two arrays are equal.
--- Expected
+++ Actual
@@ @@
 Array (
-    'remote-host' => 'another-fake.remote-host.com'
-    'remote-user' => 'www-other'
-    'root' => '/fake/path/to/root'
-    'uri' => 'default'
+    'remote-host' => null
+    'remote-user' => null
+    'root' => null
+    'uri' => null
     'command-specific' => Array (...)
 )

/usr/share/drush/tests/siteAliasUnitTest.php:58

FAILURES!
Tests: 1, Assertions: 1, Failures: 1.
juampynr’s picture

Doh! Attaching patch.

greg.1.anderson’s picture

Status: Needs review » Fixed

Committed. Thanks.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

Anonymous’s picture

Issue summary: View changes

Removed mention about custom keys.