You are here

function _autoload_registry_check_code in Autoload 6.2

Helper to check for a resource in the registry.

Parameters

$type: The type of resource we are looking up, or one of the constants REGISTRY_RESET_LOOKUP_CACHE or REGISTRY_WRITE_LOOKUP_CACHE, which signal that we should reset or write the cache, respectively.

$name: The name of the resource, or NULL if either of the REGISTRY_* constants is passed in.

Return value

TRUE if the resource was found, FALSE if not. NULL if either of the REGISTRY_* constants is passed in as $type.

Related topics

4 calls to _autoload_registry_check_code()
autoload_class in ./autoload.module
Confirm that a class is available.
autoload_exit in ./autoload.module
Implements hook_exit().
autoload_interface in ./autoload.module
Confirm that an interface is available.
_autoload_registry_update in ./autoload.registry.inc
Does the work for autoload_registry_update().

File

./autoload.module, line 147

Code

function _autoload_registry_check_code($type, $name = NULL) {
  static $lookup_cache, $cache_update_needed;
  if ($type == 'class' && class_exists($name) || $type == 'interface' && interface_exists($name)) {
    return TRUE;
  }
  if (!isset($lookup_cache)) {
    $lookup_cache = array();
    if ($cache = cache_get('autoload')) {
      $lookup_cache = $cache->data;
    }
  }

  // When we rebuild the registry, we need to reset this cache so
  // we don't keep lookups for resources that changed during the rebuild.
  if ($type == AUTOLOAD_REGISTRY_RESET_LOOKUP_CACHE) {
    $cache_update_needed = TRUE;
    $lookup_cache = NULL;
    return;
  }

  // Called from drupal_page_footer, we write to permanent storage if there
  // changes to the lookup cache for this request.
  if ($type == AUTOLOAD_REGISTRY_WRITE_LOOKUP_CACHE) {
    if ($cache_update_needed) {
      cache_set('autoload', $lookup_cache);
    }
    return;
  }

  // $type is either 'interface' or 'class', so we only need the first letter to
  // keep the cache key unique.
  $cache_key = $type[0] . $name;
  if (isset($lookup_cache[$cache_key])) {
    if ($lookup_cache[$cache_key]) {
      require_once './' . $lookup_cache[$cache_key];
    }
    return (bool) $lookup_cache[$cache_key];
  }

  // This function may get called when the default database is not active, but
  // there is no reason we'd ever want to not use the default database for
  // this query.
  $file = db_result(db_query("SELECT filename FROM {autoload_registry} WHERE name = '%s' AND type = '%s'", $name, $type));

  // Flag that we've run a lookup query and need to update the cache.
  $cache_update_needed = TRUE;

  // Misses are valuable information worth caching, so cache even if
  // $file is FALSE.
  $lookup_cache[$cache_key] = $file;
  if ($file) {
    require_once './' . $file;
    return TRUE;
  }
  else {
    if (variable_get('autoload_registry_watchdog_misses', FALSE)) {
      $variables = array(
        '%type' => $type,
        '%name' => $name,
      );
      watchdog('autoload', "Registry had no entry for %type %name", $variables, WATCHDOG_NOTICE);
    }
    return FALSE;
  }
}