forked from PocketMine/PocketMine-SPL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBaseClassLoader.php
173 lines (152 loc) · 3.92 KB
/
BaseClassLoader.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
<?php
/*
* PocketMine Standard PHP Library
* Copyright (C) 2014 PocketMine Team <https://github.com/PocketMine/PocketMine-SPL>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
class BaseClassLoader extends \Threaded implements ClassLoader{
/** @var \ClassLoader */
private $parent;
/** @var string[] */
private $lookup;
/** @var string[] */
private $classes;
/**
* @param ClassLoader $parent
*/
public function __construct(ClassLoader $parent = null){
$this->parent = $parent;
$this->lookup = new \Threaded;
$this->classes = new \Threaded;
}
/**
* Adds a path to the lookup list
*
* @param string $path
* @param bool $prepend
*/
public function addPath($path, $prepend = false){
foreach($this->lookup as $p){
if($p === $path){
return;
}
}
if($prepend){
$this->synchronized(function ($path){
$entries = $this->getAndRemoveLookupEntries();
$this->lookup[] = $path;
foreach($entries as $entry){
$this->lookup[] = $entry;
}
}, $path);
}else{
$this->lookup[] = $path;
}
}
protected function getAndRemoveLookupEntries(){
$entries = [];
while($this->count() > 0){
$entries[] = $this->shift();
}
return $entries;
}
/**
* Removes a path from the lookup list
*
* @param $path
*/
public function removePath($path){
foreach($this->lookup as $i => $p){
if($p === $path){
unset($this->lookup[$i]);
}
}
}
/**
* Returns an array of the classes loaded
*
* @return string[]
*/
public function getClasses(){
$classes = [];
foreach($this->classes as $class){
$classes[] = $class;
}
return $classes;
}
/**
* Returns the parent ClassLoader, if any
*
* @return ClassLoader
*/
public function getParent(){
return $this->parent;
}
/**
* Attaches the ClassLoader to the PHP runtime
*
* @param bool $prepend
*
* @return bool
*/
public function register($prepend = false){
spl_autoload_register([$this, "loadClass"], true, $prepend);
}
/**
* Called when there is a class to load
*
* @param string $name
*
* @return bool
*/
public function loadClass($name){
$path = $this->findClass($name);
if($path !== null){
include($path);
if(!class_exists($name, false) and !interface_exists($name, false) and !trait_exists($name, false)){
if($this->getParent() === null){
throw new ClassNotFoundException("Class $name not found");
}
return false;
}
if(method_exists($name, "onClassLoaded") and (new ReflectionClass($name))->getMethod("onClassLoaded")->isStatic()){
$name::onClassLoaded();
}
$this->classes[] = $name;
return true;
}elseif($this->getParent() === null){
throw new ClassNotFoundException("Class $name not found");
}
return false;
}
/**
* Returns the path for the class, if any
*
* @param string $name
*
* @return string|null
*/
public function findClass($name){
$components = explode("\\", $name);
$baseName = implode(DIRECTORY_SEPARATOR, $components);
foreach($this->lookup as $path){
if(PHP_INT_SIZE === 8 and file_exists($path . DIRECTORY_SEPARATOR . $baseName . "__64bit.php")){
return $path . DIRECTORY_SEPARATOR . $baseName . "__64bit.php";
}elseif(PHP_INT_SIZE === 4 and file_exists($path . DIRECTORY_SEPARATOR . $baseName . "__32bit.php")){
return $path . DIRECTORY_SEPARATOR . $baseName . "__32bit.php";
}elseif(file_exists($path . DIRECTORY_SEPARATOR . $baseName . ".php")){
return $path . DIRECTORY_SEPARATOR . $baseName . ".php";
}
}
return null;
}
}