* $gen->generateAutoload();
*/
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'] === AutoloadGenerator::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' => AutoloadGenerator::FILETYPE_PHP
+ ];
+ if ( file_exists( $this->basepath . '/extension.json' ) ) {
+ $fileinfo = [
+ 'filename' => $this->basepath . '/extension.json',
+ 'type' => AutoloadGenerator::FILETYPE_JSON
+ ];
+ } elseif ( file_exists( $this->basepath . '/skin.json' ) ) {
+ $fileinfo = [
+ 'filename' => $this->basepath . '/skin.json',
+ 'type' => AutoloadGenerator::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 );
+ }
+ }
}
/**