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>
|
2018-10-31 05:40:28 +00:00
|
|
|
*
|
|
|
|
* @experimental in 4.2
|
2018-04-08 15:33:26 +01:00
|
|
|
*/
|
|
|
|
class TraceableMessageBus implements MessageBusInterface
|
|
|
|
{
|
|
|
|
private $decoratedBus;
|
|
|
|
private $dispatchedMessages = array();
|
|
|
|
|
|
|
|
public function __construct(MessageBusInterface $decoratedBus)
|
|
|
|
{
|
|
|
|
$this->decoratedBus = $decoratedBus;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2018-10-25 11:15:03 +01:00
|
|
|
public function dispatch($message): Envelope
|
2018-04-08 15:33:26 +01:00
|
|
|
{
|
2018-10-17 16:45:44 +01:00
|
|
|
$envelope = $message instanceof Envelope ? $message : new Envelope($message);
|
|
|
|
$context = array(
|
|
|
|
'stamps' => array_values($envelope->all()),
|
|
|
|
'message' => $envelope->getMessage(),
|
|
|
|
'caller' => $this->getCaller(),
|
|
|
|
'callTime' => microtime(true),
|
|
|
|
);
|
2018-05-07 19:03:43 +01:00
|
|
|
|
2018-04-08 15:33:26 +01:00
|
|
|
try {
|
2018-10-25 11:15:03 +01:00
|
|
|
return $this->decoratedBus->dispatch($message);
|
2018-04-08 15:33:26 +01:00
|
|
|
} catch (\Throwable $e) {
|
2018-10-17 16:45:44 +01:00
|
|
|
$context['exception'] = $e;
|
2018-04-08 15:33:26 +01:00
|
|
|
|
|
|
|
throw $e;
|
2018-10-17 16:45:44 +01:00
|
|
|
} finally {
|
|
|
|
$this->dispatchedMessages[] = $context;
|
2018-04-08 15:33:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getDispatchedMessages(): array
|
|
|
|
{
|
|
|
|
return $this->dispatchedMessages;
|
|
|
|
}
|
2018-05-01 13:54:27 +01:00
|
|
|
|
|
|
|
public function reset()
|
|
|
|
{
|
|
|
|
$this->dispatchedMessages = array();
|
|
|
|
}
|
2018-05-22 18:48:15 +01:00
|
|
|
|
|
|
|
private function getCaller(): array
|
|
|
|
{
|
|
|
|
$trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 8);
|
|
|
|
|
|
|
|
$file = $trace[1]['file'];
|
|
|
|
$line = $trace[1]['line'];
|
|
|
|
|
|
|
|
for ($i = 2; $i < 8; ++$i) {
|
|
|
|
if (isset($trace[$i]['class'], $trace[$i]['function'])
|
|
|
|
&& 'dispatch' === $trace[$i]['function']
|
|
|
|
&& is_a($trace[$i]['class'], MessageBusInterface::class, true)
|
|
|
|
) {
|
|
|
|
$file = $trace[$i]['file'];
|
|
|
|
$line = $trace[$i]['line'];
|
|
|
|
|
|
|
|
while (++$i < 8) {
|
2018-10-17 16:45:44 +01:00
|
|
|
if (isset($trace[$i]['function'], $trace[$i]['file']) && empty($trace[$i]['class']) && 0 !== strpos($trace[$i]['function'], 'call_user_func')) {
|
2018-05-22 18:48:15 +01:00
|
|
|
$file = $trace[$i]['file'];
|
|
|
|
$line = $trace[$i]['line'];
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$name = str_replace('\\', '/', $file);
|
|
|
|
$name = substr($name, strrpos($name, '/') + 1);
|
|
|
|
|
|
|
|
return compact('name', 'file', 'line');
|
|
|
|
}
|
2018-04-08 15:33:26 +01:00
|
|
|
}
|