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
-
“Varien_Autoload” class located at “lib\Varien\Autoload.php”.
-
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 :
Require composer autoloader via
Mage.php
Listen to very first events which is generating by Magento and require composer autoloader via your observer. (more cleaner approach).
Put
Varien_Autoload
inlocal
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 thisNote : 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
File :
app\code\{codePool}\{Namepace}\{Module}\Model\Observer.php
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 constantAUTOLOADER_FILE
.That will register your custom autoloader and hence Magento uses your autoloader prior to the default autoloader.