Code Monkey home page Code Monkey logo

php-events-bus's Introduction

The Biurad PHP Events Bus

Latest Version Software License Workflow Status Code Maintainability Coverage Status Quality Score Sponsor development of this project

biurad/php-events-bus is an events dispatcher for PHP 7.2+ based on The Symfony EventDispatcher, created by Fabien Potencier. which provides tools that allow your application components to communicate with each other by dispatching events and listening to them.

The Symfony EventDispatcher component implements the Mediator and Observer design patterns to make all these things possible and to make your projects truly extensible. This package adds a touch of PSR-11 container, removes The Symfony Stopwatch component from traceable events, and adds a minor performance gain.

πŸ“¦ Installation & Basic Usage

This project requires PHP 7.2 or higher. The recommended way to install, is via Composer. Simply run:

$ composer require biurad/events-bus

The dispatcher is the central object of the event dispatcher system. In general, a single dispatcher is created, which maintains a registry of listeners. When an event is dispatched via the dispatcher, it notifies all listeners registered with that event. In addition to registering listeners with existing events, you can create and dispatch your own events. This is useful feature by symfony to keep different components of your own system flexible and decoupled:

use Symfony\Component\EventDispatcher\EventDispatcher;

$dispatcher = new EventDispatcher();

The lazy event dispatcher can be used to PSR-11 container autowiring for listeners called on events, you can add The DivineNii PHP Invoker library's class instance DivineNii\Invoker\Invoker, injecting optional callable resolvers and a PSR-11 container complaint instance.

use Biurad\Events\LazyEventDispatcher;

$dispatcher = new LazyEventDispatcher();

To take advantage of an existing event, you need to connect a listener to the dispatcher so that it can be notified when the event is dispatched. A call to the dispatcher's addListener() method associates any valid PHP callable to an event:

use Symfony\Contracts\EventDispatcher\Event;

$listener = new AcmeListener();

$dispatcher->addListener('acme.foo.action', [$listener, 'onFooAction']);

// or

$dispatcher->addListener('acme.foo.action', function (Event $event) {
    // will be executed when the acme.foo.action event is dispatched
});

Once a listener is registered with the dispatcher, it waits until the event is notified. In the above example, when the acme.foo.action event is dispatched, the dispatcher calls the given listener method or closure and passes the Event object as the single argument.

The Traceable EventDispatcher is an event dispatcher that wraps any other event dispatcher and can then be used to determine which event listeners have been called by the dispatcher. Pass the event dispatcher to be wrapped and an instance of the PSR-3 logger to its constructor:

use Biurad\Events\TraceableEventDispatcher;
use Psr\Log\NullLogger;

// the event dispatcher to debug
$dispatcher = ...;

$traceableEventDispatcher = new TraceableEventDispatcher($dispatcher, new NullLogger());

After your application has been processed, you can use the getCalledListeners() method to retrieve an array of event listeners that have been called in your application. Similarly, the getNotCalledListeners() method returns an array of event listeners that have not been called:

// ...

$calledListeners = $traceableEventDispatcher->getCalledListeners();
$notCalledListeners = $traceableEventDispatcher->getNotCalledListeners();

πŸ““ Documentation

To have more of documentation, I advice you check out The Symfony EventDispatcher component documentation provided on Symfony website.

⏫ Upgrading

Information on how to upgrade to newer versions of this library can be found in the UPGRADE.

🏷️ Changelog

SemVer is followed closely. Minor and patch releases should not introduce breaking changes to the codebase; See CHANGELOG for more information on what has changed recently.

Any classes or methods marked @internal are not intended for use outside of this library and are subject to breaking changes at any time, so please avoid using them.

πŸ› οΈ Maintenance & Support

When a new major version is released (1.0, 2.0, etc), the previous one (0.19.x) will receive bug fixes for at least 3 months and security updates for 6 months after that new release comes out.

(This policy may change in the future and exceptions may be made on a case-by-case basis.)

Professional support, including notification of new releases and security updates, is available at Biurad Commits.

πŸ‘·β€β™€οΈ Contributing

To report a security vulnerability, please use the Biurad Security. We will coordinate the fix and eventually commit the solution in this project.

Contributions to this library are welcome, especially ones that:

  • Improve usability or flexibility without compromising our ability to adhere to Mediator and Observer design patterns.
  • Optimize performance
  • Fix issues with adhering to this package.

Please see CONTRIBUTING for additional details.

πŸ§ͺ Testing

$ composer test

This will tests biurad/events-bus will run against PHP 7.2 version or higher.

πŸ‘₯ Credits & Acknowledgements

πŸ™Œ Sponsors

Are you interested in sponsoring development of this project? Reach out and support us on Patreon or see https://biurad.com/sponsor for a list of ways to contribute.

πŸ“„ License

biurad/php-events-bus is licensed under the BSD-3 license. See the LICENSE file for more details.

πŸ›οΈ Governance

This project is primarily maintained by Divine Niiquaye Ibok. Members of the Biurad Lap Leadership Team may occasionally assist with some of these duties.

πŸ—ΊοΈ Who Uses It?

You're free to use this package, but if it makes it to your production environment we highly appreciate you sending us an email or message mentioning this library. We publish all received request's at https://patreons.biurad.com.

Check out the other cool things people are doing with biurad/php-events-bus: https://packagist.org/packages/biurad/events-bus/dependents

php-events-bus's People

Contributors

cyclonecopp avatar dependabot[bot] avatar divineniiquaye avatar renovate-bot avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.