PHP code example of webiny / event-manager

1. Go to this page and download the library: Download webiny/event-manager library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

webiny / event-manager example snippets


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


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');

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

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

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

class YourHandler{

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

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

// 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');

// 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();
    }
    
}


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);

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

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

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


// Using callable as event handler

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

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