Events component of AttwFramework.
Tests ##Composer ###Download
{
"require": {
"attwframework/event": "dev-master"
}
}##How to use
###Creating an event
First, create an instance of Attw\Event\EventManager:
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
$eventManager->listen('after_login', function ($event) {
$params = $event->getParams();
$username = $params['username'];
echo 'Welcome ' . $username;
});or a class that implements Attw\Event\EventListenerInterface:
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;
}
}$eventManager->listen('after_login', 'You\Namespace\Event\Listener\UserListener.afterLogin');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
use Attw\Event\Event;
$event = new Event();
$event->setParams(array('username' => $user->getName()));
$eventManager->trigger('after_login', $event);###Custom events Example
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.
$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).
Removing by name
$eventManager->unlisten('some_name');Removing by listener (remove this listeners of all events)
$eventManager->unlisten(null, 'You\Namespace\Event\Listener\YourListener.methodName');Removing by name and listener (remove listener of an event)
$eventManager->unlisten('some_name', 'You\Namespace\Event\Listener\YourListener.methodName');

