/**
* @var array Map of file shortpath to list of FQCN detected within file
*/
- protected $classes = array();
+ protected $classes = [];
/**
* @var string The global variable to write output to
/**
* @var array Map of FQCN to relative path(from self::$basepath)
*/
- protected $overrides = array();
+ protected $overrides = [];
/**
* @param string $basepath Root path of the project being scanned for classes
* local - If this flag is set $wgAutoloadLocalClasses will be build instead
* of $wgAutoloadClasses
*/
- public function __construct( $basepath, $flags = array() ) {
+ public function __construct( $basepath, $flags = [] ) {
if ( !is_array( $flags ) ) {
- $flags = array( $flags );
+ $flags = [ $flags ];
}
$this->basepath = self::normalizePathSeparator( realpath( $basepath ) );
$this->collector = new ClassCollector;
}
/**
- * Write out all known classes to autoload.php in
- * the provided basedir
+ * Updates the AutoloadClasses field at the given
+ * filename.
*
- * @param string $commandName Value used in file comment to direct
- * developers towards the appropriate way to update the autoload.
+ * @param {string} $filename Filename of JSON
+ * extension/skin registration file
*/
- public function generateAutoload( $commandName = 'AutoloadGenerator' ) {
-
- // We need to check whether an extenson.json exists or not, and
- // incase it doesn't, update the autoload.php file.
-
- if ( file_exists( $this->basepath . '/extension.json' ) ) {
- require_once __DIR__ . '/../../includes/json/FormatJson.php';
- $key = 'AutoloadClasses';
- $json = FormatJson::decode( file_get_contents( $this->basepath
- . '/extension.json' ), true );
- unset( $json[$key] );
- // Inverting the key-value pairs so that they become of the
- // format class-name : path when they get converted into json.
- foreach ( $this->classes as $path => $contained ) {
- foreach ( $contained as $fqcn ) {
-
- // Using substr to remove the leading '/'
- $json[$key][$fqcn] = substr( $path, 1 );
- }
- }
- foreach ( $this->overrides as $path => $fqcn ) {
+ protected function generateJsonAutoload( $filename ) {
+ require_once __DIR__ . '/../../includes/json/FormatJson.php';
+ $key = 'AutoloadClasses';
+ $json = FormatJson::decode( file_get_contents( $filename ), true );
+ unset( $json[$key] );
+ // Inverting the key-value pairs so that they become of the
+ // format class-name : path when they get converted into json.
+ foreach ( $this->classes as $path => $contained ) {
+ foreach ( $contained as $fqcn ) {
// Using substr to remove the leading '/'
$json[$key][$fqcn] = substr( $path, 1 );
}
+ }
+ foreach ( $this->overrides as $path => $fqcn ) {
- // Sorting the list of autoload classes.
- ksort( $json[$key] );
+ // Using substr to remove the leading '/'
+ $json[$key][$fqcn] = substr( $path, 1 );
+ }
- // Update extension.json, using constants for the required
- // formatting.
- file_put_contents( $this->basepath . '/extension.json',
- FormatJson::encode( $json, true ) . "\n" );
- } else {
- $content = array();
-
- // We need to generate a line each rather than exporting the
- // full array so __DIR__ can be prepended to all the paths
- $format = "%s => __DIR__ . %s,";
- foreach ( $this->classes as $path => $contained ) {
- $exportedPath = var_export( $path, true );
- foreach ( $contained as $fqcn ) {
- $content[$fqcn] = sprintf(
- $format,
- var_export( $fqcn, true ),
- $exportedPath
- );
- }
- }
+ // 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" );
+ }
- foreach ( $this->overrides as $fqcn => $path ) {
+ /**
+ * Generates a PHP file setting up autoload information.
+ *
+ * @param {string} $commandName Command name to include in comment
+ * @param {string} $filename of PHP file to put autoload information in.
+ */
+ protected function generatePHPAutoload( $commandName, $filename ) {
+ // No existing JSON file found; update/generate PHP file
+ $content = [];
+
+ // We need to generate a line each rather than exporting the
+ // full array so __DIR__ can be prepended to all the paths
+ $format = "%s => __DIR__ . %s,";
+ foreach ( $this->classes as $path => $contained ) {
+ $exportedPath = var_export( $path, true );
+ foreach ( $contained as $fqcn ) {
$content[$fqcn] = sprintf(
$format,
var_export( $fqcn, true ),
- var_export( $path, true )
+ $exportedPath
);
}
+ }
- // sort for stable output
- ksort( $content );
+ foreach ( $this->overrides as $fqcn => $path ) {
+ $content[$fqcn] = sprintf(
+ $format,
+ var_export( $fqcn, true ),
+ var_export( $path, true )
+ );
+ }
- // extensions using this generator are appending to the existing
- // autoload.
- if ( $this->variableName === 'wgAutoloadClasses' ) {
- $op = '+=';
- } else {
- $op = '=';
- }
+ // sort for stable output
+ ksort( $content );
- $output = implode( "\n\t", $content );
- file_put_contents(
- $this->basepath . '/autoload.php',
- <<<EOD
+ // extensions using this generator are appending to the existing
+ // autoload.
+ if ( $this->variableName === 'wgAutoloadClasses' ) {
+ $op = '+=';
+ } else {
+ $op = '=';
+ }
+
+ $output = implode( "\n\t", $content );
+ file_put_contents(
+ $filename,
+ <<<EOD
<?php
// This file is generated by $commandName, do not adjust manually
// @codingStandardsIgnoreFile
global \${$this->variableName};
-\${$this->variableName} {$op} array(
+\${$this->variableName} {$op} [
{$output}
-);
+];
EOD
- );
+ );
+
+ }
+
+ /**
+ * Write out all known classes to autoload.php, extension.json, or skin.json in
+ * the provided basedir
+ *
+ * @param string $commandName Value used in file comment to direct
+ * developers towards the appropriate way to update the autoload.
+ */
+ public function generateAutoload( $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";
+ }
+
+ if ( $jsonFilename !== null ) {
+ $this->generateJsonAutoload( $jsonFilename );
+ } else {
+ $this->generatePHPAutoload( $commandName, $this->basepath . '/autoload.php' );
}
}
/**
*/
public function getClasses( $code ) {
$this->namespace = '';
- $this->classes = array();
+ $this->classes = [];
$this->startToken = null;
- $this->tokens = array();
+ $this->tokens = [];
foreach ( token_get_all( $code ) as $token ) {
if ( $this->startToken === null ) {
case T_NAMESPACE:
case T_CLASS:
case T_INTERFACE:
+ case T_TRAIT:
$this->startToken = $token;
}
}
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();
* @return string
*/
protected function implodeTokens() {
- $content = array();
+ $content = [];
foreach ( $this->tokens as $token ) {
$content[] = is_string( $token ) ? $token : $token[1];
}
- $this->tokens = array();
+ $this->tokens = [];
$this->startToken = null;
return trim( implode( '', $content ), " \n\t" );