You are here

class xautoload_ClassFinder_NamespaceOrPrefix in X Autoload 7.3

Same name and namespace in other branches
  1. 7.2 lib/ClassFinder/NamespaceOrPrefix.php \xautoload_ClassFinder_NamespaceOrPrefix

Hierarchy

Expanded class hierarchy of xautoload_ClassFinder_NamespaceOrPrefix

File

lib/ClassFinder/NamespaceOrPrefix.php, line 4

View source
class xautoload_ClassFinder_NamespaceOrPrefix extends xautoload_ClassFinder_Prefix {
  protected $namespaceMap;
  function __construct() {
    parent::__construct();
    $this->namespaceMap = new xautoload_ClassFinder_Helper_Map();
  }

  /**
   * Register a PSR-0 root folder for a given namespace.
   *
   * @param string $namespace
   *   The namespace, e.g. "My\Namespace", to cover all classes within that,
   *   e.g. My\Namespace\SomeClass, or My\Namespace\Xyz\SomeClass. This does not
   *   cover the root-level class, e.g. My\Namespace
   * @param string $path
   *   The deep path, e.g. "../lib", if classes reside in e.g.
   *   My\Namespace\SomeClass -> ../lib/My/Namespace/SomeClass.php
   * @param boolean $lazy_check
   *   If TRUE, then we are not sure if the directory at $path actually exists.
   *   If during the process we find the directory to be nonexistent, we
   *   unregister the path.
   */
  function registerNamespaceRoot($namespace, $path, $lazy_check = TRUE) {
    $namespace_path_fragment = $this
      ->namespacePathFragment($namespace);
    $deep_path = strlen($path) ? $path . DIRECTORY_SEPARATOR : '';
    $deep_path .= $namespace_path_fragment;
    $this->namespaceMap
      ->registerDeepPath($namespace_path_fragment, $deep_path, $lazy_check);
  }

  /**
   * Register PSR-0 root folders for given namespaces.
   *
   * @param array $map
   *   Associative array, the keys are the namespaces, the values are the
   *   directories.
   * @param boolean $lazy_check
   *   If TRUE, then we are not sure if the directory at $path actually exists.
   *   If during the process we find the directory to be nonexistent, we
   *   unregister the path.
   */
  function registerNamespacesRoot($map, $lazy_check = TRUE) {
    $deep_map = array();
    foreach ($map as $namespace => $path) {
      $namespace_path_fragment = $this
        ->namespacePathFragment($namespace);
      $deep_path = strlen($path) ? $path . DIRECTORY_SEPARATOR : '';
      $deep_path .= $namespace_path_fragment;
      $deep_map[$namespace_path_fragment][$deep_path] = $lazy_check;
    }
    $this->namespaceMap
      ->registerDeepPaths($deep_map);
  }

  /**
   * Alias for registerNamespaceDeepLocation()
   *
   * @param string $namespace
   *   The namespace, e.g. "My\Namespace"
   * @param string $path
   *   The deep path, e.g. "../lib/My/Namespace"
   * @param boolean $lazy_check
   *   If TRUE, then we are not sure if the directory at $path actually exists.
   *   If during the process we find the directory to be nonexistent, we
   *   unregister the path.
   */
  function registerNamespaceDeep($namespace, $path, $lazy_check = TRUE) {
    strlen($namespace);
    $namespace_path_fragment = $this
      ->namespacePathFragment($namespace);
    $deep_path = strlen($path) ? $path . DIRECTORY_SEPARATOR : '';
    $this->namespaceMap
      ->registerDeepPath($namespace_path_fragment, $deep_path, $lazy_check);
  }

  /**
   * Register a number of "deep" namespace directories at once.
   *
   * @param array $map
   * @param bool|xautoload_MissingDirPlugin_Interface $lazy_check
   */
  function registerNamespacesDeep($map, $lazy_check = TRUE) {
    $deep_map = array();
    foreach ($map as $namespace => $path) {
      $namespace_path_fragment = $this
        ->namespacePathFragment($namespace);
      $deep_path = strlen($path) ? $path . DIRECTORY_SEPARATOR : '';
      $deep_map[$namespace_path_fragment][$deep_path] = $lazy_check;
    }
    $this->namespaceMap
      ->registerDeepPaths($deep_map);
  }

  /**
   * Register a deep filesystem location for a given namespace.
   *
   * @param string $namespace
   *   The namespace, e.g. "My\Namespace"
   * @param string $path
   *   The deep path, e.g. "../lib/My/Namespace"
   * @param boolean $lazy_check
   *   If TRUE, then we are not sure if the directory at $path actually exists.
   *   If during the process we find the directory to be nonexistent, we
   *   unregister the path.
   */
  function registerNamespaceDeepLocation($namespace, $path, $lazy_check = TRUE) {
    $namespace_path_fragment = $this
      ->namespacePathFragment($namespace);
    $deep_path = strlen($path) ? $path . DIRECTORY_SEPARATOR : '';
    $this->namespaceMap
      ->registerDeepPath($namespace_path_fragment, $deep_path, $lazy_check);
  }

  /**
   * Legacy: Plugins were called Handlers before.
   */
  function registerNamespaceHandler($prefix, $plugin) {
    $this
      ->registerNamespacePlugin($prefix, $plugin);
  }

  /**
   * Register a plugin for a namespace.
   *
   * @param string $namespace
   *   The namespace, e.g. "My\Library"
   * @param xautoload_FinderPlugin_Interface $plugin
   *   The plugin.
   * @param string $base_dir
   *   Optional base path.
   */
  function registerNamespacePlugin($namespace, $plugin, $base_dir = NULL) {
    $namespace_path_fragment = $this
      ->namespacePathFragment($namespace);
    $this->namespaceMap
      ->registerPlugin($namespace_path_fragment, $plugin, $base_dir);
  }

  /**
   * Finds the path to the file where the class is defined.
   *
   * @param 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.
   * @param string $class
   *   The name of the class, with all namespaces prepended.
   *   E.g. Some\Namespace\Some\Class
   *
   * @return 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.
   */
  function findFile($api, $class) {
    if ('\\' === $class[0]) {
      $class = substr($class, 1);
    }

    // First check if the literal class name is registered.
    if (!empty($this->classes[$class])) {
      foreach ($this->classes[$class] as $filepath => $true) {
        if ($api
          ->suggestFile($filepath)) {
          return TRUE;
        }
      }
    }
    if (FALSE !== ($pos = strrpos($class, '\\'))) {

      // Loop through positions of '\\', backwards.
      $namespace_path_fragment = str_replace('\\', DIRECTORY_SEPARATOR, substr($class, 0, $pos + 1));
      $path_suffix = str_replace('_', DIRECTORY_SEPARATOR, substr($class, $pos + 1)) . '.php';
      if ($this->namespaceMap
        ->findFile_map($api, $namespace_path_fragment, $path_suffix)) {
        return TRUE;
      }
    }
    else {

      // The class is not within a namespace.
      // Fall back to the prefix-based finder.
      $prefix_path_fragment = str_replace('_', DIRECTORY_SEPARATOR, $class) . '.php';
      if ('_' === $class[0]) {
        $prefix_path_fragment[0] = '_';
      }
      if ($this->prefixMap
        ->findFile_map($api, $prefix_path_fragment, '')) {
        return TRUE;
      }
    }
  }

  /**
   * Replace the namespace separator with directory separator.
   *
   * @param string $namespace
   *   Namespace without trailing namespace separator.
   *
   * @return string
   *   Path fragment representing the namespace, with trailing DIRECTORY_SEPARATOR.
   */
  protected function namespacePathFragment($namespace) {
    return strlen($namespace) ? str_replace('\\', DIRECTORY_SEPARATOR, rtrim($namespace, '\\') . '\\') : '';
  }

}

Members

Namesort descending Modifiers Type Description Overrides
xautoload_ClassFinder_NamespaceOrPrefix::$namespaceMap protected property
xautoload_ClassFinder_NamespaceOrPrefix::findFile function Finds the path to the file where the class is defined. Overrides xautoload_ClassFinder_Prefix::findFile
xautoload_ClassFinder_NamespaceOrPrefix::namespacePathFragment protected function Replace the namespace separator with directory separator.
xautoload_ClassFinder_NamespaceOrPrefix::registerNamespaceDeep function Alias for registerNamespaceDeepLocation()
xautoload_ClassFinder_NamespaceOrPrefix::registerNamespaceDeepLocation function Register a deep filesystem location for a given namespace.
xautoload_ClassFinder_NamespaceOrPrefix::registerNamespaceHandler function Legacy: Plugins were called Handlers before.
xautoload_ClassFinder_NamespaceOrPrefix::registerNamespacePlugin function Register a plugin for a namespace.
xautoload_ClassFinder_NamespaceOrPrefix::registerNamespaceRoot function Register a PSR-0 root folder for a given namespace.
xautoload_ClassFinder_NamespaceOrPrefix::registerNamespacesDeep function Register a number of "deep" namespace directories at once.
xautoload_ClassFinder_NamespaceOrPrefix::registerNamespacesRoot function Register PSR-0 root folders for given namespaces.
xautoload_ClassFinder_NamespaceOrPrefix::__construct function Overrides xautoload_ClassFinder_Prefix::__construct
xautoload_ClassFinder_Prefix::$classes protected property
xautoload_ClassFinder_Prefix::$prefixMap protected property
xautoload_ClassFinder_Prefix::prefixPathFragment protected function Convert the underscores of a prefix into directory separators.
xautoload_ClassFinder_Prefix::registerClass function Register a filepath for an individual class.
xautoload_ClassFinder_Prefix::registerClasses function Register an array ("map") of classes to file paths.
xautoload_ClassFinder_Prefix::registerPrefixDeep function Register a PEAR-style deep path for a given class prefix.
xautoload_ClassFinder_Prefix::registerPrefixDeepLocation function Register a filesystem location for a given class prefix.
xautoload_ClassFinder_Prefix::registerPrefixesDeep function Register an array of PEAR-style deep paths for given class prefixes.
xautoload_ClassFinder_Prefix::registerPrefixesRoot function Register an array of PEAR-style deep paths for given class prefixes.
xautoload_ClassFinder_Prefix::registerPrefixHandler function Legacy: Plugins were called Handlers before.
xautoload_ClassFinder_Prefix::registerPrefixPlugin function Register a plugin for a prefix.
xautoload_ClassFinder_Prefix::registerPrefixRoot function Register a PEAR-style root path for a given class prefix.