[READ ONLY] Autoloader for Jetpack. This repository is a mirror, for issue tracking and development head to: https://github.com/automattic/jetpack
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Go to file
anomiex d3433380b4
Update dependency wikimedia/testing-access-wrapper to v3 (#34492)
4 days ago
.github/workflows autotagger: Don't tag packages with alpha deps (#29908) 8 months ago
src Backport changes for VideoPress 1.7 (#34234) 3 weeks ago
tests/php Remove unnecessary `function_exists()` checks (#34222) 3 weeks ago
.gitattributes Fix changelogger deps (#19081) 3 years ago
.gitignore Autoloader: test suite refactor (#18538) 3 years ago
.phpcs.dir.phpcompatibility.xml Subscriptions modal: enable for Jetpack sites (#33235) 2 months ago
.phpcs.dir.xml Subscriptions modal: enable for Jetpack sites (#33235) 2 months ago
CHANGELOG.md Backport changes for VideoPress 1.7 (#34234) 3 weeks ago
LICENSE.txt All Projects: Handle LICENSE.txt and SECURITY.md automatically. (#19831) 3 years ago
README.md CRM: Port back 5.8.0 release changelog, readme and package updates (#30796) 7 months ago
SECURITY.md Add information about other plugins in the repo (#33576) 2 months ago
composer.json Update dependency wikimedia/testing-access-wrapper to v3 (#34492) 4 days ago
phpunit.xml.dist CI: Run tests with PHP 8.1, experimentally (#21207) 2 years ago

README.md

A custom autoloader for Composer

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 and jetpack_autoload_filemap.php files in the vendor/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.

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 or composer 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.

Autoloader Limitations

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.