Giter Club home page Giter Club logo

event's Introduction

Neat Event components

Stable Version Build Status codecov

Neat Event components provide a clean and expressive API for your application to dispatch and listen for events. The Neat Event components implement the PSR-14 interfaces for optimal interoperability.

NOTE: Event listeners are required to have their first parameter named '$event'.

Getting started

To install this package, simply issue composer on the command line:

composer require neat/event

The event dispatcher can be created with a few lines of code.

<?php
// First create your container (used for event dispatching and dependency injection)
$container = new Neat\Service\Container();

// The dispatcher will need a service container for dependency injection
$dispatcher = new Neat\Event\Dispatcher($container);

Defining events

<?php

// A generic event can be defined using a simple PHP class without any members
class SomeEvent
{
}

// Specific events may also use inheritance using extends or interfaces
class SomeSpecificEvent extends SomeEvent
{
}

// Implement the Stoppable event interface if you want control over dispatching your event
class SomeStoppableEvent implements Neat\Event\Stoppable
{
    public function isPropagationStopped(): bool
    {
        return random_int(0, 1) == 1;
    }
}

Listen for events

Your event lister can be any callable function or method as long as it has an $event parameter that accepts the event object we're listening for.

<?php

// Now listen for events of this class or any of its subclasses or implementations
$dispatcher->listen(SomeEvent::class, function (SomeEvent $event) {
    // ...
});

// Or for a specific event
$dispatcher->listen(SomeSpecificEvent::class, function (SomeSpecificEvent $event) {
    // ...
});

Dispatch an event

Now we're ready to dispatch an event

<?php

// This will trigger only the SomeEvent listener, NOT the SomeSpecificEvent listener
$dispatcher->dispatch(new SomeEvent());

// This will trigger both the SomeEvent and SomeSpecificEvent listeners
$dispatcher->dispatch(new SomeSpecificEvent());

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.