OpenCart Event system. Part 1 of 3 - Event handling.

Sorry for my English! English is not my native language. One of the reasons to create this blog is to improve my English writing. So I will be highly obliged if you will help me with this. If you find a grammar error on this page, please select it with your mouse and press Ctrl+Enter.

As I already wrote before, the most interesting innovation of the OpenCart 2.0 is the Event system. So what is the Event system? 

What is OpenCart Event system?
 
The OpenCart Event system gives you ability to change OpenCart logic from your module without changing OpenCart core files.  Such tools exist, probably in the each serious system: Drupal has hooks, Sypfony has Events. OpenCart extremely needs this tool too, because till recently the only way in OpenCart to change some logic was vQmod  which was the reason of the many conflicts and it was one of the biggest flaws in the OpenCart.
 
Important notice! The Event system only appeared in the OpenCart and it is under development. So it is possible that it will change in future. If you find some changes, please write about them in the comments.
 
Event registration
 
The class which is responsible for the Event is located in the  system/engine/event.php
It is included as usual in the system/startup.php:

require_once(modification(DIR_SYSTEM . 'engine/event.php'));

 
which is included in the index.php:

// Startup
require_once(DIR_SYSTEM . 'startup.php');

 
This class isn't big, it is possible to show it all:  

class Event {
    private $data = array();
 
    public function __construct($registry) {
        $this->registry = $registry;
    }
 
    public function register($key, $action) {
        $this->data[$key][] = $action;
    }
 
    public function unregister($key, $action) {
        unset($this->data[$key]);
    }
 
    public function trigger($key, &$arg = array()) {
        if (isset($this->data[$key])) {
            foreach ($this->data[$key] as $event) {
                $action = new Action($event, $arg);
                $action->execute($this->registry);
            }
        }
    }
}

 
We can see that the Event class has 3 methods:
1. register for Events registration 
2. unregister for deleting Events. This method isn't used now.
3. trigger for starting Event Handlers. The method gets arguments $arg by link, it means that we can change them in the Handler.  One Event can have many handlers. For example, 2 modules can process one Event in queue. 
 
The Events are added in the index.php:

// Event
$event = new Event($registry);
$registry->set('event', $event);
 
$query = $db->query("SELECT * FROM " . DB_PREFIX . "event");
 
foreach ($query->rows as $result) {
    $event->register($result['trigger'], $result['action']);
}

 
First, the Event object is added to the registry then all the Events get from the database and registered.
 
Triggering the Event Handler
 
Every time after this code:

$this->event->trigger('event_name', $args);

 
The handlers for this Event will be triggered.
For example, there is a code in the addOrder method of the catalog/model/checkout/order.php:

$this->event->trigger('pre.order.add', $data);

 
It means that we can create a handler in our module for the 'pre.order.add' Event, in which we can change $data variable as we need.
 
How to add a handler in your module read in the second part of this article.
 

Add new comment

CAPTCHA
Spam protection
Target Image