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