Bash script to generate list of events for Magento installation

Magento Logo When developing for Magento, there are two ways to modify code provided by other modules - doing rewrites and observing events. If appropriate event is available for observing, it is preferred to use second method to alter other module's behavior. More precisely it is preferred to observe event of your interest, and when this event occurs, to trigger execution of your own code. But how are you supposed to know the list of events that are available for observing on your Magento installation? If you're interested to find out answer to this question, take the red pill, and I'll show you how deep the rabbit-hole goes.

Observing event is done trough following code in your config.xml:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?xml version="1.0" encoding="UTF-8"?>
 
<config>
    <!-- scope - admin or frontend -->
    <scope>
        <events>
            <some_event_id>
                <your_observer_id>
                    <type>singleton</type>
                    <class>Yournamespace_Yourmodule_Model_Observer</class>
                    <method>yourMethod</method>
                </your_observer_id>
            <some_event_id>
        </events>
    </scope>
<config>

Node of our interest from the previous listing is the one marked with some_event_id. This is the string used to dispatch event and it serves as event's unique identifier. Our event might be dispatched with code much like following snippet:

Mage::dispatchEvent('some_event_id', array('some_param' => $param));

Idea of event driven architecture (WordPress hooks and filters anyone?) is to initiate yourMethod() function defined as part of class like the one from following code snippet, when event with some_event_id identifier is dispatched:

1
2
3
4
5
6
7
8
9
10
11
<?php
class Yournamespace_Yourmodule_Model_Observer
{
    public function yourMethod($observer)
    {
      $event = $observer->getEvent();
      $someParam = $event->getSomeParam();   
 
      return $this;
    }
}

As you can see, our method has access to object referenced by $param in code that dispatched some_event_id event. Very useful huh? Sure, that's why most desktop application GUI toolkits, as well as many web applications (WordPress hooks and filters anyone?) employ this model. The only obstacle is how to obtain the list of events together with parameters available to methods observing these events.

In an effort to explore the Magento event driven architecture and exercise my Bash scripting skills I wrote a little Bash script to generate event lists for default Magento Community Edition code from 1.3.3.0 to 1.8.0.0 version. This script actually acts as a wrapper to GNU grep command with a few useful switches. The Bash script itself is available trough it's repository on GitHub, as well as lists of events for common Magento versions generated using this script. As a handy shortcut here are the links to Magento event lists hosted on GitHub:

Previous listings doesn't represent 100% complete lists, because great deal of Magento event identifiers like _before and _after events are generated dynamically. Also, since your Magento installation probably has at least a few extensions, to get up to date list of events you can use script available in it's GitHub repository. To get usage instruction after obtaining Magento Events List Bash script, you can use following commands:

1
2
3
4
5
# Make script executable - one time only operation
chmod +x  magento-events-list.sh
 
# Get usage instructions
bash magento-events-list.sh -h

Here's output of previous command:

Usage: magento-events-list.sh OPTIONS
 
Generates list of events from Magento source code, and saves result to file
 
OPTIONS:
-h Show this message
-i Magento root directory (required)
-o Output file (optional, "events.txt" inside working directory by default)
 
Examples:
- For Magento installation with absolute path /var/www/magento/ export to "events.txt" file inside /home/$USER/ directory
magento-events-list.sh -i /var/www/magento/ -o /home/$USER/

I really hope you find this code useful because I had much fun creating it. Happy coding!

DevGenii

E-commerce is a breeze with Magento Certified Developer Plus & Zend Certified PHP Engineer nearby. Get in touch!

One thought on “Bash script to generate list of events for Magento installation

Leave a Reply

Your email address will not be published. Required fields are marked *