|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Nip\AutoLoader\Generators; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Class ClassMap |
|
7
|
|
|
* @package Nip\AutoLoader\Generators |
|
8
|
|
|
*/ |
|
9
|
|
|
class ClassMap |
|
10
|
|
|
{ |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Generate a class map file. |
|
14
|
|
|
* |
|
15
|
|
|
* @param array|string $dirs Directories or a single path to search in |
|
16
|
|
|
* @param string $file The name of the class map file |
|
17
|
|
|
*/ |
|
18
|
|
|
public static function dump($dirs, $file) |
|
19
|
|
|
{ |
|
20
|
|
|
$dirs = (array)$dirs; |
|
21
|
|
|
$maps = []; |
|
22
|
|
|
foreach ($dirs as $dir) { |
|
23
|
|
|
$maps = array_merge($maps, static::createMap($dir)); |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
return file_put_contents($file, sprintf('<?php return %s;', var_export($maps, true))); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Iterate over all files in the given directory searching for classes. |
|
31
|
|
|
* @param \Iterator|string $dir The directory to search in or an iterator |
|
32
|
|
|
* @return array A class map array |
|
33
|
|
|
*/ |
|
34
|
|
|
public static function createMap($dir) |
|
35
|
|
|
{ |
|
36
|
|
|
ini_set('memory_limit', '300M'); |
|
37
|
|
|
|
|
38
|
|
|
if (is_string($dir)) { |
|
39
|
|
|
$dir = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($dir)); |
|
40
|
|
|
} |
|
41
|
|
|
$map = []; |
|
42
|
|
|
foreach ($dir as $file) { |
|
43
|
|
|
if (!$file->isFile()) { |
|
44
|
|
|
continue; |
|
45
|
|
|
} |
|
46
|
|
|
$path = $file->getRealPath(); |
|
47
|
|
|
if (pathinfo($path, PATHINFO_EXTENSION) !== 'php') { |
|
48
|
|
|
continue; |
|
49
|
|
|
} |
|
50
|
|
|
$classes = self::findClasses($path); |
|
51
|
|
|
if (PHP_VERSION_ID >= 70000) { |
|
52
|
|
|
// PHP 7 memory manager will not release after token_get_all(), see https://bugs.php.net/70098 |
|
53
|
|
|
gc_mem_caches(); |
|
54
|
|
|
} |
|
55
|
|
|
foreach ($classes as $class) { |
|
56
|
|
|
$map[$class] = $path; |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
return $map; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Extract the classes in the given file. |
|
65
|
|
|
* @param string $path The file to check |
|
66
|
|
|
* @return array The found classes |
|
67
|
|
|
*/ |
|
68
|
|
|
private static function findClasses($path) |
|
69
|
|
|
{ |
|
70
|
|
|
$contents = file_get_contents($path); |
|
71
|
|
|
$tokens = token_get_all($contents); |
|
72
|
|
|
$classes = []; |
|
73
|
|
|
$namespace = ''; |
|
74
|
|
|
for ($i = 0; isset($tokens[$i]); ++$i) { |
|
75
|
|
|
$token = $tokens[$i]; |
|
76
|
|
|
if (!isset($token[1])) { |
|
77
|
|
|
continue; |
|
78
|
|
|
} |
|
79
|
|
|
$class = ''; |
|
80
|
|
|
switch ($token[0]) { |
|
81
|
|
|
case T_NAMESPACE: |
|
82
|
|
|
$namespace = ''; |
|
83
|
|
|
// If there is a namespace, extract it |
|
84
|
|
|
while (isset($tokens[++$i][1])) { |
|
85
|
|
|
if (in_array($tokens[$i][0], array(T_STRING, T_NS_SEPARATOR))) { |
|
86
|
|
|
$namespace .= $tokens[$i][1]; |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
$namespace .= '\\'; |
|
90
|
|
|
break; |
|
91
|
|
|
case T_CLASS: |
|
92
|
|
|
case T_INTERFACE: |
|
93
|
|
|
case T_TRAIT: |
|
94
|
|
|
// Skip usage of ::class constant |
|
95
|
|
|
$isClassConstant = false; |
|
96
|
|
|
for ($j = $i - 1; $j > 0; --$j) { |
|
97
|
|
|
if (!isset($tokens[$j][1])) { |
|
98
|
|
|
break; |
|
99
|
|
|
} |
|
100
|
|
|
if (T_DOUBLE_COLON === $tokens[$j][0]) { |
|
101
|
|
|
$isClassConstant = true; |
|
102
|
|
|
break; |
|
103
|
|
|
} elseif (!in_array($tokens[$j][0], array(T_WHITESPACE, T_DOC_COMMENT, T_COMMENT))) { |
|
104
|
|
|
break; |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
if ($isClassConstant) { |
|
108
|
|
|
break; |
|
109
|
|
|
} |
|
110
|
|
|
// Find the classname |
|
111
|
|
|
while (isset($tokens[++$i][1])) { |
|
112
|
|
|
$t = $tokens[$i]; |
|
113
|
|
|
if (T_STRING === $t[0]) { |
|
114
|
|
|
$class .= $t[1]; |
|
115
|
|
|
} elseif ('' !== $class && T_WHITESPACE === $t[0]) { |
|
116
|
|
|
break; |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
$classes[] = ltrim($namespace.$class, '\\'); |
|
120
|
|
|
break; |
|
121
|
|
|
default: |
|
122
|
|
|
break; |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
return $classes; |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
|