dev-master
9999999-dev http://attwframework.github.ioEvents component of AttwFramework
MIT
The Requires
- php >=5.3.0
framework-component
Events component of AttwFramework
Events component of AttwFramework., (*2)
Tests, (*3)
{ "require": { "attwframework/event": "dev-master" } }
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);
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; } }
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
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');
Events component of AttwFramework
MIT
framework-component