phower/events

Events oriented programming for PHP.

1.1.0 2017-04-12 16:49 UTC

This package is auto-updated.

Last update: 2024-11-10 04:58:38 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:

  1. An instance of EventInterface, representing the event triggered;
  2. 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:

  1. Fork latest version into your own repository.
  2. Write your changes or additions and commit them.
  3. Follow PSR-2 coding style standard.
  4. Make sure you have unit tests with full coverage to your changes.
  5. 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.