Skip to content

Commit

Permalink
Remove the need for an event interface.
Browse files Browse the repository at this point in the history
  • Loading branch information
frankdejonge committed Nov 26, 2018
1 parent d2cc124 commit ccbaff9
Show file tree
Hide file tree
Showing 26 changed files with 167 additions and 158 deletions.
1 change: 1 addition & 0 deletions phpspec.yml
Expand Up @@ -8,6 +8,7 @@ extensions:
formatter.name: pretty
code_coverage:
format:
- text
- clover
output:
html: coverage
Expand Down
2 changes: 0 additions & 2 deletions spec/BufferedEmitterSpec.php
Expand Up @@ -3,11 +3,9 @@
namespace spec\League\Event;

use League\Event\Event;
use League\Event\EventInterface;
use League\Event\ListenerInterface;
use League\Event\Stub\SpyListener;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;

class BufferedEmitterSpec extends ObjectBehavior
{
Expand Down
7 changes: 5 additions & 2 deletions spec/EmitterSpec.php
Expand Up @@ -5,7 +5,6 @@
use League\Event\CallbackListener;
use League\Event\Event;
use League\Event\GeneratorInterface;
use League\Event\ListenerAcceptorInterface;
use League\Event\ListenerInterface;
use League\Event\ListenerProviderInterface;
use League\Event\Stub\Listener;
Expand Down Expand Up @@ -124,11 +123,15 @@ public function it_should_accept_custom_listeners(ListenerInterface $listener)
$this->getListeners('event')->shouldContain($listener);
}

public function it_should_convert_listeners_to_one_time_listeners(ListenerInterface $listener)
public function it_should_handle_one_time_listeners()
{
$listener = new Listener();
$this->addOneTimeListener('event', $listener);
$this->getListeners('event')->shouldContainInstanceOfType('League\Event\OneTimeListener');
$this->getListeners('event')->shouldNotContain($listener);
$this->getListeners('event')->shouldHaveCount(1);
$this->emit('event');
$this->getListeners('event')->shouldHaveCount(0);
}

public function it_should_allow_an_any_listener(ListenerInterface $listener)
Expand Down
9 changes: 5 additions & 4 deletions spec/GeneratorSpec.php
Expand Up @@ -2,8 +2,8 @@

namespace spec\League\Event;

use League\Event\EventInterface;
use PhpSpec\ObjectBehavior;
use stdClass;

class GeneratorSpec extends ObjectBehavior
{
Expand All @@ -12,13 +12,14 @@ public function it_is_initializable()
$this->shouldHaveType('League\Event\Generator');
}

public function it_should_accept_an_event_object(EventInterface $event)
public function it_should_accept_an_event_object()
{
$this->addEvent($event)->shouldReturn($this);
$this->addEvent(new stdClass())->shouldReturn($this);
}

public function it_should_release_events(EventInterface $event)
public function it_should_release_events()
{
$event = new stdClass();
$this->releaseEvents()->shouldHaveCount(0);
$this->addEvent($event);
$this->releaseEvents()->shouldContain($event);
Expand Down
3 changes: 0 additions & 3 deletions spec/League/Event/BufferedEmitterSpec.php
Expand Up @@ -3,11 +3,8 @@
namespace spec\League\Event;

use League\Event\Event;
use League\Event\EventInterface;
use League\Event\ListenerInterface;
use League\Event\Stub\SpyListener;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;

class BufferedEmitterSpec extends ObjectBehavior
{
Expand Down
17 changes: 11 additions & 6 deletions spec/OneTimeListenerSpec.php
Expand Up @@ -3,18 +3,24 @@
namespace spec\League\Event;

use League\Event\Emitter;
use League\Event\EventInterface;
use League\Event\Event;
use League\Event\ListenerInterface;
use PhpSpec\ObjectBehavior;

class OneTimeListenerSpec extends ObjectBehavior
{
protected $listener;

/**
* @var Emitter
*/
protected $emitter;

public function let(ListenerInterface $listener)
{
$this->listener = $listener;
$this->beConstructedWith($this->listener);
$this->emitter = new Emitter();
$this->beConstructedWith($this->listener, $this->emitter);
}

public function it_is_initializable()
Expand All @@ -27,11 +33,10 @@ public function it_should_expose_the_wrapped_listener()
$this->getWrappedListener()->shouldReturn($this->listener);
}

public function it_should_unregister_and_forward_the_handle_call(EventInterface $event, Emitter $emitter)
public function it_should_unregister_and_forward_the_handle_call()
{
$event->getName()->willReturn('event');
$event->getEmitter()->willReturn($emitter);
$emitter->removeListener('event', $this->listener)->shouldBeCalled();
$event = Event::named('event');
$event->setEmitter($this->emitter);
$this->listener->handle($event)->shouldBeCalled();
$this->handle($event);
}
Expand Down
5 changes: 3 additions & 2 deletions spec/Stub/DomainGeneratorSpec.php
Expand Up @@ -4,6 +4,7 @@

use League\Event\EventInterface;
use PhpSpec\ObjectBehavior;
use stdClass;

class DomainGeneratorSpec extends ObjectBehavior
{
Expand All @@ -13,9 +14,9 @@ public function it_is_initializable()
$this->shouldHaveType('League\Event\GeneratorInterface');
}

public function it_should_record_an_event_internally(EventInterface $event)
public function it_should_record_an_event_internally()
{
$this->recordAnEvent($event);
$this->recordAnEvent($event = new stdClass());
$this->releaseEvents()->shouldReturn([$event]);
}
}
53 changes: 1 addition & 52 deletions src/AbstractEvent.php
Expand Up @@ -4,59 +4,8 @@

abstract class AbstractEvent implements EventInterface
{
/**
* Has propagation stopped?
*
* @var bool
*/
protected $propagationStopped = false;
use PropagationAwareBehaviour, EmitterAwareBehaviour;

/**
* The emitter instance.
*
* @var EmitterInterface|null
*/
protected $emitter;

/**
* @inheritdoc
*/
public function setEmitter(EmitterInterface $emitter)
{
$this->emitter = $emitter;

return $this;
}

/**
* @inheritdoc
*/
public function getEmitter()
{
return $this->emitter;
}

/**
* @inheritdoc
*/
public function stopPropagation()
{
$this->propagationStopped = true;

return $this;
}

/**
* @inheritdoc
*/
public function isPropagationStopped()
{
return $this->propagationStopped;
}

/**
* @inheritdoc
*/
public function getName()
{
return get_class($this);
Expand Down
2 changes: 1 addition & 1 deletion src/BufferedEmitter.php
Expand Up @@ -5,7 +5,7 @@
class BufferedEmitter extends Emitter
{
/**
* @var EventInterface[]
* @var object[]
*/
protected $bufferedEvents = [];

Expand Down
2 changes: 1 addition & 1 deletion src/CallbackListener.php
Expand Up @@ -34,7 +34,7 @@ public function getCallback()
/**
* @inheritdoc
*/
public function handle(EventInterface $event)
public function handle($event)
{
call_user_func_array($this->callback, func_get_args());
}
Expand Down
43 changes: 24 additions & 19 deletions src/Emitter.php
Expand Up @@ -40,7 +40,7 @@ public function addListener($event, $listener, $priority = self::P_NORMAL)
public function addOneTimeListener($event, $listener, $priority = self::P_NORMAL)
{
$listener = $this->ensureListener($listener);
$listener = new OneTimeListener($listener);
$listener = new OneTimeListener($listener, $this);

return $this->addListener($event, $listener, $priority);
}
Expand All @@ -50,8 +50,7 @@ public function addOneTimeListener($event, $listener, $priority = self::P_NORMAL
*/
public function useListenerProvider(ListenerProviderInterface $provider)
{
$acceptor = new ListenerAcceptor($this);
$provider->provideListeners($acceptor);
$provider->provideListeners(new ListenerAcceptor($this));

return $this;
}
Expand Down Expand Up @@ -113,15 +112,15 @@ protected function ensureListener($listener)
return CallbackListener::fromCallable($listener);
}

throw new InvalidArgumentException('Listeners should be ListenerInterface, Closure or callable. Received type: '.gettype($listener));
throw new InvalidArgumentException('Listeners should be ListenerInterface, Closure or callable. Received type: ' . gettype($listener));
}

/**
* @inheritdoc
*/
public function hasListeners($event)
{
if (! isset($this->listeners[$event]) || count($this->listeners[$event]) === 0) {
if ( ! isset($this->listeners[$event]) || count($this->listeners[$event]) === 0) {
return false;
}

Expand Down Expand Up @@ -149,7 +148,7 @@ public function getListeners($event)
*/
protected function getSortedListeners($event)
{
if (! $this->hasListeners($event)) {
if ( ! $this->hasListeners($event)) {
return [];
}

Expand Down Expand Up @@ -199,18 +198,19 @@ public function emitGeneratedEvents(GeneratorInterface $generator)
/**
* Invoke the listeners for an event.
*
* @param string $name
* @param EventInterface $event
* @param array $arguments
* @param string $name
* @param object $event
* @param array $arguments
*
* @return void
*/
protected function invokeListeners($name, EventInterface $event, array $arguments)
protected function invokeListeners($name, $event, array $arguments)
{
$listeners = $this->getListeners($name);
$canStopPropagation = $event instanceof PropagationAwareInterface;

foreach ($listeners as $listener) {
if ($event->isPropagationStopped()) {
if ($canStopPropagation && $event->isPropagationStopped()) {
break;
}

Expand All @@ -221,36 +221,41 @@ protected function invokeListeners($name, EventInterface $event, array $argument
/**
* Prepare an event for emitting.
*
* @param string|EventInterface $event
* @param string|object $event
*
* @return array
*/
protected function prepareEvent($event)
{
$event = $this->ensureEvent($event);
$name = $event->getName();
$event->setEmitter($this);
$name = $event instanceof HasEventNameInterface
? $event->getName()
: get_class($event);

if ($event instanceof EmitterAwareInterface) {
$event->setEmitter($this);
}

return [$name, $event];
}

/**
* Ensure event input is of type EventInterface or convert it.
* Ensure event input is a valid event or convert it.
*
* @param string|EventInterface $event
* @param string|object $event
*
* @throws InvalidArgumentException
*
* @return EventInterface
* @return object
*/
protected function ensureEvent($event)
{
if (is_string($event)) {
return Event::named($event);
}

if (! $event instanceof EventInterface) {
throw new InvalidArgumentException('Events should be provides as Event instances or string, received type: '.gettype($event));
if ( ! is_object($event)) {
throw new InvalidArgumentException('Events should be provided as object or string, received type: ' . gettype($event));
}

return $event;
Expand Down
25 changes: 25 additions & 0 deletions src/EmitterAwareBehaviour.php
@@ -0,0 +1,25 @@
<?php

namespace League\Event;

trait EmitterAwareBehaviour
{
/**
* The emitter instance.
*
* @var EmitterInterface|null
*/
protected $emitter;

public function setEmitter(EmitterInterface $emitter = null)
{
$this->emitter = $emitter;

return $this;
}

public function getEmitter()
{
return $this->emitter;
}
}
2 changes: 1 addition & 1 deletion src/EmitterAwareTrait.php
Expand Up @@ -32,7 +32,7 @@ public function setEmitter(EmitterInterface $emitter = null)
*/
public function getEmitter()
{
if (! $this->emitter) {
if ( ! $this->emitter) {
$this->emitter = new Emitter();
}

Expand Down

0 comments on commit ccbaff9

Please sign in to comment.