Giter VIP home page Giter VIP logo

event-dispatcher's Introduction

Yii

Yii Event Dispatcher


Latest Stable Version Total Downloads Build Status Code Coverage Scrutinizer Code Quality Mutation testing badge static analysis type-coverage

PSR-14 compatible event dispatcher provides an ability to dispatch events and listen to events dispatched.

Features

  • PSR-14 compatible.
  • Simple and lightweight.
  • Encourages designing event hierarchy.
  • Can combine multiple event listener providers.

Requirements

  • PHP 8.0 or higher.

Installation

The package could be installed with Composer:

composer require yiisoft/event-dispatcher

General usage

The library consists of two parts: event dispatcher and event listener provider. Provider's job is to register listeners for a certain event type. Dispatcher's job is to take an event, get listeners for it from a provider and call them sequentially.

// Add some listeners.
$listeners = (new \Yiisoft\EventDispatcher\Provider\ListenerCollection())
    ->add(function (AfterDocumentProcessed $event) {
        $document = $event->getDocument();
        // Do something with document.
    });

$provider = new Yiisoft\EventDispatcher\Provider\Provider($listeners);
$dispatcher = new Yiisoft\EventDispatcher\Dispatcher\Dispatcher($provider);

The event dispatching may look like:

use Psr\EventDispatcher\EventDispatcherInterface;

final class DocumentProcessor
{
    private EventDispatcherInterface $eventDispatcher;
    
    public function __construct(EventDispatcherInterface $eventDispatcher) {
        $this->eventDispatcher = $eventDispatcher;
    }

    public function process(Document $document): void
    {
        // Process the document, then dispatch completion event.
        $this->eventDispatcher->dispatch(new AfterDocumentProcessed($document));
    }
}

Stoppable events

Event could be made stoppable by implementing Psr\EventDispatcher\StoppableEventInterface:

final class BusyEvent implements Psr\EventDispatcher\StoppableEventInterface
{
    // ...

    public function isPropagationStopped(): bool
    {
        return true;
    }
}

This way we can ensure that only first event listener will be able to handle the event. Another option is to allow stopping propagation in one of the listeners by providing corresponding event method.

Events hierarchy

Events do not have any name or wildcard matching on purpose. Event class names and class/interface hierarchy and composition could be used to achieve great flexibility:

interface DocumentEvent
{
}

final class BeforeDocumentProcessed implements DocumentEvent
{
}

final class AfterDocumentProcessed implements DocumentEvent
{
}

With the interface above listening to all document-related events could be done as:

$listeners->add(static function (DocumentEvent $event) {
    // log events here
});

Combining multiple listener providers

In case you want to combine multiple listener providers, you can use CompositeProvider:

$compositeProvider = new Yiisoft\EventDispatcher\Provider\CompositeProvider();
$provider = new Yiisoft\EventDispatcher\Provider\Provider($listeners);
$compositeProvider->add($provider);
$compositeProvider->add(new class implements ListenerProviderInterface {
    public function getListenersForEvent(object $event): iterable
    {
        yield static function ($event) {
            // handle 
        };
    }
});

$dispatcher = new Yiisoft\EventDispatcher\Dispatcher\Dispatcher($compositeProvider);

Register listeners with concrete event names

You may use a more simple listener provider, which allows you to specify which event they can provide.

It can be useful in some specific cases, for instance if one of your listeners does not need the event object passed as a parameter (can happen if the listener only needs to run at a specific stage during runtime, but does not need event data).

In that case, it is advised to use the aggregate (see above) if you need features from both providers included in this library.

$listeners = (new \Yiisoft\EventDispatcher\Provider\ListenerCollection())
    ->add(static function () {
    // this function does not need an event object as argument
}, SomeEvent::class);

Dispatching to multiple dispatchers

There may be a need to dispatch an event via multiple dispatchers at once. It could be achieved like the following:

use Yiisoft\EventDispatcher\Dispatcher\CompositeDispatcher;
use Yiisoft\EventDispatcher\Dispatcher\Dispatcher;
use Yiisoft\EventDispatcher\Provider\ListenerCollection;
use Yiisoft\EventDispatcher\Provider\Provider;

// Add some listeners.
$listeners1 = (new ListenerCollection())
    ->add(function (AfterDocumentProcessed $event) {
        $document = $event->getDocument();
        // Do something with document.
    });

$provider1 = new Provider($listeners1);

// Add some listeners.
$listeners2 = (new ListenerCollection())
    ->add(function (AfterDocumentProcessed $event) {
        $document = $event->getDocument();
        // Do something with document.
    });

$provider2 = new Provider($listeners2);


$dispatcher = new CompositeDispatcher();
$dispatcher->attach(new Dispatcher($provider1));
$dispatcher->attach(new Dispatcher($provider2));

$dispatcher->dispatch(new MyEvent());

Documentation

If you need help or have a question, the Yii Forum is a good place for that. You may also check out other Yii Community Resources.

License

The Yii Event Dispatcher is free software. It is released under the terms of the BSD License. Please see LICENSE for more information.

Maintained by Yii Software.

Credits

  • Larry Garfield (@crell) for initial implementation of deriving callable parameter type.

Support the project

Open Collective

Follow updates

Official website Twitter Telegram Facebook Slack

event-dispatcher's People

Contributors

arhell avatar azjezz avatar dependabot-preview[bot] avatar dependabot[bot] avatar devanych avatar fantom409 avatar hiqsol avatar luizcmarin avatar romkatsu avatar romm avatar roxblnfk avatar rustamwin avatar samdark avatar stylecibot avatar terabytesoftw avatar viktorprogger avatar vjik avatar xepozz avatar yiiliveext avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

event-dispatcher's Issues

Add Priorities on events' handlers

Working with Yii2 I faced a problem regarding the events "mechanism".

What is that problem:

You can only append or prepend handlers to Events. You can't decide yourself events' handlers priority.

I think this is a problem especially for developers that has previous experiences in other frameworks and also want to intervene in the request flow wherever they want to.

What I am going to implement:

So I decided to create a new Priority Queue Class based on SplPriorityQueue to handle events' handlers according their priority.

For the moment this implementation fully supports the append and prepend functionality as it is in order to support all current projects and core functionality.

However if you accept that change then we can step by step change that append-prepend approach in the near feature (regarding core implementation) and simultaneously all Yii2 developers can use handlers priorities in their new components.

Consider adding EventTrait

For using in domain entities.

Logic: entity create events, but send their to dispatcher after save entity.

Example:

final class Post
{
    use EventTrait;

    public function setName(string $name) {
        $this->name = $name;
        $this->recordEvent(new SetNameEvent()); // Save event in entity
    }

    public function setAge(int $age) {
        $this->age = $age;
        $this->recordEvent(new SetAgeEvent()); // Save event in entity
    }
}

final class PostService
{
    public function save(Post $post)
    {
        $this->postRepository->save($post);
        $events = $post->releaseEvents(); // Get all events from entity and clear
        foreach ($events as $event) {
            $this->dispatcher->dispatch($event);
        }
    }
}

Mentioned in the articles:

update irc link

What steps will reproduce the problem?

What is the expected result?

What do you get instead?

Additional info

Q A
Version 1.0.?
PHP version
Operating system

update root folder links

What steps will reproduce the problem?

What is the expected result?

What do you get instead?

Additional info

Q A
Version 1.0.?
PHP version
Operating system

Styling

Not a real issue more of a code formatting/style thing

What steps will reproduce the problem?

I see use function {php function name} is being used in multiple places.

I noticed in Provider it only imports one of the functions it uses.

What is the expected result?

Either all of the functions called out or none of them.

Other functions not called out:

  • array_values
  • class_parents
  • class_implements

What do you get instead?

use function get_class; is only called out and the other functions aren't

Additional info

N/A

possible bug with PHP8 and Union type-hint

Hi,

Nice piece of code, i think i found a possible problem in php8. I didn't do the test but looking at the code i think in some very very rare case there could have a problem in the code if the php union typehint is used.

at this line of code, the object could be a ReflectionUnionType and not a ReflectionNamedType, so the getName() function will break.
https://github.com/yiisoft/event-dispatcher/blob/master/src/Provider/ListenerCollection.php#L85

Correct me if i am missing something.
Hope this help.

Use fromCallable

Instead of parsing each type of callable individually, we should at least test the cost of using Closure::fromCallable.
Using this would significantly simplify the code.

Consider introducing cache

Reflection is used to determine event interface. It could be avoided by using cache based on \SplObjectStorage.

Before trying to implement that it is necessary to do a benchmark.

Consider being able to explicitly register for a given event name

I'm using this library as a standalone package, and I ran into an issue: one of the listener methods does not have an event argument (which is currently needed by the library). This is because this listener needs to run a process at a specific stage of the runtime, but doesn't need any information provided by the event (whereas some other listeners for the same event need it). Adding the event parameter to the said method results in a parameter being unused, which is not acceptable (and reported by static analysis tools).

I completely understand the goal of this library and find it very interesting; however, the limitation I just spoke about is very problematic in that case, and could be the reason why some people may use another package than this one.

I would suggest to either add an optional $eventName argument to the attach method, or create a new basic provider with this feature which would allow the usage of the aggregate listener provider. I really think the second option would be the best one.

I'm ready to contribute if you tell me what you think of this issue, and what might be the correct solution to your eyes.

Rename ListenerCollection::add

It's better to rename the ListenerCollection::add method to ListenerCollection::withListener because it's not clear that it's immutable

Why cloning ListenerCollection every time when adding a new listener?

I use this package in my project #22

I find this package is refactored and I'm willing to adapt to my project.

My solution is making a public listener using ListenerCollection to subscribe to events.

And ListenerCollection is singleton so that the dispatcher use the same ListenerCollection.

But the snag is ListenerCollection changes every time when adding a new event handler, so the singleton pattern doesn't work(because of clone).

I'm curious why using clone when adding a new event hander?

At least, doesn't it affect performance?

Typehint callable in `getParameterType()`

// We can't type hint $callable as it could be an array, and arrays are not callable. Sometimes. Bah, PHP.

Comment suggests we can't typehint because the parameter could be an array. But the function is private and only called once by a function that takes a callable type parameter.
So any argument that reaches getParameterType() will be callable.

Consider introducing deferred event provider

Sometimes we use events in transactions, so they should not be dispatched before the transaction is successfully committed. As an example, imagine a user registration flow:

  1. Validate data
  2. Begin transaction
  3. Write user to DBMS
  4. Dispatch UserWasCreated event
  5. Create a profile, catch an exception
  6. Rollback transaction

The dependent services may have already sent the welcome email as a result of UserWasCreated event handling, but the transaction was reverted later.

That's why we use an Emitter – a thing that collects events but does not send them to a Dispatcher until flush() is called.

The implementation can be pretty dumb:

interface EventDispatcherInterface
{
    public function flush(): void;
}

final class Emitter implements EventDispatcherInterface
{
    private $dispatcher;
    private $events = [];

    public function __construct(EventDispatcherInterface $dispatcher)
    {
        $this->dispatcher = $dispatcher;
    }

    public function dispatch(object $event)
    {
         return $this->events[] = $event;
    }

    public function flush(): void
    {
        foreach ($this->events as $event) {
            $dispatcher->dispatch($event);
        }
    }
}
$provider = new Yiisoft\EventDispatcher\Provider\Provider();
$provider->attach(function (UserWasCreated $event) {
    sendGreetingEmail();
});

$dispatcher = new Yiisoft\EventDispatcher\Dispatcher($provider);
$emitter = new Yiisoft\EventDispatcher\Emitter($dispatcher);

$emitter->dispatch(new UserWasCreated($uuid));
// ... flow contiues
$emitter->flush();

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.