Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
0.00% covered (danger)
0.00%
0 / 1
60.00% covered (warning)
60.00%
3 / 5
CRAP
75.61% covered (warning)
75.61%
31 / 41
TraceableMessageBus
0.00% covered (danger)
0.00%
0 / 1
60.00% covered (warning)
60.00%
3 / 5
22.70
75.61% covered (warning)
75.61%
31 / 41
 __construct
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
2 / 2
 dispatch
100.00% covered (success)
100.00%
1 / 1
2
100.00% covered (success)
100.00%
10 / 10
 getDispatchedMessages
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
1 / 1
 reset
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 2
 getCaller
0.00% covered (danger)
0.00%
0 / 1
17.92
69.23% covered (warning)
69.23%
18 / 26
1<?php
2
3/*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <fabien@symfony.com>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12namespace Symfony\Component\Messenger;
13
14/**
15 * @author Samuel Roze <samuel.roze@gmail.com>
16 */
17class TraceableMessageBus implements MessageBusInterface
18{
19    private $decoratedBus;
20    private $dispatchedMessages = [];
21
22    public function __construct(MessageBusInterface $decoratedBus)
23    {
24        $this->decoratedBus = $decoratedBus;
25    }
26
27    /**
28     * {@inheritdoc}
29     */
30    public function dispatch($message, array $stamps = []): Envelope
31    {
32        $envelope = Envelope::wrap($message, $stamps);
33        $context = [
34            'stamps' => array_merge([], ...array_values($envelope->all())),
35            'message' => $envelope->getMessage(),
36            'caller' => $this->getCaller(),
37            'callTime' => microtime(true),
38        ];
39
40        try {
41            return $envelope = $this->decoratedBus->dispatch($message, $stamps);
42        } catch (\Throwable $e) {
43            $context['exception'] = $e;
44
45            throw $e;
46        } finally {
47            $this->dispatchedMessages[] = $context + ['stamps_after_dispatch' => array_merge([], ...array_values($envelope->all()))];
48        }
49    }
50
51    public function getDispatchedMessages(): array
52    {
53        return $this->dispatchedMessages;
54    }
55
56    public function reset()
57    {
58        $this->dispatchedMessages = [];
59    }
60
61    private function getCaller(): array
62    {
63        $trace = debug_backtrace(\DEBUG_BACKTRACE_IGNORE_ARGS, 8);
64
65        $file = $trace[1]['file'];
66        $line = $trace[1]['line'];
67
68        $handleTraitFile = (new \ReflectionClass(HandleTrait::class))->getFileName();
69        $found = false;
70        for ($i = 1; $i < 8; ++$i) {
71            if (isset($trace[$i]['file'], $trace[$i + 1]['file'], $trace[$i + 1]['line']) && $trace[$i]['file'] === $handleTraitFile) {
72                $file = $trace[$i + 1]['file'];
73                $line = $trace[$i + 1]['line'];
74                $found = true;
75
76                break;
77            }
78        }
79
80        for ($i = 2; $i < 8 && !$found; ++$i) {
81            if (isset($trace[$i]['class'], $trace[$i]['function'])
82                && 'dispatch' === $trace[$i]['function']
83                && is_a($trace[$i]['class'], MessageBusInterface::class, true)
84            ) {
85                $file = $trace[$i]['file'];
86                $line = $trace[$i]['line'];
87
88                while (++$i < 8) {
89                    if (isset($trace[$i]['function'], $trace[$i]['file']) && empty($trace[$i]['class']) && 0 !== strpos($trace[$i]['function'], 'call_user_func')) {
90                        $file = $trace[$i]['file'];
91                        $line = $trace[$i]['line'];
92
93                        break;
94                    }
95                }
96                break;
97            }
98        }
99
100        $name = str_replace('\\', '/', $file);
101        $name = substr($name, strrpos($name, '/') + 1);
102
103        return compact('name', 'file', 'line');
104    }
105}