Event handling with Titon\Event

Event-driven programming is a popular paradigm that is used in nearly every application. It allows for the manipulation or alteration of an applications process flow. Titon provides a very light-weight and robust event system through the aptly named Event package, which is based off of the observer pattern and requires PHP 5.3. Using the package however, is rather straight forward.

Registering observers

Before an event can be emitted (or dispatched depending on your background), an observer must be registered. In Titon, there are 2 kinds of observers: callbacks which utilize closures or callable references, or listeners which are instances of a class object. Two different approaches that generate the same result in the end. The register() and registerListener() methods can be used to register observers.

The register() method accepts 3 arguments: $event, $callback, and an array of $options (optional). The name of the event is usually a unique dot notated string that represents its purpose. The callback can either by an instance of a closure, an array that contains an object instance and a method name, or the name of a global function. Read up on the callable type for more examples.

use Titon\Event\Event;
use Titon\Event\Emitter;
// Create an emitter instance
$event = new Emitter();
// Register a closure
$event->register('app.beforeDispatch', function(Event $event) {
	// Execute some logic
});
// Or register a callback
$event->register('app.afterDispatch', [$object, 'methodName']);

The third argument accepts an array of options or an integer. Passing an integer is a shortcut for setting the event priority level. The following options are available.

  • priority (int:100) - The order in which observers will be emitted in ascending order
  • overwrite (bool:false) - Overwrite an observer with the same priority level, else shift down
  • once (bool:false) - Emit the observer once and then remove itself from the list
  • exit (bool:false) - Automatically stop the emit process if an observer returns a falsey value
$event->register('app.run', [$foo, 'methodName'], 50); // Register with a priority of 50
$event->register('app.run', [$bar, 'methodName'], 50); // Will shift to 51 since overwrite is false
// Supply multiple options
$event->register('app.run', [$baz, 'methodName'], [
	'priority' => 50,
	'overwrite' => true,
	'once' => true,
	'exit' => true
]);

Registering listeners is slightly different...

Handling listeners

Listeners are class instances that implement the Titon\Event\Listener interface, which in turn registers multiple observers for events when the class instance itself is registered. The interface provides a single method, registerEvents(), which must return a mapping of event to callbacks.

When returning an array, the array key must be the name of an event, and the value should be an array of options or a method name. The available options can be found above for register(). Each event can support a single callback, or multiple callbacks. The following example demonstrates all the possible combinations.

use Titon\Event\Event;
use Titon\Event\Listener;
class Foo implements Listener {
	public function registerEvents() {
		return [
			// Register a single method with no options
			'app.beforeDispatch' => 'beforeDispatch',
			// Register a single method with options
			'app.afterDispatch' => ['method' => 'afterDispatch', 'priority' => 30],
			// Register multiple methods with and without options
			'app.run' => [
				'run',
				['method' => 'runOnce', 'once' => true, 'exit' => true]
			]
		];
	}
	// Define the methods being registered.
	public function beforeDispatch(Event $event) {}
	public function afterDispatch(Event $event) {}
	public function run(Event $event) {}
	public function runOnce(Event $event) {}
}

Once the interface has been implemented, the class object can be registered in the Emitter.

$event->registerListener($listener);
Removing observers

Removing a registered observer can be quite tricky, as the original callback must also be used to remove it. When removing a closure, the same instance closure must be used. When removing a callable, the same reference must be used. When removing a listener, the same class instance must be used. The following example demonstrates this.

$closure = function(Event $e) { };
$event->register('event.foo', $closure);
$event->remove('event.foo', $closure); // Same reference
$event->register('event.foo', [$object, 'method']);
$event->remove('event.foo', [$object, 'method']);
$listener = new Foo();
$event->registerListener($listener);
$event->removeListener($listener); // Same reference
Convenience methods

Similar to popular JavaScript frameworks, a few convenience methods exist for observer registering and removing, they are on(), off(), and once(). The on and off methods accept either a callable or a listener, allowing for easier integration. The argument list for these methods is exactly the same as their counterparts.

// Register
$event->on('event.foo', [$object, 'method']);
$event->on('event.foo', $listener);
// Register once
$event->once('event.foo', function(Event $e) {});
// Remove
$event->off('event.foo', [$object, 'method']);
$event->off('event.foo', $listener);
Emitting events

Once observers have been registered, an event can be emitted to loop through and execute its observers. This can be done through the emit() method. The response when emitting will either be a Titon\Event\Event object, or an array of objects, depending on the event scope.

$response = $event->emit('event.foo');

An array of parameters can be supplied as a second argument to emit(), which will be passed on as arguments to the observers. Parameters can also be passed by reference allowing observers to modify data.

$response = $event->emit('event.foo', [&$data, $flag]);
// Available as arguments in the observer callback
function(Event $event, array &$data, $flag) {
	$data['foo'] = 'bar';
}

Multiple events can also be emitted when separating names with a space.

$responses = $event->emit('event.foo event.bar');

Or multiple events can be emitted by using a wildcard. This will resolve all event names that fall under that path.

$responses = $event->emit('event.*');
The Event

When an event is emitted, a Titon\Event\Event object is created, passed through all the observers, and finally returned as a response. The object represents the current state of the event and provides functionality for managing that state — the most popular being the stopping of propagation.

When the propagation is stopped, the event will exit out of the observer loop and cease processing. This can be done by calling stop() on the event object within an observer.

function (Event $event) {
	$event->stop();
}

Communication between observers can also be quite complicated (or never implemented). Titon provides a way of persisting data through the event object using setData() and getData(). This allows for an observer to set data at the beginning of the loop, which in turn can be used by another observer at the end of a loop.

function (Event $event) {
	$event->setData(['foo' => 'bar']);
}
function (Event $event) {
	$data = $event->getData();
}

Data can also be set by returning a value from the observer.

function (Event $event) {
	return ['foo' => 'bar'];
}

There are also times when the priority order of the loop is uncertain. The getCallstack() method can be called to return an array of all observers in order of priority. The getIndex() method returns that position in the loop (not the priority level), and the getTime() method returns the timestamp of when the event started.

Emittable trait

As mentioned above, the package itself requires PHP 5.3. However, there is a PHP 5.4 trait available, Titon\Event\Traits\Emittable, that provides an event managed layer within a class. This allows events to be registered and emitted in context to that class; very useful for classes that require callback hooks. The trait provides the following methods: getEmitter(), setEmitter(), on(), off(), and emit().

use Titon\Event\Traits\Emittable;
class Foo {
	use Emittable;
	public function doSomething() {
		$this->emit('foo.do');
	}
}
$foo = new Foo();
$foo->on('foo.do', [$object, 'method']);
$foo->doSomething();
In closing

The Titon\Event package is very powerful and provides additional functionality that other event dispatching libraries do not provide. Some of these features include:

  • Multiple event resolving
  • Wildcard event resolving
  • Event data persistence through the object or observer return
  • Automatic propagation stopping through the exit option
  • Observer once execution through the once option
  • Observer call stack generation
  • Observer priority overwriting
  • Class layer event management through Emittable trait
  • And many more awesome features

So give it a whirl and let me know what you think!

Titon

For the past few years I've been working on a project called Titon — a PHP framework and a front-end library built on MooTools. Titon refers to anything back-end related as the "Framework", while anything front-end related as the "Toolkit". The Framework isn't full-stack in the modern sense, it's simply a collection of packages that can be installed through Composer. The Toolkit is a collection of modular UI classes and helpers written in HTML5, CSS3, Sass and MooTools 1.4.

A brief history

I started writing Titon back in September 2009, around the same time PHP 5.3 was released. The idea behind Titon was simply a learning lesson. It gave me a chance to learn namespaces, closures and all the new PHP functionality. It also permitted me to write systems I would never really write, like database abstraction layers, object relational mappers, caching and storage engines, internationalization and localization, routing, so on and so forth. It was also a great learning lesson on how to properly use interfaces and abstraction classes, while also practicing design patterns and improving with design principles like DRY, KISS, YAGNI, and many others.

Over the course of 3 years, the Titon codebase evolved into a fully fledged framework (albeit not 100% runnable). The architecture was rewritten about 3-4 times as I would try new, different and unique approaches. Classes and interfaces came and went. Functionality was stripped (YAGNI) or dumbed down (KISS). Features were completed or backlogged. Roadmaps were created and abruptly forgotten about. Writing a full-stack framework took time and eventually I stopped working on it many months at a time.

It was around 2012 when I jumped back onto Titon. With the recent release of PHP 5.4 and the ever increasing usage of Composer, it felt like the perfect time. Instead of building a full-stack framework (because do we really need another framework) like before, I decided to break it down into multiple smaller packages that can be installed through the Composer. This would encourage usability, promote decoupling and not require a developer to use a heavy framework for certain functionality. Over the course of the next year, I broke it down into 15 primary packages and 4 supporting packages.

Why am I releasing it?

Even though it was a learning lesson, these packages could still be beneficial to someone (or even you), hence the release. I don't expect Titon to compete with Symfony, CakePHP, Zend, or the many other frameworks, nor do I care. If a packages solves a problem you have, then perfect, go ahead and use it! If a package or Titon as a whole is never used, I won't mind either way. In the end, it taught me everything I currently know, and I couldn't be happier.

What are the Framework packages?

Like previously mentioned, Titon is decoupled into 15 primary packages and 4 supporting packages. A primary package is a collection of classes that provide functionality for a specific feature, for example, a model layer, route mapper, or caching engine. A supporting package is an optional dependency for primary packages that include additional functionality, for example, the MySQL driver for the Model package.

The following packages are available, fully functional and tested. They are split between PHP 5.3 and 5.4, as there was no reason to limit simplistic packages to 5.4 only.

PHP 5.3
  • Debug - Provides logging, debugging and advanced error and exception handling
  • Environment - Allows for multiple environment configuration and bootstrapping
  • Event - Notifies a list of objects during an event and allows for listener registration
  • Type - Implements class like functionality for arrays, strings, objects and enums
  • Utility - Provides convenience classes for basic tasks like validation, formatting, sanitization and more
PHP 5.4
  • Cache - Provides a data caching layer that supports multiple storage engines
  • Common - Provides global functionality like class traits and augmentation as well as dependency and configuration management
  • Controller - Provides controllers that handle the HTTP dispatch cycle and are loosely coupled with other packages
  • G11n - Implements localization and internationalization through message and locale bundles
  • HTTP - Provides classes for interacting with sessions, cookies and the request and response
  • IO - Permits file system manipulation through readers, writers and resource bundles
  • Model - Implements a lightweight database abstraction layer and an object relational mapper
    • MySQL - Enables the MySQL database driver
    • PostgreSQL - Enables the PostgreSQL database driver
    • SQLite - Enables the SQLite database driver
    • MongoDB - Enables the MongoDB database driver
  • MVC - Bundles the Model, View and Controller packages while adding support for dispatching and modularization
  • Route - Provides dynamic route handling, matching and mapping
  • View - Allows for template look-up and rendering through engines and helpers
What about the Toolkit?

The Toolkit is a collection of JavaScript classes and CSS modules known as components. Each component provides front-end functionality, like modals, tooltips and buttons. But doesn't this compete with Twitter bootstrap? In a sense yes, however, there are some major differences. The first being that Toolkit uses MooTools instead of jQuery (if you're one of the rare developers like me who don't like jQuery). The second being that Toolkit doesn't provide default styles like Bootstrap, it merely provides the functionality. This allows the developer – you – to integrate and customize the components at will.

The following components are currently available. Each component is either purely CSS or a combination of CSS and JavaScript.

JavaScript
  • Accordion - Provides collapsible support to a list of sections
  • Blackout - Displays a transparent black element over the document
  • Cache - Provides local and session storage within the class layer
  • Flyout - Displays nested flyout menus that appear below an element that activates it
  • LazyLoad - Provides an easy way to lazy-load images (inline and background) while scrolling
  • Modal - Displays dynamic modals that will display above the content
  • Pin - Pin an element in a container that stays within the viewport while scrolling
  • Popover - Displays dynamic notification elements over an element
  • Tabs - Provides tabbed support to an element containing navigation tabs and sections
  • Timers - Provides timer and interval management within the class layer
  • Tooltip - Displays dynamic tooltips over an element or the mouse cursor
  • TypeAhead - Displays a list of possible options below an input while typing
CSS
  • Animations - CSS3 transitions for specific animations: fade, slide, etc
  • Alert - Styles for block level notification messages
  • Button - Styles for generic cross-browser compatible buttons
  • ButtonGroup - Allows for the grouping of multiple buttons into 1 visual styled button
  • Icon - Allows for inline image sprites to be used at 12, 16, 24, 32, and 64 sizes
  • Label - Styles for inline tag labels
  • Badge - Styles for inline notification bubbles
  • Pagination - Styles for pagination lists
  • Visual - Provides visual aesthetics like gloss and glare over elements
What about a full application?

Nothing is stopping you from using Titon as a full application — hell, even the MVC package was built for this purpose. Since Titon is a collection of packages, there's no set guidelines on how to build an application, it's all up to how you want to piece it together. However, to make things easier, I've thrown together a skeleton application that you may use as a base.

What does the future hold?

In regards to the Framework, I have no plans to add anymore primary packages. I do have plans to add minor supporting packages, like Smarty and Twig support for the View layer. Any bugs, issues or feature requests I will handle of course. I would also like to increase unit testing and average around 90% code coverage for all packages.

The Toolkit however will receive full support and the inclusion of many more components. I currently use Toolkit on all my personal websites.

In the off chance that Titon becomes popular, I will provide in-depth documentation, a fully functional website, and reverse my decision on new packages.

How can you start using it?

By installing through Composer, or cloning the repository, or downloading the project. Since no official website exists, nor does any documentation, learning how to use each package can be tricky. Each repository contains a docs folder that provides basic documentation on each class in the repo. For any functionality that is not documented, I highly suggested reading the source code and its comments. And if no documentation exists for a specific class, let me know and I will provide one.

Using Toolkit requires some manual intervention as the source files need to be compiled. This allows for custom Toolkit builds (only requiring certain components) to be generated through Grunt. Instructions on how to do this can be found in the repository.

I will also be blogging about each package and how to use it, so expect those in the near future!

My open-source scripts officially released!

So it has taken me nearly a year to get to this point. Over the past year I have been building some very advanced PHP classes and scripts that I wish to release as open-source. The types of scripts range from database handlers, template engines, form validation and cleaning, session management, CakePHP components and behaviors, so on and so forth. For the time being im releasing 3 scripts that I deemed "stable" and "complete". I hope you love them and get as much use out of them as I have. The following 3 scripts are now officially released to the public!

Databasic - A basic wrapper class for the MySQL database engine. Contains methods for connecting to a database, fetching and returning results, building insert and update queries, optimization and more.

Gears - A template engine that loads template files, binds variables to a single file or globally without the need for custom placeholders/variables, allows for parent-child hierarchy and parses the template structure.

Commentia - Commentia is a CakePHP Behavior that automatically runs after a comment is made. Each comment is tested upon a point system to determine and classify it. If a comment has more then 1 point it is automatically approved, if it has 0 points it continues pending, and if it is in the negative point range it is either marked as spam or deleted entirely.