Download the PHP package pfaciana/composer-smart-autoloader without Composer
On this page you can find all versions of the php package pfaciana/composer-smart-autoloader. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download pfaciana/composer-smart-autoloader
More information about pfaciana/composer-smart-autoloader
Files in pfaciana/composer-smart-autoloader
Package composer-smart-autoloader
Short Description A PHP autoloader that manages class loading across multiple Composer dependencies, prioritizing the latest compatible versions.
License GPL-2.0-only
Homepage https://renderdev.com/
Informations about the package composer-smart-autoloader
ClassLoader
Overview
ClassLoader is an autoloading solution designed to handle PHP project structures with multiple Composer dependencies. It's useful when multiple composer.json
files are include the same codebase and the same repo is included with different versions (like a WordPress site with multiple plugins that have a composer.json and vendor folder).
Purpose
The primary goal of ClassLoader is to enhance the reliability and consistency of autoloading in complex PHP applications. It addresses the common issue of "first loaded, first served" in traditional autoloading mechanisms, which can lead to missing features or inconsistent behavior when older versions of packages are loaded instead of newer ones.
Usage
To use ClassLoader in your project, you can simply get the instance and run it:
If you're using this in a WordPress plugin, you can run after all the plugins have loaded
If new vendor packages have been added since you last ran it, you can re-run it using the run
method:
For example, you many want to run this ClassLoad right away, but then re-run after all plugins are installed
This will replace the cached class maps with the classes added since the last run.
NOTE:
run()
will automatically run just the first time you instantiate the ClassLoader. If you callgetInstance()
a second time, nothing will happen. If you want to re-run setting the class map, you need to call therun()
method. If you don't want the ClassLoader to automatically executerun()
at all, set the$run
parameter toFALSE
like\Render\Autoload\ClassLoader::getInstance( FALSE );
on instantiation.
Dump Autoloading (Optimize)
To ensure dependencies of dependencies are loaded in the same manner, you can run composer dumpautoload --optimize
for each composer.json
. This will still work without it, but those additional dependencies might fall back to the default Composer Autoload. If using WordPress, you can do this part of a hook that runs on a plugin install (assuming composer is installed on that server).
If you're only concerned with packages you/your team personally created, then you can make sure to run composer dumpautoload --optimize
as part of your build process.
How It Works
- Version Analysis: ClassLoader analyzes the versions of all installed packages across different
composer.json
files in the project. - Weight Calculation: It assigns weights to different versions of the same package, favoring more recent versions.
- Smart Loading: When autoloading classes, it prioritizes loading from the most up-to-date version of a package.
- Conflict Resolution: In case of version conflicts, it attempts to use the latest compatible version, reducing the risk of missing required features.
Key Features and Benefits
- Version Resolution: Attempts to estimate and load the latest version of Composer packages when conflicts arise.
- Prioritized Loading: Implements a weight-based system to prioritize loading of more up-to-date package versions.
- Conflict Resolution: Resolves conflicts when multiple projects include the same package with different versions.
- Improved Reliability: Reduces the chance of missing features by always attempting to load the latest version of a package.
- Consistency: Provides a more consistent environment across different parts of a complex application.
- Flexibility: Adapts to various project structures and composer setups.
- Performance: Optimizes class loading by prioritizing the most relevant and up-to-date sources.