Skip to content

Commit a70a801

Browse files
committed
Allow custom final writer
1 parent bc76396 commit a70a801

File tree

7 files changed

+205
-62
lines changed

7 files changed

+205
-62
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"email": "anshu.krishna5@gmail.com"
1010
}
1111
],
12-
"version": "11.3.1",
12+
"version": "11.4",
1313
"minimum-stability": "stable",
1414
"require": {
1515
"php": ">=8.1",

example/server/function/@all.php

Lines changed: 0 additions & 4 deletions
This file was deleted.
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
<?php
2-
3-
use Krishna\API\Func;
4-
5-
Func::set_definition(function(array $params, string $funcname) {
6-
\Krishna\Utilities\Debugger::dump('Test', 'Testing debug message');
7-
return 'This is default response';
1+
<?php
2+
3+
use Krishna\API\Func;
4+
5+
Func::set_definition(function(array $params, string $funcname) {
6+
\Krishna\Utilities\Debugger::dump('Test', 'Testing debug message');
7+
return 'This is default response';
88
});
Lines changed: 47 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,48 @@
1-
<?php
2-
use Krishna\API\Func;
3-
4-
Func::set_signature([
5-
'?bool' => 'bool',
6-
'?email' => 'email',
7-
'?flag' => 'flag',
8-
'?float' => 'float',
9-
'?hex' => 'hex',
10-
'?int' => 'int',
11-
'?ipv4' => 'ipv4',
12-
'?ipv6' => 'ipv6',
13-
'?json' => 'json',
14-
'?json64' => 'json64',
15-
'?mac' => 'mac',
16-
'?mixed' => 'mixed',
17-
'?number' => 'number',
18-
'?string' => 'string',
19-
'?string64' => 'string64',
20-
'?timestamp' => 'timestamp',
21-
'?unsigned' => 'unsigned',
22-
'?url' => 'url',
23-
'?url64' => 'url64',
24-
// Nullables
25-
'?_bool' => 'bool|null',
26-
'?_email' => 'email|null',
27-
'?_flag' => 'flag|null',
28-
'?_float' => 'float|null',
29-
'?_hex' => 'hex|null',
30-
'?_int' => 'int|null',
31-
'?_ipv4' => 'ipv4|null',
32-
'?_ipv6' => 'ipv6|null',
33-
'?_json' => 'json|null',
34-
'?_json64' => 'json64|null',
35-
'?_mac' => 'mac|null',
36-
'?_mixed' => 'mixed|null',
37-
'?_number' => 'number|null',
38-
'?_string' => 'string|null',
39-
'?_string64' => 'string64|null',
40-
'?_timestamp' => 'timestamp|null',
41-
'?_unsigned' => 'unsigned|null',
42-
'?_url' => 'url|null',
43-
'?_url64' => 'url64|null',
44-
]);
45-
46-
Func::set_definition(function(array $params, string $function_name) {
47-
return $params;
1+
<?php
2+
use Krishna\API\Func;
3+
4+
Func::set_signature([
5+
'?bool' => 'bool',
6+
'?email' => 'email',
7+
'?flag' => 'flag',
8+
'?float' => 'float',
9+
'?hex' => 'hex',
10+
'?int' => 'int',
11+
'?ipv4' => 'ipv4',
12+
'?ipv6' => 'ipv6',
13+
'?json' => 'json',
14+
'?json64' => 'json64',
15+
'?mac' => 'mac',
16+
'?mixed' => 'mixed',
17+
'?number' => 'number',
18+
'?string' => 'string',
19+
'?string64' => 'string64',
20+
'?timestamp' => 'timestamp',
21+
'?unsigned' => 'unsigned',
22+
'?url' => 'url',
23+
'?url64' => 'url64',
24+
// Nullables
25+
'?_bool' => 'bool|null',
26+
'?_email' => 'email|null',
27+
'?_flag' => 'flag|null',
28+
'?_float' => 'float|null',
29+
'?_hex' => 'hex|null',
30+
'?_int' => 'int|null',
31+
'?_ipv4' => 'ipv4|null',
32+
'?_ipv6' => 'ipv6|null',
33+
'?_json' => 'json|null',
34+
'?_json64' => 'json64|null',
35+
'?_mac' => 'mac|null',
36+
'?_mixed' => 'mixed|null',
37+
'?_number' => 'number|null',
38+
'?_string' => 'string|null',
39+
'?_string64' => 'string64|null',
40+
'?_timestamp' => 'timestamp|null',
41+
'?_unsigned' => 'unsigned|null',
42+
'?_url' => 'url|null',
43+
'?_url64' => 'url64|null',
44+
]);
45+
46+
Func::set_definition(function(array $params, string $function_name) {
47+
return $params;
4848
});
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
use Krishna\API\Func;
3+
use Krishna\API\Server;
4+
5+
Func::set_signature([
6+
'?bool' => 'bool',
7+
'?email' => 'email',
8+
'?flag' => 'flag',
9+
'?float' => 'float',
10+
'?hex' => 'hex',
11+
'?int' => 'int',
12+
'?ipv4' => 'ipv4',
13+
'?ipv6' => 'ipv6',
14+
'?json' => 'json',
15+
'?json64' => 'json64',
16+
'?mac' => 'mac',
17+
'?mixed' => 'mixed',
18+
'?number' => 'number',
19+
'?string' => 'string',
20+
'?string64' => 'string64',
21+
'?timestamp' => 'timestamp',
22+
'?unsigned' => 'unsigned',
23+
'?url' => 'url',
24+
'?url64' => 'url64',
25+
// Nullables
26+
'?_bool' => 'bool|null',
27+
'?_email' => 'email|null',
28+
'?_flag' => 'flag|null',
29+
'?_float' => 'float|null',
30+
'?_hex' => 'hex|null',
31+
'?_int' => 'int|null',
32+
'?_ipv4' => 'ipv4|null',
33+
'?_ipv6' => 'ipv6|null',
34+
'?_json' => 'json|null',
35+
'?_json64' => 'json64|null',
36+
'?_mac' => 'mac|null',
37+
'?_mixed' => 'mixed|null',
38+
'?_number' => 'number|null',
39+
'?_string' => 'string|null',
40+
'?_string64' => 'string64|null',
41+
'?_timestamp' => 'timestamp|null',
42+
'?_unsigned' => 'unsigned|null',
43+
'?_url' => 'url|null',
44+
'?_url64' => 'url64|null',
45+
]);
46+
47+
Func::set_definition(function(array $params, string $function_name) {
48+
return $params;
49+
});
50+
51+
52+
function array2xml(array $array, \SimpleXMLElement &$xml) {
53+
// Loop through array
54+
foreach($array as $key => $value ) {
55+
// Another array? Iterate
56+
if (is_array($value)) {
57+
array2xml($value, $xml->addChild($key));
58+
} else {
59+
$xml->addChild($key, $value);
60+
}
61+
}
62+
63+
// Return XML
64+
return $xml->asXML();
65+
}
66+
Server::set_custom_final_writer(function($data) {
67+
// [
68+
// 'status' => $status,
69+
// 'value' => $value,
70+
// 'headers' => $headers,
71+
// 'meta' => $meta
72+
// ] = $data;
73+
74+
// Dump the final data
75+
var_dump($data);
76+
});

src/Config.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ final class Config {
66
// Flags
77
public static bool
88
$dev_mode = false,
9-
$allow_jsonp = true;
9+
$allow_jsonp = false;
1010

1111
// Environment Settings
1212
public static string

src/Server.php

Lines changed: 73 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,19 @@ final class Server {
1616
'query' => []
1717
],
1818
$debug = [];
19+
1920
private static string $func_base_path;
2021

2122
private static bool
2223
$direct_exit = false,
2324
$final_called = false,
2425
$init_flag = false;
26+
27+
private static $custom_final_writer = null; // ?callable
28+
29+
public static function set_custom_final_writer(?callable $func) : void {
30+
self::$custom_final_writer = $func;
31+
}
2532

2633
public static function force_exit() : never {
2734
self::$direct_exit = true;
@@ -135,8 +142,7 @@ public static function process_path_query(array $sig_info) {
135142
}
136143
unset(self::$request['path']);
137144
}
138-
private static function final_writer(mixed $value = null, StatusType $status = StatusType::OK) : never {
139-
self::$final_called = true;
145+
private static function _default_final_writer_(mixed $value = null, StatusType $status = StatusType::OK) {
140146
if(!headers_sent()) {
141147
if(
142148
Config::$zlib === true
@@ -195,6 +201,71 @@ private static function final_writer(mixed $value = null, StatusType $status = S
195201
} else {
196202
echo $response;
197203
}
204+
}
205+
private static function final_writer(mixed $value = null, StatusType $status = StatusType::OK) : never {
206+
self::$final_called = true;
207+
if(self::$custom_final_writer === null) {
208+
self::_default_final_writer_($value, $status);
209+
exit(0);
210+
}
211+
if(!is_callable(self::$custom_final_writer)) {
212+
$status = StatusType::DEV_ERR;
213+
$value = 'Custom final writer is not callable';
214+
self::$debug[] = [
215+
'title' => 'Custom final writer',
216+
'value' => self::$custom_final_writer
217+
];
218+
self::_default_final_writer_($value, $status);
219+
exit(0);
220+
}
221+
222+
$headers = [];
223+
$meta = null;
224+
225+
if(!headers_sent()) {
226+
if(
227+
Config::$zlib === true
228+
&& extension_loaded('zlib')
229+
&& isset($_SERVER['HTTP_ACCEPT_ENCODING'])
230+
&& substr_count($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip')
231+
&& (ini_get('output_handler') != 'ob_gzhandler')
232+
) {
233+
@ini_set('zlib.output_compression', 1);
234+
}
235+
}
236+
if(Config::$disable_caching === true) {
237+
$headers[] = 'Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT';
238+
$headers[] = 'Expires: 0';
239+
$headers[] = 'Cache-Control: no-store, no-cache, must-revalidate';
240+
$headers[] = 'Cache-Control: post-check=0, pre-check=0';
241+
$headers[] = 'Pragma: no-cache';
242+
}
243+
if(Config::$access_control_headers !== null) {
244+
foreach(Config::$access_control_headers as $h) {
245+
$headers[] = $h;
246+
}
247+
}
248+
if(Config::$other_headers !== null) {
249+
foreach(Config::$other_headers as $h) {
250+
$headers[] = $h;
251+
}
252+
}
253+
if(Config::$dev_mode) {
254+
$meta = [];
255+
if(defined(__NAMESPACE__ . '\\__START_TIME__')) {
256+
$meta['exe_time'] = round(microtime(true) - constant(__NAMESPACE__ . '\\__START_TIME__'), 7);
257+
}
258+
$meta['mem_peak'] = memory_get_peak_usage();
259+
if(count(self::$debug) > 0) {
260+
$meta['debug'] = self::$debug;
261+
}
262+
}
263+
(self::$custom_final_writer)([
264+
'status' => $status,
265+
'value' => $value,
266+
'headers' => $headers,
267+
'meta' => $meta
268+
]);
198269
exit(0);
199270
}
200271
public static function error(mixed $info, StatusType $status = StatusType::EXEC_ERR) : never {

0 commit comments

Comments
 (0)