-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAutoloader.php
150 lines (125 loc) · 4.39 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<?php
class Autoloader
{
protected $appFolder = '';
protected $namespaces = array(
'Oxygen' => __DIR__ . '/Oxygen'
);
/**
* Constructor
*
* @param string $rootDir
* Directory used as root classes folder
*/
public function __construct($rootDir)
{
$this->appFolder = $rootDir;
}
/**
* Main autoloader function
*
* @param string $className
*/
public function autoload($className)
{
$classFile = $this->getClassFileFromClassName($className);
if ($classFile)
require_once $classFile;
}
/**
* Add a new autoloaded class
*
* @param string $type
* Namespace of the class
* @param string $folder
* Where are stored that class type
*/
public function addClassType($type, $folder)
{
$this->namespaces[$type] = $this->appFolder.$folder;
}
/**
* Get the folder (relative to application) of a specified namespace
*
* @param string $type
* @return boolean|string
*/
public function getClassFolder($type)
{
return isset($this->namespaces[$type]) ? $this->namespaces[$type] : false;
}
/**
* Register our autoloader
*/
public function register()
{
spl_autoload_register(array($this, 'autoload'));
}
/**
* Load a controller class from the specified className
*
* @param string $controllerName
*/
public function loadControllerClass($controllerName)
{
$config = Config::getInstance();
$controllersFolder = $config->getOption('router/controllersFolder');
$controllerPrefix = $config->getOption('router/prefix/controller');
$controllerSuffix = $config->getOption('router/suffix/controller');
$filePrefix = $config->getOption('router/prefix/controllerFile');
$fileSuffix = $config->getOption('router/suffix/controllerFile');
$controllerClassFile = ucfirst(Oxygen_Utils::convertSeparatorToUcLetters($controllerName, $filePrefix, $fileSuffix));
$classFile = $this->appFolder . $controllersFolder . DIRECTORY_SEPARATOR . $controllerClassFile . '.php';
if (!empty ($classFile) && file_exists($classFile))
require_once $classFile;
}
/**
* Get the class file from a class name
* 'Namespace_Class_Name' to 'Namespace/ClassName'
*
* Dry run will output all file proposals (without extension)
* e.g. 'Namespace_Class_Name' to ['Namespace/Class_Name.php', 'Namespace/Class/Name']
*
* @param string $className
* The class to load
* @param bool $dryRun
* if true, $result will contain all proposals
* @return boolean|array
*/
public function getClassFileFromClassName($className, $dryRun = false)
{
$classFile = false;
$classFileList = [];
$config = Config::getInstance();
foreach ($this->namespaces as $namespace => $namespaceFolder)
{
if (substr($className, 0, strlen($namespace)) === $namespace)
{
$classFileList = [];
// Remove the namespace and starting underscore in class name
$classNameWithoutNamespace = substr($className, strlen($namespace));
$classNameWithoutNamespace = $classNameWithoutNamespace[0] == '_'
? substr($classNameWithoutNamespace, 1)
: $classNameWithoutNamespace;
do
{
$pos = strpos($classNameWithoutNamespace, '_');
// Try the to load class file at the namespace root folder
$classFile = $namespaceFolder . DIRECTORY_SEPARATOR . $classNameWithoutNamespace;
if (!$dryRun && file_exists($classFile.'.php'))
{
$classFile .= '.php';
break 2;
}
elseif ($dryRun)
$classFileList[] = $classFile;
if ($pos != false)
$classNameWithoutNamespace = substr_replace($classNameWithoutNamespace, DIRECTORY_SEPARATOR, $pos, 1);
} while ($pos !== false);
}
// This is not the wanted namespace, continue...
$classFile = false;
}
return !$dryRun ? $classFile : $classFileList;
}
}