Coder Social home page Coder Social logo

event-dispatcher's Introduction

Event Dispatcher

Latest Stable Version Total Downloads Daily Downloads Monthly Downloads Latest Unstable Version License composer.lock

A simple event dispatcher that you can fit into the framework of your choice.

License

Licensed under MIT. Totally free for private or commercial projects.

Installation

composer require andrewdyer/event-dispatcher

Usage

$dispatcher = new Anddye\EventDispatcher\EventDispatcher();

// add listeners for when a user signed up event is dispatched
$dispatcher->addListener('UserRegistered', new App\Listeners\SendSignedUpEmail());
$dispatcher->addListener('UserRegistered', new App\Listeners\UpdateLastSignedInDate());

// create a user somehow
$user = new App\Models\User();
// ...

// create the user signed up event and dispatch it
$dispatcher->dispatch(new App\Events\UserSignedUp($user));

Events

All events must be an instance of Anddye\EventDispatcher\Events\EventInterface and ideally should extend Anddye\EventDispatcher\Events\AbstractEvent - which will implement the required interface by default.

When an event is dispatched, it's identified by a unique name. By default, the name of an event will be that of the class, however you can manually set an event name by overwriting the getName() method.

namespace App\Events;

use Anddye\EventDispatcher\Events\AbstractEvent;

class UserSignedUp extends AbstractEvent
{
    public function getName(): string
    {
        return 'UserRegistered';
    }
}

Listeners

All listeners must be an instance of Anddye\EventDispatcher\Listeners\ListenerInterface and ideally should extend Anddye\EventDispatcher\Listeners\AbstractListener - which will implement the required interface by default.

namespace App\Listeners;

use Anddye\EventDispatcher\Events\EventInterface;
use Anddye\EventDispatcher\Listeners\AbstractListener;

class SendSignedUpEmail extends AbstractListener
{
    public function handle(EventInterface $event): void
    {
        // TODO: This is where you would send the signed up email to the user!
    }
}

Support

If you're using this package, I'd love to hear your thoughts! Feel free to contact me on Twitter.

Found a bug? Please report it using the issue tracker, or better yet, fork the repository and submit a pull request.

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.