Skip to content

Commit 3274515

Browse files
committed
Improve code quality
1 parent 8cc58eb commit 3274515

File tree

2 files changed

+47
-27
lines changed

2 files changed

+47
-27
lines changed

api.php

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -222,24 +222,29 @@ public function set(String $key, String $value, int $ttl = 0): bool
222222
}
223223
}
224224
$string = $ttl . '|' . $value;
225-
return file_put_contents($filename, $string, LOCK_EX) !== false;
225+
return $this->filePutContents($filename, $string) !== false;
226226
}
227227

228-
private function fileGetContents($path)
228+
private function filePutContents($filename, $string)
229229
{
230-
$f = fopen($path, 'r');
231-
if ($f === false) {
230+
return file_put_contents($filename, $string, LOCK_EX);
231+
}
232+
233+
private function fileGetContents($filename)
234+
{
235+
$file = fopen($filename, 'r');
236+
if ($file === false) {
232237
return false;
233238
}
234-
$locked = flock($f, LOCK_SH);
235-
if (!$locked) {
236-
fclose($f);
239+
$lock = flock($file, LOCK_SH);
240+
if (!$lock) {
241+
fclose($file);
237242
return false;
238243
}
239-
$data = file_get_contents($path);
240-
flock($f, LOCK_UN);
241-
fclose($f);
242-
return $data;
244+
$string = file_get_contents($filename);
245+
flock($file, LOCK_UN);
246+
fclose($file);
247+
return $string;
243248
}
244249

245250
private function getString($filename): String
@@ -4482,14 +4487,16 @@ class Request
44824487
private $params;
44834488
private $body;
44844489
private $headers;
4490+
private $highPerformance;
44854491

4486-
public function __construct(String $method = null, String $path = null, String $query = null, array $headers = null, String $body = null)
4492+
public function __construct(String $method = null, String $path = null, String $query = null, array $headers = null, String $body = null, bool $highPerformance = true)
44874493
{
44884494
$this->parseMethod($method);
44894495
$this->parsePath($path);
44904496
$this->parseParams($query);
44914497
$this->parseHeaders($headers);
44924498
$this->parseBody($body);
4499+
$this->highPerformance = $highPerformance;
44934500
}
44944501

44954502
private function parseMethod(String $method = null)
@@ -4534,10 +4541,12 @@ private function parseHeaders(array $headers = null)
45344541
{
45354542
if (!$headers) {
45364543
$headers = array();
4537-
foreach ($_SERVER as $name => $value) {
4538-
if (substr($name, 0, 5) == 'HTTP_') {
4539-
$key = str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))));
4540-
$headers[$key] = $value;
4544+
if (!$this->highPerformance) {
4545+
foreach ($_SERVER as $name => $value) {
4546+
if (substr($name, 0, 5) == 'HTTP_') {
4547+
$key = str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))));
4548+
$headers[$key] = $value;
4549+
}
45414550
}
45424551
}
45434552
}
@@ -4608,6 +4617,12 @@ public function getHeader(String $key): String
46084617
if (isset($this->headers[$key])) {
46094618
return $this->headers[$key];
46104619
}
4620+
if ($this->highPerformance) {
4621+
$serverKey = 'HTTP_' . strtoupper(str_replace('_', '-', $key));
4622+
if (isset($_SERVER[$serverKey])) {
4623+
return $_SERVER[$serverKey];
4624+
}
4625+
}
46114626
return '';
46124627
}
46134628

src/Tqdev/PhpCrudApi/Cache/TempFileCache.php

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -52,24 +52,29 @@ public function set(String $key, String $value, int $ttl = 0): bool
5252
}
5353
}
5454
$string = $ttl . '|' . $value;
55-
return file_put_contents($filename, $string, LOCK_EX) !== false;
55+
return $this->filePutContents($filename, $string) !== false;
5656
}
5757

58-
private function fileGetContents($path)
58+
private function filePutContents($filename, $string)
5959
{
60-
$f = fopen($path, 'r');
61-
if ($f === false) {
60+
return file_put_contents($filename, $string, LOCK_EX);
61+
}
62+
63+
private function fileGetContents($filename)
64+
{
65+
$file = fopen($filename, 'r');
66+
if ($file === false) {
6267
return false;
6368
}
64-
$locked = flock($f, LOCK_SH);
65-
if (!$locked) {
66-
fclose($f);
69+
$lock = flock($file, LOCK_SH);
70+
if (!$lock) {
71+
fclose($file);
6772
return false;
6873
}
69-
$data = file_get_contents($path);
70-
flock($f, LOCK_UN);
71-
fclose($f);
72-
return $data;
74+
$string = file_get_contents($filename);
75+
flock($file, LOCK_UN);
76+
fclose($file);
77+
return $string;
7378
}
7479

7580
private function getString($filename): String

0 commit comments

Comments
 (0)