-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
397 additions
and
0 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
41 changes: 41 additions & 0 deletions
41
src/Symfony/Component/Messenger/Exception/MessageHandlingException.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,41 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Messenger\Exception; | ||
|
||
/** | ||
* When handling messages, some handlers caused an exception. This exception | ||
* contains all those handler exceptions. | ||
* | ||
* @author Tobias Nyholm <tobias.nyholm@gmail.com> | ||
*/ | ||
class MessageHandlingException extends \RuntimeException implements ExceptionInterface | ||
{ | ||
private $exceptions = array(); | ||
|
||
public function __construct(array $exceptions) | ||
{ | ||
$message = sprintf( | ||
"Some handlers for recorded messages threw an exception. Their messages were: \n\n%s", | ||
implode(", \n", array_map(function (\Throwable $e) { | ||
return $e->getMessage(); | ||
}, $exceptions)) | ||
); | ||
|
||
$this->exceptions = $exceptions; | ||
parent::__construct($message); | ||
} | ||
|
||
public function getExceptions(): array | ||
{ | ||
return $this->exceptions; | ||
} | ||
} |
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,55 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Messenger; | ||
|
||
use Symfony\Contracts\Service\ResetInterface; | ||
|
||
/** | ||
* @author Tobias Nyholm <tobias.nyholm@gmail.com> | ||
* @author Matthias Noback <matthiasnoback@gmail.com> | ||
*/ | ||
class MessageRecorder implements MessageRecorderInterface, RecordedMessageCollectionInterface, ResetInterface | ||
{ | ||
private $messages = array(); | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getRecordedMessages(): array | ||
{ | ||
return $this->messages; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function resetRecordedMessages(): void | ||
{ | ||
$this->reset(); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function reset() | ||
{ | ||
$this->messages = array(); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function record($message): void | ||
{ | ||
$this->messages[] = $message; | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
src/Symfony/Component/Messenger/MessageRecorderInterface.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,26 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Messenger; | ||
|
||
/** | ||
* @author Tobias Nyholm <tobias.nyholm@gmail.com> | ||
* @author Matthias Noback <matthiasnoback@gmail.com> | ||
*/ | ||
interface MessageRecorderInterface | ||
{ | ||
/** | ||
* Record a message. | ||
* | ||
* @param object $message | ||
*/ | ||
public function record($message); | ||
} |
71 changes: 71 additions & 0 deletions
71
src/Symfony/Component/Messenger/Middleware/HandleRecordedMessageMiddleware.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,71 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Messenger\Middleware; | ||
|
||
use Symfony\Component\Messenger\Exception\MessageHandlingException; | ||
use Symfony\Component\Messenger\MessageBusInterface; | ||
use Symfony\Component\Messenger\RecordedMessageCollectionInterface; | ||
|
||
/** | ||
* A middleware that takes all recorded messages and dispatch them to the bus. | ||
* | ||
* @author Tobias Nyholm <tobias.nyholm@gmail.com> | ||
* @author Matthias Noback <matthiasnoback@gmail.com> | ||
*/ | ||
class HandleRecordedMessageMiddleware implements MiddlewareInterface | ||
{ | ||
private $messageRecorder; | ||
private $messageBus; | ||
|
||
public function __construct(MessageBusInterface $messageBus, RecordedMessageCollectionInterface $messageRecorder) | ||
{ | ||
$this->messageRecorder = $messageRecorder; | ||
$this->messageBus = $messageBus; | ||
} | ||
|
||
public function handle($message, callable $next) | ||
{ | ||
// Make sure the recorder is empty before we begin | ||
$this->messageRecorder->resetRecordedMessages(); | ||
|
||
try { | ||
$returnData = $next($message); | ||
} catch (\Throwable $exception) { | ||
$this->messageRecorder->resetRecordedMessages(); | ||
|
||
throw $exception; | ||
} | ||
|
||
$exceptions = array(); | ||
while (!empty($recordedMessages = $this->messageRecorder->getRecordedMessages())) { | ||
$this->messageRecorder->resetRecordedMessages(); | ||
// Assert: The message recorder is empty, all messages are in $recordedMessages | ||
|
||
foreach ($recordedMessages as $recordedMessage) { | ||
try { | ||
$this->messageBus->dispatch($recordedMessage); | ||
} catch (\Throwable $exception) { | ||
$exceptions[] = $exception; | ||
} | ||
} | ||
} | ||
|
||
if (!empty($exceptions)) { | ||
if (1 === count($exceptions)) { | ||
throw $exceptions[0]; | ||
} | ||
throw new MessageHandlingException($exceptions); | ||
} | ||
|
||
return $returnData; | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
src/Symfony/Component/Messenger/RecordedMessageCollectionInterface.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,31 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Messenger; | ||
|
||
/** | ||
* @author Tobias Nyholm <tobias.nyholm@gmail.com> | ||
* @author Matthias Noback <matthiasnoback@gmail.com> | ||
*/ | ||
interface RecordedMessageCollectionInterface | ||
{ | ||
/** | ||
* Fetch recorded messages. | ||
* | ||
* @return object[] | ||
*/ | ||
public function getRecordedMessages(): array; | ||
|
||
/** | ||
* Remove all recorded messages. | ||
*/ | ||
public function resetRecordedMessages(): void; | ||
} |
Oops, something went wrong.