You are here

function xautoload_ClassFinder_Helper_Map::findFile_map in X Autoload 7.3

Find the file for a class that in PSR-0 or PEAR would be in $psr_0_root . '/' . $path_fragment . $path_suffix

Parameters

xautoload_InjectedAPI_findFile $api:

string $logical_base_path: Longest possible logical base path for the given class. Includes a trailing directory separator.

string $relative_path: Remaining part of the logical path, following the $logical_base_path. Ending with '.php'.

Return value

bool|NULL TRUE, if the class was found.

File

lib/ClassFinder/Helper/Map.php, line 160

Class

xautoload_ClassFinder_Helper_Map
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).

Code

function findFile_map($api, $logical_base_path, $relative_path) {
  $path = $logical_base_path . $relative_path;
  while (TRUE) {
    if (isset($this->paths[$logical_base_path])) {
      $lazy_remove = FALSE;
      foreach ($this->paths[$logical_base_path] as $dir => &$lazy_check) {
        $file = $dir . $relative_path;
        if ($api
          ->suggestFile($file)) {

          // Next time we can skip the check, because now we know that the
          // directory exists.
          $lazy_check = FALSE;
          return TRUE;
        }

        // Now we know the file does not exist. Does the directory?
        if ($lazy_check) {

          // Lazy-check whether the registered directory exists.
          if ($api
            ->is_dir($dir)) {

            // Next time we can skip the check, because now we know that the
            // directory exists.
            $lazy_check = FALSE;
          }
          else {

            // The registered directory does not exist, so we can unregister it.
            unset($this->paths[$logical_base_path][$dir]);
            $lazy_remove = TRUE;
            if (is_object($lazy_check)) {

              /**
               * @var xautoload_MissingDirPlugin_Interface $lazy_check
               */
              $new_dir = $lazy_check
                ->alternativeDir($logical_base_path);
              if ($new_dir !== $dir) {
                $file = $new_dir . $relative_path;
                if ($api
                  ->suggestFile($file)) {
                  $this->paths[$logical_base_path][$new_dir] = FALSE;
                  return TRUE;
                }
                elseif ($api
                  ->is_dir($new_dir)) {
                  $this->paths[$logical_base_path][$new_dir] = FALSE;
                }
              }
            }
          }
        }
      }
      if ($lazy_remove && empty($this->paths[$logical_base_path])) {
        unset($this->paths[$logical_base_path]);
      }
    }

    // Check any plugin registered for this fragment.
    if (isset($this->plugins[$logical_base_path])) {

      /**
       * @var xautoload_FinderPlugin_Interface $plugin
       */
      foreach ($this->plugins[$logical_base_path] as $id => $plugin) {
        if ($plugin
          ->findFile($api, $logical_base_path, $relative_path, $id)) {
          return TRUE;
        }
      }
    }

    // Continue with parent fragment.
    if ('' === $logical_base_path) {
      break;
    }
    elseif (DIRECTORY_SEPARATOR === $logical_base_path) {

      // This happens if a class begins with an underscore.
      $logical_base_path = '';
      $relative_path = $path;
    }
    elseif (FALSE !== ($pos = strrpos($logical_base_path, DIRECTORY_SEPARATOR, -2))) {
      $logical_base_path = substr($logical_base_path, 0, $pos + 1);
      $relative_path = substr($path, $pos + 1);
    }
    else {
      $logical_base_path = '';
      $relative_path = $path;
    }
  }
}