Chris@0: Chris@0: * Chris@0: * For the full copyright and license information, please view the LICENSE Chris@0: * file that was distributed with this source code. Chris@0: */ Chris@0: Chris@0: namespace Symfony\Component\ClassLoader; Chris@0: Chris@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); Chris@14: Chris@0: /** Chris@0: * ClassMapGenerator. Chris@0: * Chris@0: * @author Gyula Sallai Chris@14: * Chris@14: * @deprecated since version 3.3, to be removed in 4.0. Chris@0: */ Chris@0: class ClassMapGenerator Chris@0: { Chris@0: /** Chris@0: * Generate a class map file. Chris@0: * Chris@0: * @param array|string $dirs Directories or a single path to search in Chris@0: * @param string $file The name of the class map file Chris@0: */ Chris@0: public static function dump($dirs, $file) Chris@0: { Chris@0: $dirs = (array) $dirs; Chris@17: $maps = []; Chris@0: Chris@0: foreach ($dirs as $dir) { Chris@0: $maps = array_merge($maps, static::createMap($dir)); Chris@0: } Chris@0: Chris@0: file_put_contents($file, sprintf('isFile()) { Chris@0: continue; Chris@0: } Chris@0: Chris@0: $path = $file->getRealPath() ?: $file->getPathname(); Chris@0: Chris@14: if ('php' !== pathinfo($path, PATHINFO_EXTENSION)) { Chris@0: continue; Chris@0: } Chris@0: Chris@0: $classes = self::findClasses($path); Chris@0: Chris@12: if (\PHP_VERSION_ID >= 70000) { Chris@0: // PHP 7 memory manager will not release after token_get_all(), see https://bugs.php.net/70098 Chris@0: gc_mem_caches(); Chris@0: } Chris@0: Chris@0: foreach ($classes as $class) { Chris@0: $map[$class] = $path; Chris@0: } Chris@0: } Chris@0: Chris@0: return $map; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Extract the classes in the given file. Chris@0: * Chris@0: * @param string $path The file to check Chris@0: * Chris@0: * @return array The found classes Chris@0: */ Chris@0: private static function findClasses($path) Chris@0: { Chris@0: $contents = file_get_contents($path); Chris@0: $tokens = token_get_all($contents); Chris@0: Chris@17: $classes = []; Chris@0: Chris@0: $namespace = ''; Chris@0: for ($i = 0; isset($tokens[$i]); ++$i) { Chris@0: $token = $tokens[$i]; Chris@0: Chris@0: if (!isset($token[1])) { Chris@0: continue; Chris@0: } Chris@0: Chris@0: $class = ''; Chris@0: Chris@0: switch ($token[0]) { Chris@0: case T_NAMESPACE: Chris@0: $namespace = ''; Chris@0: // If there is a namespace, extract it Chris@0: while (isset($tokens[++$i][1])) { Chris@17: if (\in_array($tokens[$i][0], [T_STRING, T_NS_SEPARATOR])) { Chris@0: $namespace .= $tokens[$i][1]; Chris@0: } Chris@0: } Chris@0: $namespace .= '\\'; Chris@0: break; Chris@0: case T_CLASS: Chris@0: case T_INTERFACE: Chris@0: case T_TRAIT: Chris@0: // Skip usage of ::class constant Chris@0: $isClassConstant = false; Chris@0: for ($j = $i - 1; $j > 0; --$j) { Chris@0: if (!isset($tokens[$j][1])) { Chris@0: break; Chris@0: } Chris@0: Chris@0: if (T_DOUBLE_COLON === $tokens[$j][0]) { Chris@0: $isClassConstant = true; Chris@0: break; Chris@17: } elseif (!\in_array($tokens[$j][0], [T_WHITESPACE, T_DOC_COMMENT, T_COMMENT])) { Chris@0: break; Chris@0: } Chris@0: } Chris@0: Chris@0: if ($isClassConstant) { Chris@0: break; Chris@0: } Chris@0: Chris@0: // Find the classname Chris@0: while (isset($tokens[++$i][1])) { Chris@0: $t = $tokens[$i]; Chris@0: if (T_STRING === $t[0]) { Chris@0: $class .= $t[1]; Chris@0: } elseif ('' !== $class && T_WHITESPACE === $t[0]) { Chris@0: break; Chris@0: } Chris@0: } Chris@0: Chris@0: $classes[] = ltrim($namespace.$class, '\\'); Chris@0: break; Chris@0: default: Chris@0: break; Chris@0: } Chris@0: } Chris@0: Chris@0: return $classes; Chris@0: } Chris@0: }