-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautoloader.php
103 lines (90 loc) · 2.09 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
<?php
namespace Blaze;
/**
* Blaze class autoloader
*
* @copyright Copyright (c) 2016-2017, Kirill Latyshev
* @author Kirill Latyshev <kirlat@yula.media>
*/
class Autoloader
{
protected static $instance;
private $blazePrefix;
private $baseDir;
/**
* Autoloader constructor.
*
* @param string $baseDir Blaze Core base directory (default: dirname(__FILE__))
*/
private function __construct($baseDir = null)
{
$this->blazePrefix = 'Blaze\\';
if ($baseDir === null) {
$baseDir = dirname(__FILE__);
}
// realpath doesn't always work, for example, with stream URIs
$realDir = realpath($baseDir);
if (is_dir($realDir)) {
$this->baseDir = $realDir;
} else {
$this->baseDir = $baseDir;
}
}
/**
* Private clone method to prevent cloning of the instance of the
* WpSite instance.
*
* @return void
*/
private function __clone()
{
}
/**
* Private unserialize method to prevent unserializing of the WpSite
* instance.
*
* @return void
*/
private function __wakeup()
{
}
/**
* Register a new instance as an SPL autoloader.
*
* @param string $baseDir Blaze Core base directory (default: dirname(__FILE__))
*
* @return Autoloader Registered Autoloader instance
*/
public static function register($baseDir = null)
{
if (is_null(self::$instance)) {
self::$instance = new self($baseDir);
spl_autoload_register(array(self::$instance, 'autoload'));
}
return self::$instance;
}
/**
* Autoload Blaze Core classes.
*
* @param string $class
*/
public function autoload($class)
{
if ($class[0] === '\\') {
$class = substr($class, 1);
}
// Handle only Blaze subnamespaces
if (strpos($class, $this->blazePrefix) !== 0) {
return;
}
$name = substr($class, strlen($this->blazePrefix));
$name = explode('\\', str_replace('_', '-', strtolower($name)));
$name[sizeof($name) - 1] = 'class-' . $name[sizeof($name)-1];
$name = implode(DIRECTORY_SEPARATOR, $name);
$file = sprintf('%s' . DIRECTORY_SEPARATOR .'%s.php', $this->baseDir, $name);
if (is_file($file)) {
require $file;
}
}
}
Autoloader::register();