Page MenuHomePhabricator

Composer's autoloader should be autoloaded in extension registry (if configured)
Closed, ResolvedPublic

Description

Think about an extension, that requires a library which can be installed using composer (the library is in the require section of composer). Even with extension registration (and the old "php file" based installation method), the extension has to manually make sure, that the composer autoloader (in ./vendor/autoload.php) has been loaded as early as possible (at least earlier as the first usage of one of the required libraries). Usually this is done by one of these ways:

  • load the autoloader in the main php file or a registered extension function (wgExtensionFunction)
		if ( file_exists( __DIR__ . '/vendor/autoload.php' ) {
			require_once __DIR__ . '/vendor/autoload.php';
		}
  • the above code in a callback registered in extension.json

(Example from Elastica)
This is mostly a standard way, maybe copy-paste, thing, which should be easier.

Instead of letting the extension developer handle the inclusion of the composer autoloader, there shoud be a standardised, and very simple way of loading it, e.g. thorugh extension registration. The proposed way would add a new option to the extension.json schema, which, if set, triggers extension registration to check, if an autoloader is present in the extension's vendor file and loads it right after the processing of the extension JSON-data, but before the callback (to allow an extension to use composer libraries in the callback function):

The proposed property of the extension.json schema would be:

		"load_composer_autoloader": {
			"type": "boolean",
			"description": "If set to true, extension registration will try to load composer's autoloader of this extension, if present."
		}

Event Timeline

Paladox raised the priority of this task from to Needs Triage.
Paladox updated the task description. (Show Details)
Paladox subscribed.
Paladox added a subscriber: Legoktm.

Adding @Legoktm since he said I could add him to anything to do with extension registration.

It will also allow other settings to be used before the extension is loaded.

Florian added a project: good first task.
Florian subscribed.

I edited the task description[1] to make it more clearly and added the good first task tag, even if I would love to read the opinion of @Legoktm :) I think this would be a useful addiotion to extension registration.

The task itself (if legoktm is fine with it :]) is easy enough to be marked as easy (I think) and I would propose to make it a GCI task :)

Yeah, I think this is a good idea, +1 from me. As for implementation, I think we should add a method like getExtraAutoloaderPaths to the Processor interface, and if "load_composer_autoloader": true is set, ExtensionProcessor will add "$dir/vendor/autoload.php" to the list. ExtensionRegistry will need to read from the new method, do the file_exists/require_once check for each autoloader, and then add the extra paths into the cache. If that seems reasonable for a GCI task, lets do it.

Florian renamed this task from Support setting autoloading vendor in extension registration to Composer's autoloader should be autoloaded in extension registry (if configured).Dec 6 2015, 1:12 AM

Change 261107 had a related patch set uploaded (by Victorbarbu):
Autoload Composer's autoloader in Extension Registry

https://gerrit.wikimedia.org/r/261107

Change 261107 merged by jenkins-bot:
registration: Allow loading composer's autoloader if it exists

https://gerrit.wikimedia.org/r/261107

Florian assigned this task to Victorbarbu.
Florian removed a project: Patch-For-Review.

Change 261597 had a related patch set uploaded (by Paladox):
registration: Allow loading composer's autoloader if it exists

https://gerrit.wikimedia.org/r/261597

Change 261633 had a related patch set uploaded (by Florianschmidtwelzow):
Maintenance: convertExtensionToRegistration: Detect if composer autoloader is needed

https://gerrit.wikimedia.org/r/261633

Change 261633 merged by jenkins-bot:
convertExtensionToRegistration: Detect if composer autoloader is needed

https://gerrit.wikimedia.org/r/261633

Change 261597 abandoned by Krinkle:
registration: Allow loading composer's autoloader if it exists

Reason:
Extensions have REL1_26 branches that were auto-created before MediaWIki gained the autoload feature. So there shouldn't be any issue. If you install MW 1.26 with the 1.26 version of an extension, all should work well.

https://gerrit.wikimedia.org/r/261597

Reopening since it looks for vendor in mediawiki core not the extension and mediawiki core.

I tested and failed an extension but including

// Include the composer autoloader if it is present.
if ( is_readable( DIR . '/vendor/autoload.php' ) ) {
include_once( DIR . '/vendor/autoload.php' );
}

on top worked.

Failed https://travis-ci.org/JeroenDeDauw/Maps/builds/121273121 I had to include

if ( is_readable( DIR . '/vendor/autoload.php' ) ) {
include_once( DIR . '/vendor/autoload.php' );
}

at the top of the php main file so using extension.json on its own wont work.

The code says something else:
https://github.com/wikimedia/mediawiki/blob/master/includes/registration/ExtensionProcessor.php#L389-L394

		if ( isset( $info['load_composer_autoloader'] ) && $info['load_composer_autoloader'] === true ) {
			$path = "$dir/vendor/autoload.php";
			if ( file_exists( $path ) ) {
				$paths[] = $path;
			}
		}

And I remember at least one extension which uses this feature without any problems. Please open a new task, if you've encounter problems, and properly describe what you do and whaty ou expect.

Yes that works for Mailgun.

@Paladox Did you composer install/update in the extension folder too?

Change 261597 restored by Paladox:
registration: Allow loading composer's autoloader if it exists

https://gerrit.wikimedia.org/r/261597

Change 261597 abandoned by Hashar:
registration: Allow loading composer's autoloader if it exists

Reason:
1.26 still has support but that does not mean adding new features to it. As mentioned by Timo the extensions at REL1_26 should not rely on autoloading since MediaWiki core does not provide it.

https://gerrit.wikimedia.org/r/261597