phower / events
Events oriented programming for PHP.
Requires
- php: ^5.6 || ^7.0
- phower/container: ^1.0
Requires (Dev)
- phpunit/phpunit: ^5.7
- squizlabs/php_codesniffer: ^2.8
- symfony/var-dumper: ^3.2
This package is auto-updated.
Last update: 2024-12-10 05:14:03 UTC
README
Events oriented programming for PHP.
Requirements
Phower Events requires:
Instalation
Add Phower Events to any PHP project using Composer:
composer require phower/events
Usage
This package provides an event-oriented mechanism which allows any PHP application to implement a way for separated parts to communicate with each other by dispatching events and listening to them.
An implementation of PhowerEvents requires an EventHandler class and at least an event listener which must be any valud PHP callable:
use Phower\Events\EventHandlerInterface; use Phower\Events\EventInterface; class MyEventListener { public onDummyEvent(EventInterface $event, EventHandlerInterface $handler) { print_r($event->getName()); print_r($handler->getListeners($event)); } }
Note that any listener method should always expect two arguments:
- An instance of
EventInterface
, representing the event triggered;- An intance of
EventHandlerInterface
, the handler which have triggered that event.
Before being able to trigger the listner above we must to attach it to an handler instance:
use Phower\Events\EventHandler; $handler = new EventHandler(); $handler->addListener('dummy', 'MyEventListener::onDummyEvent'); // later at any point where the handler is available: $handler->trigger('dummy');
Container aware handlers
When a Container is part of an application it may be useful to automatically pass it across the listeners.
For this situation we can switch to an instance of ContainerAwareEventHandlerInterface and make our listeners also instances of ContainerAwareInterface.
Running Tests
Tests are available in a separated namespace and can run with PHPUnit in the command line:
vendor/bin/phpunit
Coding Standards
Phower code is written under PSR-2 coding style standard. To enforce that CodeSniffer tools are also provided and can run as:
vendor/bin/phpcs
Reporting Issues
In case you find issues with this code please open a ticket in Github Issues at https://github.com/phower/container/issues.
Contributors
Open Source is made of contribuition. If you want to contribute to Phower please follow these steps:
- Fork latest version into your own repository.
- Write your changes or additions and commit them.
- Follow PSR-2 coding style standard.
- Make sure you have unit tests with full coverage to your changes.
- Go to Github Pull Requests at https://github.com/phower/container/pulls and create a new request.
Thank you!
Changes and Versioning
All relevant changes on this code are logged in a separated log file.
Version numbers follow recommendations from Semantic Versioning.
License
Phower code is maintained under The MIT License.