Convert Figma logo to code with AI

symfony logoevent-dispatcher

Provides tools that allow your application components to communicate with each other by dispatching events and listening to them

8,485
70
8,485
0

Top Related Projects

1,517

Event package for your app and domain

ReactPHP's core reactor event loop that libraries can use for evented I/O.

Quick Overview

The Symfony EventDispatcher component implements the Mediator pattern in PHP, providing a system for dispatching events and registering event listeners. It allows for loose coupling between components in an application by enabling communication through events rather than direct dependencies.

Pros

  • Promotes loose coupling and modular design
  • Easily extendable and customizable
  • Well-documented and part of the widely-used Symfony ecosystem
  • Lightweight and can be used independently of the full Symfony framework

Cons

  • Can lead to complex event chains if overused
  • Potential performance overhead for applications with a large number of events
  • Debugging can be challenging due to the indirect nature of event-driven communication
  • Learning curve for developers new to event-driven architecture

Code Examples

  1. Creating and dispatching an event:
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Contracts\EventDispatcher\Event;

$dispatcher = new EventDispatcher();

$event = new Event();
$dispatcher->dispatch($event, 'user.registered');
  1. Adding an event listener:
$dispatcher->addListener('user.registered', function (Event $event) {
    // Handle the user.registered event
    echo "A new user has registered!";
});
  1. Creating a custom event class:
use Symfony\Contracts\EventDispatcher\Event;

class UserRegisteredEvent extends Event
{
    private $username;

    public function __construct(string $username)
    {
        $this->username = $username;
    }

    public function getUsername(): string
    {
        return $this->username;
    }
}
  1. Using the custom event:
$event = new UserRegisteredEvent('johndoe');
$dispatcher->dispatch($event, UserRegisteredEvent::class);

$dispatcher->addListener(UserRegisteredEvent::class, function (UserRegisteredEvent $event) {
    echo "User {$event->getUsername()} has registered!";
});

Getting Started

To start using the Symfony EventDispatcher component, follow these steps:

  1. Install the component using Composer:

    composer require symfony/event-dispatcher
    
  2. Create an instance of the EventDispatcher:

    use Symfony\Component\EventDispatcher\EventDispatcher;
    
    $dispatcher = new EventDispatcher();
    
  3. Define event listeners and dispatch events as needed in your application.

For more detailed information and advanced usage, refer to the Symfony EventDispatcher documentation.

Competitor Comparisons

1,517

Event package for your app and domain

Pros of Event

  • More lightweight and focused solely on event dispatching
  • Supports emitting events with return values
  • Allows for easier creation of custom event objects

Cons of Event

  • Less widely adopted compared to Symfony's implementation
  • Fewer advanced features like event subscribers or prioritized listeners
  • Less integration with other PHP frameworks and libraries

Code Comparison

Event (thephpleague/event):

$emitter = new League\Event\Emitter();
$emitter->addListener('user.created', function ($event) {
    // Handle event
});
$emitter->emit('user.created', new User());

Event Dispatcher (symfony/event-dispatcher):

$dispatcher = new Symfony\Component\EventDispatcher\EventDispatcher();
$dispatcher->addListener('user.created', function ($event) {
    // Handle event
});
$dispatcher->dispatch(new UserCreatedEvent(), 'user.created');

Both libraries provide similar basic functionality for event dispatching, but Symfony's implementation offers more advanced features and wider ecosystem integration. Event focuses on simplicity and flexibility, while Event Dispatcher provides a more robust and standardized approach to event handling in larger applications.

ReactPHP's core reactor event loop that libraries can use for evented I/O.

Pros of Event Loop

  • Designed for asynchronous programming and non-blocking I/O operations
  • Provides a more efficient way to handle concurrent tasks in event-driven applications
  • Offers better performance for long-running processes and high-concurrency scenarios

Cons of Event Loop

  • Steeper learning curve, especially for developers new to asynchronous programming
  • May introduce complexity in code structure and flow control
  • Less suitable for simple, synchronous event handling scenarios

Code Comparison

Event Loop:

$loop = React\EventLoop\Factory::create();
$loop->addTimer(1.0, function () {
    echo "Hello after 1 second\n";
});
$loop->run();

Event Dispatcher:

$dispatcher = new EventDispatcher();
$dispatcher->addListener('my_event', function (Event $event) {
    echo "Event handled\n";
});
$dispatcher->dispatch('my_event', new Event());

Key Differences

  • Event Loop focuses on managing asynchronous operations and timers, while Event Dispatcher is primarily for synchronous event handling and communication between components.
  • Event Loop is more suited for building reactive, non-blocking applications, whereas Event Dispatcher is better for implementing the observer pattern and decoupling application components.
  • Event Loop requires running a loop to process events, while Event Dispatcher allows for immediate event dispatching and handling.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

EventDispatcher Component

The EventDispatcher component provides tools that allow your application components to communicate with each other by dispatching events and listening to them.

Resources