Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
frankdejonge committed Nov 24, 2023
1 parent 221867a commit 9e24911
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 0 deletions.
29 changes: 29 additions & 0 deletions src/BufferedEventDispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,33 @@ public function subscribeListenersFrom(ListenerSubscriber $subscriber): void

$this->dispatcher->subscribeListenersFrom($subscriber);
}



public function unsubscribeFor(string $event, callable $listener): void
{
if ( ! $this->dispatcher instanceof ListenerRegistry) {
throw UnableToUnsubscribeListener::becauseTheEventDispatcherIsNotARegistry($this->dispatcher);
}

$this->dispatcher->unsubscribeFor($event, $listener);
}

public function unsubscribeAllFor(string $event): void
{
if ( ! $this->dispatcher instanceof ListenerRegistry) {
throw UnableToUnsubscribeListener::becauseTheEventDispatcherIsNotARegistry($this->dispatcher);
}

$this->dispatcher->unsubscribeAllFor($event);
}

public function unsubscribeAll(callable $listener): void
{
if ( ! $this->dispatcher instanceof ListenerRegistry) {
throw UnableToUnsubscribeListener::becauseTheEventDispatcherIsNotARegistry($this->dispatcher);
}

$this->dispatcher->unsubscribeAll($listener);
}
}
27 changes: 27 additions & 0 deletions src/EventDispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,31 @@ public function subscribeListenersFrom(ListenerSubscriber $subscriber): void

$this->listenerProvider->subscribeListenersFrom($subscriber);
}

public function unsubscribeFor(string $event, callable $listener): void
{
if ( ! $this->listenerProvider instanceof ListenerRegistry) {
throw UnableToUnsubscribeListener::becauseTheListenerProviderIsNotARegistry($this->listenerProvider);
}

$this->listenerProvider->unsubscribeFor($event, $listener);
}

public function unsubscribeAllFor(string $event): void
{
if ( ! $this->listenerProvider instanceof ListenerRegistry) {
throw UnableToUnsubscribeListener::becauseTheListenerProviderIsNotARegistry($this->listenerProvider);
}

$this->listenerProvider->unsubscribeAllFor($event);
}

public function unsubscribeAll(callable $listener): void
{
if ( ! $this->listenerProvider instanceof ListenerRegistry) {
throw UnableToUnsubscribeListener::becauseTheListenerProviderIsNotARegistry($this->listenerProvider);
}

$this->listenerProvider->unsubscribeAll($listener);
}
}
5 changes: 5 additions & 0 deletions src/ListenerRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@

namespace League\Event;

/**
* @method void unsubscribeFor(string $event, callable $listener)
* @method void unsubscribeAll(callable $listener)
* @method void unsubscribeAllFor(string $event)
*/
interface ListenerRegistry
{
public function subscribeTo(string $event, callable $listener, int $priority = ListenerPriority::NORMAL): void;
Expand Down
33 changes: 33 additions & 0 deletions src/UnableToUnsubscribeListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace League\Event;

use LogicException;
use Psr\EventDispatcher\EventDispatcherInterface;
use Psr\EventDispatcher\ListenerProviderInterface;

class UnableToUnsubscribeListener extends LogicException
{
public static function becauseTheListenerProviderIsNotARegistry(
ListenerProviderInterface $configuredListenerProvider
): UnableToUnsubscribeListener {
$providerClass = get_class($configuredListenerProvider);

return new UnableToUnsubscribeListener(
"Unable to remove listener because the configured listener provider {$providerClass} is not an instance of "
. ListenerRegistry::class
);
}
public static function becauseTheEventDispatcherIsNotARegistry(
EventDispatcherInterface $configuredListenerProvider
): UnableToUnsubscribeListener {
$providerClass = get_class($configuredListenerProvider);

return new UnableToUnsubscribeListener(
"Unable to remove listener because the internal dispatcher {$providerClass} is not an instance of "
. ListenerRegistry::class
);
}
}

0 comments on commit 9e24911

Please sign in to comment.