This repository was archived by the owner on Jan 24, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathLowerCasePSR4.php
More file actions
105 lines (104 loc) · 2.73 KB
/
LowerCasePSR4.php
File metadata and controls
105 lines (104 loc) · 2.73 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
<?php
namespace RedCat\Autoload;
class LowerCasePSR4{
protected $namespaces = [];
protected $checked = [];
protected $useCache = true;
protected $useIncludePath = false;
private static $instance;
static function getInstance(){
if(!isset(self::$instance))
self::$instance = new self;
return self::$instance;
}
function addNamespaces($a){
foreach($a as $prefix=>$base_dir){
$this->addNamespace($prefix,$base_dir);
}
return $this;
}
function addNamespace($prefix, $base_dir, $prepend = false){
if(is_array($base_dir)){
foreach($base_dir as $dir){
$this->addNamespace($prefix, $dir, $prepend);
}
}
else{
$prefix = trim($prefix, '\\').'\\';
$base_dir = rtrim($base_dir, '/').'/';
if(!isset($this->namespaces[$prefix]))
$this->namespaces[$prefix] = [];
if ($prepend)
array_unshift($this->namespaces[$prefix], $base_dir);
else
array_push($this->namespaces[$prefix], $base_dir);
}
return $this;
}
function useCache($b=true){
$this->useCache = $b;
}
function useIncludePath($b=true){
$this->useIncludePath = $b;
}
protected function loadFile($file,$class){
if(file_exists($file)
||($this->useIncludePath&&($file=stream_resolve_include_path($file))))
{
requireFile($file);
if(!class_exists($class,false)&&!interface_exists($class,false)&&!trait_exists($class,false))
throw new \Exception('Class "'.$class.'" not found as expected in "'.$file.'"');
if($this->useCache)
$this->checked[] = $class;
return true;
}
return false;
}
protected function findRelative($class,$relative_class,$prefix){
if(isset($this->namespaces[$prefix])){
foreach($this->namespaces[$prefix] as $base_dir){
$file = $base_dir.static::snake(str_replace('\\', '/', $relative_class)).'.php';
if($this->loadFile($file,$class))
return true;
}
}
}
function findClass($class){
$prefix = $class;
while($prefix!='\\'){
$prefix = rtrim($prefix, '\\');
$pos = strrpos($prefix, '\\');
if($pos!==false){
$prefix = substr($class, 0, $pos + 1);
$relative_class = substr($class, $pos + 1);
}
else{
$prefix = '\\';
$relative_class = $class;
}
if($this->findRelative($class,$relative_class,$prefix))
return true;
}
}
function classLoad($class){
if($this->useCache&&in_array($class,$this->checked))
return;
if($this->findClass($class))
return;
}
function __invoke($class){
return $this->classLoad($class);
}
function splRegister(){
spl_autoload_register([$this,'classLoad']);
}
function splUnregister(){
spl_autoload_unregister([$this,'classLoad']);
}
static function snake($str){
return str_replace(' ', '-', strtolower(preg_replace('/([a-z])([A-Z])/', '$1 $2', $str)));
}
}
function requireFile(){
require func_get_arg(0);
}