re-add legacy support for namespaceHandler() and prefixHandler() as alias to namespac...
[project/xautoload.git] / lib / InjectedAPI / hookXautoload.php
1 <?php
2
3
4 class xautoload_InjectedAPI_hookXautoload {
5
6 protected $finder;
7 protected $extensionDir;
8
9 /**
10 * @param xautoload_ClassFinder $finder
11 * The class finder.
12 */
13 function __construct($finder) {
14 $this->finder = $finder;
15 }
16
17 /**
18 * Register an additional namespace for this module.
19 * Note: Drupal\<module name>\ is already registered for <module dir>/lib.
20 *
21 * @param string $namespace
22 * The namespace
23 * @param string $psr_0_root_dir
24 * PSR-0 root dir.
25 * If $relative is TRUE, this is relative to the current module dir.
26 * If $relative is FALSE, this is an absolute path.
27 * @param boolean $relative
28 * Whether or not the path is relative to the current extension dir.
29 */
30 function namespaceRoot($namespace, $psr_0_root_dir = NULL, $relative = TRUE) {
31 $psr_0_root_dir = $this->processDir($psr_0_root_dir, $relative);
32 $this->finder->registerNamespaceRoot($namespace, $psr_0_root_dir);
33 }
34
35 /**
36 * Register an additional namespace for this module.
37 * Note: Drupal\<module name>\ is already registered for <module dir>/lib.
38 *
39 * @param string $namespace
40 * The namespace
41 * @param string $prefix_root_dir
42 * Prefix root dir.
43 * If $relative is TRUE, this is relative to the extension module dir.
44 * If $relative is FALSE, this is an absolute path.
45 * @param boolean $relative
46 * Whether or not the path is relative to the current extension dir.
47 */
48 function prefixRoot($prefix, $prefix_root_dir = NULL, $relative = TRUE) {
49 $prefix_root_dir = $this->processDir($prefix_root_dir, $relative);
50 $this->finder->registerPrefixRoot($prefix, $prefix_root_dir);
51 }
52
53 /**
54 * Register an additional namespace for this module.
55 * Note: Drupal\<module name>\ is already registered for <module dir>/lib.
56 *
57 * @param string $namespace
58 * The namespace
59 * @param string $psr_0_root_dir
60 * PSR-0 root dir.
61 * If $relative is TRUE, this is relative to the current extension dir.
62 * If $relative is FALSE, this is an absolute path.
63 * @param boolean $relative
64 * Whether or not the path is relative to the current extension dir.
65 */
66 function namespaceDeep($namespace, $namespace_deep_dir = NULL, $relative = TRUE) {
67 $namespace_deep_dir = $this->processDir($namespace_deep_dir, $relative);
68 $this->finder->registerNamespaceDeep($namespace, $namespace_deep_dir);
69 }
70
71 /**
72 * Register an additional namespace for this module.
73 * Note: Drupal\<module name>\ is already registered for <module dir>/lib.
74 *
75 * @param string $namespace
76 * The namespace
77 * @param string $prefix_deep_dir
78 * PSR-0 root dir.
79 * If $relative is TRUE, this is relative to the current extension dir.
80 * If $relative is FALSE, this is an absolute path.
81 * @param boolean $relative
82 * Whether or not the path is relative to the current extension dir.
83 */
84 function prefixDeep($prefix, $prefix_deep_dir = NULL, $relative = TRUE) {
85 $prefix_root_dir = $this->processDir($prefix_deep_dir, $relative);
86 $this->finder->registerPrefixDeep($prefix, $prefix_deep_dir);
87 }
88
89 /**
90 * Legacy: Plugins were called "Handler" before.
91 */
92 function namespaceHandler($namespace, $plugin) {
93 $this->finder->registerNamespacePlugin($namespace, $plugin);
94 }
95
96 /**
97 * Legacy: Plugins were called "Handler" before.
98 */
99 function prefixHandler($prefix, $plugin) {
100 $this->finder->registerPrefixPlugin($prefix, $plugin);
101 }
102
103 /**
104 * Register a namespace plugin object
105 */
106 function namespacePlugin($namespace, $plugin) {
107 $this->finder->registerNamespacePlugin($namespace, $plugin);
108 }
109
110 /**
111 * Register a prefix plugin object
112 */
113 function prefixPlugin($prefix, $plugin) {
114 $this->finder->registerPrefixPlugin($prefix, $plugin);
115 }
116
117 /**
118 * Process a given directory to make it relative to Drupal root,
119 * instead of relative to the current extension dir.
120 */
121 protected function processDir($dir, $relative) {
122 if (!isset($dir)) {
123 $dir = $this->extensionDir . '/lib';
124 }
125 elseif ($relative) {
126 // Root dir is relative to module root.
127 if (empty($dir)) {
128 $dir = $this->extensionDir;
129 }
130 else {
131 $dir = $this->extensionDir . '/' . $dir;
132 }
133 }
134 else {
135 // Leave the $dir as it is.
136 }
137 return $dir;
138 }
139
140 /**
141 * Set a module to use as base for relative paths.
142 */
143 function setModule($module) {
144 $this->extensionDir = drupal_get_path('module', $module);
145 }
146
147 /**
148 * Set a theme to use as base for relative paths.
149 */
150 function setTheme($theme) {
151 $this->extensionDir = drupal_get_path('theme', $theme);
152 }
153
154 /**
155 * Set a library to use as base for relative paths.
156 */
157 function setLibrary($library) {
158 if (!module_exists('libraries')) {
159 throw new Exception('Libraries module not installed.');
160 }
161 $this->extensionDir = libraries_get_path($library);
162 }
163
164 /**
165 * Explicitly set the base for relative paths.
166 */
167 function setExtensionDir($dir) {
168 $this->extensionDir = $dir;
169 }
170 }