5. Event Driven

Events in Eden are for executing sub routines not originally part of an application scope. Emailing when a comment as been made on your post or logging when an error has been thrown would be valid implementations for an event driven design. With Eden, you can easily design your applications to be plugin ready. The example below shows a skinned down version of an order processing function.

Figure 1. Processing Orders
function processOrder($email, $price) {
	if($price < 1) {
		return false;
	}
	
	//insert into database
	
	return true;
}

if(processOrder('info@openovate.com', 56.99)) {
	echo 'Success!';
}

When given a set of functional requirements we would think to add it in processOrder() linearly. In event driven design, a function should only perform the main process stated on the function name. processOrder(), for example should just insert the order into the database and nothing more. After it is done, we should trigger that this action has been performed.

Figure 1. Adding a Trigger
function processOrder($email, $price) {
	if($price < 1) {
		return false;
	}
	
	//insert into database
	
	eden('event')->trigger('success', $email, 'Success!', 'We triggered a success.');
	
	return true;
}

if(processOrder('info@openovate.com', 56.99)) {
	echo 'Success!';
}

In Figure 2, we add a trigger passing all possible variables to whatever other method is listening to that. This is the only requirement to make your application event driven. The example below represents how to build a plugin that will listen for a success event to occur.

Figure 1. Listen
class Plugin_Email extends Eden_Class {
	public function __construct() {
		$this->Event()->listen('success', $this, 'send');
	}
	
	public function send($event, $email, $subject, $message) {
		mail($email, $subject, $message);
		return $this;
	}
}

Plugin_Email::i();

In the example above, we created a plugin class that will listen to a success event. When a success event is triggered the send() method will be called passing all the arguments as specified by the trigger.

Figure 1b. PHP 5.3 Listen Anonymously
eden('event')->listen('success', function() {
	mail($email, $subject, $message);
});
Eden_Event, when called will be a singleton. This makes it a global event handler. You can create a separate set of events by simply extending this class.

Our next section 6. Data Types will show you some tricks to manipulating strings and arrays.


© 2012 Openovate Labs. All rights reserved.