1
|
|
|
<?php |
2
|
|
|
namespace HexMakina\kadro; |
3
|
|
|
|
4
|
|
|
use \HexMakina\LocalFS\FileSystem; |
|
|
|
|
5
|
|
|
|
6
|
|
|
class PSR4Autoloader |
7
|
|
|
{ |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* An associative array where the key is a namespace prefix and the value |
11
|
|
|
* is an array of base directories for classes in that namespace. |
12
|
|
|
* |
13
|
|
|
* @var array |
14
|
|
|
*/ |
15
|
|
|
protected $prefixes = []; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Register loader with SPL autoloader stack. |
19
|
|
|
* |
20
|
|
|
* @return void |
21
|
|
|
*/ |
22
|
|
|
|
23
|
|
|
public function register() |
24
|
|
|
{ |
25
|
|
|
spl_autoload_register(array($this, 'loadClass')); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Adds a base directory for a namespace prefix. |
30
|
|
|
* |
31
|
|
|
* @param string $prefix The namespace prefix. |
32
|
|
|
* @param string $base_dir A base directory for class files in the |
33
|
|
|
* namespace. |
34
|
|
|
* @param bool $prepend If true, prepend the base directory to the stack |
35
|
|
|
* instead of appending it; this causes it to be searched first rather |
36
|
|
|
* than last. |
37
|
|
|
* @return void |
38
|
|
|
*/ |
39
|
|
|
public function addNamespaceTree($base_dir, $prepend = false) |
|
|
|
|
40
|
|
|
{ |
41
|
|
|
$dir_content = FileSystem::preg_scandir($base_dir, '/^[A-Z]{1}[A-Za-z]+(?!\.class.\.php)$/'); |
42
|
|
|
|
43
|
|
|
foreach($dir_content as $res) |
44
|
|
|
{ |
45
|
|
|
if(is_dir($fullpath = $base_dir.'/'.$res)) |
46
|
|
|
{ |
47
|
|
|
$this->addNamespace('HexMakina\kadro\\'.$res, $fullpath); |
48
|
|
|
$this->addNamespaceTree($fullpath); |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function addNamespace($prefix, $base_dir, $prepend = false) |
54
|
|
|
{ |
55
|
|
|
|
56
|
|
|
// normalize namespace prefix |
57
|
|
|
$prefix = trim($prefix, '\\') . '\\'; |
58
|
|
|
|
59
|
|
|
// normalize the base directory with a trailing separator |
60
|
|
|
$base_dir = rtrim($base_dir, DIRECTORY_SEPARATOR) . '/'; |
61
|
|
|
|
62
|
|
|
// initialize the namespace prefix array |
63
|
|
|
if (isset($this->prefixes[$prefix]) === false) { |
64
|
|
|
$this->prefixes[$prefix] = array(); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
// retain the base directory for the namespace prefix |
68
|
|
|
if ($prepend) { |
69
|
|
|
array_unshift($this->prefixes[$prefix], $base_dir); |
70
|
|
|
} else { |
71
|
|
|
array_push($this->prefixes[$prefix], $base_dir); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Loads the class file for a given class name. |
77
|
|
|
* |
78
|
|
|
* @param string $class The fully-qualified class name. |
79
|
|
|
* @return mixed The mapped file name on success, or boolean false on |
80
|
|
|
* failure. |
81
|
|
|
*/ |
82
|
|
|
public function loadClass($class) |
83
|
|
|
{ |
84
|
|
|
// var_dump(__FUNCTION__."($class)"); |
85
|
|
|
|
86
|
|
|
// the current namespace prefix |
87
|
|
|
$prefix = $class; |
88
|
|
|
// work backwards through the namespace names of the fully-qualified |
89
|
|
|
// class name to find a mapped file name |
90
|
|
|
while (false !== $pos = strrpos($prefix, '\\')) |
91
|
|
|
{ |
92
|
|
|
// retain the trailing namespace separator in the prefix |
93
|
|
|
$prefix = substr($class, 0, $pos + 1); |
94
|
|
|
// var_dump("prefix: $prefix"); |
95
|
|
|
// the rest is the relative class name |
96
|
|
|
$relative_class = substr($class, $pos + 1); |
97
|
|
|
// try to load a mapped file for the prefix and relative class |
98
|
|
|
|
99
|
|
|
$mapped_file = $this->loadMappedFile($prefix, $relative_class); |
100
|
|
|
if ($mapped_file) { |
101
|
|
|
return $mapped_file; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
// remove the trailing namespace separator for the next iteration |
105
|
|
|
// of strrpos() |
106
|
|
|
$prefix = rtrim($prefix, '\\'); |
107
|
|
|
} |
108
|
|
|
// never found a mapped file |
109
|
|
|
return false; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Load the mapped file for a namespace prefix and relative class. |
114
|
|
|
* |
115
|
|
|
* @param string $prefix The namespace prefix. |
116
|
|
|
* @param string $relative_class The relative class name. |
117
|
|
|
* @return mixed Boolean false if no mapped file can be loaded, or the |
118
|
|
|
* name of the mapped file that was loaded. |
119
|
|
|
*/ |
120
|
|
|
protected function loadMappedFile($prefix, $relative_class) |
121
|
|
|
{ |
122
|
|
|
// are there any base directories for this namespace prefix? |
123
|
|
|
if (isset($this->prefixes[$prefix]) === false) { |
124
|
|
|
return false; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
// look through base directories for this namespace prefix |
128
|
|
|
foreach ($this->prefixes[$prefix] as $base_dir) |
129
|
|
|
{ |
130
|
|
|
// replace the namespace prefix with the base directory, |
131
|
|
|
// replace namespace separators with directory separators |
132
|
|
|
// in the relative class name, append with .php |
133
|
|
|
$file = $base_dir |
134
|
|
|
. str_replace('\\', '/', $relative_class) |
135
|
|
|
. '.class.php'; |
136
|
|
|
|
137
|
|
|
// if the mapped file exists, require it |
138
|
|
|
if ($this->requireFile($file)) |
139
|
|
|
{// yes, we're done |
140
|
|
|
// var_dump(__FUNCTION__." SUCCESS requireFile($file)"); |
141
|
|
|
return $file; |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
// never found it |
146
|
|
|
return false; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* If a file exists, require it from the file system. |
151
|
|
|
* |
152
|
|
|
* @param string $file The file to require. |
153
|
|
|
* @return bool True if the file exists, false if not. |
154
|
|
|
*/ |
155
|
|
|
protected function requireFile($file) |
156
|
|
|
{ |
157
|
|
|
// var_dump(__FUNCTION__."($file)"); |
158
|
|
|
if (file_exists($file)) { |
159
|
|
|
require_once $file; |
160
|
|
|
return true; |
161
|
|
|
} |
162
|
|
|
return false; |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths