mscherer c39903648b Doc block return type array specification. 7 years ago
..
Decorator 7a85883e43 Simplify returns 8 years ago
Event.php 8a98ec8ed7 add missing deprecation into Event::data() 7 years ago
EventDispatcherInterface.php 020915ecaf Fix incorrect doc block 8 years ago
EventDispatcherTrait.php 64ef1f0e28 Fix phpstan reported doc block and casting issues. 7 years ago
EventInterface.php 1e822f6245 Add missing EventInterface to ease future typehinting using interfaces. 8 years ago
EventList.php 8d4a13a3d1 Use HTTPS for various other URLs 8 years ago
EventListenerInterface.php c39903648b Doc block return type array specification. 7 years ago
EventManager.php c27c01c395 Update docblocks. 8 years ago
EventManagerInterface.php bde6b0afe5 Remove @package from classes 8 years ago
EventManagerTrait.php 3a6bd75832 Use HTTPS for the opensource.org URL 8 years ago
LICENSE.txt c61ab5ee95 Use HTTPS for the cakefoundation.org URL 8 years ago
README.md 4eb9fd139c Apply getEventManager() in src. 9 years ago
composer.json 8400540c75 Add missing core dependencies and bump versions. 8 years ago

README.md

Total Downloads License

CakePHP Event Library

This library emulates several aspects of how events are triggered and managed in popular JavaScript libraries such as jQuery: An event object is dispatched to all listeners. The event object holds information about the event, and provides the ability to stop event propagation at any point. Listeners can register themselves or can delegate this task to other objects and have the chance to alter the state and the event itself for the rest of the callbacks.

Usage

Listeners need to be registered into a manager and events can then be triggered so that listeners can be informed of the action.

use Cake\Event\Event;
use Cake\Event\EventDispatcherTrait;

class Orders
{

	use EventDispatcherTrait;

	public function placeOrder($order)
	{
		$this->doStuff();
		$event = new Event('Orders.afterPlace', $this, [
			'order' => $order
		]);
		$this->getEventManager()->dispatch($event);
	}
}

$orders = new Orders();
$orders->getEventManager()->on(function ($event) {
	// Do something after the order was placed
	...
}, 'Orders.afterPlace');

$orders->placeOrder($order);

The above code allows you to easily notify the other parts of the application that an order has been created. You can then do tasks like send email notifications, update stock, log relevant statistics and other tasks in separate objects that focus on those concerns.

Documentation

Please make sure you check the official documentation