-
Notifications
You must be signed in to change notification settings - Fork 16
/
autoloader.php
75 lines (60 loc) · 1.84 KB
/
autoloader.php
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
73
74
75
<?php
call_user_func(function () {
$root = dirname(__FILE__);
/**
* Set include paths for all modules that we have
*/
$include_paths = glob($root . '/modules/*', GLOB_ONLYDIR);
$include_paths[] = $root . '/application';
$include_paths[] = $root . '/system';
/**
* This function locates classes as Kohanas find_file does it
* but can ignore caching since a class will never be located
* twice once included, and ignore config files and more since
* this can only load classes.
*/
$locate = function ($directory, $filename) use ($include_paths) {
$file = false;
$classpath = $directory.'/'.$filename.'.php';
foreach ($include_paths as $path) {
if (is_file($path.'/'.$classpath)) {
$file = $path.'/'.$classpath;
break;
}
}
return $file;
};
/**
* This replicates the behaviour that was previously in the Kohana::auto_load
* but does not utilize global constants.
*/
$autoloader = function ($class) use ($locate) {
$suffix = (($suffix = strrpos($class, '_')) > 0) ? substr($class, $suffix + 1) : false;
if ($suffix === 'Controller') {
$type = 'controllers';
$file = strtolower(substr($class, 0, -11));
} elseif ($suffix === 'Model') {
$type = 'models';
$file = strtolower(substr($class, 0, -6));
} elseif ($suffix === 'Driver') {
$type = 'libraries/drivers';
$file = str_replace('_', '/', substr($class, 0, -7));
} elseif ($suffix === 'Widget') {
$type = 'widgets';
$classname = substr($class, 0, -7);
$file = $classname . '/' . $classname;
} else {
$type = ($class[0] < 'a') ? 'libraries' : 'helpers';
$file = $class;
}
/**
* If we find the file we require it and "hopefully" the class should
* now be availables.
*/
if ($filename = $locate($type, $file)) {
require $filename;
return true;
} else return false;
};
spl_autoload_register($autoloader);
});