Skip to content

Commit

Permalink
Update integration test and add module
Browse files Browse the repository at this point in the history
  • Loading branch information
overclokk committed Sep 7, 2023
1 parent 0c21e8d commit 34e8ed1
Show file tree
Hide file tree
Showing 13 changed files with 223 additions and 90 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"rector/rector": "^0.15.17",
"psr/container": "^1.0",
"psr/event-dispatcher": "^1.0",
"fig/event-dispatcher-util": "^1.0",
"fig/event-dispatcher-util": "^1.3",
"crell/tukio": "^1.0",
"inpsyde/object-hooks-remover": "^0.1",
"italystrap/config": "^2.2",
Expand Down
8 changes: 6 additions & 2 deletions src/Dispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@

namespace ItalyStrap\Event;

use Psr\EventDispatcher\{ListenerProviderInterface, StoppableEventInterface};
use Psr\EventDispatcher\{
EventDispatcherInterface,
ListenerProviderInterface,
StoppableEventInterface
};

/**
* @psalm-api
*/
class Dispatcher implements \Psr\EventDispatcher\EventDispatcherInterface
class Dispatcher implements EventDispatcherInterface
{
private ListenerProviderInterface $listenerProvider;
private StateInterface $state;
Expand Down
2 changes: 1 addition & 1 deletion src/EventSubscription.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class EventSubscription
* @param int $acceptedArgs
*/
public function __construct(
$callback,
callable $callback,
int $priority = 10,
int $acceptedArgs = 3
) {
Expand Down
2 changes: 1 addition & 1 deletion src/GlobalOrderedListenerProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public function getListenersForEvent(object $event): iterable
/**
* \WP_Hook::callbacks is a multidimensional array
* with priority as the first dimension and
* the callback as the second dimension.
* the callback name as the second dimension.
* Example:
* [
* 10 => [ // $priority
Expand Down
18 changes: 18 additions & 0 deletions src/ListenerRegistryTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,24 @@

trait ListenerRegistryTrait
{
/**
* Right now is only an experimental method, an idea to fetch $eventName
* from the listener first argument using reflection.
* I make this private for now to avoid to deprecated it in the future.
*/
private function addListenerFromCallable(
callable $listener,
string $eventName = null,
int $priority = 10,
int $accepted_args = 3
): bool {
if (null === $eventName) {
return false;
}

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

public function addListener(
string $eventName,
callable $listener,
Expand Down
37 changes: 37 additions & 0 deletions src/Module.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

namespace ItalyStrap\Event;

use Psr\EventDispatcher\ListenerProviderInterface;

/**
* @psalm-api
*/
class Module
{
public function __invoke(): array
{
return [
'aliases' => [
// Global
EventDispatcherInterface::class => EventDispatcher::class,
SubscriberRegisterInterface::class => SubscriberRegister::class,
// PSR-14
\Psr\EventDispatcher\EventDispatcherInterface::class => Dispatcher::class,
ListenerProviderInterface::class => GlobalOrderedListenerProvider::class,
ListenerRegistryInterface::class => GlobalOrderedListenerProvider::class,
StateInterface::class => GlobalState::class,
],
'sharing' => [
EventDispatcher::class,
SubscriberRegister::class,
// PSR-14
Dispatcher::class,
GlobalOrderedListenerProvider::class,
GlobalState::class,
],
];
}
}
24 changes: 0 additions & 24 deletions src/ParameterKeys.php

This file was deleted.

14 changes: 7 additions & 7 deletions src/SubscribersConfigExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,18 @@ class SubscribersConfigExtension implements Extension
/** @var string */
public const SUBSCRIBERS = 'subscribers';

private \ItalyStrap\Event\SubscriberRegister $event_manager;
private \ItalyStrap\Event\SubscriberRegister $subscriberRegister;

private \ItalyStrap\Config\ConfigInterface $config;

/**
* EventResolverExtension constructor.
* @param SubscriberRegister $event_manager
* @param SubscriberRegister $subscriberRegister
* @param ConfigInterface $config
*/
public function __construct(SubscriberRegister $event_manager, ConfigInterface $config)
public function __construct(SubscriberRegister $subscriberRegister, ConfigInterface $config)
{
$this->event_manager = $event_manager;
$this->subscriberRegister = $subscriberRegister;
$this->config = $config;
}

Expand All @@ -47,7 +47,7 @@ public function name(): string
*/
public function execute(AurynConfigInterface $application): void
{
$application->walk($this->name(), [$this, 'walk']);
$application->walk($this->name(), $this);
}

/**
Expand All @@ -57,7 +57,7 @@ public function execute(AurynConfigInterface $application): void
* @throws ConfigException
* @throws InjectionException
*/
public function walk(string $class, $index_or_optionName, Injector $injector): void
public function __invoke(string $class, $index_or_optionName, Injector $injector): void
{

if (\is_string($index_or_optionName) && empty($this->config->get($index_or_optionName, false))) {
Expand All @@ -66,6 +66,6 @@ public function walk(string $class, $index_or_optionName, Injector $injector): v

/** @var SubscriberInterface $subscriber */
$subscriber = $injector->share($class)->make($class);
$this->event_manager->addSubscriber($subscriber);
$this->subscriberRegister->addSubscriber($subscriber);
}
}
48 changes: 0 additions & 48 deletions tests/_data/experiment/PsrDispatcher/DebugDispatcher.php

This file was deleted.

0 comments on commit 34e8ed1

Please sign in to comment.