-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #171 from prooph/event_bus_fixes
Fix Bugs in Event Bus
- Loading branch information
Showing
7 changed files
with
304 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
/** | ||
* This file is part of the prooph/service-bus. | ||
* (c) 2014-2017 prooph software GmbH <[email protected]> | ||
* (c) 2015-2017 Sascha-Oliver Prolic <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace ProophTest\ServiceBus\Mock; | ||
|
||
final class CustomMessageEventHandler2 | ||
{ | ||
private $lastMessage; | ||
|
||
private $invokeCounter = 0; | ||
|
||
public function on($message): void | ||
{ | ||
$this->lastMessage = $message; | ||
$this->invokeCounter++; | ||
} | ||
|
||
public function getLastMessage() | ||
{ | ||
return $this->lastMessage; | ||
} | ||
|
||
public function getInvokeCounter(): int | ||
{ | ||
return $this->invokeCounter; | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
tests/Mock/CustomMessageEventHandlerThrowingExceptions.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
/** | ||
* This file is part of the prooph/service-bus. | ||
* (c) 2014-2017 prooph software GmbH <[email protected]> | ||
* (c) 2015-2017 Sascha-Oliver Prolic <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace ProophTest\ServiceBus\Mock; | ||
|
||
final class CustomMessageEventHandlerThrowingExceptions | ||
{ | ||
public function onEvent($message): void | ||
{ | ||
throw new \Exception('bar'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
/** | ||
* This file is part of the prooph/service-bus. | ||
* (c) 2014-2017 prooph software GmbH <[email protected]> | ||
* (c) 2015-2017 Sascha-Oliver Prolic <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace ProophTest\ServiceBus\Mock; | ||
|
||
use Prooph\Common\Event\ActionEvent; | ||
use Prooph\ServiceBus\EventBus; | ||
use Prooph\ServiceBus\MessageBus; | ||
use Prooph\ServiceBus\Plugin\AbstractPlugin; | ||
|
||
final class CustomOnEventStrategy extends AbstractPlugin | ||
{ | ||
public function attachToMessageBus(MessageBus $messageBus): void | ||
{ | ||
$this->listenerHandlers[] = $messageBus->attach( | ||
MessageBus::EVENT_DISPATCH, | ||
function (ActionEvent $actionEvent): void { | ||
$target = $actionEvent->getTarget(); | ||
$message = $actionEvent->getParam(MessageBus::EVENT_PARAM_MESSAGE); | ||
$handlers = $actionEvent->getParam(EventBus::EVENT_PARAM_EVENT_LISTENERS, []); | ||
|
||
foreach ($handlers as $handler) { | ||
if (is_callable($handler) || ! is_object($handler) || ! is_callable([$handler, 'on'])) { | ||
continue; | ||
} | ||
|
||
try { | ||
$handler->on($message); | ||
} catch (\Throwable $e) { | ||
if ($target->isCollectingExceptions()) { | ||
$target->addCollectedException($e); | ||
} else { | ||
throw $e; | ||
} | ||
} | ||
} | ||
|
||
$actionEvent->setParam(MessageBus::EVENT_PARAM_MESSAGE_HANDLED, true); | ||
}, | ||
MessageBus::PRIORITY_INVOKE_HANDLER | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters