-
Notifications
You must be signed in to change notification settings - Fork 177
/
Copy pathRequestFetcher.php
76 lines (65 loc) · 2.16 KB
/
RequestFetcher.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<?php
declare(strict_types=1);
namespace Sentry\SentryBundle\Integration;
use GuzzleHttp\Psr7\HttpFactory;
use Psr\Http\Message\ServerRequestInterface;
use Sentry\Integration\RequestFetcherInterface;
use Symfony\Bridge\PsrHttpMessage\Factory\PsrHttpFactory;
use Symfony\Bridge\PsrHttpMessage\HttpMessageFactoryInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
/**
* This class fetches the server request from the request stack and converts it
* into a PSR-7 request that is suitable to be used by the {@see \Sentry\Integration\RequestIntegration}
* integration.
*/
final class RequestFetcher implements RequestFetcherInterface
{
/**
* @var RequestStack The request stack
*/
private $requestStack;
/**
* @var Request|null The current request
*/
private $currentRequest;
/**
* @var HttpMessageFactoryInterface The factory to convert Symfony requests to PSR-7 requests
*/
private $httpMessageFactory;
/**
* Class constructor.
*
* @param RequestStack $requestStack The request stack
* @param HttpMessageFactoryInterface|null $httpMessageFactory The factory to convert Symfony requests to PSR-7 requests
*/
public function __construct(RequestStack $requestStack, ?HttpMessageFactoryInterface $httpMessageFactory = null)
{
$this->requestStack = $requestStack;
$this->httpMessageFactory = $httpMessageFactory ?? new PsrHttpFactory(
new HttpFactory(),
new HttpFactory(),
new HttpFactory(),
new HttpFactory()
);
}
/**
* {@inheritdoc}
*/
public function fetchRequest(): ?ServerRequestInterface
{
$request = $this->currentRequest ?? $this->requestStack->getCurrentRequest();
if (null === $request) {
return null;
}
try {
return $this->httpMessageFactory->createRequest($request);
} catch (\Throwable $exception) {
return null;
}
}
public function setRequest(?Request $request): void
{
$this->currentRequest = $request;
}
}