Merge "Cleaned up JobQueueRedis exceptions"
[lhc/web/wiklou.git] / includes / utils / AutoloadGenerator.php
index 727f485..9cf8cab 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 ) {
@@ -160,7 +167,7 @@ class AutoloadGenerator {
                        <<<EOD
 <?php
 // This file is generated by $commandName, do not adjust manually
-
+// @codingStandardsIgnoreFile
 global \${$this->variableName};
 
 \${$this->variableName} {$op} array(
@@ -170,6 +177,16 @@ global \${$this->variableName};
 EOD
                );
        }
+
+       /**
+        * 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 );
+       }
 }
 
 /**
@@ -227,7 +244,7 @@ class ClassCollector {
                if ( is_string( $token ) ) {
                        return;
                }
-               switch( $token[0] ) {
+               switch ( $token[0] ) {
                case T_NAMESPACE:
                case T_CLASS:
                case T_INTERFACE:
@@ -241,7 +258,7 @@ class ClassCollector {
         * @param array
         */
        protected function tryEndExpect( $token ) {
-               switch( $this->startToken[0] ) {
+               switch ( $this->startToken[0] ) {
                case T_NAMESPACE:
                        if ( $token === ';' || $token === '{' ) {
                                $this->namespace = $this->implodeTokens() . '\\';