diff options
author | Luke Shumaker <lukeshu@sbcglobal.net> | 2016-05-01 15:32:59 -0400 |
---|---|---|
committer | Luke Shumaker <lukeshu@sbcglobal.net> | 2016-05-01 15:32:59 -0400 |
commit | 6dc1997577fab2c366781fd7048144935afa0012 (patch) | |
tree | 8918d28c7ab4342f0738985e37af1dfc42d0e93a /includes/utils/AutoloadGenerator.php | |
parent | 150f94f051128f367bc89f6b7e5f57eb2a69fc62 (diff) | |
parent | fa89acd685cb09cdbe1c64cbb721ec64975bbbc1 (diff) |
Merge commit 'fa89acd'
# Conflicts:
# .gitignore
# extensions/ArchInterWiki.sql
Diffstat (limited to 'includes/utils/AutoloadGenerator.php')
-rw-r--r-- | includes/utils/AutoloadGenerator.php | 74 |
1 files changed, 68 insertions, 6 deletions
diff --git a/includes/utils/AutoloadGenerator.php b/includes/utils/AutoloadGenerator.php index 9cf8cab5..7d631563 100644 --- a/includes/utils/AutoloadGenerator.php +++ b/includes/utils/AutoloadGenerator.php @@ -119,13 +119,49 @@ class AutoloadGenerator { } /** - * 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' ) { + 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 ) { + + // Using substr to remove the leading '/' + $json[$key][$fqcn] = substr( $path, 1 ); + } + + // 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" ); + } + + /** + * 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 = array(); // We need to generate a line each rather than exporting the @@ -163,7 +199,7 @@ class AutoloadGenerator { $output = implode( "\n\t", $content ); file_put_contents( - $this->basepath . '/autoload.php', + $filename, <<<EOD <?php // This file is generated by $commandName, do not adjust manually @@ -176,9 +212,35 @@ global \${$this->variableName}; 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' ); + } + } + /** * Ensure that Unix-style path separators ("/") are used in the path. * * @param string $path |