且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何将ezComponents与magento集成

更新时间:2023-02-07 16:33:39

我这里的基本方法是为观察者创建自定义模块

My basic approach here would be to create a custom module with an observer for the

controller_front_init_before

事件.在事件观察器中,您可以根据需要设置自动加载器.在设置事件观察者上,有一个Magento Wiki文章. controller_front_init_before事件是在Magento中触发的第一个非通用事件之一.这就是我们使用它的原因.

event. In the event observer, you'd be able to setup your autoloader however you want. There's a Magento Wiki article on Setting up Event Observers. The controller_front_init_before event is one of the first non-generic events to fire in Magento. That's why we're using it.

我们需要解决的最大问题是:Magento的自动加载器首先在堆栈上,并且如果找不到文件(EZComponent类就是这种情况),则其include将引发一个错误,该错误将停止执行.

The big problem we need to solve is this: Magento's autoloader is on the stack first, and if it doesn't find a file (which will be the case with the EZComponent classes), its include will raise a error that will halt execution.

因此,我们在上面的事件观察器中需要做的是

So, what we need to do in our event observer above is to

  1. spl_autoload stack

注册我们自己的自动装带器(我们将使用Zend_Autoloader,因为它随Magento一起提供,您似乎对此很熟悉)

Register our own autoloader (we'll use Zend_Autoloader, since it ships with Magento and you seem familiar with it)

Varien_Autoloader重新添加到堆栈中

由于要在Zend名称空间中加载类通常由我们将要删除的自动加载器来处理,因此我们将需要做一些额外的操作.查看评论以获取更多详细信息

There'll be a little extra jiggery-pokery we'll need to do since loading of the classes in the Zend namespace is normally handled by the autoloader we'll be removing. See comments for more details

//we need to manually include Zend_Loader, or else our zend autoloader
//will try to use it, won't find it, and then try to use Zend_Loader to
//load Zend_Loader
require_once('lib/Zend/Loader.php');


//instantiate a zend autoloader first, since we 
//won't be able to do it in an unautoloader universe
$autoLoader = Zend_Loader_Autoloader::getInstance();        

//get a list of call the registered autoloader callbacks
//and pull out the Varien_Autoload.  It's unlikely there
//are others, but famous last words and all that
$autoloader_callbacks = spl_autoload_functions();
$original_autoload=null;
foreach($autoloader_callbacks as $callback)
{
    if(is_array($callback) && $callback[0] instanceof Varien_Autoload)
    {
        $original_autoload = $callback;
    }
}

//remove the Varien_Autoloader from the stack
spl_autoload_unregister($original_autoload);

//register our autoloader, which gets on the stack first
require_once('library/EZComponents/Base/src/base.php');
$autoLoader->pushAutoloader(array('ezcBase', 'autoload'), 'ezc');           

//lets just make sure we can instantiate an EZ class
#$test = new ezcBaseFile();
#var_dump(get_class($test));

//IMPORANT: add the Varien_Autoloader back to the stack
spl_autoload_register($original_autoload);      

将上面的代码放在观察者方法中,您应该会很好.

Put the above code in an observer method and you should be good to go.

您可以采用的另一种方法(更适合Magento模式)是创建一个实现EZComponent加载程序的自定义模块.

The other approach you could take, one that would fit in more with Magento patterns, would be to create a custom module that implemented an EZComponent loader.

$o = Mypackage_Mymodule_Loader::getModel('ezcBaseFile');

然后,您将在静态getModel方法中实现自动加载器样式的require代码,并在需要ezcBaseFile类时使用它.如果您想在ezcBaseFile基类上调用静态方法,则可能需要加载类而不实例化对象的方法.

You'd then implement autoloader style require code in your static getModel method, and use it whenever you wanted an ezcBaseFile class. You'd probably want methods for loading a class without instantiating an object in case you wanted to call a static method on an ezcBaseFile base class.

$o = Mypackage_Mymodule_Loader::getLoadclass('ezcBaseFile');