Statistics
8458
4
0
152d
Badges
Dependencies

Events

Simple events for Nette.


Build Status Code coverage Downloads total Latest stable HHVM Status

Discussion / Help

Join the chat

Install

$ composer require minetro/events

Prolog

In case you're looking for a complex solution like Symfony\EventDispatcher so there is a great up-to-date adaptation 🎉 contributte\event-dispatcher ❤️ for you.

Usage

Register extension

Register in your config file (e.q. config.neon).

extensions:
    events: Minetro\Events\EventsExtension

Register events

On Container compile - EventsExtension collect all services which implement EventsSubscriber and call their onEvents($em) method.

use Minetro\Events\EventsSubscriber;
use Minetro\Events\EventsManager;

class TestService implements EventsSubscriber 
{
    /**
     * @param EventsManager $em
     */
    public function onEvents(EventsManager $em) {
        $em->on('order.update', function($state) {
            // Some logic..
        });
    }
}

Register lazy events

Name tag as event name with prefix event.

services:
    {class: TestService, tags: [event.order.update]}

Or use tag arrays with key name events.

services:
    {class: TestService, tags: [events: [order.update]]}

This prevents usage of other tags.

If EventsSubscriber register more events and also is lazy registered (by tags in neon). Implemented method onEvents(EventsManager $em) is called only once.

use Minetro\Events\EventsSubscriber;
use Minetro\Events\EventsManager;

class TestSubscriber implements EventsSubscriber 
{
    
    public function onEvents(EventsManager $em) {
        $em->on('order.create', function($state) {
            // Some logic..
        });
        
        $em->on('order.update', function($state) {
            // Some logic..
        });
        
        $em->on('order.delete', function($state) {
            // Some logic..
        });
    }
}

Fire events

Inject to your class ultra-simple EventsManager.

use Minetro\Events\EventsManager;

/** @var EventsManager @inject **/
public $em;

public function save() {
    // Some logic..
    
    // Fire order update events
    $this->em->trigger('order.update', $order->state);
}

No release at this moment. Try to create first one.

Is this addon outdated? Did you find an issue? Please report it.

Componette Componette admin@componette.com