You are here

function GenericPrefixMap::loadClass in X Autoload 7.4

Same name and namespace in other branches
  1. 7.5 src/ClassFinder/GenericPrefixMap.php \Drupal\xautoload\ClassFinder\GenericPrefixMap::loadClass()

Parameters

string $class:

string $logical_path: Class name translated into a logical path, either with PSR-4 or with PEAR translation rules.

int|bool $lastpos: Position of the last directory separator in $logical_path. FALSE, if there is no directory separator in $logical_path.

Return value

bool|NULL TRUE, if the class was found.

File

lib/ClassFinder/GenericPrefixMap.php, line 132

Class

GenericPrefixMap
Helper class for the class finder. This is not part of ClassFinder, because we want to use the same logic for namespaces (PSR-0) and prefixes (PEAR).

Namespace

Drupal\xautoload\ClassFinder

Code

function loadClass($class, $logical_path, $lastpos) {
  $pos = $lastpos;
  while (TRUE) {
    $logical_base_path = FALSE === $pos ? '' : substr($logical_path, 0, $pos + 1);
    if (isset($this->paths[$logical_base_path])) {
      foreach ($this->paths[$logical_base_path] as $dir => $behavior) {
        if ($behavior instanceof DefaultDirectoryBehavior) {

          // PSR-4 and PEAR
          if (file_exists($file = $dir . substr($logical_path, $pos + 1))) {
            require $file;
            return TRUE;
          }
        }
        elseif ($behavior instanceof Psr0DirectoryBehavior) {

          // PSR-0
          if (file_exists($file = $dir . substr($logical_path, $pos + 1, $lastpos - $pos) . str_replace('_', '/', substr($logical_path, $lastpos + 1)))) {
            require $file;
            return TRUE;
          }
        }
        elseif ($behavior instanceof xautoload_FinderPlugin_Interface) {

          // Legacy "FinderPlugin".
          $api = new LoadClassInjectedAPI($class);
          if ($behavior
            ->findFile($api, $logical_base_path, substr($logical_path, $pos + 1), $dir)) {
            return TRUE;
          }
        }
      }
    }

    // Continue with parent fragment.
    if (FALSE === $pos) {
      return NULL;
    }
    $pos = strrpos($logical_base_path, '/', -2);
  }
  return NULL;
}