如何将 ezComponents 与 magento 集成
在“本机”Zend Framework 应用程序中,我将通过将 ezComponents 的自动加载器添加到 Zends 自动加载器来启用 ezComponents:
$autoLoader = Zend_Loader_Autoloader::getInstance();
require_once('../library/EZComponents/Base/src/base.php');
$autoLoader->pushAutoloader(array('ezcBase', 'autoload'), 'ezc');
现在,我想知道如何对 Magento 执行相同的操作。 有没有办法扩展 Varien_Autoload (magentos autoloader) 以实现 ezComponents 的轻松集成? 或者: 有没有一种方法可以在 Magento 自动加载器旁边使用 Zends 自动加载器而不互相干扰?
编辑:
嗯,我实现了一个解决方法,通过将以下内容添加到 Varien_Autoload 中的函数 autoload() 中:
if(substr($class, 0, 3) == 'ezc'){
require_once('EZComponents/Base/src/base.php');
return ezcBase::autoload($class);
}
不过,我认为这是一个非常糟糕的黑客,因为在升级 Magento 时它会被覆盖。有人有更好的主意吗?
in a 'native' Zend Framework application I would enable the use of ezComponents by adding the autoloader of ezComponents to Zends autoloader:
$autoLoader = Zend_Loader_Autoloader::getInstance();
require_once('../library/EZComponents/Base/src/base.php');
$autoLoader->pushAutoloader(array('ezcBase', 'autoload'), 'ezc');
Now, I'm wondering how I could do the same with Magento.
Is there a way to extend Varien_Autoload (magentos autoloader) to enable easy integration of ezComponents?
OR:
Is there a way to use Zends autoloader beside the one from Magento without interfering each other?
edit:
Well, I implemented a workaround, by adding the following to function autoload() in Varien_Autoload:
if(substr($class, 0, 3) == 'ezc'){
require_once('EZComponents/Base/src/base.php');
return ezcBase::autoload($class);
}
I consider this as a very bad hack though, because it will be overwritten, when upgrading Magento. Does anyone has a better idea?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我的基本方法是创建一个带有
事件观察者的自定义模块。在事件观察器中,您可以根据需要设置自动加载器。 Magento Wiki 上有一篇关于设置事件观察器的文章。
controller_front_init_before
事件是 Magento 中最先触发的非通用事件之一。这就是我们使用它的原因。我们需要解决的大问题是:Magento 的自动加载器首先在堆栈上,如果它找不到文件(EZComponent 类就是这种情况),它的包含将引发一个错误,从而停止执行。
因此,我们在上面的事件观察器中需要做的是
从
spl_autoload 堆栈中删除
Varien_Autoloader
注册我们自己的自动加载器(我们将使用
Zend_Autoloader
,因为它随 Magento 一起提供,而且您似乎很熟悉它)将
Varien_Autoloader
重新添加到堆栈中由于在
Zend
命名空间中加载类,我们需要做一些额外的调整通常由我们将要删除的自动加载器处理。有关更多详细信息,请参阅注释将上述代码放入观察者方法中,您应该可以开始了。
您可以采取的另一种方法(更适合 Magento 模式)是创建一个实现 EZComponent 加载器的自定义模块。
然后,您可以在静态 getModel 方法中实现自动加载器风格的 require 代码,并在需要 ezcBaseFile 类时使用它。如果您想在
ezcBaseFile
基类上调用静态方法,您可能需要加载类而不实例化对象的方法。My basic approach here would be to create a custom module with an observer for the
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.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
Remove the
Varien_Autoloader
from thespl_autoload stack
Register our own autoloader (we'll use
Zend_Autoloader
, since it ships with Magento and you seem familiar with it)Re-add the
Varien_Autoloader
to the stackThere'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 detailsPut the above code in an observer method and you should be good to go.
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.
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.我快速浏览了 Varien 自动加载器的代码,它似乎使用了对 spl_autoload_register 的调用,其中是一个用于执行自动加载的堆栈。虽然我认为添加到默认的 Magento 自动加载器不会取得多大成功,但这意味着您应该能够在 Magento 的顶部推送另一个自动加载器。
希望有帮助!
谢谢,
乔
I took a quick look at the code for Varien's autoloader and it appears to use a call to spl_autoload_register, which is a stack for performing autoloads. While I don't think you'll have much success adding to the default Magento autoloader, this means that you should be able to push another autoloader on top of Magento's.
Hope that helps!
Thanks,
Joe
我刚刚将 Sailthru_Client 类集成到 Magento 中,认为这可能会有所帮助。
我有
sailthru.php
,Sailthru 客户端 API,其中包含Sailthru_Client
类。我创建了
magentoroot/lib/Sailthru
文件夹,然后将 sailthru.php 复制到其中,然后重命名为Client.php
使其成为magentoroot/lib/Sailthru/Client.php
。该模式由Varien_Autoload
类自动加载。I've just integrated the Sailthru_Client class into Magento, thought this might help.
I have
sailthru.php
, the Sailthru Client API, which containsSailthru_Client
class.I created
magentoroot/lib/Sailthru
folder then copy sailthru.php into it, then renamed toClient.php
making itmagentoroot/lib/Sailthru/Client.php
. This pattern is auto-loaded byVarien_Autoload
class.