Skip to content
This repository was archived by the owner on Jan 5, 2025. It is now read-only.

Commit 8f7f121

Browse files
committed
refactored
1 parent 9d5043d commit 8f7f121

File tree

4 files changed

+24
-34
lines changed

4 files changed

+24
-34
lines changed

src/App.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ public static function start()
142142
// fix for f3 not populating $_GET when run on the command line
143143
$uri = $f3->get('SERVER.REQUEST_URI');
144144
$querystring = preg_split('/&/', \UTF::instance()->substr($uri, 1 + \UTF::instance()->strpos($uri . '&', '?')));
145-
$get = [];
145+
$get = [];
146146
if (!empty($querystring) && count($querystring)) {
147147
foreach ($querystring as $pair) {
148148
if (0 == count($pair)) {
@@ -175,6 +175,8 @@ public static function finish()
175175

176176
if (\Registry::exists('logger')) {
177177
$logger = \Registry::get('logger');
178+
} else {
179+
$logger = null;
178180
}
179181

180182
if (!empty($logger) && is_object($logger) && $debug || 'production' !== $f3->get('app.env')) {

src/Helpers/Response.php

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -43,41 +43,33 @@ public static function json(array $data, array $params = [], bool $output = true
4343
$headers['Access-Control-Allow-Credentials'] = array_key_exists('acl_credentials', $params) ? $params['credentials'] : 'false';
4444

4545
$body = json_encode($data, JSON_PRETTY_PRINT);
46-
if (!empty($output)) {
47-
$headers['Content-Length'] = \UTF::instance()->strlen($body);
48-
}
49-
5046
$headers['ETag'] = array_key_exists('etag', $params) ? $params['etag'] : md5($body);
47+
$headers['Content-Length'] = \UTF::instance()->strlen($body);
5148

49+
// if returning output data or sending a HEAD request...
5250
if (empty($output)) {
51+
if ('HEAD' == $f3->get('VERB')) {
52+
$body = null;
53+
}
5354
return ['headers' => $headers, 'body' => $body];
54-
} else {
55+
}
5556

56-
// send the headers + data
57-
foreach ($headers as $header => $value) {
58-
if (is_string($value) && empty($value)) {
59-
continue;
60-
}
61-
header($header.': '.$value);
62-
}
63-
if (!array_key_exists('cookie', $params)) {
64-
header_remove('Set-Cookie'); // prevent php session
65-
}
66-
// default status is 200 - OK
67-
$f3->status(array_key_exists('http_status', $params) ? $params['http_status'] : 200);
57+
// do not send session cookie
58+
if (!array_key_exists('cookie', $params)) {
59+
header_remove('Set-Cookie'); // prevent php session
60+
}
6861

69-
$method = $f3->get('VERB');
62+
// default status is 200 - OK
63+
$f3->status(array_key_exists('http_status', $params) ? $params['http_status'] : 200);
7064

71-
switch ($method) {
72-
case 'HEAD':
73-
break;
74-
default:
75-
case 'GET':
76-
case 'PUT':
77-
case 'POST':
78-
case 'DELETE':
79-
echo $body;
80-
}
65+
// send the headers + data
66+
foreach ($headers as $header => $value) {
67+
header($header.': '.$value);
68+
}
69+
70+
// HEAD request should be identical headers to GET request but no body
71+
if ('HEAD' !== $f3->get('VERB')) {
72+
echo $body;
8173
}
8274
}
8375
}

src/Helpers/Time.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,6 @@ public static function database(int $unixtime = null, string $dbms = null): stri
4747
*/
4848
public static function HTTP(int $unixtime = null, string $zone = ''): string
4949
{
50-
$f3 = \Base::instance();
51-
5250
// use current time if bad time value or unset
5351
$unixtime = (int) $unixtime;
5452

src/Helpers/Url.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@ class Url extends \Prefab
2323
*/
2424
public static function external(string $url, $params = null, bool $https = true): string
2525
{
26-
$f3 = \Base::instance();
27-
2826
if (!empty($params)) {
2927

3028
if (is_array($params)) {
@@ -53,7 +51,7 @@ public static function external(string $url, $params = null, bool $https = true)
5351
* converting 2nd argument $params to querystring if array
5452
*
5553
* @param string $url name of the url alias if beginning with @ or relative
56-
*
54+
*
5755
* @param mixed $params url params as string or array
5856
* @param boolean $full default true
5957
* @return string

0 commit comments

Comments
 (0)