| 199 |
lars |
1 |
<?php namespace Clockwork\DataSource;
|
|
|
2 |
|
|
|
3 |
use Clockwork\DataSource\DataSource;
|
|
|
4 |
use Clockwork\Helpers\Serializer;
|
|
|
5 |
use Clockwork\Request\Request;
|
|
|
6 |
|
|
|
7 |
// Data source providing data obtainable in vanilla PHP
|
|
|
8 |
class PhpDataSource extends DataSource
|
|
|
9 |
{
|
|
|
10 |
// Adds request, response information, session data and peak memory usage to the request
|
|
|
11 |
public function resolve(Request $request)
|
|
|
12 |
{
|
|
|
13 |
$request->time = PHP_SAPI !== 'cli' ? $this->getRequestTime() : $request->time;
|
|
|
14 |
$request->method = $this->getRequestMethod();
|
|
|
15 |
$request->url = $this->getRequestUrl();
|
|
|
16 |
$request->uri = $this->getRequestUri();
|
|
|
17 |
$request->headers = $this->getRequestHeaders();
|
|
|
18 |
$request->getData = $this->getGetData();
|
|
|
19 |
$request->postData = $this->getPostData();
|
|
|
20 |
$request->requestData = $this->getRequestData();
|
|
|
21 |
$request->sessionData = $this->getSessionData();
|
|
|
22 |
$request->cookies = $this->getCookies();
|
|
|
23 |
$request->responseStatus = $this->getResponseStatus();
|
|
|
24 |
$request->responseTime = $this->getResponseTime();
|
|
|
25 |
$request->memoryUsage = $this->getMemoryUsage();
|
|
|
26 |
|
|
|
27 |
return $request;
|
|
|
28 |
}
|
|
|
29 |
|
|
|
30 |
// Get the request cookies (normalized with passwords removed)
|
|
|
31 |
protected function getCookies()
|
|
|
32 |
{
|
|
|
33 |
return $this->removePasswords((new Serializer)->normalizeEach($_COOKIE));
|
|
|
34 |
}
|
|
|
35 |
|
|
|
36 |
// Get the request GET data (normalized with passwords removed)
|
|
|
37 |
protected function getGetData()
|
|
|
38 |
{
|
|
|
39 |
return $this->removePasswords((new Serializer)->normalizeEach($_GET));
|
|
|
40 |
}
|
|
|
41 |
|
|
|
42 |
// Get the request POST data (normalized with passwords removed)
|
|
|
43 |
protected function getPostData()
|
|
|
44 |
{
|
|
|
45 |
return $this->removePasswords((new Serializer)->normalizeEach($_POST));
|
|
|
46 |
}
|
|
|
47 |
|
|
|
48 |
// Get the request body data (attempt to parse as json, normalized with passwords removed)
|
|
|
49 |
protected function getRequestData()
|
|
|
50 |
{
|
|
|
51 |
// The data will already be parsed into POST data by PHP in case of application/x-www-form-urlencoded requests
|
|
|
52 |
if (count($_POST)) return;
|
|
|
53 |
|
|
|
54 |
$requestData = file_get_contents('php://input');
|
|
|
55 |
$requestJsonData = json_decode($requestData, true);
|
|
|
56 |
|
|
|
57 |
return is_array($requestJsonData)
|
|
|
58 |
? $this->removePasswords((new Serializer)->normalizeEach($requestJsonData))
|
|
|
59 |
: $requestData;
|
|
|
60 |
}
|
|
|
61 |
|
|
|
62 |
// Get the request headers
|
|
|
63 |
protected function getRequestHeaders()
|
|
|
64 |
{
|
|
|
65 |
$headers = [];
|
|
|
66 |
|
|
|
67 |
foreach ($_SERVER as $key => $value) {
|
|
|
68 |
if (substr($key, 0, 5) !== 'HTTP_') continue;
|
|
|
69 |
|
|
|
70 |
$header = substr($key, 5);
|
|
|
71 |
$header = str_replace('_', ' ', $header);
|
|
|
72 |
$header = ucwords(strtolower($header));
|
|
|
73 |
$header = str_replace(' ', '-', $header);
|
|
|
74 |
|
|
|
75 |
if (! isset($headers[$header])) {
|
|
|
76 |
$headers[$header] = [ $value ];
|
|
|
77 |
} else {
|
|
|
78 |
$headers[$header][] = $value;
|
|
|
79 |
}
|
|
|
80 |
}
|
|
|
81 |
|
|
|
82 |
ksort($headers);
|
|
|
83 |
|
|
|
84 |
return $headers;
|
|
|
85 |
}
|
|
|
86 |
|
|
|
87 |
// Get the request method
|
|
|
88 |
protected function getRequestMethod()
|
|
|
89 |
{
|
|
|
90 |
if (isset($_SERVER['REQUEST_METHOD'])) {
|
|
|
91 |
return $_SERVER['REQUEST_METHOD'];
|
|
|
92 |
}
|
|
|
93 |
}
|
|
|
94 |
|
|
|
95 |
// Get the response time
|
|
|
96 |
protected function getRequestTime()
|
|
|
97 |
{
|
|
|
98 |
if (isset($_SERVER['REQUEST_TIME_FLOAT'])) {
|
|
|
99 |
return $_SERVER['REQUEST_TIME_FLOAT'];
|
|
|
100 |
}
|
|
|
101 |
}
|
|
|
102 |
|
|
|
103 |
// Get the request URL
|
|
|
104 |
protected function getRequestUrl()
|
|
|
105 |
{
|
|
|
106 |
$https = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on';
|
|
|
107 |
$host = isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : null;
|
|
|
108 |
$addr = isset($_SERVER['SERVER_ADDR']) ? $_SERVER['SERVER_ADDR'] : null;
|
|
|
109 |
$port = isset($_SERVER['SERVER_PORT']) ? $_SERVER['SERVER_PORT'] : null;
|
|
|
110 |
$uri = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : null;
|
|
|
111 |
|
|
|
112 |
$scheme = $https ? 'https' : 'http';
|
|
|
113 |
$host = $host ?: $addr;
|
|
|
114 |
$port = (! $https && $port != 80 || $https && $port != 443) ? ":{$port}" : '';
|
|
|
115 |
|
|
|
116 |
// remove port number from the host
|
|
|
117 |
$host = $host ? preg_replace('/:\d+$/', '', trim($host)) : null;
|
|
|
118 |
|
|
|
119 |
return "{$scheme}://{$host}{$port}{$uri}";
|
|
|
120 |
}
|
|
|
121 |
|
|
|
122 |
// Get the request URI
|
|
|
123 |
protected function getRequestUri()
|
|
|
124 |
{
|
|
|
125 |
if (isset($_SERVER['REQUEST_URI'])) {
|
|
|
126 |
return $_SERVER['REQUEST_URI'];
|
|
|
127 |
}
|
|
|
128 |
}
|
|
|
129 |
|
|
|
130 |
// Get the response status code
|
|
|
131 |
protected function getResponseStatus()
|
|
|
132 |
{
|
|
|
133 |
return http_response_code();
|
|
|
134 |
}
|
|
|
135 |
|
|
|
136 |
// Get the response time (current time, assuming most of the application code has already run at this point)
|
|
|
137 |
protected function getResponseTime()
|
|
|
138 |
{
|
|
|
139 |
return microtime(true);
|
|
|
140 |
}
|
|
|
141 |
|
|
|
142 |
// Get the session data (normalized with passwords removed)
|
|
|
143 |
protected function getSessionData()
|
|
|
144 |
{
|
|
|
145 |
if (! isset($_SESSION)) return [];
|
|
|
146 |
|
|
|
147 |
return $this->removePasswords((new Serializer)->normalizeEach($_SESSION));
|
|
|
148 |
}
|
|
|
149 |
|
|
|
150 |
// Get the peak memory usage in bytes
|
|
|
151 |
protected function getMemoryUsage()
|
|
|
152 |
{
|
|
|
153 |
return memory_get_peak_usage(true);
|
|
|
154 |
}
|
|
|
155 |
}
|