2018-04-08 15:33:26 +01:00
|
|
|
<?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 Samuel Roze <samuel.roze@gmail.com>
|
|
|
|
*/
|
|
|
|
class TraceableMessageBus implements MessageBusInterface
|
|
|
|
{
|
|
|
|
private $decoratedBus;
|
|
|
|
private $dispatchedMessages = array();
|
|
|
|
|
|
|
|
public function __construct(MessageBusInterface $decoratedBus)
|
|
|
|
{
|
|
|
|
$this->decoratedBus = $decoratedBus;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function dispatch($message)
|
|
|
|
{
|
2018-05-08 17:54:52 +01:00
|
|
|
$callTime = microtime(true);
|
2018-05-07 19:03:43 +01:00
|
|
|
$messageToTrace = $message instanceof Envelope ? $message->getMessage() : $message;
|
2018-05-08 17:54:52 +01:00
|
|
|
$envelopeItems = $message instanceof Envelope ? array_values($message->all()) : null;
|
2018-05-07 19:03:43 +01:00
|
|
|
|
2018-04-08 15:33:26 +01:00
|
|
|
try {
|
|
|
|
$result = $this->decoratedBus->dispatch($message);
|
|
|
|
|
|
|
|
$this->dispatchedMessages[] = array(
|
2018-05-08 17:54:52 +01:00
|
|
|
'envelopeItems' => $envelopeItems,
|
2018-05-07 19:03:43 +01:00
|
|
|
'message' => $messageToTrace,
|
2018-04-08 15:33:26 +01:00
|
|
|
'result' => $result,
|
2018-05-08 17:54:52 +01:00
|
|
|
'callTime' => $callTime,
|
2018-04-08 15:33:26 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
} catch (\Throwable $e) {
|
|
|
|
$this->dispatchedMessages[] = array(
|
2018-05-08 17:54:52 +01:00
|
|
|
'envelopeItems' => $envelopeItems,
|
2018-05-07 19:03:43 +01:00
|
|
|
'message' => $messageToTrace,
|
2018-04-08 15:33:26 +01:00
|
|
|
'exception' => $e,
|
2018-05-08 17:54:52 +01:00
|
|
|
'callTime' => $callTime,
|
2018-04-08 15:33:26 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
throw $e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getDispatchedMessages(): array
|
|
|
|
{
|
|
|
|
return $this->dispatchedMessages;
|
|
|
|
}
|
2018-05-01 13:54:27 +01:00
|
|
|
|
|
|
|
public function reset()
|
|
|
|
{
|
|
|
|
$this->dispatchedMessages = array();
|
|
|
|
}
|
2018-04-08 15:33:26 +01:00
|
|
|
}
|