Latest release: 2.0.0

Events

Simple events for Nette.

Content

Usage

Register extension

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

extensions:
    events: Contributte\DummyEvents\DI\EventsExtension
1
2

Register events

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

use Contributte\DummyEvents\EventsSubscriber;
use Contributte\DummyEvents\EventsManager;

class TestService implements EventsSubscriber 
{
    /**
     * @param EventsManager $em
     */
    public function onEvents(EventsManager $em) {
        $em->on('order.update', function($state) {
            // Some logic..
        });
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14

Register lazy events

Name tag as event name with prefix event.

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

Or use tag arrays with key name events.

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

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 Contributte\DummyEvents\EventsSubscriber;
use Contributte\DummyEvents\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..
        });
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

Fire events

Inject to your class ultra-simple EventsManager.

use Contributte\DummyEvents\EventsManager;

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

public function save() {
    // Some logic..
    
    // Fire order update events
    $this->em->trigger('order.update', $order->state);
}
1
2
3
4
5
6
7
8
9
10
11