Using Library with Namespaces in Magento – Varien Autoload Tips

autoloadervarien-autoload

Im using eBay SDK for PHP from David Sadler https://github.com/davidtsadler/ebay-sdk-examples. That library request to use Composer's autoloader. Code display information like:

/**
 * The namespaces provided by the SDK.
 */
use \DTS\eBaySDK\Constants;
use \DTS\eBaySDK\Finding\Services;
use \DTS\eBaySDK\Finding\Types;

There are two main files in magento for auto loading

  1. “Varien_Autoload” class located at “lib\Varien\Autoload.php”.

  2. Mage.php file located at ‘app/Mage.php’

anyone know the way I have to modify those code to autoload this library file in magento. its the correct way to load libraries?

EDIT

I have another library that provides an autoload with the following configuration:

<?php

require "library/libraryname.php";

$resourcePath = __DIR__ . DIRECTORY_SEPARATOR . 'library'. DIRECTORY_SEPARATOR . 'Resources';
//$resourcePath = "/library/Resources";

define("LIBRARY_RESOURCE_PATH", $resourcePath);

/**
 * Simple autoloader that follows the PHP Standards Recommendation #0 (PSR-0)
 * @see https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-0.md for more information.
 */
spl_autoload_register(function($className) {
    $className = ltrim($className, '\\');
    $fileName = '';
    $namespace = '';
    if ($lastNsPos = strripos($className, '\\')) {
        $namespace = substr($className, 0, $lastNsPos);
        $className = substr($className, $lastNsPos + 1);
        $fileName = str_replace('\\', DIRECTORY_SEPARATOR, $namespace) . DIRECTORY_SEPARATOR;
    }
    $fileName = __DIR__ . DIRECTORY_SEPARATOR . $fileName . $className . '.php';
    if (file_exists($fileName)) {
        require $fileName;

        return true;
    }

    return false;
});

Do I have to create one for this new library?. any other option?.

Best Answer

You can't do this without a core hack. My recommendation is use composer autoloader along with default autoloader in Magento. You can implement this either by :

  1. Require composer autoloader via Mage.php

  2. Listen to very first events which is generating by Magento and require composer autoloader via your observer. (more cleaner approach).

  3. Put Varien_Autoload in local codepool and put your autoloader logic in that file. (Recommendation from Vinai Kopp)

You have a wonderful answer from Alex from this thread.

EDIT

Solution - 1

If you just want to autoload classes inside a particular namespace (if they are follows PSR-0 standards), then Magento-PSR-0-Autoloader will be the best option. All you need to do is, install this extension and edit your app/etc/local.xml file and put this

<psr0_namespaces>
    <DTS\eBaySDK\Constants />
</psr0_namespaces>

Note : However I have no past experience with this extension. I may be wrong. But play around it. I think it would be sufficient for you.

Solution - 2

However if you really need to go on with your autoloader in your question, Then create a module and observe to the event resource_get_tablename.

File : app\code\{codePool}\{Namepace}\{Module}\etc\config.xml

<config>
    <global>
        <events>
            <resource_get_tablename>
                <observers>
                    <namespace_module>
                        <class>namespace_module/observer</class>
                        <method>addAutoloader</method>
                    </namespace_module>
                </observers>
            </resource_get_tablename>
        </events>
    </global>
</config>

File : app\code\{codePool}\{Namepace}\{Module}\Model\Observer.php

<?php
class Namespace_Module_Model_Observer
{
    const AUTOLOADER_FILE = 'path/to/your/autoloader.php';
    public function addAutoloader()
    {
        require_once self::AUTOLOADER_FILE;
        return $this;
    }
}

Here we are observing to resource_get_tablename event because which is the very first event that is fired by Magento. In our observer we are simply requiring the autoloader file in the question. So dont forget to put the correct file path for the constant AUTOLOADER_FILE.

That will register your custom autoloader and hence Magento uses your autoloader prior to the default autoloader.