-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathAutoloader.php
More file actions
205 lines (185 loc) · 8.61 KB
/
Copy pathAutoloader.php
File metadata and controls
205 lines (185 loc) · 8.61 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
<?php
/**
* APIShift Engine v1.0.0
*
* Copyright 2020-present Sapir Shemer, DevShift (devshift.biz)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* @author Sapir Shemer
*/
namespace APIShift\Core;
/**
* This autoloader helps us avoid loading files in a hardcoded way, and loads them automatically.
* Avoids re-loading files.
*
* Based on the PSR-4 implementation (https://www.php-fig.org/psr/psr-4/)
* This loader saves prefixes and their path data using a tree structure, smart, modular and can be extended
*/
class Autoloader {
/**
* A nested array with the prefixes and their parent folders
*
* Each element represents a namespace and can be either a string representing the path/name of the folder or an array.
* In case an element is an array the system will try to find the "path" key in the array to know the folder name.
* If there is no "path" key present in an element the loader will not assign a folder name and will continue accordingly.
* Each array element can have sub-elements which represent sub-namespaces and also can be either a string or an array.
* A sub-element can have a boolean key "ignore" which means to ignore the previous folder and unset it - in other words
* override the parent folder.
*/
protected static $prefixes = [
"APIShift" => [
"path" => __DIR__ . "/..",
"Extensions" => "../extensions"
]
];
/**
* Register loader with SPL autoloader stack.
*
* @return void
*/
public static function register()
{
spl_autoload_register('APIShift\Core\Autoloader::loader');
}
/**
* Adds a base directory for a namespace prefix.
*
* @param string $prefix The namespace prefix.
* @param string $base_dir A base directory for class files in the namespace.
* @param bool|null $ignore If true, then the loader will ignore the directory of the parent namespace
*
* @return void
*/
public static function addNamespace($prefix, $base_dir, bool $ignore = null)
{
// Collection of namespaces
$prefixCollection = explode("\\", $prefix);
// normalize the base directory with a trailing separator
$base_dir = rtrim($base_dir, DIRECTORY_SEPARATOR);
// Construct base element
$prefixData = [];
if($ignore != null)
{
$prefixData = [
"path" => $base_dir,
"ignore" => $ignore
];
}
else $prefixData = $base_dir;
// Find existing parents in prefix tree
$lastNamespaceParentkey = 0; // Stores the last parent key found
$tempDependencyTreeIterator = &self::$prefixes;
while(isset($tempDependencyTreeIterator[$prefixCollection[$lastNamespaceParentkey]]) && $lastNamespaceParentkey >= 0)
{
$tempDependencyTreeIterator = &$tempDependencyTreeIterator[$prefixCollection[$lastNamespaceParentkey]];
$lastNamespaceParentkey++;
}
// Add new child elements from last existing parent
while($lastNamespaceParentkey < count($prefixCollection))
{
$tempDependencyTreeIterator[$prefixCollection[$lastNamespaceParentkey]] = [];
$tempDependencyTreeIterator = &$tempDependencyTreeIterator[$prefixCollection[$lastNamespaceParentkey]];
$lastNamespaceParentkey++;
}
// Update last element
if(is_array($prefixData) || (is_string($prefixData)
&& is_string($tempDependencyTreeIterator))) $tempDependencyTreeIterator = $prefixData;
else $tempDependencyTreeIterator["path"] = $prefixData;
}
/**
* Returns the path of the package selected
*
* @param $package The package to get path from
*
* @return void
*/
protected static function getPathFromPackage($package) {
// Get vendor parent directory
if(is_array($package) && isset($package["path"])) return $package["path"];
else if(is_string($package)) return $package;
// Otherwise, if only array with children, delete the element
else if(is_array($package) && count($package) > 0) return "";
}
/**
* Loads the class by translating the prefixes into the containing folder and choosing from the folder
* or src/test folders if present - priority by order (root folder, src folder then the test folder)
*
* @param string $className The class name.
* @return mixed The file name or false on failure.
*/
public static function loader($className) {
// No need to load exisitng classes
if(class_exists($className) || interface_exists($className)) return true;
$classPath = explode("\\", $className);
// Stores the last key changed by the prefixes to avoid lower casing
$lastKeyChanged = -1;
// Stores the file key in case we unset paths due to prefix rules
$fileKey = count($classPath) - 1;
// Handle the vendor namespaces - avoids recursion even though we have a tree ;)
if(isset(self::$prefixes[$classPath[0]])) {
// Get vendor parent directory
$vendorName = $classPath[0];
$classPath[0] = self::getPathFromPackage(self::$prefixes[$classPath[0]]);
if($classPath[0] == "") unset($classPath[0]);
$lastKeyChanged = 0;
// Get sub dependency directories
$tempDependencyTree = &self::$prefixes[$vendorName];
for($iter = 1; $iter < count($classPath); $iter++) {
// When reaching a child containing only a path, break to loop
if(is_string($tempDependencyTree) || (count($tempDependencyTree) == 1 && isset($tempDependencyTree["path"]))) break;
// Assign the directory name
else if(isset($tempDependencyTree[$classPath[$iter]])) {
// Ignore previous folder
if(isset($tempDependencyTree[$classPath[$iter]]["ignore"])
&& $tempDependencyTree[$classPath[$iter]]["ignore"] == true) {
$prevPathIndex = $iter;
// Move back until last path to ignore
while($prevPathIndex > 0 && !isset($classPath[--$prevPathIndex]));
if(isset($classPath[$prevPathIndex])) unset($classPath[$prevPathIndex]);
}
// Change current folder name
$package = $classPath[$iter];
$classPath[$iter] = self::getPathFromPackage($tempDependencyTree[$classPath[$iter]]);
if($classPath[$iter] == "") unset($classPath[$iter]);
$lastKeyChanged = $iter;
// Move to next child
$tempDependencyTree = &$tempDependencyTree[$package];
}
// No child found
else break;
}
}
// External namespaces are located at the "../externals/vendor" folder
else $classPath[0] = __DIR__ . "../../externals/vendor/" . $classPath[0];
// Make folders as lowercase
for($iter = $lastKeyChanged + 1; $iter < $fileKey; $iter++) $classPath[$iter] = strtolower($classPath[$iter]);
// Construct paths to find files - ordered by priority
$pathToFindClass_regular = implode("/", $classPath) . ".php";
$pathToFindClass_src = implode("/", array_splice($classPath, 0, count($classPath) - 1)) . "/src/" . end($classPath) . ".php";
$pathToFindClass_test = implode("/", array_splice($classPath, 0, count($classPath) - 1)) . "/test/" . end($classPath) . ".php";
$foundPath = "";
// Load the files
if(file_exists($pathToFindClass_regular)) $foundPath = $pathToFindClass_regular;
else if(file_exists($pathToFindClass_src)) $foundPath = $pathToFindClass_src;
else if(file_exists($pathToFindClass_test)) $foundPath = $pathToFindClass_test;
// Move to other registered autoloaders
else return false;
// Require the class & return the name
require $foundPath;
return $foundPath;
}
}
// Register the loader
Autoloader::register();
?>