-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAutoload.php
97 lines (89 loc) · 2.29 KB
/
Autoload.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
<?php
namespace Dahl;
/**
* Autoloader for terminal io lib.
*
* @copyright Copyright (C) 2015 Albert Dahlin
* @author Albert Dahlin <[email protected]>
* @license MIT License <http://opensource.org/licenses/MIT>
*/
class Autoload
{
/**
* Holds basedirs for namespaces.
*
* @var array
* @access protected
*/
static protected $_baseDirs = array();
/**
* Register a namespace base dir for autoloading.
*
* @param string $namespace
* @param string $dir
* @static
* @access public
* @return void
*/
static public function registerBase($namespace, $dir)
{
$nameArr = explode('\\', $namespace);
$currentDir = &self::$_baseDirs;
foreach ($nameArr as $name) {
if (!isset($currentDir[$name])) {
$currentDir[$name] = array();
}
$currentDir = &$currentDir[$name];
}
$currentDir = $dir;
}
/**
* Register spl autoload function.
*
* @static
* @access public
* @return void
*/
static public function register()
{
spl_autoload_register(array('Dahl\\Autoload', 'autoload'), true, true);
}
/**
* Autoload function.
*
* @param string $class
* @static
* @access public
* @return boolean
*/
static public function autoload($class)
{
$classArr = explode('\\', $class);
$currentDir = self::$_baseDirs;
$filename = '';
$isFound = false;
foreach ($classArr as $name) {
if ($isFound) {
$filename .= DIRECTORY_SEPARATOR . $name;
continue;
}
if (isset($currentDir[$name])) {
$currentDir = $currentDir[$name];
} elseif (isset($currentDir['*'])) {
$currentDir = $currentDir['*'];
$filename .= DIRECTORY_SEPARATOR . $name;
}
if (is_string($currentDir)) {
$isFound = true;
}
}
if (is_string($currentDir)) {
$file = $currentDir . $filename . '.php';
include $file;
return true;
}
return false;
}
}
Autoload::registerBase('Dahl\\PhpTerm', dirname(__file__));
Autoload::register();