summaryrefslogtreecommitdiffstats
path: root/lib/ClassFinder/NamespaceOrPrefix.php
blob: 08eedadd64aa0ab9aac87904e127fa27ba57a64a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<?php


class xautoload_ClassFinder_NamespaceOrPrefix extends xautoload_ClassFinder_Prefix {

  protected $namespaceMap;

  function __construct() {
    parent::__construct();
    $this->namespaceMap = new xautoload_ClassFinder_Helper_RecursiveMapEvaluator();
  }

  function registerNamespaceRoot($namespace, $root_path, $lazy_check = TRUE) {
    $subdir = str_replace('\\', DIRECTORY_SEPARATOR, $namespace);
    $deep_path
      = !strlen($root_path)
      ? $subdir
      : !strlen($subdir)
      ? $root_path
      : ($root_path . DIRECTORY_SEPARATOR . $subdir)
    ;
    $this->registerNamespaceDeepLocation($namespace, $deep_path, $lazy_check);
  }

  function registerNamespaceDeep($namespace, $path, $lazy_check = TRUE) {
    $this->registerNamespaceDeepLocation($namespace, $path, $lazy_check);
  }

  /**
   * Register a filesystem location for a given namespace.
   */
  function registerNamespaceDeepLocation($namespace, $path, $lazy_check = TRUE) {
    $path_prefix_symbolic = str_replace('\\', DIRECTORY_SEPARATOR, $namespace . '\\');
    $this->namespaceMap->registerDeepPath($path_prefix_symbolic, $path . '/', $lazy_check);
  }

  function registerNamespaceHandler($namespace, $handler) {
    $path_prefix_symbolic =
      strlen($namespace)
      ? str_replace('\\', DIRECTORY_SEPARATOR, $namespace . '\\')
      : ''
    ;
    $this->namespaceMap->registerNamespaceHandler($path_prefix_symbolic, $handler);
  }

  /**
   * Finds the path to the file where the class is defined.
   *
   * @param string $class
   *   The name of the class
   * @return string|null
   *   The path, if found
   */
  function findFile($api, $class) {

    if ('\\' == $class[0]) {
      $class = substr($class, 1);
    }

    if (false !== $pos = strrpos($class, '\\')) {
      // namespaced class name
      if ($class{$pos + 1} === '_') return;
      $path_prefix_symbolic = str_replace('\\', DIRECTORY_SEPARATOR, substr($class, 0, $pos + 1));
      $path_suffix = str_replace('_', DIRECTORY_SEPARATOR, substr($class, $pos + 1)) . '.php';
      return $this->namespaceMap->findFile_rec($api, $path_prefix_symbolic, $path_suffix);
    }
    else {
      // class name with prefix
      return parent::findFile($api, $class);
    }
  }
}