function _rules_discover_module

Determines the module providing the given class.

Parameters

string $class: The name of the class or interface plugins to discover.

Return value

string|false The path of the class, relative to the Drupal installation root, or FALSE if not discovered.

1 call to _rules_discover_module()
rules_discover_plugins in ./rules.module
Discover plugin implementations.

File

./rules.module, line 344

Code

function _rules_discover_module($class) {
  static $symlink_bases = array();
  $paths =& drupal_static(__FUNCTION__);
  if (!isset($paths)) {
    // Build up a map of modules keyed by their directory.
    foreach (system_list('module_enabled') as $name => $module_info) {
      $paths[dirname($module_info->filename)] = $name;
    }
  }
  // Retrieve the class file and convert its absolute path to a regular Drupal
  // path relative to the installation root.
  $drupal_root = realpath(DRUPAL_ROOT) . DIRECTORY_SEPARATOR;
  $reflection = new ReflectionClass($class);
  $file_path = realpath($reflection->getFileName());
  // Check if the class file lives outside of the Drupal root. If so, we have
  // to assume that this is a symlink. Symlinks are then resolved to fit to the
  // Drupal structure again. Don't use realpath() with the symlinks as this will
  // resolve the symlink again.
  if (stristr(realpath(DRUPAL_ROOT), realpath(dirname($file_path))) === FALSE) {
    // Check if this matches a resolved symlink.
    if ($symlink_bases) {
      // Cutting the $file_path means just one iteration left below.
      $file_path = str_replace($symlink_bases, '', $file_path);
    }
    // Split the file path in parts and attempt to find where the parts start
    // to match the drupal file structure again.
    $parts = explode(DIRECTORY_SEPARATOR, $file_path);
    do {
      $path = implode(DIRECTORY_SEPARATOR, $parts);
      $symlink_path = $drupal_root . $path;
      $symlink_base = str_replace($path, '', $file_path);
    } while (file_exists($symlink_path) === FALSE && array_shift($parts));
    if (file_exists($symlink_path)) {
      $file_path = $symlink_path;
      $symlink_bases[$symlink_base] = $symlink_base;
    }
  }
  $path = str_replace($drupal_root, '', dirname($file_path));
  $path = DIRECTORY_SEPARATOR != '/' ? str_replace(DIRECTORY_SEPARATOR, '/', $path) : $path;
  // Go up the path until we match a module.
  $parts = explode('/', $path);
  while (!isset($paths[$path]) && array_pop($parts)) {
    $path = dirname($path);
  }
  return isset($paths[$path]) ? $paths[$path] : FALSE;
}