From 9dec78d4c02c64f008c0015b1051c04b96aeeb26 Mon Sep 17 00:00:00 2001 From: Florian Date: Wed, 30 Dec 2015 16:11:38 +0100 Subject: [PATCH] convertExtensionToRegistration: Detect if composer autoloader is needed If an extension specifies a require section in its composer.json, which contains some dependencies, we can assume that this extension needs to load the composer autoloader. The maintenance script convertExtensionToRegistration.php will now check if the extension has such a composer.json and, if so, adds the load_composer_autoloader property in extension.json, which will try to load the autoloader of composer is it is present. Also add a check for existence of a require section in ComposerJson library (ComposerJson::getRequiredDependencies()). Bug: T119766 Change-Id: Icdbc37abc44e642afee2aab4c0e9298d3471124d --- includes/libs/composer/ComposerJson.php | 8 +++++--- .../convertExtensionToRegistration.php | 20 +++++++++++++++++++ 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/includes/libs/composer/ComposerJson.php b/includes/libs/composer/ComposerJson.php index 796acb56c7..bb55ac6380 100644 --- a/includes/libs/composer/ComposerJson.php +++ b/includes/libs/composer/ComposerJson.php @@ -27,9 +27,11 @@ class ComposerJson { */ public function getRequiredDependencies() { $deps = array(); - foreach ( $this->contents['require'] as $package => $version ) { - if ( $package !== "php" && strpos( $package, 'ext-' ) !== 0 ) { - $deps[$package] = self::normalizeVersion( $version ); + if ( isset( $this->contents['require'] ) ) { + foreach ( $this->contents['require'] as $package => $version ) { + if ( $package !== "php" && strpos( $package, 'ext-' ) !== 0 ) { + $deps[$package] = self::normalizeVersion( $version ); + } } } diff --git a/maintenance/convertExtensionToRegistration.php b/maintenance/convertExtensionToRegistration.php index 608605c015..2de2e222c1 100644 --- a/maintenance/convertExtensionToRegistration.php +++ b/maintenance/convertExtensionToRegistration.php @@ -116,6 +116,13 @@ class ConvertExtensionToRegistration extends Maintenance { } } + // check, if the extension requires composer libraries + if ( $this->needsComposerAutoloader( dirname( $this->getArg( 0 ) ) ) ) { + // set the load composer autoloader automatically property + $this->output( "Detected composer dependencies, setting 'load_composer_autoloader' to true.\n" ); + $this->json['load_composer_autoloader'] = true; + } + // Move some keys to the top $out = array(); foreach ( $this->promote as $key ) { @@ -246,6 +253,19 @@ class ConvertExtensionToRegistration extends Maintenance { $this->json['ResourceFileModulePaths'] = $defaults; } } + + protected function needsComposerAutoloader( $path ) { + $path .= '/composer.json'; + if ( file_exists( $path ) ) { + // assume, that the composer.json file is in the root of the extension path + $composerJson = new ComposerJson( $path ); + // check, if there are some dependencies in the require section + if ( $composerJson->getRequiredDependencies() ) { + return true; + } + } + return false; + } } $maintClass = 'ConvertExtensionToRegistration'; -- 2.20.1