如何将 ezComponents 与 magento 集成

发布于 2024-09-30 02:25:19 字数 760 浏览 6 评论 0原文

在“本机”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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

青巷忧颜 2024-10-07 02:25:19

我的基本方法是创建一个带有

controller_front_init_before

事件观察者的自定义模块。在事件观察器中,您可以根据需要设置自动加载器。 Magento Wiki 上有一篇关于设置事件观察器的文章。 controller_front_init_before 事件是 Magento 中最先触发的非通用事件之一。这就是我们使用它的原因。

我们需要解决的大问题是:Magento 的自动加载器首先在堆栈上,如果它找不到文件(EZComponent 类就是这种情况),它的包含将引发一个错误,从而停止执行。

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

  1. spl_autoload 堆栈中删除 Varien_Autoloader

  2. 注册我们自己的自动加载器(我们将使用 Zend_Autoloader,因为它随 Magento 一起提供,而且您似乎很熟悉它)

  3. Varien_Autoloader 重新添加到堆栈中

由于在 Zend 命名空间中加载类,我们需要做一些额外的调整通常由我们将要删除的自动加载器处理。有关更多详细信息,请参阅注释

//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);      

将上述代码放入观察者方法中,您应该可以开始了。

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

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

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

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

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

controller_front_init_before

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

  1. Remove the Varien_Autoloader from the spl_autoload stack

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

  3. Re-add the Varien_Autoloader to the stack

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.

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');

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');
白芷 2024-10-07 02:25:19

我快速浏览了 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

风蛊 2024-10-07 02:25:19

我刚刚将 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 contains Sailthru_Client class.

I created magentoroot/lib/Sailthru folder then copy sailthru.php into it, then renamed to Client.php making it magentoroot/lib/Sailthru/Client.php. This pattern is auto-loaded by Varien_Autoload class.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文