Merge "Split limit report out of Parser::parse()"
[lhc/web/wiklou.git] / includes / utils / AutoloadGenerator.php
1 <?php
2
3 /**
4 * Accepts a list of files and directories to search for
5 * php files and generates $wgAutoloadLocalClasses or $wgAutoloadClasses
6 * lines for all detected classes. These lines are written out
7 * to an autoload.php file in the projects provided basedir.
8 *
9 * Usage:
10 *
11 * $gen = new AutoloadGenerator( __DIR__ );
12 * $gen->readDir( __DIR__ . '/includes' );
13 * $gen->readFile( __DIR__ . '/foo.php' )
14 * $gen->getAutoload();
15 */
16 class AutoloadGenerator {
17 const FILETYPE_JSON = 'json';
18 const FILETYPE_PHP = 'php';
19
20 /**
21 * @var string Root path of the project being scanned for classes
22 */
23 protected $basepath;
24
25 /**
26 * @var ClassCollector Helper class extracts class names from php files
27 */
28 protected $collector;
29
30 /**
31 * @var array Map of file shortpath to list of FQCN detected within file
32 */
33 protected $classes = [];
34
35 /**
36 * @var string The global variable to write output to
37 */
38 protected $variableName = 'wgAutoloadClasses';
39
40 /**
41 * @var array Map of FQCN to relative path(from self::$basepath)
42 */
43 protected $overrides = [];
44
45 /**
46 * Directories that should be excluded
47 *
48 * @var string[]
49 */
50 protected $excludePaths = [];
51
52 /**
53 * @param string $basepath Root path of the project being scanned for classes
54 * @param array|string $flags
55 *
56 * local - If this flag is set $wgAutoloadLocalClasses will be build instead
57 * of $wgAutoloadClasses
58 */
59 public function __construct( $basepath, $flags = [] ) {
60 if ( !is_array( $flags ) ) {
61 $flags = [ $flags ];
62 }
63 $this->basepath = self::normalizePathSeparator( realpath( $basepath ) );
64 $this->collector = new ClassCollector;
65 if ( in_array( 'local', $flags ) ) {
66 $this->variableName = 'wgAutoloadLocalClasses';
67 }
68 }
69
70 /**
71 * Directories that should be excluded
72 *
73 * @since 1.31
74 * @param string[] $paths
75 */
76 public function setExcludePaths( array $paths ) {
77 $this->excludePaths = $paths;
78 }
79
80 /**
81 * Whether the file should be excluded
82 *
83 * @param string $path File path
84 * @return bool
85 */
86 private function shouldExclude( $path ) {
87 foreach ( $this->excludePaths as $dir ) {
88 if ( strpos( $path, $dir ) === 0 ) {
89 return true;
90 }
91 }
92
93 return false;
94 }
95
96 /**
97 * Force a class to be autoloaded from a specific path, regardless of where
98 * or if it was detected.
99 *
100 * @param string $fqcn FQCN to force the location of
101 * @param string $inputPath Full path to the file containing the class
102 * @throws Exception
103 */
104 public function forceClassPath( $fqcn, $inputPath ) {
105 $path = self::normalizePathSeparator( realpath( $inputPath ) );
106 if ( !$path ) {
107 throw new \Exception( "Invalid path: $inputPath" );
108 }
109 $len = strlen( $this->basepath );
110 if ( substr( $path, 0, $len ) !== $this->basepath ) {
111 throw new \Exception( "Path is not within basepath: $inputPath" );
112 }
113 $shortpath = substr( $path, $len );
114 $this->overrides[$fqcn] = $shortpath;
115 }
116
117 /**
118 * @param string $inputPath Path to a php file to find classes within
119 * @throws Exception
120 */
121 public function readFile( $inputPath ) {
122 // NOTE: do NOT expand $inputPath using realpath(). It is perfectly
123 // reasonable for LocalSettings.php and similiar files to be symlinks
124 // to files that are outside of $this->basepath.
125 $inputPath = self::normalizePathSeparator( $inputPath );
126 $len = strlen( $this->basepath );
127 if ( substr( $inputPath, 0, $len ) !== $this->basepath ) {
128 throw new \Exception( "Path is not within basepath: $inputPath" );
129 }
130 if ( $this->shouldExclude( $inputPath ) ) {
131 return;
132 }
133 $result = $this->collector->getClasses(
134 file_get_contents( $inputPath )
135 );
136 if ( $result ) {
137 $shortpath = substr( $inputPath, $len );
138 $this->classes[$shortpath] = $result;
139 }
140 }
141
142 /**
143 * @param string $dir Path to a directory to recursively search
144 * for php files with either .php or .inc extensions
145 */
146 public function readDir( $dir ) {
147 $it = new RecursiveDirectoryIterator(
148 self::normalizePathSeparator( realpath( $dir ) ) );
149 $it = new RecursiveIteratorIterator( $it );
150
151 foreach ( $it as $path => $file ) {
152 $ext = pathinfo( $path, PATHINFO_EXTENSION );
153 // some older files in mw use .inc
154 if ( $ext === 'php' || $ext === 'inc' ) {
155 $this->readFile( $path );
156 }
157 }
158 }
159
160 /**
161 * Updates the AutoloadClasses field at the given
162 * filename.
163 *
164 * @param string $filename Filename of JSON
165 * extension/skin registration file
166 * @return string Updated Json of the file given as the $filename parameter
167 */
168 protected function generateJsonAutoload( $filename ) {
169 $key = 'AutoloadClasses';
170 $json = FormatJson::decode( file_get_contents( $filename ), true );
171 unset( $json[$key] );
172 // Inverting the key-value pairs so that they become of the
173 // format class-name : path when they get converted into json.
174 foreach ( $this->classes as $path => $contained ) {
175 foreach ( $contained as $fqcn ) {
176 // Using substr to remove the leading '/'
177 $json[$key][$fqcn] = substr( $path, 1 );
178 }
179 }
180 foreach ( $this->overrides as $path => $fqcn ) {
181 // Using substr to remove the leading '/'
182 $json[$key][$fqcn] = substr( $path, 1 );
183 }
184
185 // Sorting the list of autoload classes.
186 ksort( $json[$key] );
187
188 // Return the whole JSON file
189 return FormatJson::encode( $json, "\t", FormatJson::ALL_OK ) . "\n";
190 }
191
192 /**
193 * Generates a PHP file setting up autoload information.
194 *
195 * @param string $commandName Command name to include in comment
196 * @param string $filename of PHP file to put autoload information in.
197 * @return string
198 */
199 protected function generatePHPAutoload( $commandName, $filename ) {
200 // No existing JSON file found; update/generate PHP file
201 $content = [];
202
203 // We need to generate a line each rather than exporting the
204 // full array so __DIR__ can be prepended to all the paths
205 $format = "%s => __DIR__ . %s,";
206 foreach ( $this->classes as $path => $contained ) {
207 $exportedPath = var_export( $path, true );
208 foreach ( $contained as $fqcn ) {
209 $content[$fqcn] = sprintf(
210 $format,
211 var_export( $fqcn, true ),
212 $exportedPath
213 );
214 }
215 }
216
217 foreach ( $this->overrides as $fqcn => $path ) {
218 $content[$fqcn] = sprintf(
219 $format,
220 var_export( $fqcn, true ),
221 var_export( $path, true )
222 );
223 }
224
225 // sort for stable output
226 ksort( $content );
227
228 // extensions using this generator are appending to the existing
229 // autoload.
230 if ( $this->variableName === 'wgAutoloadClasses' ) {
231 $op = '+=';
232 } else {
233 $op = '=';
234 }
235
236 $output = implode( "\n\t", $content );
237 return
238 <<<EOD
239 <?php
240 // This file is generated by $commandName, do not adjust manually
241 // @codingStandardsIgnoreFile
242 global \${$this->variableName};
243
244 \${$this->variableName} {$op} [
245 {$output}
246 ];
247
248 EOD;
249 }
250
251 /**
252 * Returns all known classes as a string, which can be used to put into a target
253 * file (e.g. extension.json, skin.json or autoload.php)
254 *
255 * @param string $commandName Value used in file comment to direct
256 * developers towards the appropriate way to update the autoload.
257 * @return string
258 */
259 public function getAutoload( $commandName = 'AutoloadGenerator' ) {
260 // We need to check whether an extenson.json or skin.json exists or not, and
261 // incase it doesn't, update the autoload.php file.
262
263 $fileinfo = $this->getTargetFileinfo();
264
265 if ( $fileinfo['type'] === self::FILETYPE_JSON ) {
266 return $this->generateJsonAutoload( $fileinfo['filename'] );
267 } else {
268 return $this->generatePHPAutoload( $commandName, $fileinfo['filename'] );
269 }
270 }
271
272 /**
273 * Returns the filename of the extension.json of skin.json, if there's any, or
274 * otherwise the path to the autoload.php file in an array as the "filename"
275 * key and with the type (AutoloadGenerator::FILETYPE_JSON or AutoloadGenerator::FILETYPE_PHP)
276 * of the file as the "type" key.
277 *
278 * @return array
279 */
280 public function getTargetFileinfo() {
281 $fileinfo = [
282 'filename' => $this->basepath . '/autoload.php',
283 'type' => self::FILETYPE_PHP
284 ];
285 if ( file_exists( $this->basepath . '/extension.json' ) ) {
286 $fileinfo = [
287 'filename' => $this->basepath . '/extension.json',
288 'type' => self::FILETYPE_JSON
289 ];
290 } elseif ( file_exists( $this->basepath . '/skin.json' ) ) {
291 $fileinfo = [
292 'filename' => $this->basepath . '/skin.json',
293 'type' => self::FILETYPE_JSON
294 ];
295 }
296
297 return $fileinfo;
298 }
299
300 /**
301 * Ensure that Unix-style path separators ("/") are used in the path.
302 *
303 * @param string $path
304 * @return string
305 */
306 protected static function normalizePathSeparator( $path ) {
307 return str_replace( '\\', '/', $path );
308 }
309
310 /**
311 * Initialize the source files and directories which are used for the MediaWiki default
312 * autoloader in {mw-base-dir}/autoload.php including:
313 * * includes/
314 * * languages/
315 * * maintenance/
316 * * mw-config/
317 * * /*.php
318 */
319 public function initMediaWikiDefault() {
320 foreach ( [ 'includes', 'languages', 'maintenance', 'mw-config' ] as $dir ) {
321 $this->readDir( $this->basepath . '/' . $dir );
322 }
323 foreach ( glob( $this->basepath . '/*.php' ) as $file ) {
324 $this->readFile( $file );
325 }
326 }
327 }
328
329 /**
330 * Reads PHP code and returns the FQCN of every class defined within it.
331 */
332 class ClassCollector {
333
334 /**
335 * @var string Current namespace
336 */
337 protected $namespace = '';
338
339 /**
340 * @var array List of FQCN detected in this pass
341 */
342 protected $classes;
343
344 /**
345 * @var array Token from token_get_all() that started an expect sequence
346 */
347 protected $startToken;
348
349 /**
350 * @var array List of tokens that are members of the current expect sequence
351 */
352 protected $tokens;
353
354 /**
355 * @var array Class alias with target/name fields
356 */
357 protected $alias;
358
359 /**
360 * @param string $code PHP code (including <?php) to detect class names from
361 * @return array List of FQCN detected within the tokens
362 */
363 public function getClasses( $code ) {
364 $this->namespace = '';
365 $this->classes = [];
366 $this->startToken = null;
367 $this->alias = null;
368 $this->tokens = [];
369
370 foreach ( token_get_all( $code ) as $token ) {
371 if ( $this->startToken === null ) {
372 $this->tryBeginExpect( $token );
373 } else {
374 $this->tryEndExpect( $token );
375 }
376 }
377
378 return $this->classes;
379 }
380
381 /**
382 * Determine if $token begins the next expect sequence.
383 *
384 * @param array $token
385 */
386 protected function tryBeginExpect( $token ) {
387 if ( is_string( $token ) ) {
388 return;
389 }
390 // Note: When changing class name discovery logic,
391 // AutoLoaderTest.php may also need to be updated.
392 switch ( $token[0] ) {
393 case T_NAMESPACE:
394 case T_CLASS:
395 case T_INTERFACE:
396 case T_TRAIT:
397 case T_DOUBLE_COLON:
398 $this->startToken = $token;
399 break;
400 case T_STRING:
401 if ( $token[1] === 'class_alias' ) {
402 $this->startToken = $token;
403 $this->alias = [];
404 }
405 }
406 }
407
408 /**
409 * Accepts the next token in an expect sequence
410 *
411 * @param array $token
412 */
413 protected function tryEndExpect( $token ) {
414 switch ( $this->startToken[0] ) {
415 case T_DOUBLE_COLON:
416 // Skip over T_CLASS after T_DOUBLE_COLON because this is something like
417 // "self::static" which accesses the class name. It doens't define a new class.
418 $this->startToken = null;
419 break;
420 case T_NAMESPACE:
421 if ( $token === ';' || $token === '{' ) {
422 $this->namespace = $this->implodeTokens() . '\\';
423 } else {
424 $this->tokens[] = $token;
425 }
426 break;
427
428 case T_STRING:
429 if ( $this->alias !== null ) {
430 // Flow 1 - Two string literals:
431 // - T_STRING class_alias
432 // - '('
433 // - T_CONSTANT_ENCAPSED_STRING 'TargetClass'
434 // - ','
435 // - T_WHITESPACE
436 // - T_CONSTANT_ENCAPSED_STRING 'AliasName'
437 // - ')'
438 // Flow 2 - Use of ::class syntax for first parameter
439 // - T_STRING class_alias
440 // - '('
441 // - T_STRING TargetClass
442 // - T_DOUBLE_COLON ::
443 // - T_CLASS class
444 // - ','
445 // - T_WHITESPACE
446 // - T_CONSTANT_ENCAPSED_STRING 'AliasName'
447 // - ')'
448 if ( $token === '(' ) {
449 // Start of a function call to class_alias()
450 $this->alias = [ 'target' => false, 'name' => false ];
451 } elseif ( $token === ',' ) {
452 // Record that we're past the first parameter
453 if ( $this->alias['target'] === false ) {
454 $this->alias['target'] = true;
455 }
456 } elseif ( is_array( $token ) && $token[0] === T_CONSTANT_ENCAPSED_STRING ) {
457 if ( $this->alias['target'] === true ) {
458 // We already saw a first argument, this must be the second.
459 // Strip quotes from the string literal.
460 $this->alias['name'] = substr( $token[1], 1, -1 );
461 }
462 } elseif ( $token === ')' ) {
463 // End of function call
464 $this->classes[] = $this->alias['name'];
465 $this->alias = null;
466 $this->startToken = null;
467 } elseif ( !is_array( $token ) || (
468 $token[0] !== T_STRING &&
469 $token[0] !== T_DOUBLE_COLON &&
470 $token[0] !== T_CLASS &&
471 $token[0] !== T_WHITESPACE
472 ) ) {
473 // Ignore this call to class_alias() - compat/Timestamp.php
474 $this->alias = null;
475 $this->startToken = null;
476 }
477 }
478 break;
479
480 case T_CLASS:
481 case T_INTERFACE:
482 case T_TRAIT:
483 $this->tokens[] = $token;
484 if ( is_array( $token ) && $token[0] === T_STRING ) {
485 $this->classes[] = $this->namespace . $this->implodeTokens();
486 }
487 }
488 }
489
490 /**
491 * Returns the string representation of the tokens within the
492 * current expect sequence and resets the sequence.
493 *
494 * @return string
495 */
496 protected function implodeTokens() {
497 $content = [];
498 foreach ( $this->tokens as $token ) {
499 $content[] = is_string( $token ) ? $token : $token[1];
500 }
501
502 $this->tokens = [];
503 $this->startToken = null;
504
505 return trim( implode( '', $content ), " \n\t" );
506 }
507 }