automattic / jetpack-autoloader
Creates a custom autoloader for a plugin or theme.
Installs: 3 641 748
Dependents: 53
Suggesters: 93
Security: 0
Stars: 40
Watchers: 7
Forks: 8
Type:composer-plugin
Requires
- php: >=7.0
- composer-plugin-api: ^1.1 || ^2.0
Requires (Dev)
- automattic/jetpack-changelogger: ^4.2.8
- composer/composer: ^1.1 || ^2.0
- yoast/phpunit-polyfills: ^1.1.1
- dev-trunk / 4.0.x-dev
- v3.1.3
- v3.1.2
- v3.1.1
- v3.1.0
- v3.0.10
- v3.0.9
- v3.0.8
- v3.0.7
- v3.0.6
- v3.0.5
- v3.0.4
- v3.0.3
- v3.0.2
- v3.0.1
- v3.0.0
- v2.12.0
- v2.11.23
- v2.11.22
- v2.11.21
- v2.11.20
- v2.11.19
- v2.11.18
- v2.11.17
- v2.11.16
- v2.11.15
- v2.11.14
- v2.11.13
- v2.11.12
- v2.11.11
- v2.11.10
- v2.11.9
- v2.11.8
- v2.11.7
- v2.11.6
- v2.11.5
- v2.11.4
- v2.11.3
- v2.11.2
- v2.11.1
- v2.11.0
- v2.10.13
- v2.10.12
- v2.10.11
- v2.10.10
- v2.10.9
- v2.10.8
- v2.10.7
- v2.10.6
- v2.10.5
- v2.10.4
- v2.10.3
- v2.10.2
- 2.10.1
- v2.10.0
- v2.9.1
- v2.9.0
- v2.8.0
- v2.7.1
- v2.7.0
- v2.6.0
- v2.5.0
- v2.4.0
- v2.3.0
- v2.2.0
- v2.1.0
- v2.0.2
- v2.0.1
- v2.0.0
- v2.0.0-beta
- v1.7.0
- v1.6.0
- v1.5.0
- v1.4.1
- v1.4.0
- v1.3.8
- v1.3.7
- v1.3.6
- v1.3.5
- v1.3.4
- v1.3.3
- v1.3.2
- v1.3.1
- v1.3.0
- v1.2.0
- v1.1.0
- v1.0.0
- dev-fix/slack-workflow-branch-detection
- dev-fix/release-branch-typo
- dev-update/generate-branch-plugin
- dev-release-v2.10.0
- dev-release-v2.9.1
- dev-release-v2.9.0
- dev-feature/reorg
- dev-release-v2.8.0
- dev-release-v2.7.0
- dev-release-v2.6.0
- dev-release-v2.5.0
- dev-release-v2.4.0
- dev-release-v2.3.0
- dev-release-v2.2.0
- dev-release-v2.1.0
- dev-release-v2.0.2
- dev-release-v2.0.1
- dev-release-v2.0.0
- dev-release-v2.0.0-beta
- dev-release-v1.7.0
- dev-release-v1.6.0
This package is auto-updated.
Last update: 2024-11-13 17:43:20 UTC
README
This is a custom autoloader generator that uses a classmap to always load the latest version of a class.
The problem this autoloader is trying to solve is conflicts that arise when two or more plugins use the same package, but one of the plugins uses an older version of said package.
This is solved by keeping an in memory map of all the different classes that can be loaded, and updating the map with the path to the latest version of the package for the autoloader to find when we instantiate the class.
It diverges from the default Composer autoloader setup in the following ways:
- It creates
jetpack_autoload_classmap.php
andjetpack_autoload_filemap.php
files in thevendor/composer
directory. - This file includes the version numbers from each package that is used.
- The autoloader will only load the latest version of the package no matter what plugin loads the package. This behavior is guaranteed only when every plugin that uses the package uses this autoloader. If any plugin that requires the package uses a different autoloader, this autoloader may not be able to control which version of the package is loaded.
Usage
In your project's composer.json
, add the following lines:
{ "require": { "automattic/jetpack-autoloader": "^2" } }
Your project must use the default composer vendor directory, vendor
.
After the next update/install, you will have a vendor/autoload_packages.php
file.
Load the file in your plugin via main plugin file.
In the main plugin you will also need to include the files like this.
require_once plugin_dir_path( __FILE__ ) . '/vendor/autoload_packages.php';
Working with Development Versions of Packages
The autoloader will attempt to use the package with the latest semantic version.
During development, you can force the autoloader to use development package versions by setting the JETPACK_AUTOLOAD_DEV
constant to true. When JETPACK_AUTOLOAD_DEV
is true, the autoloader will prefer the following versions over semantic versions:
9999999-dev
- Versions with a
dev-
prefix.
Autoloader Limitations and Caveats
Plugin Updates
When moving a package class file, renaming a package class file, or changing a package class namespace, make sure that the class will not be loaded after a plugin update.
The autoloader builds the in memory classmap as soon as the autoloader is loaded. The package class file paths in the map are not updated after a plugin update. If a plugins's package class files are moved during a plugin update and a moved file is autoloaded after the update, an error will occur.
Moving classes to a different package
Jetpack Autoloader determines the hierarchy of class versions by package version numbers. It can cause problems if a class is moved to a newer package with a lower version number, it will get overshadowed by the old package.
For instance, if your newer version of a class comes from a new package versioned 0.1.0, and the older version comes from a different package with a greater version number 2.0.1, the newer class will not get loaded.
Jetpack Autoloader uses transient cache
This is a caveat to be aware of when dealing with issues. The JP Autoloader uses transients to cache a list of available plugins to speed up the lookup process. This can sometimes mask problems that arise when loading code too early. See the Debugging section for more information on how to detect situations like this.
Debugging
A common cause of confusing errors is when a plugin autoloads classes during the plugin load, before the 'plugins_loaded' hook. If that plugin has an older version of the class, that older version may be loaded before a plugin providing the newer version of the class has a chance to register. Even more confusingly, this will likely be intermittent, only showing up when the autoloader's plugin cache is invalidated. To debug this situation, you can set the JETPACK_AUTOLOAD_DEBUG_EARLY_LOADS
constant to true.
Another common cause of confusing errors is when a plugin registers its own autoloader at a higher priority than the Jetpack Autoloader, and that autoloader would load packages that should be handled by the Jetpack Autoloader. Setting the JETPACK_AUTOLOAD_DEBUG_CONFLICTING_LOADERS
constant to true will check for standard Composer autoloaders with such a conflict.
Autoloading Standards
All new Jetpack package development should use classmap autoloading, which allows the class and file names to comply with the WordPress Coding Standards.
Optimized Autoloader
An optimized autoloader is generated when:
composer install
orcomposer update
is called with-o
or--optimize-autoloader
composer dump-autoload
is called with-o
or--optimize
PSR-4 and PSR-0 namespaces are converted to classmaps.
Unoptimized Autoloader
Supports PSR-4 autoloading. PSR-0 namespaces are converted to classmaps.