|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Autoloader for Anax environment, including composer autoloader, if available. |
|
4
|
|
|
* |
|
5
|
|
|
*/ |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Autoloader for classes using Anax-base names. |
|
9
|
|
|
* |
|
10
|
|
|
* @param string $class The fully-qualified class name. |
|
11
|
|
|
* @return void |
|
12
|
|
|
*/ |
|
13
|
|
|
spl_autoload_register(function ($class) { |
|
14
|
|
|
$path = ANAX_SOURCE_PATH . "/{$class}/{$class}.php"; |
|
15
|
|
|
if(is_file($path)) { |
|
16
|
|
|
require($path); |
|
17
|
|
|
} |
|
18
|
|
|
}); |
|
19
|
|
|
|
|
20
|
|
|
|
|
21
|
|
|
|
|
22
|
|
|
// Maybe just use the CLoader-class and put loader code into it? |
|
23
|
|
|
|
|
24
|
|
|
|
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* PSR-0 autoloader for classes supporting namespaces, adapted to Anax environment. |
|
28
|
|
|
* |
|
29
|
|
|
* @link http://www.php-fig.org/psr/psr-0/ |
|
30
|
|
|
* @param string $class The fully-qualified class name. |
|
31
|
|
|
* @return void |
|
32
|
|
|
*/ |
|
33
|
|
|
spl_autoload_register(function ($className) { |
|
34
|
|
|
|
|
35
|
|
|
$path = ANAX_SOURCE_PATH . DIRECTORY_SEPARATOR; |
|
36
|
|
|
$className = ltrim($className, '\\'); |
|
37
|
|
|
$fileName = $path; |
|
38
|
|
|
$namespace = ''; |
|
39
|
|
|
|
|
40
|
|
|
if ($lastNsPos = strrpos($className, '\\')) { |
|
41
|
|
|
$namespace = substr($className, 0, $lastNsPos); |
|
42
|
|
|
$className = substr($className, $lastNsPos + 1); |
|
43
|
|
|
$fileName .= str_replace('\\', DIRECTORY_SEPARATOR, $namespace) . DIRECTORY_SEPARATOR; |
|
44
|
|
|
} |
|
45
|
|
|
$fileName .= str_replace('_', DIRECTORY_SEPARATOR, $className) . '.php'; |
|
46
|
|
|
|
|
47
|
|
|
if(is_file($fileName)) { |
|
48
|
|
|
require $fileName; |
|
49
|
|
|
} |
|
50
|
|
|
}); |
|
51
|
|
|
|
|
52
|
|
|
|
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* PSR-4 autoloader for Anax environment. |
|
56
|
|
|
* |
|
57
|
|
|
* @link http://www.php-fig.org/psr/psr-4/ |
|
58
|
|
|
* @param string $class The fully-qualified class name. |
|
59
|
|
|
* @return void |
|
60
|
|
|
*/ |
|
61
|
|
View Code Duplication |
spl_autoload_register(function ($class) { |
|
|
|
|
|
|
62
|
|
|
|
|
63
|
|
|
// project-specific namespace prefix |
|
64
|
|
|
$prefix = 'Anax\\'; |
|
65
|
|
|
|
|
66
|
|
|
// base directory for the namespace prefix |
|
67
|
|
|
$base_dir = ANAX_SOURCE_PATH; |
|
68
|
|
|
|
|
69
|
|
|
// does the class use the namespace prefix? |
|
70
|
|
|
$len = strlen($prefix); |
|
71
|
|
|
if (strncmp($prefix, $class, $len) !== 0) { |
|
72
|
|
|
// no, move to the next registered autoloader |
|
73
|
|
|
return; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
// get the relative class name |
|
77
|
|
|
$relative_class = substr($class, $len); |
|
78
|
|
|
|
|
79
|
|
|
// replace the namespace prefix with the base directory, replace namespace |
|
80
|
|
|
// separators with directory separators in the relative class name, append |
|
81
|
|
|
// with .php |
|
82
|
|
|
$file = $base_dir . '/' . str_replace('\\', '/', $relative_class) . '.php'; |
|
83
|
|
|
|
|
84
|
|
|
// if the file exists, require it |
|
85
|
|
|
if (file_exists($file)) { |
|
86
|
|
|
require $file; |
|
87
|
|
|
} |
|
88
|
|
|
}); |
|
89
|
|
|
|
|
90
|
|
|
|
|
91
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.