|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Arki\RequestId library. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Alexandru Furculita <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* This source file is subject to the MIT license that is bundled |
|
9
|
|
|
* with this source code in the file LICENSE.md. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Arki\RequestId\Integrations\Symfony\EventSubscribers; |
|
13
|
|
|
|
|
14
|
|
|
use Arki\RequestId\Providers\RequestAware; |
|
15
|
|
|
use Arki\RequestId\Providers\RequestIdProviderFactory; |
|
16
|
|
|
use Arki\RequestId\RequestId; |
|
17
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
|
18
|
|
|
use Symfony\Component\HttpKernel\Event\GetResponseEvent; |
|
19
|
|
|
use Symfony\Component\HttpKernel\HttpKernelInterface; |
|
20
|
|
|
use Symfony\Component\HttpKernel\KernelEvents; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Adds request ID on requests. |
|
24
|
|
|
*/ |
|
25
|
|
|
final class AddsRequestIdOnRequest implements EventSubscriberInterface |
|
26
|
|
|
{ |
|
27
|
|
|
/** |
|
28
|
|
|
* @var RequestIdProviderFactory |
|
29
|
|
|
*/ |
|
30
|
|
|
private $providerFactory; |
|
31
|
|
|
/** |
|
32
|
|
|
* @var string |
|
33
|
|
|
*/ |
|
34
|
|
|
private $requestHeader; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @param RequestIdProviderFactory $providerFactory |
|
38
|
|
|
* @param string $requestHeader |
|
39
|
|
|
*/ |
|
40
|
|
|
public function __construct(RequestIdProviderFactory $providerFactory, $requestHeader = RequestId::HEADER_NAME) |
|
41
|
|
|
{ |
|
42
|
|
|
$this->providerFactory = $providerFactory; |
|
43
|
|
|
$this->requestHeader = $requestHeader; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @param GetResponseEvent $event |
|
48
|
|
|
*/ |
|
49
|
|
|
public function onRequest(GetResponseEvent $event) |
|
50
|
|
|
{ |
|
51
|
|
|
if (!$event->getRequestType() === HttpKernelInterface::MASTER_REQUEST) { |
|
52
|
|
|
return; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
if ($this->providerFactory instanceof RequestAware) { |
|
56
|
|
|
$this->providerFactory->setRequest($event->getRequest()); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
$provider = $this->providerFactory->create(); |
|
60
|
|
|
|
|
61
|
|
|
if (null === $provider->getRequestId()) { |
|
62
|
|
|
return; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
$event->getRequest()->headers->set($this->requestHeader, $provider->getRequestId()); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* {@inheritdoc} |
|
70
|
|
|
*/ |
|
71
|
|
|
public static function getSubscribedEvents() |
|
72
|
|
|
{ |
|
73
|
|
|
return [KernelEvents::REQUEST => ['onRequest', 200]]; |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|