Skip to content

Commit 8cc58eb

Browse files
committed
Optional performance improvement
1 parent 7f06164 commit 8cc58eb

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

src/Tqdev/PhpCrudApi/Request.php

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,16 @@ class Request
99
private $params;
1010
private $body;
1111
private $headers;
12+
private $highPerformance;
1213

13-
public function __construct(String $method = null, String $path = null, String $query = null, array $headers = null, String $body = null)
14+
public function __construct(String $method = null, String $path = null, String $query = null, array $headers = null, String $body = null, bool $highPerformance = true)
1415
{
1516
$this->parseMethod($method);
1617
$this->parsePath($path);
1718
$this->parseParams($query);
1819
$this->parseHeaders($headers);
1920
$this->parseBody($body);
21+
$this->highPerformance = $highPerformance;
2022
}
2123

2224
private function parseMethod(String $method = null)
@@ -61,10 +63,12 @@ private function parseHeaders(array $headers = null)
6163
{
6264
if (!$headers) {
6365
$headers = array();
64-
foreach ($_SERVER as $name => $value) {
65-
if (substr($name, 0, 5) == 'HTTP_') {
66-
$key = str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))));
67-
$headers[$key] = $value;
66+
if (!$this->highPerformance) {
67+
foreach ($_SERVER as $name => $value) {
68+
if (substr($name, 0, 5) == 'HTTP_') {
69+
$key = str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))));
70+
$headers[$key] = $value;
71+
}
6872
}
6973
}
7074
}
@@ -135,6 +139,12 @@ public function getHeader(String $key): String
135139
if (isset($this->headers[$key])) {
136140
return $this->headers[$key];
137141
}
142+
if ($this->highPerformance) {
143+
$serverKey = 'HTTP_' . strtoupper(str_replace('_', '-', $key));
144+
if (isset($_SERVER[$serverKey])) {
145+
return $_SERVER[$serverKey];
146+
}
147+
}
138148
return '';
139149
}
140150

0 commit comments

Comments
 (0)