You are here

function xautoload_ClassFinder_NamespaceOrPrefix::findFile in X Autoload 7.2

Same name and namespace in other branches
  1. 7.3 lib/ClassFinder/NamespaceOrPrefix.php \xautoload_ClassFinder_NamespaceOrPrefix::findFile()

Finds the path to the file where the class is defined.

Parameters

xautoload_InjectedAPI_findFile $api: API object with a suggestFile() method. We are supposed to call $api->suggestFile($file) with all suggestions we can find, until it returns TRUE. Once suggestFile() returns TRUE, we stop and return TRUE as well. The $file will be in the $api object, so we don't need to return it.

string $class: The name of the class, with all namespaces prepended. E.g. Some\Namespace\Some\Class

Return value

TRUE|NULL TRUE, if we found the file for the class. That is, if the $api->suggestFile($file) method returned TRUE one time. NULL, if we have no more suggestions.

Overrides xautoload_ClassFinder_Prefix::findFile

File

lib/ClassFinder/NamespaceOrPrefix.php, line 115

Class

xautoload_ClassFinder_NamespaceOrPrefix

Code

function findFile($api, $class) {
  if ('\\' == $class[0]) {
    $class = substr($class, 1);
  }
  if (FALSE !== ($pos = strrpos($class, '\\'))) {

    // The class is within a namespace.
    if ($class[$pos + 1] === '_') {

      // We do not autoload classes where the class name begins with '_'.
      return;
    }

    // Loop through positions of '\\', backwards.
    $first_part = str_replace('\\', DIRECTORY_SEPARATOR, substr($class, 0, $pos + 1));
    $second_part = str_replace('_', DIRECTORY_SEPARATOR, substr($class, $pos + 1)) . '.php';
    $path = $first_part . $second_part;
    while (TRUE) {
      if ($this->namespaceMap
        ->findFile_nested($api, $first_part, $second_part)) {
        return TRUE;
      }
      $pos = strrpos($first_part, DIRECTORY_SEPARATOR, -2);
      if (FALSE === $pos) {
        break;
      }
      $first_part = substr($path, 0, $pos + 1);
      $second_part = substr($path, $pos + 1);
    }

    // Check if anything is registered for the root namespace.
    if ($this->namespaceMap
      ->findFile_nested($api, '', $path)) {
      return TRUE;
    }
  }
  else {

    // The class is not within a namespace.
    // Fall back to the prefix-based finder.
    return parent::findFile($api, $class);
  }
}