TYPO3 CMS  TYPO3_8-7
ClassMapGenerator.php
Go to the documentation of this file.
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 
20 
23 
31 {
38  public static function dump($dirs, $file)
39  {
40  $maps = [];
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 
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 = [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  1476049953
72  );
73  }
74  }
75 
76  $map = [];
77 
78  foreach ($path as $file) {
79  $filePath = $file->getRealPath();
80 
81  if (!in_array(pathinfo($filePath, PATHINFO_EXTENSION), ['php', 'inc', 'hh'])) {
82  continue;
83  }
84 
85  if ($blacklist && preg_match($blacklist, strtr($filePath, '\\', '/'))) {
86  continue;
87  }
88 
89  $classes = self::findClasses($filePath);
90 
91  foreach ($classes as $class) {
92  // skip classes not within the given namespace prefix
93  if (null !== $namespace && 0 !== strpos($class, $namespace)) {
94  continue;
95  }
96 
97  if (!isset($map[$class])) {
98  $map[$class] = $filePath;
99  } elseif ($io && $map[$class] !== $filePath && !preg_match('{/(test|fixture|example|stub)s?/}i', strtr($map[$class] . ' ' . $filePath, '\\', '/'))) {
100  $io->writeError(
101  '<warning>Warning: Ambiguous class resolution, "' . $class . '"' .
102  ' was found in both "' . $map[$class] . '" and "' . $filePath . '", the first will be used.</warning>'
103  );
104  }
105  }
106  }
107 
108  return $map;
109  }
110 
118  private static function findClasses($path)
119  {
120  $extraTypes = PHP_VERSION_ID < 50400 ? '' : '|trait';
121  if (defined('HHVM_VERSION') && version_compare(HHVM_VERSION, '3.3', '>=')) {
122  $extraTypes .= '|enum';
123  }
124 
125  try {
126  $contents = @php_strip_whitespace($path);
127  if (!$contents) {
128  if (!file_exists($path)) {
129  throw new \Exception('File does not exist', 1476049981);
130  }
131  if (!is_readable($path)) {
132  throw new \Exception('File is not readable', 1476049990);
133  }
134  }
135  } catch (\Exception $e) {
136  throw new \RuntimeException('Could not scan for classes inside ' . $path . ": \n" . $e->getMessage(), 1476050009, $e);
137  }
138 
139  // return early if there is no chance of matching anything in this file
140  if (!preg_match('{\b(?:class|interface' . $extraTypes . ')\s}i', $contents)) {
141  return [];
142  }
143 
144  // strip heredocs/nowdocs
145  $contents = preg_replace('{<<<\s*(\'?)(\w+)\\1(?:\r\n|\n|\r)(?:.*?)(?:\r\n|\n|\r)\\2(?=\r\n|\n|\r|;)}s', 'null', $contents);
146  // strip strings
147  $contents = preg_replace('{"[^"\\\\]*+(\\\\.[^"\\\\]*+)*+"|\'[^\'\\\\]*+(\\\\.[^\'\\\\]*+)*+\'}s', 'null', $contents);
148  // strip leading non-php code if needed
149  if (substr($contents, 0, 2) !== '<?') {
150  $contents = preg_replace('{^.+?<\?}s', '<?', $contents, 1, $replacements);
151  if ($replacements === 0) {
152  return [];
153  }
154  }
155  // strip non-php blocks in the file
156  $contents = preg_replace('{\?>.+<\?}s', '?><?', $contents);
157  // strip trailing non-php code if needed
158  $pos = strrpos($contents, '?>');
159  if (false !== $pos && false === strpos(substr($contents, $pos), '<?')) {
160  $contents = substr($contents, 0, $pos);
161  }
162 
163  preg_match_all('{
164  (?:
165  \b(?<![\$:>])(?P<type>class|interface' . $extraTypes . ') \s++ (?P<name>[a-zA-Z_\x7f-\xff:][a-zA-Z0-9_\x7f-\xff:\-]*+)
166  | \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*+ [\{;]
167  )
168  }ix', $contents, $matches);
169 
170  $classes = [];
171  $namespace = '';
172 
173  for ($i = 0, $len = count($matches['type']); $i < $len; $i++) {
174  if (!empty($matches['ns'][$i])) {
175  $namespace = str_replace([' ', "\t", "\r", "\n"], '', $matches['nsname'][$i]) . '\\';
176  } else {
177  $name = $matches['name'][$i];
178  if ($name[0] === ':') {
179  // This is an XHP class, https://github.com/facebook/xhp
180  $name = 'xhp' . substr(str_replace(['-', ':'], ['_', '__'], $name), 1);
181  } elseif ($matches['type'][$i] === 'enum') {
182  // In Hack, something like:
183  // enum Foo: int { HERP = '123'; }
184  // The regex above captures the colon, which isn't part of
185  // the class name.
186  $name = rtrim($name, ':');
187  }
188  $classes[] = ltrim($namespace . $name, '\\');
189  }
190  }
191 
192  return $classes;
193  }
194 }
static createMap($path, $blacklist=null, IOInterface $io=null, $namespace=null)