Download the PHP package timatanga/events without Composer

On this page you can find all versions of the php package timatanga/events. 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 events

Events

Bringing together various application packages requires a common foundation for communication needs. For that reason events serve best to decouple packages while allowing to hook in to state changes.

This package is heavily influenced by Laravel and Symfony. The passion for great code and influence they provide for the community is massive. Please see there documentation for more: https://symfony.com/doc/current/components/event_dispatcher.html#events https://laravel.com/docs/8.x/events

Why not using there components and providing an alternate component? There are various reasons why I've choosen this path:

So why not bringing these advantages together in a single, easy and lightweight events package.

Installation

composer require timatanga/events

Dispatcher

The Dispatcher class is like the heartbeat of an event notification system. Events and listeners Creating an event dispatcher instance is as easy as:

use timatanga\Events\Dispatcher;

$dispatcher = new Dispatcher();

Supported capabilities by the event dispatcher are

// register one event (as string) or multiple events (as array of strings) for listener
$dispatcher->listen( $events, $listener );

// unregister event listeners
$dispatcher->unlisten( $events, $listener );

// subscribe subscriber implementing EventSubscriberInterface 
$dispatcher->subscribe( $subscriber );

// unregister subscriber
$dispatcher->unsubscribe( $subscriber );

// dispatch event class (payload is then event itself) or event name as string with optional payload argument
$dispatcher->dispatch( $event, $payload );

// check if any listener is registered for event (or all listeners when argument left away )
$dispatcher->hasListeners( $event );

// get event listeners for event (or all listeners when argument left away )
$dispatcher->getListeners( $event );

Events

It is common to dispatch event classes for communication between application packages. An event class serves as container for related datasets. Depending on the use case, building event classes for any event might be too much of overhead. What is common for both cases, they share an event name and event data.

Event Naming

When an event is dispatched, it’s identified by a unique name, which any number of listeners might be listening to. The unique event name can be any string, but optionally follows a few naming conventions:

To ensure these naming conventions, the event dispatcher includes some checks and transformations:

Event Payload

Events dispatched as event classes as well as in the lightweight mode are capable of transfering payload.

Event playload for dispatches event classes:

// dispatching event class
$dispatcher->dispatch(new Event($subject, [..]))

// event name: 'event'
// event payload: event class itself

Please consider the event class chapter for further details about the generic event class

Event playload for lightweight mode:

// dispatching simple event
$dispatcher->dispatch('event', [...])

// event name: 'event'
// event payload: [...]

Event Class

The base timatanga\Events\Event class is available for convenience for those who wish to use just one event object throughout their application. It is suitable for most purposes straight out of the box, because it follows the standard observer pattern where the event object encapsulates an event ‘subject’, but has the addition of optional extra arguments.

__construct($subject, $arguments): Constructor takes the event subject and any arguments;
getSubject(): Get the subject;
setArgument(): Sets an argument by key;
setArguments(): Sets arguments array;
getArgument(): Gets an argument by key;
getArguments(): Getter for all arguments;
hasArgument(): Returns true if the argument key exists;

The Event class implements ArrayAccess and IteratorAggregate which makes it very convenient to pass extra arguments regarding the event subject.

The Event class implements the StoppableEventInterface. Since a listener has access to the event itself, it can stop further progagation by using $e->stopPropagation(). Any listener waiting to be processed for that event name will not get notified by the event dispatcher.

Listeners

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 listen() method associates any valid PHP callable to an event. The listen() method takes up to two arguments:

Listener Callables

A PHP callable is a PHP variable that can be used by the call_user_func() function and returns true when passed to the is_callable() function. It can be one of the following scenarios:

Wildcard Listeners

The event dispatcher provided by this package supports wildcard events for more generic listeners. E.g.

Subscribers

The most common way to listen to an event is to register an event listener with the dispatcher. This listener can listen to one or more events and is notified each time those events are dispatched.

Another way to listen to events is via an event subscriber. An event subscriber is a PHP class that’s able to tell the dispatcher exactly which events it should subscribe to. It implements the timatanga\Events\Contracts\EventSubscriberInterface interface, which requires a single static method called getSubscribedEvents(). Based on the subscriber class, the dispatcher resolves and registeres all events the listener should be executed.

class mySubscriber implements EventSubscriberInterface
{
    /*
     * Only method required by the EventSubscriberInterface
     */
    public static function getSubscribedEvents()
    {
        return [
            order.created => [ onOrderCreated, onOrderCreatedPost ],
            order.closed => [],
        ];
    }

    public function onOrderCreated( $event )
    {
        // ...
    }

    public function onOrderCreatedPostfix( $event, $dispatcher )
    {
        // ...
    }

The dispatcher invokes the registered methods if an event gets dispatched. The dispatcher allows for two listener arguments

Auto Discovery

Instead of programmatically register all event listeners and event subscribers individually the package provides an event discovery feature. Within the config/events.php file, directories containing listeners and subscribers can be added for auto discovery.

return [

    'listeners' => [
        'timatanga/events/tests/Data'
    ],

    'subscribers' => [
        'timatanga/events/tests/Data'
    ],
];

The above configuration would look for event listeners and subscribers within the timatanga/events/tests/Data directories.

Event listeners must implement the timatanga\Events\Contracts\EventListenerInterface which enforced a static handle method to execute on the event. Event subscribers must implement the timatanga\Events\Contracts\EventSubscriberInterface as described in the previous chapters.

After identifying classes implementing according Interfaces, the listeners and subscribers get registered in the event dispatcher. If you wand to avoid auto discovery, please create a dispatcher instance like

$dispatcher = new Dispatcher(['autoDiscovery' => false]);

All versions of events with dependencies

PHP Build Version
Package Version
Requires php Version ^8.0
psr/event-dispatcher Version ^1.0
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 timatanga/events contains the following files

Loading the files please wait ....