-
Notifications
You must be signed in to change notification settings - Fork 1
/
xautoload.emulate.inc
71 lines (63 loc) · 1.83 KB
/
xautoload.emulate.inc
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
<?php
/**
* @file xautoload.emulate.inc
*
* This file contains code to copy+paste and adapt, if you want to use PSR-0 or
* xautoload-style autoloading, without making xautoload an explicit dependency.
*/
/**
* Register a dedicated class loader only for this module.
*
* You want to rename this function as something like mymodule_include(),
* and call it any time to initialize xautoloading for a specific module.
*/
function _MYMODULE_include() {
static $first_run = TRUE;
if (!$first_run) {
return;
}
$first_run = FALSE;
if (!module_exists('xautoload')) {
spl_autoload_register('_MYMODULE_autoload');
}
// This could be the place for some procedural includes.
// ..
}
/**
* The autoload callback for this specific module, used if xautoload is not
* present.
*
* You want to replace any "MYMODULE" with your module name.
*
* @param string $class
* The class we want to load.
*/
function _MYMODULE_autoload($class) {
static $lib_dir;
if (!isset($lib_dir)) {
$lib_dir = __DIR__ . '/lib/';
}
// Replace MYMODULE with your module name.
$module = 'MYMODULE';
$l = strlen($module);
if (FALSE !== $nspos = strrpos($class, '\\')) {
// PSR-0 mode. Omit this if you use only the PHP 5.2 compatibility mode.
if ("Drupal\\$module\\" === substr($class, 0, $l + 8)) {
$namespace = substr($class, 0, $nspos);
$classname = substr($class, $nspos + 1);
$path = $lib_dir . str_replace('\\', '/', $namespace) . '/' . str_replace('_', '/', $classname) . '.php';
if (is_file($path)) {
include $path;
}
}
}
else {
// PHP 5.2 compatibility mode. Omit this if you use only PSR-0.
if ($module . '_' === substr($class, 0, $l + 1)) {
$path = $lib_dir . str_replace('_', '/', $class) . '.php';
if (is_file($path)) {
include $path;
}
}
}
}