-
Notifications
You must be signed in to change notification settings - Fork 5
/
AutoLoader.php
84 lines (70 loc) · 1.51 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
<?php
namespace Quetzal;
/**
* Автозагрузчик для пространства Quetzal
*
* Class AutoLoader
*
* @author Grigory Bychek <[email protected]>
*
* @package Quetzal
*/
class AutoLoader
{
static private $recursiveSearch = true;
public function __construct()
{}
/**
* @return string
*/
protected static function getBasePath()
{
return __DIR__;
}
/**
* @param string $path
* @param string $file
*
* @return string
*/
protected static function generateFilePath($path, $file)
{
return str_replace('/Quetzal/', '/', sprintf('%s/%s.php', $path, str_replace('\\', '/', $file)));
}
/**
* @param string $file
*/
public static function autoLoad($file)
{
$path = self::getBasePath();
$filePath = self::generateFilePath($path, $file);
if (file_exists($filePath)) {
require_once($filePath);
} else {
self::$recursiveSearch = true;
self::recursiveLoad($file, $path);
}
}
/**
* @param string $file
* @param string $path
*/
public static function recursiveLoad($file, $path)
{
if (false !== ($handle = opendir($path)) && self::$recursiveSearch) {
while (false !== ($dir = readdir($handle)) && self::$recursiveSearch) {
if (strpos($dir, '.') === false) {
$path2 = $path . '/' . $dir;
$filePath = $path2 . '/' . $file . '.php';
if (file_exists($filePath)) {
self::$recursiveSearch = false;
require_once($filePath);
break;
}
self::recursiveLoad($file, $path2);
}
}
closedir($handle);
}
}
}