Composer/autoloading

< Composer
Revision as of 09:37, 19 November 2024 by Admin (talk | contribs) (Created page with "MediaWiki has a full autoloading regimen that follows PSR standards<ref>https://www.mediawiki.org/wiki/Manual:$wgAutoloadClasses</ref><ref>https://www.mediawiki.org/wiki/Manual:$wgAutoloadLocalClasses</ref><ref>https://www.mediawiki.org/wiki/Manual:GenerateLocalAutoload.php</ref><ref>https://phabricator.wikimedia.org/source/mediawiki/browse/master/maintenance/generateLocalAutoload.php</ref><ref>https://www.mediawiki.org/wiki/Manual:AutoLoader.php</ref>. Jeroen DeDauw...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

MediaWiki has a full autoloading regimen that follows PSR standards[1][2][3][4][5]. Jeroen DeDauw wrote a blog post in 2013 entitled "Introduction to Composer for MediaWiki developers"

As a PHP Dependency manager, of course Composer follows these same standards and integrates with the MediaWiki software. MediaWiki delegates some authority to composer for 3rd-party dependency resolution.

In brief:

MediaWiki 1.25+ depends on some external libraries which are managed with Composer. Composer creates an autoloader at vendor/autoload.php, which is included by WebStart.php.[6]

Composer.json best practices for MediaWiki developers

Configuration

  • prepend-autoloader: false. Composer's autoloader is slower than MediaWiki's, and most of the classes being loaded are likely to be found inside MediaWiki, so append the Composer autoloader instead of prepending it. See this ancient 2014 thread for some more details.
  • optimize-autoloader: true. No reason not to optimize.

Dependencies

Use a tag.

If none is available, then use a specific (SemVer) version.

Last resort: use a specific SHA1 commit id.[7]

Optimize

"Composer optimize autoloader" refers to a feature within the Composer dependency manager for PHP that allows you to improve the performance of your application by optimizing how classes are loaded at runtime, essentially speeding up the process of finding and including necessary PHP files when a class is called, by generating a classmap that directly maps class names to file locations, resulting in faster autoloading times, especially in production environments.

Due to the way PSR-0/4 autoloading rules are defined, the Composer autoloader checks the filesystem before resolving a classname conclusively. In production, Composer allows for optimization to convert the PSR-0 and PSR-4 autoloading rules into classmap rules, making autoloading quite a bit faster.[8]

References