-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLoader.php
More file actions
172 lines (155 loc) · 4.26 KB
/
Loader.php
File metadata and controls
172 lines (155 loc) · 4.26 KB
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
<?php
namespace Broneq\Autoloader;
/**
* Class Loader
*/
class Loader
{
const NAMESPACE_SEPARATOR = '\\';
private $registered = false;
private $namespaces = [];
private $classes = [];
private $files = [];
/**
* Registers Namespace pointing to directory
* @param string $namespace
* @param string $path
* @return self
*/
public function registerNamespace($namespace, $path)
{
$this->namespaces[$this->fixClassName($namespace)] = $path;
return $this;
}
/**
* Register Class pointing to single file
* @param string $classNamespace namespace of class
* @param string $fileName filename of class
* @return self
*/
public function registerClass($classNamespace, $fileName)
{
$this->classes[$this->fixClassName($classNamespace)] = $fileName;
return $this;
}
/**
* Register filename - it's working like include
* @param string $fileName
* @return self
*/
public function registerFile($fileName)
{
$this->files[] = $fileName;
return $this;
}
/**
* Load class by namespace
* @param string $className
* @return bool
*/
public function load($className)
{
$fixedClassName = $this->fixClassName($className);
return $this->processClasses($fixedClassName) || $this->processNamespaces($fixedClassName);
}
/**
* Register autoloader definitions
* @param bool $prepend If true, spl_autoload_register() will prepend the autoloader on the autoload stack instead of
* appending it.
* @return bool
* @throws \Exception
*/
public function register($prepend = true)
{
if (!$this->registered) {
$this->processFiles();
spl_autoload_register([$this, "load"], true, $prepend);
$this->registered = true;
return true;
}
return false;
}
/**
* Unregister autoloader definitions
* @return bool
*/
public function unregister()
{
if ($this->registered) {
spl_autoload_unregister([$this, "load"]);
$this->registered = false;
return true;
}
return false;
}
/**
* Fix class name separator
* @param string $className
* @return string
*/
private function fixClassName($className)
{
return ltrim($className, self::NAMESPACE_SEPARATOR);
}
/**
* Loads class from namespaces definition if exist
* @param $namespace
* @return bool
*/
private function processNamespaces($namespace)
{
foreach ($this->namespaces as $registeredNamespace => $path) {
if (strpos($namespace, $registeredNamespace) === 0) {
return $this->includeFile($this->namespaceToPath($registeredNamespace, $path, $namespace) . '.php');
}
}
return false;
}
/**
* Converts namespace to path
* @param string $className
* @return string
*/
private function namespaceToPath($registeredNamespace, $registeredDirectory, $namespace)
{
$strippedNs = str_replace($registeredNamespace . self::NAMESPACE_SEPARATOR, '', $namespace);
return rtrim($registeredDirectory, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . str_replace(self::NAMESPACE_SEPARATOR, DIRECTORY_SEPARATOR, $strippedNs);
}
/**
* Loads class from classes definition if exist
* @param string $className
* @return bool
*/
private function processClasses($className)
{
if (array_key_exists($className, $this->classes)) {
return $this->includeFile($this->classes[$className]);
}
return false;
}
/**
* Loads files from definition
* @throws \Exception
*/
private function processFiles()
{
foreach ($this->files as $file) {
if (!$this->includeFile($file)) {
throw new \Exception('Can\'t load file: ' . $file);
}
}
}
/**
* Include file if exists
* @param $file
* @return bool
*/
private function includeFile($file)
{
if (file_exists($file)) {
require_once $file;
return true;
}
return false;
}
}