-
-
Notifications
You must be signed in to change notification settings - Fork 333
Expand file tree
/
Copy pathPsrMessageDataSource.php
More file actions
93 lines (74 loc) · 2.61 KB
/
Copy pathPsrMessageDataSource.php
File metadata and controls
93 lines (74 loc) · 2.61 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<?php namespace Clockwork\DataSource;
use Clockwork\DataSource\DataSource;
use Clockwork\Helpers\Serializer;
use Clockwork\Request\Request;
use Psr\Http\Message\{ResponseInterface as PsrResponse, ServerRequestInterface as PsrRequest};
// Data source providing data obtainable from the PSR-7 request and response interfaces
class PsrMessageDataSource extends DataSource
{
// PSR Messages
protected $psrRequest;
protected $psrResponse;
// Create a new data source, takes PSR-7 request and response as arguments
public function __construct(?PsrRequest $psrRequest = null, ?PsrResponse $psrResponse = null)
{
$this->psrRequest = $psrRequest;
$this->psrResponse = $psrResponse;
}
// Adds request and response information to the request
public function resolve(Request $request)
{
if ($this->psrRequest) {
$request->method = $this->psrRequest->getMethod();
$request->uri = $this->getRequestUri();
$request->headers = $this->getRequestHeaders();
$request->getData = $this->sanitize($this->psrRequest->getQueryParams());
$request->postData = $this->sanitize($this->psrRequest->getParsedBody());
$request->cookies = $this->sanitize($this->psrRequest->getCookieParams());
$request->time = $this->getRequestTime();
}
if ($this->psrResponse !== null) {
$request->responseStatus = $this->psrResponse->getStatusCode();
$request->responseTime = $this->getResponseTime();
}
return $request;
}
// Normalize items in the array and remove passwords
protected function sanitize($data)
{
return is_array($data) ? $this->removePasswords((new Serializer)->normalizeEach($data)) : $data;
}
// Get the response time, fetching it from ServerParams
protected function getRequestTime()
{
$env = $this->psrRequest->getServerParams();
return $env['REQUEST_TIME_FLOAT'] ?? null;
}
// Get the response time (current time, assuming most of the application code has already run at this point)
protected function getResponseTime()
{
return microtime(true);
}
// Get the request headers
protected function getRequestHeaders()
{
$headers = [];
foreach ($this->psrRequest->getHeaders() as $header => $values) {
if (strtoupper(substr($header, 0, 5)) === 'HTTP_') {
$header = substr($header, 5);
}
$header = str_replace('_', ' ', $header);
$header = ucwords(strtolower($header));
$header = str_replace(' ', '-', $header);
$headers[$header] = $values;
}
ksort($headers);
return $headers;
}
// Get the request URI
protected function getRequestUri()
{
$uri = $this->psrRequest->getUri();
return $uri->getPath() . ($uri->getQuery() ? '?' . $uri->getQuery() : '');
}
}