function _drush_add_commandfiles

8.0.x command.inc _drush_add_commandfiles($searchpath, $phase = NULL, $reset = FALSE)
6.x command.inc _drush_add_commandfiles($searchpath, $phase = NULL, $reset = FALSE)
7.x command.inc _drush_add_commandfiles($searchpath, $phase = NULL, $reset = FALSE)
4.x command.inc _drush_add_commandfiles($searchpath, $phase = NULL)
5.x command.inc _drush_add_commandfiles($searchpath, $phase = NULL, $reset = FALSE)
master command.inc _drush_add_commandfiles($searchpath, $phase = NULL, $reset = FALSE)
3 calls to _drush_add_commandfiles()
drush_pm_enable_validate in commands/pm/pm.drush.inc
Validate callback. Determine the modules and themes that the user would like enabled.
releaseInfoCase::testReleaseXML in tests/releaseInfoTest.php
Pick right release from the XML (dev, latest published+recommended, ...).
_drush_find_commandfiles in includes/command.inc

File

includes/command.inc, line 1444
The drush command engine.

Code

function _drush_add_commandfiles($searchpath, $phase = NULL, $reset = FALSE) {
  static $evaluated = array();
  static $deferred = array();

  $cache = &drush_get_context('DRUSH_COMMAND_FILES', array());

  if (count($searchpath)) {
    if (!$reset) {
      // Assemble a cid specific to the bootstrap phase and searchpaths.
      // Bump $cf_version when making a change to a dev version of Drush
      // that invalidates the commandfile cache.
      $cf_version = 3;
      $cid = drush_get_cid('commandfiles-' . $phase, array(), array_merge($searchpath, array($cf_version)));
      $command_cache = drush_cache_get($cid);
      if (isset($command_cache->data)) {
        $cached_list = $command_cache->data;
        // Take drush_make out of the cache to help people transitioning
        // from using drush make as a contrib module to drush make in core.
        // See: http://drupal.org/node/1366746
        unset($cached_list['drush_make']);
        // If we want to temporarily ignore modules via 'ignored-modules',
        // then we need to take these out of the cache as well.
        foreach (drush_get_option_list('ignored-modules') as $ignored) {
          unset($cached_list[$ignored]);
        }
      }
    }

    // Build a list of all of the modules to attempt to load.
    // Start with any modules deferred from a previous phase.
    $list = $deferred;
    if (isset($cached_list)) {
      $list = array_merge($list, $cached_list);
    }
    else {
      // Scan for drush command files; add to list for consideration if found.
      foreach (array_unique($searchpath) as $path) {
        if (is_dir($path)) {
          $nomask = array_merge(drush_filename_blacklist(), drush_get_option_list('ignored-modules'));
          $dmv = DRUSH_MAJOR_VERSION;
          $files = drush_scan_directory($path, "/\.drush($dmv|)\.inc$/", $nomask);
          foreach ($files as $filename => $info) {
            $module = basename($filename);
            $module = preg_replace('/\.*drush[0-9]*\.inc/', '', $module);
            // Only try to bootstrap modules that we have never seen before, or that we
            // have tried to load but did not due to an unmet _drush_load() requirement.
            if (!array_key_exists($module, $evaluated) && file_exists($filename)) {
              $evaluated[$module] = TRUE;
              $list[$module] = $filename;
            }
          }
        }
      }
      if (isset($cid)) {
        drush_cache_set($cid, $list);
      }
    }
    // Check each file in the consideration list; if there is
    // a modulename_drush_load() function in modulename.drush.load.inc,
    // then call it to determine if this file should be loaded.
    foreach ($list as $module => $filename) {
      $load_command = TRUE;
      $module_versionless = preg_replace('/\.d([0-9]+)$/', '', $module);
      if (!isset($cache[$module_versionless])) {
        $drupal_version = '';
        if (preg_match('/\.d([0-9]+)$/', $module, $matches)) {
          $drupal_version = $matches[1];
        }
        if (!empty($drupal_version) && ($drupal_version != drush_drupal_major_version())) {
          $load_command = FALSE;
        }
        else {
          $load_test_inc = dirname($filename) . "/" . $module . ".drush.load.inc";
          if (file_exists($load_test_inc)) {
            include_once ($load_test_inc);
            $module_versionless = preg_replace('/\.d([0-9]+)$/', '', $module);
            $load_test_func = $module_versionless . "_drush_load";
            if (function_exists($load_test_func)) {
              $load_command = $load_test_func($phase);
            }
          }
        }
        if ($load_command) {
          // Only try to require if the file exists. If not, a file from the
          // command file cache may not be available anymore, in which case
          // we rebuild the cache for this phase.
          if ($filepath = realpath($filename)) {
            $cache[$module_versionless] = $filename;
            require_once $filepath;
            unset($deferred[$module]);
          }
          elseif (!$reset) {
            _drush_add_commandfiles($searchpath, $phase, TRUE);
          }
        }
        else {
          unset($list[$module]);
          // Signal that we should try again on
          // the next bootstrap phase.  We set
          // the flag to the filename of the first
          // module we find so that only that one
          // will be retried.
          $deferred[$module] = $filename;
        }
      }
    }

    if (count($list)) {
      ksort($cache);
    }
  }
}