function _drush_sitealias_find_and_load_alias

8.0.x sitealias.inc _drush_sitealias_find_and_load_alias($aliasname, $alias_path_context = NULL)
6.x sitealias.inc _drush_sitealias_find_and_load_alias($aliasname, $alias_path_context = NULL)
7.x sitealias.inc _drush_sitealias_find_and_load_alias($aliasname, $alias_path_context = NULL)
3.x sitealias.inc _drush_sitealias_find_and_load_alias($aliasname, $alias_path_context = NULL)
4.x sitealias.inc _drush_sitealias_find_and_load_alias($aliasname, $alias_path_context = NULL)
5.x sitealias.inc _drush_sitealias_find_and_load_alias($aliasname, $alias_path_context = NULL)
master sitealias.inc _drush_sitealias_find_and_load_alias($aliasname, $alias_path_context = NULL)

Worker function called by _drush_sitealias_load_alias and drush_sitealias_load_all. Traverses the alias search path and finds the specified alias record.

Parameters

$aliasname: The name of the alias without the leading '@' (i.e. 'name') or NULL to load every alias found in every alias file.

$alias_path_context: When looking up a relative alias, the alias path context is the primary alias that we will start our search from.

Return value

An empty array if nothing was loaded. If $aliasname is not null, then the array returned is the alias record for $aliasname. If $aliasname is NULL, then the array returned is a $kay => $value pair of alias names and alias records loaded.

2 calls to _drush_sitealias_find_and_load_alias()
drush_sitealias_load_all in includes/sitealias.inc
Load every alias file that can be found anywhere in the alias search path.
_drush_sitealias_load_alias in includes/sitealias.inc
Check and see if an alias definition for $alias is available. If it is, load it into the list of aliases cached in the 'site-aliases' context.

File

includes/sitealias.inc, line 381
The site alias API.

Code

function _drush_sitealias_find_and_load_alias($aliasname, $alias_path_context = NULL) {
  $result = array();

  // Special checking for '@sites' alias
  if ($aliasname == 'sites') {
    $drupal_root = NULL;
    if ($alias_path_context != null) {
      if (array_key_exists('root', $alias_path_context) && !array_key_exists('remote-host', $alias_path_context)) {
        $drupal_root = $alias_path_context['root'];
      }
    }
    else {
      $drupal_root = drush_get_context('DRUSH_DRUPAL_ROOT');
      if ($drupal_root == NULL) {
        $drupal_root = drush_get_option(array('r', 'root'), drush_locate_root());
      }
    }
    if (isset($drupal_root) && !is_array($drupal_root)) {
      drush_sitealias_create_sites_alias($drupal_root);
    }
  }

  // The alias path is a list of folders to search for alias settings files
  $alias_path = drush_sitealias_alias_path($alias_path_context);

  // $alias_files contains a list of filename patterns
  // to search for.  We will find any matching file in
  // any folder in the alias path.  The directory scan
  // is not deep, though; only files immediately in the
  // search path are considered.
  $alias_files = array('/.*aliases\.drushrc\.php/');
  if ($aliasname == NULL) {
    $alias_files[] = '/.*\.alias\.drushrc\.php/';
  }
  else {
    $alias_files[] = '/' . preg_quote($aliasname) . '\.alias\.drushrc\.php/';
  }

  // Search each path in turn
  foreach ($alias_path as $path) {
    // Find all of the matching files in this location
    $alias_files_to_consider = array();
    foreach ($alias_files as $file_pattern_to_search_for) {
      $alias_files_to_consider = array_merge($alias_files_to_consider, array_keys(drush_scan_directory($path, $file_pattern_to_search_for, array('.', '..', 'CVS'), 0, FALSE)));
    }

    // For every file that matches, check inside it for
    // an alias with a matching name.
    foreach ($alias_files_to_consider as $filename) {
      if (file_exists($filename)) {
        $aliases = $options = array();
        include $filename;
        unset($options['site-aliases']); // maybe unnecessary

        // If $aliases are not set, but $options are, then define one alias named
        // after the first word of the file, before '.alias.drushrc.php.
        if (empty($aliases) && !empty($options)) {
          $this_alias_name = substr(basename($filename), 0, strpos(basename($filename), '.'));
          $aliases[$this_alias_name] = $options;
          $options = array();
        }
        // If this is a group alias file, then make an
        // implicit alias from the group name that contains
        // a site-list of all of the aliases in the file
        if (substr($filename, -20) == ".aliases.drushrc.php") {
          $group_name = basename($filename, ".aliases.drushrc.php");
          if (!array_key_exists($group_name, $aliases)) {
            $alias_names = array();
            foreach (array_keys($aliases) as $one_alias) {
              $alias_names[] = "@$one_alias";
            }
            $aliases[$group_name] = array('site-list' => implode(',', $alias_names));
          }
        }
        // If aliasname is NULL, then we will store
        // all $aliases into the alias cache
        if ($aliasname == NULL) {
          if (!empty($aliases)) {
            if (!empty($options)) {
              foreach ($aliases as $name => $value) {
                $aliases[$name] = array_merge($options, $value);
              }
              $options = array();
            }

            foreach ($aliases as $name => $value) {
              _drush_sitealias_initialize_alias_record($aliases[$name]);
            }

            $result = array_merge($result, $aliases);
          }
        }
        // If aliasname is not NULL, then we will store
        // only the named alias into the alias cache
        elseif ((isset($aliases)) && array_key_exists($aliasname, $aliases)) {
          drush_set_config_special_contexts($options); // maybe unnecessary
          $result = array_merge($options, $aliases[$aliasname]);
          _drush_sitealias_initialize_alias_record($result);
        }
      }
    }
  }

  return $result;
}