Skip to content

Commit

Permalink
added initial console implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
ivannis committed Jun 17, 2016
1 parent 9f8fa7d commit a7ec552
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 18 deletions.
13 changes: 6 additions & 7 deletions Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Cubiche\Core\EventDispatcher;

/**
Expand All @@ -23,7 +22,7 @@ class Event implements EventInterface
*
* @var string
*/
protected $name;
protected $eventName;

/**
* Has propagation stopped?
Expand All @@ -35,19 +34,19 @@ class Event implements EventInterface
/**
* Create a new event instance.
*
* @param string $name
* @param string $eventName
*/
public function __construct($name = null)
public function __construct($eventName = null)
{
$this->name = $name;
$this->eventName = $eventName;
}

/**
* {@inheritdoc}
*/
public function name()
public function eventName()
{
return $this->name ? $this->name : get_class($this);
return $this->eventName ? $this->eventName : get_class($this);
}

/**
Expand Down
17 changes: 16 additions & 1 deletion EventDispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,26 @@ public function dispatch($event)
{
$event = $this->ensureEvent($event);

$eventName = $event->name();
// pre dispatch event
$preDispatchEvent = new PreDispatchEvent($event);
$eventName = $preDispatchEvent->eventName();
if ($listeners = $this->eventListeners($eventName)) {
$this->doDispatch($listeners, $preDispatchEvent);
}

// dispatch event
$eventName = $event->eventName();
if ($listeners = $this->eventListeners($eventName)) {
$this->doDispatch($listeners, $event);
}

// post dispatch event
$postDispatchEvent = new PostDispatchEvent($event);
$eventName = $postDispatchEvent->eventName();
if ($listeners = $this->eventListeners($eventName)) {
$this->doDispatch($listeners, $postDispatchEvent);
}

return $event;
}

Expand Down
3 changes: 1 addition & 2 deletions EventInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Cubiche\Core\EventDispatcher;

use Cubiche\Core\Serializer\SerializableInterface;
Expand Down Expand Up @@ -39,5 +38,5 @@ public function isPropagationStopped();
*
* @return string
*/
public function name();
public function eventName();
}
42 changes: 42 additions & 0 deletions PostDispatchEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

/**
* This file is part of the Cubiche package.
*
* Copyright (c) Cubiche
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Cubiche\Core\EventDispatcher;

/**
* PostDispatchEvent class.
*
* @author Ivannis Suárez Jerez <ivannis.suarez@gmail.com>
*/
class PostDispatchEvent extends Event
{
/**
* @var EventInterface
*/
protected $event;

/**
* PreDispatchEvent constructor.
*
* @param EventInterface $event
*/
public function __construct(EventInterface $event)
{
$this->event = $event;
}

/**
* @return EventInterface
*/
public function event()
{
return $this->event;
}
}
42 changes: 42 additions & 0 deletions PreDispatchEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

/**
* This file is part of the Cubiche package.
*
* Copyright (c) Cubiche
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Cubiche\Core\EventDispatcher;

/**
* PreDispatchEvent class.
*
* @author Ivannis Suárez Jerez <ivannis.suarez@gmail.com>
*/
class PreDispatchEvent extends Event
{
/**
* @var EventInterface
*/
protected $event;

/**
* PreDispatchEvent constructor.
*
* @param EventInterface $event
*/
public function __construct(EventInterface $event)
{
$this->event = $event;
}

/**
* @return EventInterface
*/
public function event()
{
return $this->event;
}
}
10 changes: 5 additions & 5 deletions Tests/Units/EventDispatcherTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,8 @@ public function testDispatch()
$this
->given($dispatcher = $this->createEventDispatcher())
->and($event = new LoginUserEvent('ivan@cubiche.com'))
->and($dispatcher->addListener($event->name(), array(new LoginUserEventListener(), 'onLogin')))
->and($dispatcher->addListener($event->name(), function (LoginUserEvent $event) {
->and($dispatcher->addListener($event->eventName(), array(new LoginUserEventListener(), 'onLogin')))
->and($dispatcher->addListener($event->eventName(), function (LoginUserEvent $event) {
$this
->string($event->email())
->isEqualTo('info@cubiche.org')
Expand Down Expand Up @@ -193,7 +193,7 @@ public function testListenerPriority()
->given($dispatcher = $this->createEventDispatcher())
->and($listener1 = array(new LoginUserEventListener(), 'onLogin'))
->and($listener2 = function (Event $event) {
return $event->name();
return $event->eventName();
})
->and($listener3 = function (Event $event) {

Expand Down Expand Up @@ -222,7 +222,7 @@ public function testHasListeners()
->given($dispatcher = $this->createEventDispatcher())
->and($listener1 = array(new LoginUserEventListener(), 'onLogin'))
->and($listener2 = function (Event $event) {
return $event->name();
return $event->eventName();
})
->and($listener3 = function (Event $event) {

Expand All @@ -249,7 +249,7 @@ public function testRemoveListener()
->given($dispatcher = $this->createEventDispatcher())
->and($listener1 = array(new LoginUserEventListener(), 'onLogin'))
->and($listener2 = function (Event $event) {
return $event->name();
return $event->eventName();
})
->and($listener3 = function (Event $event) {

Expand Down
6 changes: 3 additions & 3 deletions Tests/Units/EventTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ public function testName()
{
$this
->given($event = new Event())
->when($name = $event->name())
->when($name = $event->eventName())
->then()
->string($name)
->isEqualTo(Event::class)
;

$this
->given($event = new Event('foo.event'))
->when($name = $event->name())
->when($name = $event->eventName())
->then()
->string($name)
->isEqualTo('foo.event')
Expand All @@ -48,7 +48,7 @@ public function testNamed()
{
$this
->given($event = Event::named('foo.event'))
->when($name = $event->name())
->when($name = $event->eventName())
->then()
->string($name)
->isEqualTo('foo.event')
Expand Down

0 comments on commit a7ec552

Please sign in to comment.