Magento Composer Autoloader
Complete and simple to install Composer autoloader or Magento, (*1)
Why Does This Even Exist?
- Other Composer autoloader integrations aren't complete. The most common issues with other implementations:
- They allow both Magento and Composer to register autoloaders. This causes problems when using libraries that expect
class_exists
to work properlyâMagento's will always attempt to include the file.
- They allow Magento's autoload to register itself, then manually de-register it. This is wholly unnecessary overhead.
- They require manual configuration of
composer.json
for Magento autoloading.
- With more and more need to integrate namespaced systems with Magento, a
require
-able, redistributable, easy to set up module is needed
Installation
Until it's listed in packagist or firegento, add the repository to your composer.json
:, (*2)
{
"type": "vcs",
"url": "https://github.com/mattwellss/magento-composer-autoloader"
}
Add the library to your require
section:, (*3)
"require": {
"mattwellss/magento-composer-autoloader": "~1.0"
}
Configuration
Post install, the new Varien_Autoload
class needs to know where vendor/autoload.php
is hiding. This must be configured., (*4)
Defining the vendor root directory
Regardless of how the vendor dir is defined, it must not end with a trailing slash., (*5)
The two methods of describing the vendor root directory are:
- VENDOR_ROOT
constant. Must be defined in all entry points.
- MAGE_VENDOR_ROOT
env variable. Be sure it's defined for all PHP environments (cli and fpm/mod_php)., (*6)
Optimizing the Autoloader
Normally, composer dump -o
is all one does to optimize a composer autoloader. However, running php shell/classmap_generator.php
when deploying new code to production is recommended. The script generates a classmap file to be used by composer to speed autoloading of files in Magento's code pools., (*7)
To enable use of the generated classmap, include define('OPTIMIZED_COMPOSER', true)
in includes/config.php
., (*8)