-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathautoloader.php
67 lines (56 loc) · 1.53 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
<?php
/**
* Created by PhpStorm.
* User: newexe
* Date: 30.06.18
* Time: 21:55
*/
/**
* Find needed file by namespace of class.
* Char '_' in namespace will be converted to '-'.
*
* @param $className
*/
function namespaceAutoload($className)
{
if (preg_match('/\\\\/', $className)) {
$className = str_replace('\\', DIRECTORY_SEPARATOR, $className);
$path = "{$className}.php";
$path = str_replace('_', '-', $path);
$path = ROOT . '/' . $path;
if (file_exists($path)) {
require_once $path;
}
}
}
/**
* Walk through all PHP files in project
* until the needed file is found
*
* @param $className
*/
function allFilesWalkerAutoload($className)
{
$explodedClassName = explode('\\', $className);
$requiredFileName = end($explodedClassName) . '.php';
unset($explodedClassName);
$dir = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator(ROOT, FilesystemIterator::SKIP_DOTS),
true);
/** @var SplFileInfo $file */
foreach ($dir as $file)
{
if ($file->isDir() || // exclude dirs
$file->getExtension() !== 'php' || // exclude non-php files
$file->getPathname() === __FILE__ // exclude current file
) {
continue;
}
if ($file->getFilename() === $requiredFileName) {
require_once $file->getPathname();
break;
}
}
}
spl_autoload_register('namespaceAutoload');
spl_autoload_register('allFilesWalkerAutoload');