* $gen = new AutoloadGenerator( __DIR__ );
* $gen->readDir( __DIR__ . '/includes' );
* $gen->readFile( __DIR__ . '/foo.php' )
- * $gen->generateAutoload();
+ * $gen->getAutoload();
*/
class AutoloadGenerator {
+ const FILETYPE_JSON = 'json';
+ const FILETYPE_PHP = 'php';
+
/**
* @var string Root path of the project being scanned for classes
*/
* Updates the AutoloadClasses field at the given
* filename.
*
- * @param {string} $filename Filename of JSON
+ * @param string $filename Filename of JSON
* extension/skin registration file
+ * @return string Updated Json of the file given as the $filename parameter
*/
protected function generateJsonAutoload( $filename ) {
- require_once __DIR__ . '/../../includes/json/FormatJson.php';
$key = 'AutoloadClasses';
$json = FormatJson::decode( file_get_contents( $filename ), true );
unset( $json[$key] );
// Sorting the list of autoload classes.
ksort( $json[$key] );
- // Update file, using constants for the required
- // formatting.
- file_put_contents( $filename,
- FormatJson::encode( $json, true ) . "\n" );
+ // Return the whole JSON file
+ return FormatJson::encode( $json, true ) . "\n";
}
/**
}
$output = implode( "\n\t", $content );
- file_put_contents(
- $filename,
+ return
<<<EOD
<?php
// This file is generated by $commandName, do not adjust manually
{$output}
];
-EOD
- );
+EOD;
}
/**
- * Write out all known classes to autoload.php, extension.json, or skin.json in
- * the provided basedir
+ * Returns all known classes as a string, which can be used to put into a target
+ * file (e.g. extension.json, skin.json or autoload.php)
*
* @param string $commandName Value used in file comment to direct
* developers towards the appropriate way to update the autoload.
+ * @return string
*/
- public function generateAutoload( $commandName = 'AutoloadGenerator' ) {
+ public function getAutoload( $commandName = 'AutoloadGenerator' ) {
// We need to check whether an extenson.json or skin.json exists or not, and
// incase it doesn't, update the autoload.php file.
- $jsonFilename = null;
- if ( file_exists( $this->basepath . "/extension.json" ) ) {
- $jsonFilename = $this->basepath . "/extension.json";
- } elseif ( file_exists( $this->basepath . "/skin.json" ) ) {
- $jsonFilename = $this->basepath . "/skin.json";
- }
+ $fileinfo = $this->getTargetFileinfo();
- if ( $jsonFilename !== null ) {
- $this->generateJsonAutoload( $jsonFilename );
+ if ( $fileinfo['type'] === self::FILETYPE_JSON ) {
+ return $this->generateJsonAutoload( $fileinfo['filename'] );
} else {
- $this->generatePHPAutoload( $commandName, $this->basepath . '/autoload.php' );
+ return $this->generatePHPAutoload( $commandName, $fileinfo['filename'] );
}
}
+
+ /**
+ * Returns the filename of the extension.json of skin.json, if there's any, or
+ * otherwise the path to the autoload.php file in an array as the "filename"
+ * key and with the type (AutoloadGenerator::FILETYPE_JSON or AutoloadGenerator::FILETYPE_PHP)
+ * of the file as the "type" key.
+ *
+ * @return array
+ */
+ public function getTargetFileinfo() {
+ $fileinfo = [
+ 'filename' => $this->basepath . '/autoload.php',
+ 'type' => self::FILETYPE_PHP
+ ];
+ if ( file_exists( $this->basepath . '/extension.json' ) ) {
+ $fileinfo = [
+ 'filename' => $this->basepath . '/extension.json',
+ 'type' => self::FILETYPE_JSON
+ ];
+ } elseif ( file_exists( $this->basepath . '/skin.json' ) ) {
+ $fileinfo = [
+ 'filename' => $this->basepath . '/skin.json',
+ 'type' => self::FILETYPE_JSON
+ ];
+ }
+
+ return $fileinfo;
+ }
+
/**
* Ensure that Unix-style path separators ("/") are used in the path.
*
protected static function normalizePathSeparator( $path ) {
return str_replace( '\\', '/', $path );
}
+
+ /**
+ * Initialize the source files and directories which are used for the MediaWiki default
+ * autoloader in {mw-base-dir}/autoload.php including:
+ * * includes/
+ * * languages/
+ * * maintenance/
+ * * mw-config/
+ * * /*.php
+ */
+ public function initMediaWikiDefault() {
+ foreach ( [ 'includes', 'languages', 'maintenance', 'mw-config' ] as $dir ) {
+ $this->readDir( $this->basepath . '/' . $dir );
+ }
+ foreach ( glob( $this->basepath . '/*.php' ) as $file ) {
+ $this->readFile( $file );
+ }
+ }
+}
+
+/**
+ * Reads PHP code and returns the FQCN of every class defined within it.
+ */
+class ClassCollector {
+
+ /**
+ * @var string Current namespace
+ */
+ protected $namespace = '';
+
+ /**
+ * @var array List of FQCN detected in this pass
+ */
+ protected $classes;
+
+ /**
+ * @var array Token from token_get_all() that started an expect sequence
+ */
+ protected $startToken;
+
+ /**
+ * @var array List of tokens that are members of the current expect sequence
+ */
+ protected $tokens;
+
+ /**
+ * @var string $code PHP code (including <?php) to detect class names from
+ * @return array List of FQCN detected within the tokens
+ */
+ public function getClasses( $code ) {
+ $this->namespace = '';
+ $this->classes = [];
+ $this->startToken = null;
+ $this->tokens = [];
+
+ foreach ( token_get_all( $code ) as $token ) {
+ if ( $this->startToken === null ) {
+ $this->tryBeginExpect( $token );
+ } else {
+ $this->tryEndExpect( $token );
+ }
+ }
+
+ return $this->classes;
+ }
+
+ /**
+ * Determine if $token begins the next expect sequence.
+ *
+ * @param array $token
+ */
+ protected function tryBeginExpect( $token ) {
+ if ( is_string( $token ) ) {
+ return;
+ }
+ switch ( $token[0] ) {
+ case T_NAMESPACE:
+ case T_CLASS:
+ case T_INTERFACE:
+ case T_TRAIT:
+ case T_DOUBLE_COLON:
+ $this->startToken = $token;
+ }
+ }
+
+ /**
+ * Accepts the next token in an expect sequence
+ *
+ * @param array
+ */
+ protected function tryEndExpect( $token ) {
+ switch ( $this->startToken[0] ) {
+ case T_DOUBLE_COLON:
+ // Skip over T_CLASS after T_DOUBLE_COLON because this is something like
+ // "self::static" which accesses the class name. It doens't define a new class.
+ $this->startToken = null;
+ break;
+ case T_NAMESPACE:
+ if ( $token === ';' || $token === '{' ) {
+ $this->namespace = $this->implodeTokens() . '\\';
+ } else {
+ $this->tokens[] = $token;
+ }
+ break;
+
+ case T_CLASS:
+ case T_INTERFACE:
+ case T_TRAIT:
+ $this->tokens[] = $token;
+ if ( is_array( $token ) && $token[0] === T_STRING ) {
+ $this->classes[] = $this->namespace . $this->implodeTokens();
+ }
+ }
+ }
+
+ /**
+ * Returns the string representation of the tokens within the
+ * current expect sequence and resets the sequence.
+ *
+ * @return string
+ */
+ protected function implodeTokens() {
+ $content = [];
+ foreach ( $this->tokens as $token ) {
+ $content[] = is_string( $token ) ? $token : $token[1];
+ }
+
+ $this->tokens = [];
+ $this->startToken = null;
+
+ return trim( implode( '', $content ), " \n\t" );
+ }
}