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