Also support skin.json for updating JSON AutoloadClasses
[lhc/web/wiklou.git] / includes / utils / AutoloadGenerator.php
index 4e65e11..7d63156 100644 (file)
@@ -50,7 +50,7 @@ class AutoloadGenerator {
                if ( !is_array( $flags ) ) {
                        $flags = array( $flags );
                }
-               $this->basepath = realpath( $basepath );
+               $this->basepath = self::normalizePathSeparator( realpath( $basepath ) );
                $this->collector = new ClassCollector;
                if ( in_array( 'local', $flags ) ) {
                        $this->variableName = 'wgAutoloadLocalClasses';
@@ -63,9 +63,10 @@ class AutoloadGenerator {
         *
         * @param string $fqcn FQCN to force the location of
         * @param string $inputPath Full path to the file containing the class
+        * @throws Exception
         */
        public function forceClassPath( $fqcn, $inputPath ) {
-               $path = realpath( $inputPath );
+               $path = self::normalizePathSeparator( realpath( $inputPath ) );
                if ( !$path ) {
                        throw new \Exception( "Invalid path: $inputPath" );
                }
@@ -78,9 +79,14 @@ class AutoloadGenerator {
        }
 
        /**
-        * @var string $inputPath Path to a php file to find classes within
+        * @param string $inputPath Path to a php file to find classes within
+        * @throws Exception
         */
        public function readFile( $inputPath ) {
+               // NOTE: do NOT expand $inputPath using realpath(). It is perfectly
+               // reasonable for LocalSettings.php and similiar files to be symlinks
+               // to files that are outside of $this->basepath.
+               $inputPath = self::normalizePathSeparator( $inputPath );
                $len = strlen( $this->basepath );
                if ( substr( $inputPath, 0, $len ) !== $this->basepath ) {
                        throw new \Exception( "Path is not within basepath: $inputPath" );
@@ -99,7 +105,8 @@ class AutoloadGenerator {
         *  for php files with either .php or .inc extensions
         */
        public function readDir( $dir ) {
-               $it = new RecursiveDirectoryIterator( realpath( $dir ) );
+               $it = new RecursiveDirectoryIterator(
+                       self::normalizePathSeparator( realpath( $dir ) ) );
                $it = new RecursiveIteratorIterator( $it );
 
                foreach ( $it as $path => $file ) {
@@ -112,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
@@ -156,11 +199,11 @@ 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
-
+// @codingStandardsIgnoreFile
 global \${$this->variableName};
 
 \${$this->variableName} {$op} array(
@@ -169,6 +212,42 @@ 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
+        * @return string
+        */
+       protected static function normalizePathSeparator( $path ) {
+               return str_replace( '\\', '/', $path );
        }
 }