Giter VIP home page Giter VIP logo

dispatch's Introduction

dispatch() CI Typed codecov

๐Ÿ•Š๏ธ Event dispatcher awareness made simple.

Install

composer require leocavalcante/dispatch:dev-main

Usage

Define a dispatcher

It can be anyone that implements the PSR-14 Event Dispatcher Interface.

use function Dispatch\use_dispatcher;
use_dispatcher(Psr\EventDispatcher\EventDispatcherInterface);

Then just call dispatch()

use function Dispatch\dispatch;
dispatch(new MyEvent());

The idea is exactly to make it able to dispatch events from anywhere in your code avoiding EventDispatcherInterface injection boilerplate.

Example

use League\Event\EventDispatcher;
use function Dispatch\{dispatch, use_dispatcher};

class UpperCased {
    public function __construct(
        public string $result,
    ) {}
}

function to_upper(string $str): string {
    // Do something
    $upper_case_str = strtoupper($str);

    // And dispatch an Event without EventDispatcherInterface injection boilerplate
    dispatch(new UpperCased($upper_case_str));

    return $upper_case_str;
}

class UpperCauser {
    public function toUpper(string $str): string {
        // Do something
        $upper_case_str = strtoupper($str);

        // And dispatch an Event without EventDispatcherInterface injection boilerplate
        dispatch(new UpperCased($upper_case_str));

        return $upper_case_str;
    }
}

$dispatcher = new EventDispatcher();
$dispatcher->subscribeTo(UpperCased::class, static function (UpperCased $event): void {
    echo "Some string was upper cased and it is: {$event->result}\n";
});

use_dispatcher($dispatcher);

to_upper('dispatch');
(new UpperCauser())->toUpper('rocks!');

// Some string was upper cased and it is: DISPATCH
// Some string was upper cased and it is: ROCKS!

Dispatchers

As said, this is only a small Facade on top of EventDispatcherInterface providing namespaced functions to reduce dispatcher injection boilerplate.

You still need a concrete implementation of an Event Dispatcher, anyone that implements the EventDispatcherInterface can be used, but here is a list of a few of them:

FAQ

Does it compromise my tests?

It shouldn't. Unless you are testing that your module (class, function, method etc) is dispatching an Event, than it does not interfere the module's behavior.

In case you want to test if the module is dispatching an Event.

And want to inject your fixture/stub/mock for EventDispatcherInterface, you can always just call use_dispatcher() in the test code or test setup. The tricky part is that this dispatcher will be used globally all along. But you can always call desuse_dispatcher() from your tests teardown.

Doesn't it make my module dependency implicit?

Yes, it does. You have to think about dispatch() as like a PHP core/built-in function and as if event dispatching is a core part of your application. You model your domain on top of it.

But remember, it depends on EventDispatcherInterface only! It is an interface that can receive any concrete implementation and is a accepted and consolidated PSR interface.

Conclusion

Trade-offs you should accept to pay are:

  • The extra handling of the global EventDispatcherInterface on tests that should be isolated.
  • Vendor lock-in on this project as if it is a superset of PHP, since you will be not injecting interfaces. Unless you create your own dispatch() function under the Dispatch namespace (that is why this namespace name is so generic).

If these are ok to you. Happy coding! I'm sure it will make your event-driven apps pleasant to code with reduce boilerplate.

Inspired by

dispatch's People

Contributors

leocavalcante avatar

Stargazers

 avatar  avatar

Watchers

 avatar  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.