Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Safely Using Magento's Pre-configuration Events

In the Magento Ecommerce System, there are three events that fire before the system is fully bootstrapped

resource_get_tablename    
core_collection_abstract_load_before
core_collection_abstract_load_after

These events also fire after Magento has bootstrapped.

What's a safe and elegant (and maybe event Mage core team blessed) way to detect when Magento has fully bootstrapped so you may safely use these events?

If you attempt to use certain features in the pre-bootstrapped state, the entire request will 404. The best I've come up with (self-link for context) so far is something like this

class Packagename_Modulename_Model_Observer
{
    public function observerMethod($observer)
    {
        $is_safe = true;
        try
        {
            $store = Mage::app()->getSafeStore();
        }
        catch(Exception $e)
        {
            $is_safe = false;
        }
        if(!$is_safe)
        {
            return;
        }     

        //if we're still here, we could initialize store object
        //and should be well into router initialization
    }

}

but that's a little unwieldy.

like image 527
Alan Storm Avatar asked Sep 27 '12 18:09

Alan Storm


1 Answers

I don't think there is any event tailored for that.

You could add you own and file a pull request / Magento ticket to include a good one.

Until then I think the only way is to use one of the events you found and do some checks on how far Magento is initialized.

Did you try to get Mage::app()->getStores()? This might save you from the Exception catching.

like image 59
Alex Avatar answered Oct 18 '22 14:10

Alex