Daan is sweet to Kelley and delegates himself TestCMS code reflection
[ssproject1617.git] / testcms-final-anon / system / classes / autoload.php
1 <?php defined('IN_CMS') or die('No direct access allowed.');
2
3 class Autoloader {
4
5 private static $mappings = array();
6 private static $directories = array();
7
8 public static function register() {
9 spl_autoload_register(array('Autoloader', 'load'));
10 }
11
12 public static function unregister() {
13 spl_autoload_unregister(array('Autoloader', 'load'));
14 }
15
16 public static function map($map) {
17 static::$mappings = array_merge(static::$mappings, $map);
18 }
19
20 public static function directory($dir) {
21 static::$directories = array_merge(static::$directories, $dir);
22 }
23
24 public static function load($class) {
25 // does the class have a direct map
26 if(isset(static::$mappings[$class])) {
27 // load class
28 require static::$mappings[$class];
29
30 return true;
31 }
32
33 // search directories
34 $file = str_replace(array('//', '\\'), '/', trim(strtolower($class), '/'));
35
36 // get file path
37 if(($path = static::find($file)) === false) {
38 return false;
39 }
40
41 require $path;
42
43 return true;
44 }
45
46 public static function find($file) {
47 // search controllers
48 if(strpos($file, '_controller') !== false) {
49 $file = rtrim($file, '_controller');
50 $path = PATH . 'system/admin/controllers/';
51
52 if(file_exists($path . $file . '.php')) {
53 return $path . $file . '.php';
54 }
55 }
56
57 // search application classes
58 foreach(static::$directories as $path) {
59 if(file_exists($path . $file . '.php')) {
60 return $path . $file . '.php';
61 }
62 }
63
64 return false;
65 }
66
67 }