Merge "registration: Allow loading composer's autoloader if it exists"
authorjenkins-bot <jenkins-bot@gerrit.wikimedia.org>
Wed, 30 Dec 2015 00:16:43 +0000 (00:16 +0000)
committerGerrit Code Review <gerrit@wikimedia.org>
Wed, 30 Dec 2015 00:16:43 +0000 (00:16 +0000)
docs/extension.schema.json
includes/registration/ExtensionProcessor.php
includes/registration/ExtensionRegistry.php
includes/registration/Processor.php
tests/phpunit/includes/registration/ExtensionRegistryTest.php

index b635467..8c760cc 100644 (file)
                "ParserTestFiles": {
                        "type": "array",
                        "description": "Parser test suite files to be run by parserTests.php when no specific filename is passed to it"
+               },
+               "load_composer_autoloader": {
+                       "type": "boolean",
+                       "description": "Load the composer autoloader for this extension, if one is present"
                }
        }
 }
index 84e873d..cab889f 100644 (file)
@@ -102,6 +102,7 @@ class ExtensionProcessor implements Processor {
                'ParserTestFiles',
                'AutoloadClasses',
                'manifest_version',
+               'load_composer_autoloader',
        );
 
        /**
@@ -353,4 +354,15 @@ class ExtensionProcessor implements Processor {
                        $array[$name] = $value;
                }
        }
+
+       public function getExtraAutoloaderPaths( $dir, array $info ) {
+               $paths = array();
+               if ( isset( $info['load_composer_autoloader'] ) && $info['load_composer_autoloader'] === true ) {
+                       $path = "$dir/vendor/autoload.php";
+                       if ( file_exists( $path ) ) {
+                               $paths[] = $path;
+                       }
+               }
+               return $paths;
+       }
 }
index 732b4a0..e37e7f5 100644 (file)
@@ -29,7 +29,7 @@ class ExtensionRegistry {
        /**
         * Bump whenever the registration cache needs resetting
         */
-       const CACHE_VERSION = 1;
+       const CACHE_VERSION = 2;
 
        /**
         * Special key that defines the merge strategy
@@ -173,6 +173,7 @@ class ExtensionRegistry {
        public function readFromQueue( array $queue ) {
                global $wgVersion;
                $autoloadClasses = array();
+               $autoloaderPaths = array();
                $processor = new ExtensionProcessor();
                $incompatible = array();
                $coreVersionParser = new CoreVersionChecker( $wgVersion );
@@ -208,6 +209,9 @@ class ExtensionRegistry {
                                        . '.';
                                continue;
                        }
+                       // Get extra paths for later inclusion
+                       $autoloaderPaths = array_merge( $autoloaderPaths,
+                               $processor->getExtraAutoloaderPaths( dirname( $path ), $info ) );
                        // Compatible, read and extract info
                        $processor->extractInfo( $path, $info, $version );
                }
@@ -226,6 +230,7 @@ class ExtensionRegistry {
                }
                $data['globals']['wgExtensionCredits'][self::MERGE_STRATEGY] = 'array_merge_recursive';
                $data['autoload'] = $autoloadClasses;
+               $data['autoloaderPaths'] = $autoloaderPaths;
                return $data;
        }
 
@@ -279,8 +284,11 @@ class ExtensionRegistry {
                        call_user_func( $cb );
                }
 
-               $this->loaded += $info['credits'];
+               foreach ( $info['autoloaderPaths'] as $path ) {
+                       require_once $path;
+               }
 
+               $this->loaded += $info['credits'];
                if ( $info['attributes'] ) {
                        if ( !$this->attributes ) {
                                $this->attributes = $info['attributes'];
index e5669d2..a4100bb 100644 (file)
@@ -40,4 +40,14 @@ interface Processor {
         *              like 'MediaWiki'. Values are a constraint string like "1.26.1".
         */
        public function getRequirements( array $info );
+
+       /**
+        * Get the path for additional autoloaders, e.g. the one of Composer.
+        *
+        * @param string $dir
+        * @param array $info
+        * @return array Containing the paths for autoloader file(s).
+        * @since 1.27
+        */
+       public function getExtraAutoloaderPaths( $dir, array $info );
 }
index 201cbfc..543eb5c 100644 (file)
@@ -25,6 +25,7 @@ class ExtensionRegistryTest extends MediaWikiTestCase {
                        'defines' => array(),
                        'credits' => array(),
                        'attributes' => array(),
+                       'autoloaderPaths' => array()
                );
                $registry = new ExtensionRegistry();
                $class = new ReflectionClass( 'ExtensionRegistry' );