Some documentation improvements
[project/xautoload.git] / lib / InjectedAPI / hookXautoload.php
1 <?php
2
3
4 /**
5 * An instance of this class is passed around to implementations of
6 * hook_xautoload().
7 */
8 class xautoload_InjectedAPI_hookXautoload {
9
10 protected $finder;
11 protected $extensionDir;
12
13 /**
14 * @param xautoload_ClassFinder $finder
15 * The class finder object.
16 */
17 function __construct($finder) {
18 $this->finder = $finder;
19 }
20
21 /**
22 * Register an additional namespace for this module.
23 * Note: Drupal\<module name>\ is already registered for <module dir>/lib.
24 *
25 * @param string $namespace
26 * The namespace
27 * @param string $psr_0_root_dir
28 * PSR-0 root dir.
29 * If $relative is TRUE, this is relative to the current module dir.
30 * If $relative is FALSE, this is an absolute path.
31 * @param boolean $relative
32 * Whether or not the path is relative to the current extension dir.
33 */
34 function namespaceRoot($namespace, $psr_0_root_dir = NULL, $relative = TRUE) {
35 $psr_0_root_dir = $this->processDir($psr_0_root_dir, $relative);
36 $this->finder->registerNamespaceRoot($namespace, $psr_0_root_dir);
37 }
38
39 /**
40 * Register an additional namespace for this module.
41 * Note: Drupal\<module name>\ is already registered for <module dir>/lib.
42 *
43 * @param string $namespace
44 * The namespace
45 * @param string $prefix_root_dir
46 * Prefix root dir.
47 * If $relative is TRUE, this is relative to the extension module dir.
48 * If $relative is FALSE, this is an absolute path.
49 * @param boolean $relative
50 * Whether or not the path is relative to the current extension dir.
51 */
52 function prefixRoot($prefix, $prefix_root_dir = NULL, $relative = TRUE) {
53 $prefix_root_dir = $this->processDir($prefix_root_dir, $relative);
54 $this->finder->registerPrefixRoot($prefix, $prefix_root_dir);
55 }
56
57 /**
58 * Register an additional namespace for this module.
59 * Note: Drupal\<module name>\ is already registered for <module dir>/lib.
60 *
61 * @param string $namespace
62 * The namespace
63 * @param string $psr_0_root_dir
64 * PSR-0 root dir.
65 * If $relative is TRUE, this is relative to the current extension dir.
66 * If $relative is FALSE, this is an absolute path.
67 * @param boolean $relative
68 * Whether or not the path is relative to the current extension dir.
69 */
70 function namespaceDeep($namespace, $namespace_deep_dir = NULL, $relative = TRUE) {
71 $namespace_deep_dir = $this->processDir($namespace_deep_dir, $relative);
72 $this->finder->registerNamespaceDeep($namespace, $namespace_deep_dir);
73 }
74
75 /**
76 * Register an additional namespace for this module.
77 * Note: Drupal\<module name>\ is already registered for <module dir>/lib.
78 *
79 * @param string $namespace
80 * The namespace
81 * @param string $prefix_deep_dir
82 * PSR-0 root dir.
83 * If $relative is TRUE, this is relative to the current extension dir.
84 * If $relative is FALSE, this is an absolute path.
85 * @param boolean $relative
86 * Whether or not the path is relative to the current extension dir.
87 */
88 function prefixDeep($prefix, $prefix_deep_dir = NULL, $relative = TRUE) {
89 $prefix_root_dir = $this->processDir($prefix_deep_dir, $relative);
90 $this->finder->registerPrefixDeep($prefix, $prefix_deep_dir);
91 }
92
93 /**
94 * Legacy: Plugins were called "Handler" before.
95 */
96 function namespaceHandler($namespace, $plugin) {
97 $this->finder->registerNamespacePlugin($namespace, $plugin);
98 }
99
100 /**
101 * Legacy: Plugins were called "Handler" before.
102 */
103 function prefixHandler($prefix, $plugin) {
104 $this->finder->registerPrefixPlugin($prefix, $plugin);
105 }
106
107 /**
108 * Register a namespace plugin object
109 */
110 function namespacePlugin($namespace, $plugin) {
111 $this->finder->registerNamespacePlugin($namespace, $plugin);
112 }
113
114 /**
115 * Register a prefix plugin object
116 */
117 function prefixPlugin($prefix, $plugin) {
118 $this->finder->registerPrefixPlugin($prefix, $plugin);
119 }
120
121 /**
122 * Process a given directory to make it relative to Drupal root,
123 * instead of relative to the current extension dir.
124 *
125 * @param string $dir
126 * The directory path that we want to make absolute.
127 * @param boolean $relative
128 * If TRUE, the $dir will be transformed from relative to absolute.
129 * If FALSE, the $dir is assumed to already be absolute, and remain unchanged.
130 *
131 * @return string
132 * The modified (absolute) directory path.
133 */
134 protected function processDir($dir, $relative) {
135 if (!isset($dir)) {
136 $dir = $this->extensionDir . '/lib';
137 }
138 elseif ($relative) {
139 // Root dir is relative to module root.
140 if (empty($dir)) {
141 $dir = $this->extensionDir;
142 }
143 else {
144 $dir = $this->extensionDir . '/' . $dir;
145 }
146 }
147 else {
148 // Leave the $dir as it is.
149 }
150 return $dir;
151 }
152
153 /**
154 * Set a module to use as base for relative paths.
155 * This is typically called before each invocation of hook_xautoload() on a
156 * module.
157 * It can also be called by a module or theme that implements hook_xautoload(),
158 * to register class loading information on behalf of another module.
159 *
160 * @param string $module
161 * Machine name of the module.
162 */
163 function setModule($module) {
164 $this->extensionDir = drupal_get_path('module', $module);
165 }
166
167 /**
168 * Set a theme to use as base for relative paths.
169 * This is typically called before each invocation of hook_xautoload() on a
170 * theme.
171 * It can also be called by a module or theme that implements hook_xautoload(),
172 * to register class loading information on behalf of another theme.
173 *
174 * @param string $theme
175 * Machine name of the theme.
176 */
177 function setTheme($theme) {
178 $this->extensionDir = drupal_get_path('theme', $theme);
179 }
180
181 /**
182 * Set a library to use as base for relative paths.
183 * This is called e.g. in libraries_xautoload(), before passing the $api
184 * object to library xautoload callbacks.
185 *
186 * @param string $library
187 * Machine name of the library.
188 */
189 function setLibrary($library) {
190 if (!module_exists('libraries')) {
191 throw new Exception('Libraries module not installed.');
192 }
193 $this->extensionDir = libraries_get_path($library);
194 }
195
196 /**
197 * Explicitly set the base for relative paths.
198 *
199 * @param string $dir
200 * New relative base path.
201 */
202 function setExtensionDir($dir) {
203 $this->extensionDir = $dir;
204 }
205 }