Skip to content

Commit

Permalink
fixed ListenerProviderInterface stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
phossa committed Nov 9, 2019
1 parent 73839e8 commit a0e9c4f
Show file tree
Hide file tree
Showing 10 changed files with 15 additions and 143 deletions.
10 changes: 0 additions & 10 deletions .idea/composerJson.xml

This file was deleted.

43 changes: 0 additions & 43 deletions .idea/event.iml

This file was deleted.

11 changes: 0 additions & 11 deletions .idea/inspectionProfiles/Project_Default.xml

This file was deleted.

6 changes: 0 additions & 6 deletions .idea/misc.xml

This file was deleted.

8 changes: 0 additions & 8 deletions .idea/modules.xml

This file was deleted.

45 changes: 0 additions & 45 deletions .idea/php.xml

This file was deleted.

6 changes: 0 additions & 6 deletions .idea/vcs.xml

This file was deleted.

2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "phoole/event",
"type": "library",
"license": "Apache-2.0",
"version": "1.0.3",
"version": "1.0.5",
"description": "A slim PSR-14 event implementation",
"keywords": [
"phoole",
Expand Down
11 changes: 5 additions & 6 deletions src/Dispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
use Phoole\Base\Queue\UniquePriorityQueue;
use Psr\EventDispatcher\StoppableEventInterface;
use Psr\EventDispatcher\EventDispatcherInterface;
use Psr\EventDispatcher\ListenerProviderInterface;

/**
* Dispatcher
Expand All @@ -24,16 +23,16 @@
class Dispatcher implements EventDispatcherInterface
{
/**
* @var ListenerProviderInterface[]
* @var Provider[]
*/
protected $providers = [];

/**
* Dispatcher constructor.
*
* @param ListenerProviderInterface ...$providers
* @param Provider ...$providers
*/
public function __construct(ListenerProviderInterface ...$providers)
public function __construct(Provider ...$providers)
{
foreach ($providers as $p) {
$this->addProvider($p);
Expand Down Expand Up @@ -63,11 +62,11 @@ public function dispatch(object $event)
/**
* Add a provider to the dispatcher
*
* @param ListenerProviderInterface $provider
* @param Provider $provider
* @return void
* @throws \RuntimeException if provider duplicated
*/
protected function addProvider(ListenerProviderInterface $provider)
protected function addProvider(Provider $provider)
{
$hash = \spl_object_hash($provider);
if (!isset($this->providers[$hash])) {
Expand Down
16 changes: 9 additions & 7 deletions src/Provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@

namespace Phoole\Event;

use Phoole\Base\Queue\PriorityQueue;
use Phoole\Base\Reflect\ParameterTrait;
use Phoole\Base\Queue\UniquePriorityQueue;
use Psr\EventDispatcher\ListenerProviderInterface;

/**
Expand All @@ -27,7 +27,7 @@ class Provider implements ListenerProviderInterface
/**
* event classes listened
*
* @var PriorityQueue[]
* @var UniquePriorityQueue[]
*/
protected $listened = [];

Expand All @@ -40,7 +40,7 @@ class Provider implements ListenerProviderInterface
*/
public function getListenersForEvent(object $event): iterable
{
$queue = new PriorityQueue();
$queue = new UniquePriorityQueue();
foreach ($this->listened as $class => $q) {
if (is_a($event, $class) && count($q)) {
$queue = $queue->combine($q);
Expand All @@ -54,15 +54,15 @@ public function getListenersForEvent(object $event): iterable
*
* @param callable $callable MUST be type-compatible with $event.
* @param int $priority range 0 - 100, 0 means lower priority
* @return ListenerProviderInterface $this
* @return $this
* @throws \RuntimeException reflection problem found
* @throws \InvalidArgumentException unknown type of callable found
*/
public function attach(callable $callable, int $priority = 50): ListenerProviderInterface
public function attach(callable $callable, int $priority = 50)
{
$class = $this->getEventClass($callable);
if (!isset($this->listened[$class])) {
$this->listened[$class] = new PriorityQueue();
$this->listened[$class] = new UniquePriorityQueue();
}
$this->listened[$class]->insert($callable, $priority);
return $this;
Expand All @@ -80,12 +80,14 @@ protected function getEventClass(callable $callable): string
{
try {
$params = $this->getCallableParameters($callable);

$error = 'Listener must declare one object as event';

// only one parameter allowed
if (1 != count($params)) {
throw new \InvalidArgumentException($error);
}

// must be object
$type = $params[0]->getType()->getName();
if (class_exists($type) || interface_exists($type)) {
return $type;
Expand Down

0 comments on commit a0e9c4f

Please sign in to comment.