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