4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\ClassLoader;
14 @trigger_error('The '.__NAMESPACE__.'\ClassMapGenerator class is deprecated since Symfony 3.3 and will be removed in 4.0. Use Composer instead.', E_USER_DEPRECATED);
19 * @author Gyula Sallai <salla016@gmail.com>
21 * @deprecated since version 3.3, to be removed in 4.0.
23 class ClassMapGenerator
26 * Generate a class map file.
28 * @param array|string $dirs Directories or a single path to search in
29 * @param string $file The name of the class map file
31 public static function dump($dirs, $file)
33 $dirs = (array) $dirs;
36 foreach ($dirs as $dir) {
37 $maps = array_merge($maps, static::createMap($dir));
40 file_put_contents($file, sprintf('<?php return %s;', var_export($maps, true)));
44 * Iterate over all files in the given directory searching for classes.
46 * @param \Iterator|string $dir The directory to search in or an iterator
48 * @return array A class map array
50 public static function createMap($dir)
52 if (\is_string($dir)) {
53 $dir = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($dir));
58 foreach ($dir as $file) {
59 if (!$file->isFile()) {
63 $path = $file->getRealPath() ?: $file->getPathname();
65 if ('php' !== pathinfo($path, PATHINFO_EXTENSION)) {
69 $classes = self::findClasses($path);
71 if (\PHP_VERSION_ID >= 70000) {
72 // PHP 7 memory manager will not release after token_get_all(), see https://bugs.php.net/70098
76 foreach ($classes as $class) {
85 * Extract the classes in the given file.
87 * @param string $path The file to check
89 * @return array The found classes
91 private static function findClasses($path)
93 $contents = file_get_contents($path);
94 $tokens = token_get_all($contents);
99 for ($i = 0; isset($tokens[$i]); ++$i) {
100 $token = $tokens[$i];
102 if (!isset($token[1])) {
111 // If there is a namespace, extract it
112 while (isset($tokens[++$i][1])) {
113 if (\in_array($tokens[$i][0], array(T_STRING, T_NS_SEPARATOR))) {
114 $namespace .= $tokens[$i][1];
122 // Skip usage of ::class constant
123 $isClassConstant = false;
124 for ($j = $i - 1; $j > 0; --$j) {
125 if (!isset($tokens[$j][1])) {
129 if (T_DOUBLE_COLON === $tokens[$j][0]) {
130 $isClassConstant = true;
132 } elseif (!\in_array($tokens[$j][0], array(T_WHITESPACE, T_DOC_COMMENT, T_COMMENT))) {
137 if ($isClassConstant) {
141 // Find the classname
142 while (isset($tokens[++$i][1])) {
144 if (T_STRING === $t[0]) {
146 } elseif ('' !== $class && T_WHITESPACE === $t[0]) {
151 $classes[] = ltrim($namespace.$class, '\\');