2017 © Pedro Peláez
 

library event-manager

Webiny Event Manager Component

image

webiny/event-manager

Webiny Event Manager Component

  • Friday, September 29, 2017
  • by SvenAlHamad
  • Repository
  • 2 Watchers
  • 0 Stars
  • 8,288 Installations
  • PHP
  • 5 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 23 Versions
  • 9 % Grown

The README.md

Event Manager Component

EventManager allows you to easily manage events throughout your application., (*1)

Install the component

The best way to install the component is using Composer., (*2)

composer require webiny/event-manager

For additional versions of the package, visit the Packagist page., (*3)

Usage

Accessing EventManager can be done in 2 ways. The preferable way is using EventManagerTrait, but you can also access it directly, using EventManager::getInstance(). Let's see a simple example of subscribing to an event called some.event with an instance of YourHandler:, (*4)

class YourClass{
    use EventManagerTrait;

    public function index(){
        $this->eventManager()->listen('some.event')->handler(new YourHandler());
    }
}

You're done! You have just subscribed to an event and the moment some.event is fired, your handler will process it., (*5)

Event handlers

Now let's take a look at YourHandler. An event handler can be any class. EventManager will call handle(Event $event) method on your handler object, by default. You can, however, specify a method you want EventManager to call:, (*6)

class YourHandler{

    public function customHandle(Event $event){
        // Do something with the $event...
    }

}

// Using your custom method
$this->eventManager()->listen('some.event')->handler(new YourHandler())->method('customHandle');

Besides using classes, you can also respond to an event using a callable:, (*7)

// Using callable as event handler

$handler = function(Event $event){
    // Do something with the $event...
};

$this->eventManager()->listen('some.event')->handler($handler);

Firing events

To fire a simple event use the following code:, (*8)

$this->eventManager()->fire('some.event');

You can also pass some data when firing an event, which will be passed to every event listener:, (*9)

$data = [
    'some' => 'data',
    'ip' => '192.168.1.10'
];

$this->eventManager()->fire('some.event', $data);

Any given data that is not an Event object, will be converted to generic Event object and your data will be accessible either by using array keys, or as object properties:, (*10)

class YourHandler{

    public function customHandle(Event $event){
        // Access your data 
        echo $event->some; // 'data'
        echo $event['some'] // 'data'
    }

}

If you want to use custom Event data types, refer to section Custom event classes, (*11)

Firing events using a wildcard

You can also use wildcard to fire multiple events at once. The following code will fire all events starting with event. and pass $data to each one of them:, (*12)

$this->eventManager()->fire('event.*', $data);

Execution priority

EventManager allows you to specify an execution priority using priority() method. Here's an example:, (*13)

// Specify a priority of execution for your event listeners
$this->eventManager()->listen('some.event')->handler(new YourHandler())->method('customHandler')->priority(250);
$this->eventManager()->listen('some.event')->handler(new YourHandler())->method('secondCustomHandler')->priority(400);
$this->eventManager()->listen('some.event')->handler(new YourHandler())->method('thirdCustomHandler');

// Now let's fire an event
$this->eventManager()->fire('some.event');

After firing an event, the event listeners will be ordered by priority in descending order. The higher the priority, the sooner the listener will be executed. In this example, the order of execution will be as follows: secondCustomHandler, customHandler, thirdCustomHandler. Default priority is 101, so thirdCustomHandler is executed last., (*14)

Custom event classes

When firing events, you can also pass your own event classes, that extend generic Event class. For example, you want to fire an event called cms.page_saved and pass the Page object. Of course, you could simply pass an array like ['page' => $pageObject], but for the sake of the example, let's pretend it's more complicated than that:, (*15)

// Create your `PageEvent` class

class PageEvent extends Event{

    private $_page;

    public function __construct(Page $page){
        // Call constructor of parent Event class
        parent::__construct();

        // Set your page object
        $this->_page = $page;
    }

    public function getPage(){
        return $this->_page;
    }

}

// Fire an event

$pageEvent = new PageEvent($pageObject);

$this->eventManager()->fire('cms.page_saved', $pageEvent);

// In your handler, you can now access page object using $event->getPage()

class YourHandler{

    public function customHandle(PageEvent $event){
        $pageObject = $event->getPage();
    }

}

This is a simple example, but it shows the power of creating your own Event classes and add as much functionality to your events as you need., (*16)

Event subscriber

Another cool feature of the EventManager is the ability to subscribe to multiple events at once. You will need to create a subscriber class implementing EventSubscriberInterface:, (*17)

class PageEventSubscriber implements EventSubscriberInterface {
    use EventManagerTrait;

    /**
     * Handle page creation event
     */
    public function onPageCreated($event)
    {
        //
    }

    /**
     * Handle page update
     */
    public function onPageUpdated($event)
    {
        //
    }

    /**
     * Register the listeners for the subscriber.
     */
    public function subscribe()
    {
        $this->eventManager()->listen('cms.page_created')->handler($this)->method('onPageCreated');
        $this->eventManager()->listen('cms.page_updated')->handler($this)->method('onPageUpdated');
    }

}

// Subscriber to multiple events using your new subscriber class
$this->eventManager()->subscribe($subscriber);

There are situations when you need to temporarily disable EventManager. For example, deleting a huge portion of files that are not directly related to the application (local cache files) does not require firing all of related events. In this case use the following methods:, (*18)

// Disabling EventManager
$this->eventManager()->disable();

// Do some work that would fire loads of unnecessary events...

// Enabling EventManager
$this->eventManager()->enable();

Resources

To run unit tests, you need to use the following command:, (*19)

$ cd path/to/Webiny/Component/EventManager/
$ composer.phar install
$ phpunit

The Versions

29/09 2017

dev-master

9999999-dev http://www.webiny.com/

Webiny Event Manager Component

  Sources   Download

MIT

The Requires

 

The Development Requires

by Sven Al Hamad
by Pavel Denisjuk

events event manager

29/09 2017

1.6.x-dev

1.6.9999999.9999999-dev http://www.webiny.com/

Webiny Event Manager Component

  Sources   Download

MIT

The Requires

 

The Development Requires

by Sven Al Hamad
by Pavel Denisjuk

events event manager

29/09 2017

v1.6.0

1.6.0.0 http://www.webiny.com/

Webiny Event Manager Component

  Sources   Download

MIT

The Requires

 

The Development Requires

by Sven Al Hamad
by Pavel Denisjuk

events event manager

29/09 2017

v1.6.1

1.6.1.0 http://www.webiny.com/

Webiny Event Manager Component

  Sources   Download

MIT

The Requires

 

The Development Requires

by Sven Al Hamad
by Pavel Denisjuk

events event manager

08/09 2017

1.5.x-dev

1.5.9999999.9999999-dev http://www.webiny.com/

Webiny Event Manager Component

  Sources   Download

MIT

The Requires

 

The Development Requires

by Sven Al Hamad
by Pavel Denisjuk

events event manager

08/09 2017

v1.5.0

1.5.0.0 http://www.webiny.com/

Webiny Event Manager Component

  Sources   Download

MIT

The Requires

 

The Development Requires

by Sven Al Hamad
by Pavel Denisjuk

events event manager

21/08 2016

1.4.x-dev

1.4.9999999.9999999-dev http://www.webiny.com/

Webiny Event Manager Component

  Sources   Download

MIT

The Requires

 

The Development Requires

by Sven Al Hamad
by Pavel Denisjuk

events event manager

21/08 2016

v1.4.3

1.4.3.0 http://www.webiny.com/

Webiny Event Manager Component

  Sources   Download

MIT

The Requires

 

The Development Requires

by Sven Al Hamad
by Pavel Denisjuk

events event manager

04/02 2016

v1.4.0

1.4.0.0 http://www.webiny.com/

Webiny Event Manager Component

  Sources   Download

MIT

The Requires

 

The Development Requires

by Sven Al Hamad
by Pavel Denisjuk

events event manager

04/02 2016

v1.4.1

1.4.1.0 http://www.webiny.com/

Webiny Event Manager Component

  Sources   Download

MIT

The Requires

 

The Development Requires

by Sven Al Hamad
by Pavel Denisjuk

events event manager

04/02 2016

v1.4.2

1.4.2.0 http://www.webiny.com/

Webiny Event Manager Component

  Sources   Download

MIT

The Requires

 

The Development Requires

by Sven Al Hamad
by Pavel Denisjuk

events event manager

16/03 2015

1.3.x-dev

1.3.9999999.9999999-dev http://www.webiny.com/

Webiny Event Manager Component

  Sources   Download

MIT

The Requires

 

The Development Requires

by Sven Al Hamad
by Pavel Denisjuk

events event manager

16/03 2015

v1.3.0

1.3.0.0 http://www.webiny.com/

Webiny Event Manager Component

  Sources   Download

MIT

The Requires

 

The Development Requires

by Sven Al Hamad
by Pavel Denisjuk

events event manager

05/02 2015

v1.2.2

1.2.2.0 http://www.webiny.com/

Webiny Event Manager Component

  Sources   Download

MIT

The Requires

 

The Development Requires

by Sven Al Hamad
by Pavel Denisjuk

events event manager

05/02 2015

v1.2.3

1.2.3.0 http://www.webiny.com/

Webiny Event Manager Component

  Sources   Download

MIT

The Requires

 

The Development Requires

by Sven Al Hamad
by Pavel Denisjuk

events event manager

04/02 2015

1.2.x-dev

1.2.9999999.9999999-dev http://www.webiny.com/

Webiny Event Manager Component

  Sources   Download

MIT

The Requires

 

The Development Requires

by Sven Al Hamad
by Pavel Denisjuk

events event manager

04/02 2015

v1.2.0

1.2.0.0 http://www.webiny.com/

Webiny Event Manager Component

  Sources   Download

MIT

The Requires

 

The Development Requires

by Sven Al Hamad
by Pavel Denisjuk

events event manager

04/02 2015

v1.2.1

1.2.1.0 http://www.webiny.com/

Webiny Event Manager Component

  Sources   Download

MIT

The Requires

 

The Development Requires

by Sven Al Hamad
by Pavel Denisjuk

events event manager

28/11 2014

v1.1.1

1.1.1.0 http://www.webiny.com/

Webiny Event Manager Component

  Sources   Download

MIT

The Requires

 

The Development Requires

by Sven Al Hamad
by Pavel Denisjuk

events event manager

23/11 2014

1.1.x-dev

1.1.9999999.9999999-dev http://www.webiny.com/

Webiny Event Manager Component

  Sources   Download

MIT

The Requires

 

The Development Requires

by Sven Al Hamad
by Pavel Denisjuk

events event manager

23/11 2014

v1.1.0

1.1.0.0 http://www.webiny.com/

Webiny Event Manager Component

  Sources   Download

MIT

The Requires

 

The Development Requires

by Sven Al Hamad
by Pavel Denisjuk

events event manager

20/09 2014

1.0.x-dev

1.0.9999999.9999999-dev http://www.webiny.com/

Webiny Event Manager Component

  Sources   Download

MIT

The Requires

 

The Development Requires

by Sven Al Hamad
by Pavel Denisjuk

events event manager

20/09 2014

v1.0.0

1.0.0.0 http://www.webiny.com/

Webiny Event Manager Component

  Sources   Download

MIT

The Requires

 

The Development Requires

by Sven Al Hamad
by Pavel Denisjuk

events event manager