Skip to content

Commit

Permalink
Add some integration tests and rename EventDispatcher to GlobalDispat…
Browse files Browse the repository at this point in the history
…cher
  • Loading branch information
overclokk committed Sep 23, 2023
1 parent 893f24d commit a5ad7b2
Show file tree
Hide file tree
Showing 15 changed files with 226 additions and 100 deletions.
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,10 @@
"psr-4": {
"ItalyStrap\\Event\\": "src/",
"ItalyStrap\\PsrDispatcher\\": "tests/_data/experiment/PsrDispatcher/"
}
},
"files": [
"namespace-bc-aliases.php"
]
},
"autoload-dev": {
"psr-4": {
Expand Down
2 changes: 1 addition & 1 deletion index.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
*/

add_action('plugins_loaded', function () {
require( __DIR__ . '/vendor/autoload.php' );
require __DIR__ . '/vendor/autoload.php';

$listenerProvider = new \ItalyStrap\Event\GlobalOrderedListenerProvider();
$state = new \ItalyStrap\Event\GlobalState();
Expand Down
13 changes: 13 additions & 0 deletions namespace-bc-aliases.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

\class_alias(
\ItalyStrap\Event\GlobalDispatcherInterface::class,
\ItalyStrap\Event\EventDispatcherInterface::class
);

\class_alias(
\ItalyStrap\Event\GlobalDispatcher::class,
\ItalyStrap\Event\EventDispatcher::class
);
7 changes: 1 addition & 6 deletions src/Dispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,7 @@ public function dispatch(object $event): object
break;
}

try {
$listener($event);
} catch (\Throwable $e) {
// $this->dispatch(new ExceptionEvent($e, $event));
throw $e;
}
$listener($event);
}

$this->state->progress(StateInterface::AFTER);
Expand Down
89 changes: 61 additions & 28 deletions src/EventDispatcher.php → src/GlobalDispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,58 +15,82 @@
/**
* @psalm-api
*/
class EventDispatcher implements EventDispatcherInterface
class GlobalDispatcher implements EventDispatcherInterface, ListenerRegisterInterface
{
private const M_DEPRECATION_PATTERN = 'This method %1$s::%2$s() is deprecated, use %3$s() instead.';

public function addListener(
string $eventName,
callable $listener,
int $priority = 10,
int $accepted_args = 3
): bool {

$this->deprecated(
self::M_DEPRECATION_PATTERN,
__FUNCTION__,
'\ItalyStrap\Event\GlobalOrderedListenerProvider::' . __FUNCTION__
);

return add_filter($eventName, $listener, $priority, $accepted_args);
}

public function removeListener(
string $eventName,
callable $listener,
int $priority = 3
int $priority = 10
): bool {

$this->deprecated(
self::M_DEPRECATION_PATTERN,
__FUNCTION__,
'\ItalyStrap\Event\GlobalOrderedListenerProvider::' . __FUNCTION__
);

return remove_filter($eventName, $listener, $priority);
}

public function removeAllListener(string $eventName, $priority = false): bool
{

$this->deprecated(
self::M_DEPRECATION_PATTERN,
__FUNCTION__,
'\ItalyStrap\Event\GlobalOrderedListenerProvider::' . __FUNCTION__
);

return remove_all_filters($eventName, $priority);
}

public function hasListener(string $eventName, $callback = false)
{

$this->deprecated(
self::M_DEPRECATION_PATTERN,
__FUNCTION__,
'\ItalyStrap\Event\GlobalOrderedListenerProvider::' . __FUNCTION__
);

return has_filter($eventName, $callback);
}

/**
* @param string $event_name
* @param mixed ...$args
* @return object
* @infection-ignore-all
* @psalm-suppress MissingReturnType
* @psalm-suppress PossiblyUnusedParam
*/
public function dispatch($event_name, ...$args): object
{
// phpcs:disable
$pattern = <<<'D_MESSAGE'
Arguments for %1$s::%2$s() are deprecated, in a future release this method will be removed or it will be make compliant with %3$s::%4$s() so only one argument as object type will be accepted.
D_MESSAGE; //phpcs:enable

@\trigger_error(
\sprintf(
$pattern,
self::class,
__FUNCTION__,
\Psr\EventDispatcher\EventDispatcherInterface::class,
'dispatch'
),
\E_USER_DEPRECATED
$this->deprecated(
self::M_DEPRECATION_PATTERN,
__FUNCTION__,
'\Psr\EventDispatcher\EventDispatcherInterface::' . __FUNCTION__
);

do_action($event_name, ...$args);
$this->trigger($event_name, ...$args);

if (isset($args[0]) && \is_object($args[0])) {
return $args[0];
Expand All @@ -77,25 +101,18 @@ public function dispatch($event_name, ...$args): object
}

/**
* @param mixed ...$args
* @deprecated
* @infection-ignore-all
* @param mixed ...$args
* @psalm-suppress PossiblyUnusedParam
*/
public function execute(string $event_name, ...$args): void
{
$pattern = <<<'D_MESSAGE'
This method %1$s::%2$s() is deprecated, use %1$s::%3$s() instead.
D_MESSAGE;

@\trigger_error(
\sprintf(
$pattern,
self::class,
__FUNCTION__,
'action'
),
\E_USER_DEPRECATED
);
$this->deprecated($pattern, __FUNCTION__, 'trigger');

$this->trigger($event_name, ...$args);
}
Expand All @@ -110,8 +127,24 @@ public function filter(string $event_name, $value, ...$args)
return apply_filters($event_name, $value, ...$args);
}

public function currentEventName()
public function currentEventName(): string
{
return current_filter();
}

/**
* @psalm-suppress UnusedMethod
*/
private function deprecated(string $pattern, string $oldMethodName, string $newMethodName): void
{
\trigger_error(
\sprintf(
$pattern,
self::class,
$oldMethodName,
$newMethodName
),
\E_USER_DEPRECATED
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
/**
* @psalm-api
*/
interface EventDispatcherInterface extends ListenerRegisterInterface
interface GlobalDispatcherInterface
{
/**
* Executes all the callbacks registered with the given event.
Expand All @@ -19,16 +19,6 @@ interface EventDispatcherInterface extends ListenerRegisterInterface
*/
public function trigger(string $event_name, ...$args);

/**
* @psalm-suppress MissingReturnType
* Executes all the callbacks registered with the given event.
*
* @param string $event_name The name of the action to be executed.
* @param mixed ...$args Optional. Additional arguments which are passed on to the
* listeners to the action. Default empty.
*/
public function dispatch(string $event_name, ...$args);

/**
* Filters the given value by applying all the changes from the callbacks
* registered with the given event. Returns the filtered value.
Expand All @@ -39,12 +29,4 @@ public function dispatch(string $event_name, ...$args);
* @return mixed The filtered value after all listeners are applied to it.
*/
public function filter(string $event_name, $value, ...$args);

/**
* Get the name of the event that WordPress plugin API is executing. Returns
* false if it isn't executing an event.
*
* @return string|bool
*/
public function currentEventName();
}
26 changes: 9 additions & 17 deletions src/GlobalOrderedListenerProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,30 +15,22 @@ public function addListener(
string $eventName,
callable $listener,
int $priority = 10,
int $accepted_args = null
int $accepted_args = 3
): bool {
return \add_filter(
$eventName,
$listener,
$priority,
);
return \add_filter($eventName, $listener, $priority, $accepted_args);
}

public function removeListener(string $eventName, callable $listener, int $priority = 10): bool
{
return \remove_filter(
$eventName,
$listener,
$priority,
);
public function removeListener(
string $eventName,
callable $listener,
int $priority = 10
): bool {
return \remove_filter($eventName, $listener, $priority);
}

public function removeAllListener(string $eventName, $priority = false): bool
{
return \remove_all_filters(
$eventName,
$priority,
);
return \remove_all_filters($eventName, $priority);
}

public function hasListener(string $eventName, $callback = false)
Expand Down
3 changes: 2 additions & 1 deletion src/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public function __invoke(): array
return [
'aliases' => [
// Global
EventDispatcherInterface::class => EventDispatcher::class,
GlobalDispatcherInterface::class => GlobalDispatcher::class,
SubscriberRegisterInterface::class => SubscriberRegister::class,
// PSR-14
\Psr\EventDispatcher\EventDispatcherInterface::class => Dispatcher::class,
Expand All @@ -25,6 +25,7 @@ public function __invoke(): array
StateInterface::class => GlobalState::class,
],
'sharing' => [
// Global
EventDispatcher::class,
SubscriberRegister::class,
// PSR-14
Expand Down
10 changes: 5 additions & 5 deletions src/SubscriberRegister.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ class SubscriberRegister implements SubscriberRegisterInterface
private const ACCEPTED_ARGS = 1;
private const PRIORITY = 10;

private EventDispatcherInterface $listenerRegistry;
private ListenerRegisterInterface $listenerRegister;

public function __construct(EventDispatcherInterface $listenerRegistry)
public function __construct(ListenerRegisterInterface $listenerRegister)
{
$this->listenerRegistry = $listenerRegistry;
$this->listenerRegister = $listenerRegister;
}

public function addSubscriber(Subscriber $subscriber): void
Expand All @@ -54,7 +54,7 @@ public function addSubscriber(Subscriber $subscriber): void
*/
private function addSubscriberListener(Subscriber $subscriber, string $event_name, $parameters): void
{
$this->listenerRegistry->addListener(
$this->listenerRegister->addListener(
$event_name,
$this->buildCallable($subscriber, $parameters),
...$this->buildParameters($parameters)
Expand Down Expand Up @@ -84,7 +84,7 @@ public function removeSubscriber(Subscriber $subscriber): void
*/
private function removeSubscriberListener(Subscriber $subscriber, string $event_name, $parameters): void
{
$this->listenerRegistry->removeListener(
$this->listenerRegister->removeListener(
$event_name,
$this->buildCallable($subscriber, $parameters),
...$this->buildParameters($parameters)
Expand Down
8 changes: 6 additions & 2 deletions tests/_data/experiment/PsrDispatcher/PsrDispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace ItalyStrap\PsrDispatcher;

use ItalyStrap\Event\EventDispatcherInterface;
use ItalyStrap\Event\ListenerRegisterInterface;
use Psr\EventDispatcher\EventDispatcherInterface as PsrDispatcherInterface;

// https://make.wordpress.org/core/2016/09/08/wp_hook-next-generation-actions-and-filters/
Expand All @@ -18,15 +19,18 @@ class PsrDispatcher implements PsrDispatcherInterface
private \ItalyStrap\PsrDispatcher\CallableFactoryInterface $factory;

private \ItalyStrap\Event\EventDispatcherInterface $dispatcher;
private ListenerRegisterInterface $listenerRegister;

public function __construct(
array &$wp_filter,
CallableFactoryInterface $factory,
ListenerRegisterInterface $listenerRegister,
EventDispatcherInterface $dispatcher
) {
$this->wp_filter = &$wp_filter;
$this->factory = $factory;
$this->dispatcher = $dispatcher;
$this->listenerRegister = $listenerRegister;
}

/**
Expand All @@ -40,7 +44,7 @@ public function addListener(
): bool {
/** @var callable $callback */
$callback = $this->factory->buildCallable($listener);
return $this->dispatcher->addListener($event_name, $callback, $priority, $accepted_args);
return $this->listenerRegister->addListener($event_name, $callback, $priority, $accepted_args);
}

/**
Expand Down Expand Up @@ -77,7 +81,7 @@ public function removeListener(
*/
public function dispatch(object $event): object
{
$this->dispatcher->dispatch(\get_class($event), $event);
$this->dispatcher->trigger(\get_class($event), $event);
return $event;
}
}
Loading

0 comments on commit a5ad7b2

Please sign in to comment.