Download the PHP package ostrolucky/app-event-dispatcher without Composer

On this page you can find all versions of the php package ostrolucky/app-event-dispatcher. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package app-event-dispatcher

AppEventDispatcher

Most event dispatchers are designed to be used as hook system by frameworks and other libraries. This one is different. It's strictly for use in domain of your application. This allows it to:

Install

Via Composer

Requirements

Usage

Basic usage is following:

If you use Symfony built in event dispatcher for dispatching your domain events, I provide compiler pass you can easily use as compatible replacement.

All you need to do is:

  1. Define this event dispatcher as a service, such as:

  2. Add RegisterListenersPass in your main bundle, as such:

  3. Replace kernel.event_subscriber and kernel.event_listener tags with app.event_listener. Yes, you no longer need to differentiate between them. It's going to be treated as a subscriber if it implements EventSubscriber interface, otherwise it's going to be treated as regular listener. Don't worry, it will also alert you when you try to define events in a tag for a subscriber.

  4. In places you dispatch events, replace event_dispatcher service with app.event_dispatcher

Advantages in detail

1. Less restrictive public dispatching interface

In contrast to most event dispatchers, this dispatcher encourages you to shift responsibility of argument signature validation into event listener. You are free to dispatch directly whatever arguments your listener needs. Any types, any number of them.

In most dispatchers you are forced to wrap all of the arguments into single argument. In case of Symfony event dispatcher it's event object:

Then in listener if you want to ensure correct arguments are passed, in most dispatchers you are forced to unwrap it and check the types manually:

vs. this dispatcher:

As you can see, symfony event dispatcher violates DRY hard and makes writing new listeners very repetitive with lot of boilerplate. And even if you decide type safety isn't worth it so you skip all of this argument validation, you are still required to write annotations if you want your IDE understand what type of arguments you are working with.

To be fair, in case of symfony event dispatcher you are encouraged to write custom event class instead, where you can utilize type hints. In that case you can move argument validation into this class. However you need to do this again every time your new listener requires different parameters and you still need to write boilerplate code for injecting these parameters into your new event object and retrieving them. That's why it's in practice in non-library applications almost never done and some generic event class is used instead.

2. Stricter validation

Since this dispatcher isn't meant to be used as a hook system, it allows it to do stricter validation:

  1. Alert you when you are trying to dispatch event for which you did not attach any event listener. There is number of reasons this could happen:
    • You forgot to attach event listener for this event, or you made a mistake during this process
    • You removed all listeners listening to provided event and forgot to remove dispatching code for this event
    • You made a typo in event name
    • You are dispatching event dynamically, but don't check if something is listening for it
  2. Alert you when you are trying to attach listener which is already attached, or when you are trying to detach listener which isn't attached. It's a sign of a bug in your application, because something in your code is trying to do operation which has already been done.

None of these cases are handled by other dispatchers.

3. Skip problematic features

Number of problematic features have been purposely left out because they don't make much sense when you have full control over attaching. This allows to make this dispatcher super lightweight:

  1. Hooks. This dispatcher doesn't expect javascript style listeners, which can stop propagation. If you need to do this, you can implement it easily with single listener which redirects the call further, according your constraints. This allows you to be confident that all of the attached listeners will always be called.
  2. Priorities inside event dispatcher. This dispatcher itself is FIFO style and as such it does not do any sorting. Correct place to do this is in code which attaches listeners to dispatcher. In case of Symfony framework you can use my compiler pass and it will attach listeners in correct order based on priorities you specify.
  3. "Event subscribers" inside of event dispatcher. This shouldn't be a responsibility of event dispatcher, but responsibility of code which you use to attach listeners to dispatcher. It's easy to create own implementation by simply iterating over the list of events|callbacks and attach them them to event dispatcher in regular way. Still, this is supported in my compiler pass for Symfony framework.

FAQ

Q: What's the point of using dispatcher for application events, instead of doing direct service calls?

A: I agree that most people are doing it wrong and they should use direct service calls instead, because using dispatcher means harder debugging, since it's only known at runtime what listeners are actually attached. It makes it hard to know what callback will be triggered by following regular flow of the program, because attaching is mostly done totally out of context of dispatch call. That said:

Q: Why don't you provide implementation for LaxEventDispatcherInterface for usage as a library?

A: I didn't do this because regular event dispatchers are better suited for this. Their restrictive public interface and additional features are actually plus here, as it allows to keep better backwards compatibility for libraries and more control over process when multiple different 3rd party libraries listen to same event. That's why is this dispatcher focused on usage in application domain only.


All versions of app-event-dispatcher with dependencies

PHP Build Version
Package Version
Requires php Version >=5.6
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package ostrolucky/app-event-dispatcher contains the following files

Loading the files please wait ....