Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Replace Magento autoloader with composer based autoloader. Note: you are losing the ability to use the compiler.
<?php
/**
* Classes source autoload
*/
class Varien_Autoload
{
/** @var \Composer\Autoload\ClassLoader */
private static $autoloader;
/** @var self */
private static $instance;
/**
* Singleton pattern implementation
*
* @return Varien_Autoload
*/
static public function instance()
{
if (!self::$instance) {
self::$instance = new Varien_Autoload();
}
return self::$instance;
}
/**
* Register composer autoloader
*/
static public function register()
{
if (!static::$autoloader) {
static::$autoloader = require __DIR__ . '/../../../../vendor/autoload.php';
}
}
/**
* Load class source code
*
* @param string $class
* @return bool
*/
public function autoload($class)
{
return static::$autoloader->loadClass($class);
}
public function registerScope($scope)
{}
public static function getScope()
{}
}
@lstrojny

This comment has been minimized.

Copy link
Owner Author

@lstrojny lstrojny commented Apr 16, 2013

The autoloading part for your composer.json:

    "autoload": {
        "psr-0": {
            "": ["app/code/local", "app/code/community", "app/code/core", "lib"]
        }
    }
@michaelkrieg

This comment has been minimized.

Copy link

@michaelkrieg michaelkrieg commented Apr 16, 2013

benefit?

@LeeSaferite

This comment has been minimized.

Copy link

@LeeSaferite LeeSaferite commented Mar 2, 2015

Being able to have external, non-Magento, libraries brought in via composer.

@elfeffe

This comment has been minimized.

Copy link

@elfeffe elfeffe commented Apr 18, 2019

This is really great!
Thank you
I will just change require for require_once, just in case you have some plugin like Hackaton one, that also load autoloader

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment