commit the 7.x-2.x branch
[project/xautoload.git] / lib / ClassFinder / NamespaceOrPrefix.php
1 <?php
2
3
4 class xautoload_ClassFinder_NamespaceOrPrefix extends xautoload_ClassFinder_Prefix {
5
6 protected $namespaceMap;
7
8 function __construct() {
9 parent::__construct();
10 $this->namespaceMap = new xautoload_ClassFinder_Helper_RecursiveMapEvaluator();
11 }
12
13 function registerNamespaceRoot($namespace, $root_path, $lazy_check = TRUE) {
14 $subdir = str_replace('\\', DIRECTORY_SEPARATOR, $namespace);
15 $deep_path
16 = !strlen($root_path)
17 ? $subdir
18 : !strlen($subdir)
19 ? $root_path
20 : ($root_path . DIRECTORY_SEPARATOR . $subdir)
21 ;
22 $this->registerNamespaceDeepLocation($namespace, $deep_path, $lazy_check);
23 }
24
25 function registerNamespaceDeep($namespace, $path, $lazy_check = TRUE) {
26 $this->registerNamespaceDeepLocation($namespace, $path, $lazy_check);
27 }
28
29 /**
30 * Register a filesystem location for a given namespace.
31 */
32 function registerNamespaceDeepLocation($namespace, $path, $lazy_check = TRUE) {
33 $path_prefix_symbolic = str_replace('\\', DIRECTORY_SEPARATOR, $namespace . '\\');
34 $this->namespaceMap->registerDeepPath($path_prefix_symbolic, $path . '/', $lazy_check);
35 }
36
37 function registerNamespaceHandler($namespace, $handler) {
38 $path_prefix_symbolic =
39 strlen($namespace)
40 ? str_replace('\\', DIRECTORY_SEPARATOR, $namespace . '\\')
41 : ''
42 ;
43 $this->namespaceMap->registerNamespaceHandler($path_prefix_symbolic, $handler);
44 }
45
46 /**
47 * Finds the path to the file where the class is defined.
48 *
49 * @param string $class
50 * The name of the class
51 * @return string|null
52 * The path, if found
53 */
54 function findFile($api, $class) {
55
56 if ('\\' == $class[0]) {
57 $class = substr($class, 1);
58 }
59
60 if (false !== $pos = strrpos($class, '\\')) {
61 // namespaced class name
62 if ($class{$pos + 1} === '_') return;
63 $path_prefix_symbolic = str_replace('\\', DIRECTORY_SEPARATOR, substr($class, 0, $pos + 1));
64 $path_suffix = str_replace('_', DIRECTORY_SEPARATOR, substr($class, $pos + 1)) . '.php';
65 return $this->namespaceMap->findFile_rec($api, $path_prefix_symbolic, $path_suffix);
66 }
67 else {
68 // class name with prefix
69 return parent::findFile($api, $class);
70 }
71 }
72 }