[BUGFIX] Harden fallback class map generation
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Resources / PHP / ClassMapGenerator.php
1 <?php
2
3 /*
4 * This file is part of Composer.
5 *
6 * (c) Nils Adermann <naderman@naderman.de>
7 * Jordi Boggiano <j.boggiano@seld.be>
8 *
9 * For the full copyright and license information, please view the LICENSE
10 * file that was distributed with this source code.
11 */
12
13 /*
14 * This file is copied from the Symfony package.
15 *
16 * (c) Fabien Potencier <fabien@symfony.com>
17 */
18
19 namespace Composer\Autoload;
20
21 use Symfony\Component\Finder\Finder;
22 use Composer\IO\IOInterface;
23
24 /**
25 * ClassMapGenerator
26 *
27 * @author Gyula Sallai <salla016@gmail.com>
28 * @author Jordi Boggiano <j.boggiano@seld.be>
29 */
30 class ClassMapGenerator
31 {
32 /**
33 * Generate a class map file
34 *
35 * @param \Traversable $dirs Directories or a single path to search in
36 * @param string $file The name of the class map file
37 */
38 public static function dump($dirs, $file)
39 {
40 $maps = array();
41
42 foreach ($dirs as $dir) {
43 $maps = array_merge($maps, static::createMap($dir));
44 }
45
46 file_put_contents($file, sprintf('<?php return %s;', var_export($maps, true)));
47 }
48
49 /**
50 * Iterate over all files in the given directory searching for classes
51 *
52 * @param \Iterator|string $path The path to search in or an iterator
53 * @param string $blacklist Regex that matches against the file path that exclude from the classmap.
54 * @param IOInterface $io IO object
55 * @param string $namespace Optional namespace prefix to filter by
56 *
57 * @throws \RuntimeException When the path is neither an existing file nor directory
58 * @return array A class map array
59 */
60 public static function createMap($path, $blacklist = null, IOInterface $io = null, $namespace = null)
61 {
62 if (is_string($path)) {
63 if (is_file($path)) {
64 $path = array(new \SplFileInfo($path));
65 } elseif (is_dir($path)) {
66 $path = Finder::create()->files()->followLinks()->name('/\.(php|inc|hh)$/')->in($path);
67 } else {
68 throw new \RuntimeException(
69 'Could not scan for classes inside "'.$path.
70 '" which does not appear to be a file nor a folder'
71 );
72 }
73 }
74
75 $map = array();
76
77 foreach ($path as $file) {
78 $filePath = $file->getRealPath();
79
80 if (!in_array(pathinfo($filePath, PATHINFO_EXTENSION), array('php', 'inc', 'hh'))) {
81 continue;
82 }
83
84 if ($blacklist && preg_match($blacklist, strtr($filePath, '\\', '/'))) {
85 continue;
86 }
87
88 $classes = self::findClasses($filePath);
89
90 foreach ($classes as $class) {
91 // skip classes not within the given namespace prefix
92 if (null !== $namespace && 0 !== strpos($class, $namespace)) {
93 continue;
94 }
95
96 if (!isset($map[$class])) {
97 $map[$class] = $filePath;
98 } elseif ($io && $map[$class] !== $filePath && !preg_match('{/(test|fixture|example|stub)s?/}i', strtr($map[$class].' '.$filePath, '\\', '/'))) {
99 $io->writeError(
100 '<warning>Warning: Ambiguous class resolution, "'.$class.'"'.
101 ' was found in both "'.$map[$class].'" and "'.$filePath.'", the first will be used.</warning>'
102 );
103 }
104 }
105 }
106
107 return $map;
108 }
109
110 /**
111 * Extract the classes in the given file
112 *
113 * @param string $path The file to check
114 * @throws \RuntimeException
115 * @return array The found classes
116 */
117 private static function findClasses($path)
118 {
119 $extraTypes = PHP_VERSION_ID < 50400 ? '' : '|trait';
120 if (defined('HHVM_VERSION') && version_compare(HHVM_VERSION, '3.3', '>=')) {
121 $extraTypes .= '|enum';
122 }
123
124 try {
125 $contents = @php_strip_whitespace($path);
126 if (!$contents) {
127 if (!file_exists($path)) {
128 throw new \Exception('File does not exist');
129 }
130 if (!is_readable($path)) {
131 throw new \Exception('File is not readable');
132 }
133 }
134 } catch (\Exception $e) {
135 throw new \RuntimeException('Could not scan for classes inside '.$path.": \n".$e->getMessage(), 0, $e);
136 }
137
138 // return early if there is no chance of matching anything in this file
139 if (!preg_match('{\b(?:class|interface'.$extraTypes.')\s}i', $contents)) {
140 return array();
141 }
142
143 // strip heredocs/nowdocs
144 $contents = preg_replace('{<<<\s*(\'?)(\w+)\\1(?:\r\n|\n|\r)(?:.*?)(?:\r\n|\n|\r)\\2(?=\r\n|\n|\r|;)}s', 'null', $contents);
145 // strip strings
146 $contents = preg_replace('{"[^"\\\\]*+(\\\\.[^"\\\\]*+)*+"|\'[^\'\\\\]*+(\\\\.[^\'\\\\]*+)*+\'}s', 'null', $contents);
147 // strip leading non-php code if needed
148 if (substr($contents, 0, 2) !== '<?') {
149 $contents = preg_replace('{^.+?<\?}s', '<?', $contents, 1, $replacements);
150 if ($replacements === 0) {
151 return array();
152 }
153 }
154 // strip non-php blocks in the file
155 $contents = preg_replace('{\?>.+<\?}s', '?><?', $contents);
156 // strip trailing non-php code if needed
157 $pos = strrpos($contents, '?>');
158 if (false !== $pos && false === strpos(substr($contents, $pos), '<?')) {
159 $contents = substr($contents, 0, $pos);
160 }
161
162 preg_match_all('{
163 (?:
164 \b(?<![\$:>])(?P<type>class|interface'.$extraTypes.') \s++ (?P<name>[a-zA-Z_\x7f-\xff:][a-zA-Z0-9_\x7f-\xff:\-]*+)
165 | \b(?<![\$:>])(?P<ns>namespace) (?P<nsname>\s++[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*+(?:\s*+\\\\\s*+[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*+)*+)? \s*+ [\{;]
166 )
167 }ix', $contents, $matches);
168
169 $classes = array();
170 $namespace = '';
171
172 for ($i = 0, $len = count($matches['type']); $i < $len; $i++) {
173 if (!empty($matches['ns'][$i])) {
174 $namespace = str_replace(array(' ', "\t", "\r", "\n"), '', $matches['nsname'][$i]) . '\\';
175 } else {
176 $name = $matches['name'][$i];
177 if ($name[0] === ':') {
178 // This is an XHP class, https://github.com/facebook/xhp
179 $name = 'xhp'.substr(str_replace(array('-', ':'), array('_', '__'), $name), 1);
180 } elseif ($matches['type'][$i] === 'enum') {
181 // In Hack, something like:
182 // enum Foo: int { HERP = '123'; }
183 // The regex above captures the colon, which isn't part of
184 // the class name.
185 $name = rtrim($name, ':');
186 }
187 $classes[] = ltrim($namespace . $name, '\\');
188 }
189 }
190
191 return $classes;
192 }
193 }