2017 © Pedro Peláez
 

library event

Events component of AttwFramework

image

attwframework/event

Events component of AttwFramework

  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

Event

Total Downloads Latest Unstable Version License, (*1)

Events component of AttwFramework., (*2)

Tests, (*3)

Composer

Download

{
    "require": {
        "attwframework/event": "dev-master"
    }
}

How to use

Creating an event

First, create an instance of Attw\Event\EventManager:, (*4)

use Attw\Event\EventManager;

$eventManager = EventManager::getInstance();

After, create a listener. The listener will do the actions of an event. It can be a callable function, (*5)

$eventManager->listen('after_login', function ($event) {
    $params = $event->getParams();
    $username = $params['username'];

    echo 'Welcome ' . $username;
});

or a class that implements Attw\Event\EventListenerInterface:, (*6)

namespace You\Namespace\Event\Listener;

use Attw\Event\EventListenerInterface;

class UserListener implements EventListenerInterface
{
    public function afterLogin(Event $event)
    {
        $params = $event->getParams();
        $username = $params['username'];

        echo 'Welcome ' . $username;
    }
}

```php $eventManager->listen('after_login', 'You\Namespace\Event\Listener\UserListener.afterLogin');, (*7)

> **Note:** Listeners always receive an event as a parameter.

###Throwing an event
Create an instance of ```Attw\Event\Event``` and set the params necessary to listener
```php
use Attw\Event\Event;

$event = new Event();
$event->setParams(array('username' => $user->getName()));

$eventManager->trigger('after_login', $event);

Custom events

Example, (*8)

namespace You\Namespace\Event;

use Attw\Event\Event;

class UserEvent extends Event
{
    private $user;

    public function setUsername($username)
    {
        $this->username = $username;
    }

    public function getUsername()
    {
        return $this->username;
    }
}

Prioritizing an event

Pass as third argument the number of order of prioritizing., (*9)

$eventManager->listen('some_name', 'You\Namespace\Event\Listener\YourListener.methodName1', 2);//The last to be executed
$eventManager->listen('some_name', 'You\Namespace\Event\Listener\YourListener.methodName2', 4);//The first to be executed
$eventManager->listen('some_name', 'You\Namespace\Event\Listener\YourListener.methodName3', 3);//The second to be executed

Removing an event

Use method Attw\Event\EventManager::unlisten($name = null, $listener = null)., (*10)

Removing by name, (*11)

$eventManager->unlisten('some_name');

Removing by listener (remove this listeners of all events), (*12)

$eventManager->unlisten(null, 'You\Namespace\Event\Listener\YourListener.methodName');

Removing by name and listener (remove listener of an event), (*13)

$eventManager->unlisten('some_name', 'You\Namespace\Event\Listener\YourListener.methodName');

The Versions

28/09 2014

dev-master

9999999-dev http://attwframework.github.io

Events component of AttwFramework

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

framework-component