-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoxphp.stub.php
More file actions
1794 lines (1565 loc) · 63.6 KB
/
oxphp.stub.php
File metadata and controls
1794 lines (1565 loc) · 63.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* OxPHP Extension Stub File
*
* Provides IDE autocompletion and static analysis support for
* functions and classes defined by the oxphp_sapi PHP extension.
*
* This file is NOT loaded at runtime — it is only used by IDEs
* (PhpStorm, VS Code + Intelephense) and static analyzers (PHPStan, Psalm).
*
* @package OxPHP
* @version 0.3.0
* @link https://github.com/oxphp/oxphp
*/
// ═══════════════════════════════════════════════════════════════
// Global Functions
// ═══════════════════════════════════════════════════════════════
/**
* Returns the HTTP Request object for the current request context.
*
* The object is a lightweight proxy — data is fetched lazily from Rust
* thread-local storage via FFI only when a method is called. No allocation
* overhead for data you never access.
*
* @return \OxPHP\Http\RequestInterface Typed, read-only request object
*
* @throws \OxPHP\Http\Exception\NoActiveRequestException If no active request context
* @throws \OxPHP\Http\Exception\AsyncContextException If called from an oxphp_async() callback
* @throws \OxPHP\Http\Exception\WorkerIdleException If worker is between requests
*
* @example
* $request = oxphp_http_request();
* $page = $request->query('page', 1);
* $token = $request->header('Authorization');
*/
function oxphp_http_request(): \OxPHP\Http\RequestInterface {}
/**
* Check if PHP superglobals ($_GET, $_POST, etc.) are populated.
*
* When SUPERGLOBALS_ENABLED=false, the object API via oxphp_http_request()
* is the only way to access request data.
*
* @return bool true if superglobals are enabled (default)
*
* @example
* if (!oxphp_superglobals_enabled()) {
* // must use oxphp_http_request() for all request data
* $request = oxphp_http_request();
* }
*/
function oxphp_superglobals_enabled(): bool {}
/**
* Returns the unique request ID for the current request.
*
* The same value is sent in the X-Request-ID response header.
* If the client sends an X-Request-ID header, the server passes
* it through instead of generating a new one.
*
* @return string 16-character hex request ID (e.g. "67b9a3c100000042")
*
* @example
* $id = oxphp_request_id();
* error_log("[$id] Processing order");
*/
function oxphp_request_id(): string {}
/**
* Returns the index of the PHP ZTS worker thread handling this request.
*
* Worker indices range from 0 to PHP_WORKERS - 1. Useful for
* per-worker caching, debugging, and log correlation.
*
* @return int Zero-based worker thread index
*
* @example
* $tmp = "/tmp/worker_" . oxphp_worker_id() . "_buffer.dat";
*/
function oxphp_worker_id(): int {}
/**
* Returns server metadata for the current request.
*
* The request_time is a Unix timestamp with microsecond precision,
* set before php_request_startup() for accurate timing.
*
* @return array{sapi: string, version: string, worker_id: int, request_time: float}
*
* @example
* $info = oxphp_server_info();
* // ["sapi" => "oxphp", "version" => "0.1.0", "worker_id" => 3, "request_time" => 1740000000.123]
*/
function oxphp_server_info(): array {}
/**
* Flushes the response to the client and marks the request as finished.
*
* Any code after this call continues executing without blocking
* the HTTP response. Similar to fastcgi_finish_request() in PHP-FPM.
*
* Returns false if already called on this request.
*
* @return bool true on success, false if already finished
*
* @example
* echo json_encode(["status" => "accepted"]);
* oxphp_finish_request();
* // background work — client already got 200 OK
* send_notification_email($user);
*/
function oxphp_finish_request(): bool {}
/**
* Checks whether the server is running in worker mode.
*
* In worker mode, PHP boots once and handles multiple requests via
* oxphp_worker(). In traditional mode, each request spawns a fresh
* PHP process. Use this to conditionally enable worker-specific logic.
*
* @return bool true if running in worker mode
*
* @example
* if (oxphp_is_worker()) {
* // persistent connections, shared state, etc.
* }
*/
function oxphp_is_worker(): bool {}
/**
* Checks whether the current request is in streaming mode.
*
* In streaming mode (SSE, chunked transfer), output is flushed
* to the client immediately rather than buffered.
*
* @return bool true if streaming mode is active
*
* @example
* if (oxphp_is_streaming()) {
* echo "data: " . json_encode($event) . "\n\n";
* flush();
* }
*/
function oxphp_is_streaming(): bool {}
/**
* Extends the request timeout to prevent the server from killing
* long-running scripts.
*
* Call periodically in long-running loops. The timeout is extended
* by the given number of seconds from the time of the call.
*
* @param int $time Seconds to extend the timeout by (default: 10)
* @return bool Always true
*
* @example
* foreach ($large_dataset as $row) {
* oxphp_request_heartbeat(30);
* process($row);
* }
*/
function oxphp_request_heartbeat(int $time = 10): bool {}
/**
* Activate streaming mode and flush buffered output as a chunk to the client.
*
* On the first call, HTTP headers are sent immediately. Each subsequent call
* flushes any output written since the last flush as a new chunk.
*
* Use this for Server-Sent Events (SSE), chunked transfer, or any real-time
* streaming pattern. Streaming mode is also auto-activated when PHP sets
* Content-Type: text/event-stream.
*
* Returns false if oxphp_finish_request() was already called.
*
* @return bool true on success, false if request is already finished
*
* @example
* header('Content-Type: text/event-stream');
* header('Cache-Control: no-cache');
* for ($i = 0; $i < 10; $i++) {
* echo "data: " . json_encode(["counter" => $i]) . "\n\n";
* oxphp_stream_flush();
* sleep(1);
* }
*/
function oxphp_stream_flush(): bool {}
/**
* Cooperative sleep: suspends the current fiber to let other requests
* proceed on this worker thread.
*
* When called inside a fiber (worker mode with multiplexing), the fiber
* is suspended and a timer is registered. The scheduler resumes it after
* the specified duration. Other requests can be handled in the meantime.
*
* When called outside a fiber (traditional mode), falls back to blocking usleep().
*
* @param float $seconds Duration to sleep in seconds (e.g. 0.5 for 500ms)
* @return void
*
* @example
* oxphp_worker(function () {
* // Non-blocking: other requests proceed during sleep
* oxphp_sleep(0.1); // 100ms cooperative sleep
* echo "done";
* });
*/
function oxphp_sleep(float $seconds): void {}
/**
* Cooperative microsecond sleep: suspends the current fiber to let other
* requests proceed on this worker thread.
*
* Identical to oxphp_sleep() but accepts microseconds as an integer.
* Falls back to blocking usleep() when not inside a fiber.
*
* @param int $microseconds Duration to sleep in microseconds
* @return void
*
* @example
* oxphp_worker(function () {
* oxphp_usleep(50000); // 50ms cooperative sleep
* echo "done";
* });
*/
function oxphp_usleep(int $microseconds): void {}
/**
* Enter worker mode loop. The handler is called for each HTTP request.
*
* Between requests, a soft reset cleans per-request state (output buffers,
* headers, superglobals) without destroying the PHP heap. Bootstrap state
* (autoloader, DI container, routes, variables in the outer scope) persists.
*
* Only available when WORKER_FILE env var is set. Returns true on graceful
* shutdown (channel closed), or exits the loop on max_requests/max_memory
* limits. Code after oxphp_worker() runs during shutdown.
*
* @param callable $handler Called for each request with fresh superglobals
* @return bool true on graceful exit, false if not in worker mode
*
* @example
* $app = new App(); // boot once
* oxphp_worker(function () use ($app) {
* $app->handle(); // called per request
* });
* $app->terminate(); // graceful shutdown
*/
function oxphp_worker(callable $handler): bool {}
/**
* Dispatch a closure for asynchronous execution on the dedicated async worker pool.
*
* The closure is transferred to a separate OS thread (PHP ZTS). Variables captured
* via `use` and arguments passed via ...$args are serialized on the source thread
* and deserialized on the async worker thread (independent copies).
*
* Supported argument types: null, bool, int, float, string, array.
* Resources and objects are rejected with E_WARNING.
*
* Requires ASYNC_WORKERS > 0. Returns false if the async pool is disabled or
* the queue is full.
*
* @param \Closure $closure The closure to execute asynchronously
* @param mixed ...$args Arguments serialized to the async worker thread
* @return int|false Promise ID (positive integer) on success, false on failure
*
* @example
* $p = oxphp_async(function(int $x, int $y): int {
* return $x + $y;
* }, 10, 20);
* $result = oxphp_async_await($p); // 30
*/
function oxphp_async(\Closure $closure, mixed ...$args): int|false {}
/**
* Block until the async task completes and return its result.
*
* The return value is deserialized from the async worker thread onto the
* current thread's heap.
*
* Each promise ID can only be awaited once. Non-awaited promises are cleaned up
* automatically at request end (RSHUTDOWN) with a 5-second timeout.
*
* @param int $promise_id Promise ID returned by oxphp_async()
* @param float|null $timeout Maximum seconds to wait, null = wait indefinitely
* @return mixed The return value of the closure
*
* @throws \OxPHP\Async\Exception If the closure threw an exception or called die()/exit()
* @throws \OxPHP\Async\TimeoutException If the timeout expired before completion
*
* @example
* $p = oxphp_async(function(): string { return 'hello'; });
* $result = oxphp_async_await($p); // "hello"
*
* // With timeout:
* try {
* $result = oxphp_async_await($p, 2.0);
* } catch (\OxPHP\Async\TimeoutException $e) {
* // task took longer than 2 seconds
* }
*/
function oxphp_async_await(int $promise_id, ?float $timeout = null): mixed {}
/**
* Await multiple promises and return all results.
*
* Blocks until every promise completes (or fails/times out). Returns an
* associative array mapping each promise ID to its result value.
*
* @param int[] $promise_ids Array of promise IDs from oxphp_async()
* @param float|null $timeout Per-promise timeout in seconds, null = no limit
* @return array<int, mixed> Map of promise ID => result value
*
* @throws \OxPHP\Async\Exception If any promise fails
* @throws \OxPHP\Async\TimeoutException If any promise times out
*
* @example
* $p1 = oxphp_async(fn() => 1);
* $p2 = oxphp_async(fn() => 2);
* $p3 = oxphp_async(fn() => 3);
* $results = oxphp_async_await_all([$p1, $p2, $p3]);
* // [$p1 => 1, $p2 => 2, $p3 => 3]
*/
function oxphp_async_await_all(array $promise_ids, ?float $timeout = null): array {}
/**
* Race multiple promises and return the first to complete.
*
* Uses true concurrent race semantics (futures::select_all) — the fastest
* promise wins regardless of array order. Non-winning promises remain
* individually awaitable via oxphp_async_await().
*
* On timeout, all specified promises are cancelled and cannot be awaited.
*
* @param int[] $promise_ids Array of promise IDs from oxphp_async()
* @param float|null $timeout Overall timeout in seconds, null = no limit
* @return array{id: int, value: mixed} The winning promise ID and its result
*
* @throws \OxPHP\Async\Exception If the winning promise threw an exception
* @throws \OxPHP\Async\TimeoutException If no promise completes within timeout
*
* @example
* $p1 = oxphp_async(fn() => slow_api_a());
* $p2 = oxphp_async(fn() => slow_api_b());
* $winner = oxphp_async_await_any([$p1, $p2]);
* // ['id' => $p2, 'value' => ...] (whichever finished first)
* $other = oxphp_async_await($p1); // non-winner still awaitable
*/
function oxphp_async_await_any(array $promise_ids, ?float $timeout = null): array {}
/**
* Register a PHP class as an attribute-based decorator.
*
* The class must implement OxPHP\Decorator\AttributeInterface and be
* marked with #[Attribute(...)]. Once registered, any function, method,
* or class annotated with this attribute will have before()/after()
* called around each invocation.
*
* Call once during application bootstrap (after autoloader setup).
*
* @param string $class Fully qualified class name
* @return bool true on success, false with E_WARNING on validation failure
*
* @example
* oxphp_register_decorator(Timer::class);
*
* #[Timer(label: 'api')]
* function handle_request(): void { ... }
*/
function oxphp_register_decorator(string $class): bool {}
// ═══════════════════════════════════════════════════════════════
// OxPHP\Http — Request Object API
// ═══════════════════════════════════════════════════════════════
namespace OxPHP\Http {
/**
* Read-only HTTP request interface.
*
* All HTTP data is fixed at the moment of receipt. The only mutable
* component is attributes() for middleware enrichment. Data is fetched
* lazily from Rust via FFI — only what you access crosses the bridge.
*/
interface RequestInterface
{
// ── URI & Method ──
/** HTTP method (e.g. "GET", "POST"). */
public function method(): string;
/** URI path without query string (e.g. "/users/42"). */
public function path(): string;
/**
* Full URI including scheme, host, port (if non-default), path, and query.
* Port is omitted when it matches the scheme default (80/443).
*
* @example "https://example.com:8080/users/42?page=2"
*/
public function fullUri(): string;
/** URI scheme: "http" or "https". */
public function scheme(): string;
/** Hostname from Host header, or empty string if absent. */
public function host(): string;
/** Port from Host header, or scheme default (80 for http, 443 for https). */
public function port(): int;
/** Raw query string without leading "?", or null if absent. */
public function queryString(): ?string;
/** Whether the request arrived over TLS. */
public function isSecure(): bool;
/** Case-insensitive HTTP method comparison. */
public function isMethod(string $method): bool;
// ── Protocol ──
/** Full protocol string (e.g. "HTTP/1.1"). */
public function httpProtocol(): string;
/** Protocol version only (e.g. "1.1", "2"). */
public function httpProtocolVersion(): string;
// ── Query Parameters ($_GET replacement) ──
/**
* Access query string parameters.
*
* Supports bracket notation: ?a[]=1&a[]=2 → ['a' => ['1', '2']].
* Bridge returns flat pairs; bracket parsing happens on PHP side.
*
* @param string|null $key Specific key, or null for all params
* @param mixed $default Returned when key is absent
* @return mixed Array of all params, or single value, or $default
*/
public function query(?string $key = null, mixed $default = null): mixed;
// ── Parsed Body ($_POST + JSON replacement) ──
/**
* Access parsed request body based on Content-Type.
*
* - application/x-www-form-urlencoded → array
* - multipart/form-data → array
* - application/json → decoded array/object (null on invalid JSON)
* - other Content-Type → null
*
* Not tied to HTTP method — works with POST, PUT, PATCH, etc.
* Parsed result is cached per request. Parsing happens in Rust.
*
* @param string|null $key Specific key, or null for full body
* @param mixed $default Returned when key is absent
* @return mixed Parsed body or single value or $default
*/
public function payload(?string $key = null, mixed $default = null): mixed;
// ── Headers ──
/**
* Get a header value by name (case-insensitive).
*
* Returns the raw value as-is. For multi-value headers (Accept,
* X-Forwarded-For), the full string is returned:
* "text/html,application/xhtml+xml,application/xml;q=0.9"
*
* @param string $name Header name
* @param string|null $default Returned when header is absent
* @return string|null Header value or $default
*/
public function header(string $name, ?string $default = null): ?string;
/** All headers as name => value array. */
public function headers(): array;
/** Check if a header exists (case-insensitive). */
public function hasHeader(string $name): bool;
// ── Cookies ($_COOKIE replacement) ──
/**
* Get a cookie value by name.
*
* @param string $name Cookie name
* @param string|null $default Returned when cookie is absent
* @return string|null Cookie value or $default
*/
public function cookie(string $name, ?string $default = null): ?string;
/** All cookies as name => value array. */
public function cookies(): array;
// ── Raw Body (php://input replacement) ──
/** Raw request body bytes. Not cached — FFI call each time. */
public function body(): string;
/** Content-Type header value, or null. */
public function contentType(): ?string;
// ── File Uploads ($_FILES replacement) ──
/**
* Get a single uploaded file by field name.
* For array fields (name="photos[]"), returns the first file.
*
* @return UploadedFileInterface|null The file or null if not found
*/
public function file(string $name): ?UploadedFileInterface;
/**
* Get uploaded files.
*
* Without argument: all files as a flat UploadedFileInterface[] array.
* With name: all files for that field (supports name="photos[]").
*
* @param string|null $name Field name filter, or null for all
* @return UploadedFileInterface[]
*/
public function files(?string $name = null): array;
// ── Client ──
/** Client IP address (REMOTE_ADDR). */
public function ip(): string;
// ── Timing ──
/**
* Request start timestamp.
*
* @param bool $asFloat true for float with sub-second precision
* @return int|float Unix timestamp
*/
public function startTime(bool $asFloat = false): int|float;
// ── Attributes (mutable middleware container) ──
/**
* Mutable key-value container for middleware enrichment.
*
* Per-request, shared between Fibers on the same thread.
* The Attributes object is created on first call and cached.
*/
public function attributes(): AttributesInterface;
// ── Session ──
/**
* Live read-only view on $_SESSION.
*
* Returns null if session_start() has not been called.
* Values reflect $_SESSION state at the time of each method call.
* Session management (start, save, destroy, set) via native session_*().
*/
public function session(): ?SessionInterface;
}
/**
* Read-only view on $_SESSION.
*
* Session lifecycle (start, save, destroy, write) is managed through
* native PHP session_*() functions. This interface only reads.
*/
interface SessionInterface
{
/** Current session ID (session_id()). */
public function id(): string;
/** Current session name (session_name()). */
public function name(): string;
/**
* Get a session value by key.
*
* @param string $key Session key
* @param mixed $default Returned when key is absent
* @return mixed Session value or $default
*/
public function get(string $key, mixed $default = null): mixed;
/** Check if a key exists in the session. */
public function has(string $key): bool;
/** All session data as an array. */
public function all(): array;
}
/**
* Represents an uploaded file with server-side MIME type detection.
*
* type() detects MIME from file contents (magic bytes), not from the
* client-provided Content-Type which can be spoofed. The detected type
* is cached on first call. moveTo() automatically calls type() before
* moving to ensure the cache is populated.
*/
interface UploadedFileInterface
{
/** Original filename from the client. */
public function name(): string;
/** MIME type reported by the client (unreliable, can be spoofed). */
public function clientType(): string;
/**
* MIME type detected from file contents (magic bytes).
*
* Cached on first call. Returns "application/octet-stream" if
* detection fails. moveTo() auto-calls type() before moving.
*/
public function type(): string;
/** File size in bytes. */
public function size(): int;
/** Path to the temporary uploaded file. */
public function tmpPath(): string;
/** Upload error code (UPLOAD_ERR_* constant). */
public function error(): int;
/** Whether the upload succeeded (error === UPLOAD_ERR_OK). */
public function isValid(): bool;
/**
* Move the uploaded file to a destination path.
*
* Automatically calls type() before moving to cache MIME detection.
* Returns false if the file is not valid or the move fails.
*
* @param string $path Destination file path
* @return bool true on success
*/
public function moveTo(string $path): bool;
}
/**
* Mutable key-value container for request attributes.
*
* Used by middleware to attach data to the request (auth user, locale,
* route parameters, etc.). Per-request, shared between Fibers.
*/
interface AttributesInterface
{
/**
* @param string $key Attribute key
* @param mixed $default Returned when key is absent
* @return mixed Attribute value or $default
*/
public function get(string $key, mixed $default = null): mixed;
/** Set an attribute value. */
public function set(string $key, mixed $value): void;
/** Check if an attribute exists. */
public function has(string $key): bool;
/** Remove an attribute. */
public function remove(string $key): void;
/** All attributes as an array. */
public function all(): array;
}
/** @internal Native implementation — use RequestInterface for type hints. */
final class Request implements RequestInterface
{
public function method(): string {}
public function path(): string {}
public function fullUri(): string {}
public function scheme(): string {}
public function host(): string {}
public function port(): int {}
public function queryString(): ?string {}
public function isSecure(): bool {}
public function isMethod(string $method): bool {}
public function httpProtocol(): string {}
public function httpProtocolVersion(): string {}
public function query(?string $key = null, mixed $default = null): mixed {}
public function payload(?string $key = null, mixed $default = null): mixed {}
public function header(string $name, ?string $default = null): ?string {}
public function headers(): array {}
public function hasHeader(string $name): bool {}
public function cookie(string $name, ?string $default = null): ?string {}
public function cookies(): array {}
public function body(): string {}
public function contentType(): ?string {}
public function file(string $name): ?UploadedFileInterface {}
public function files(?string $name = null): array {}
public function ip(): string {}
public function startTime(bool $asFloat = false): int|float {}
public function attributes(): AttributesInterface {}
public function session(): ?SessionInterface {}
}
/** @internal Native implementation — use SessionInterface for type hints. */
final class Session implements SessionInterface
{
public function id(): string {}
public function name(): string {}
public function get(string $key, mixed $default = null): mixed {}
public function has(string $key): bool {}
public function all(): array {}
}
/** @internal Native implementation — use UploadedFileInterface for type hints. */
final class UploadedFile implements UploadedFileInterface
{
public function name(): string {}
public function clientType(): string {}
public function type(): string {}
public function size(): int {}
public function tmpPath(): string {}
public function error(): int {}
public function isValid(): bool {}
public function moveTo(string $path): bool {}
}
/** @internal Native implementation — use AttributesInterface for type hints. */
final class Attributes implements AttributesInterface
{
public function get(string $key, mixed $default = null): mixed {}
public function set(string $key, mixed $value): void {}
public function has(string $key): bool {}
public function remove(string $key): void {}
public function all(): array {}
}
}
// ═══════════════════════════════════════════════════════════════
// OxPHP\Http\Exception — Context-aware request exceptions
// ═══════════════════════════════════════════════════════════════
namespace OxPHP\Http\Exception {
/**
* No active HTTP request in this context.
*
* Base class for all request-context exceptions.
* Thrown by oxphp_http_request() when called outside a request lifecycle.
*/
class NoActiveRequestException extends \RuntimeException {}
/**
* Cannot access request from an oxphp_async() worker thread.
*
* Async workers run on separate OS threads without request context.
*/
class AsyncContextException extends NoActiveRequestException {}
/**
* Worker is idle — waiting for the next request.
*
* Thrown when oxphp_http_request() is called in worker mode
* but outside the request handler callback.
*/
class WorkerIdleException extends NoActiveRequestException {}
}
// ═══════════════════════════════════════════════════════════════
// OxPHP — Async & Decorator classes
// ═══════════════════════════════════════════════════════════════
namespace OxPHP\Async {
/**
* Thrown when an async task fails — the closure threw an exception,
* or called die()/exit().
*
* The message contains the original exception class and message:
* "Async task failed: [DomainException] invalid value"
*/
class Exception extends \Exception {}
/**
* Thrown when oxphp_async_await() times out before the task completes.
*/
class TimeoutException extends Exception {}
/**
* Thrown by every access to a BorrowedProxy — proxies substituted for
* `use`-captured variables in oxphp_async() callbacks that would not
* be safe to touch on the promise thread.
*/
class BorrowException extends \Exception {}
/**
* Opaque stand-in for a `use`-captured value in an oxphp_async() closure
* when the original value is still borrowed by the source thread.
*
* Every access — property read/write, method call, isset/unset, casts,
* JSON serialisation — throws {@see BorrowException}. The only safe
* thing a handler can do with a BorrowedProxy is check its class with
* `instanceof` and fall back to data captured by value.
*
* @internal Produced by the runtime; never constructed from PHP.
*/
final class BorrowedProxy implements \JsonSerializable
{
/** @throws BorrowException Always. */
public function __get(string $name): mixed {}
/** @throws BorrowException Always. */
public function __set(string $name, mixed $value): void {}
/** @throws BorrowException Always. */
public function __call(string $name, array $arguments): mixed {}
/** @throws BorrowException Always. */
public function __isset(string $name): bool {}
/** @throws BorrowException Always. */
public function __unset(string $name): void {}
/** @throws BorrowException Always. */
public function __toString(): string {}
/** @throws BorrowException Always. */
public function __debugInfo(): ?array {}
/** @throws BorrowException Always. */
public function jsonSerialize(): mixed {}
}
}
namespace OxPHP\Decorator {
/**
* Interface for attribute-based decorators.
*
* Implement this interface and register with oxphp_register_decorator()
* to intercept function/method calls via PHP 8+ attributes.
*
* @example
* #[Attribute(Attribute::TARGET_FUNCTION | Attribute::TARGET_METHOD)]
* class Timer implements AttributeInterface {
* public function before(Context $ctx): void {
* // called before the decorated function
* }
* public function after(Context $ctx): void {
* // called after the decorated function
* }
* }
*/
interface AttributeInterface {
public function before(Context $ctx): void;
public function after(Context $ctx): void;
}
/**
* Context passed to decorator before()/after() methods.
*
* Properties are populated by the server before each call.
* Lazy methods (getParams, getResult) avoid overhead when not used.
*/
final class Context {
/** Full target name: "App\Service::method" or "my_function" */
public readonly string $target;
/** Class name, or "" for standalone functions */
public readonly string $class;
/** Method name, or "" for standalone functions */
public readonly string $method;
/** Function name for TARGET_FUNCTION, or "" for methods */
public readonly string $function;
/** spl_object_id for method calls, 0 for functions */
public readonly int $objectId;
/** Current request ID from the server */
public readonly string $requestId;
/** W3C trace ID (if distributed tracing is enabled) */
public readonly string $traceId;
/**
* Get the arguments passed to the decorated function.
*
* Lazy: the array is built from zvals on demand. Zero cost if not called.
*
* @return array Indexed array of argument values
*/
public function getParams(): array {}
/**
* Get the return value of the decorated function.
*
* Only meaningful in after(). Returns null in before() or when
* the function threw an exception.
*
* @return mixed Return value, or null
*/
public function getResult(): mixed {}
/**
* Check whether the decorated function returned a value.
*
* Returns false in before(), or in after() when the function threw.
*
* @return bool true if getResult() has a meaningful value
*/
public function hasResult(): bool {}
}
/**
* Thrown when a Rust-native decorator rejects a function call
* via DecoratorAction::Reject.
*/
class RejectedException extends \Exception {}
}
// ═══════════════════════════════════════════════════════════════
// OxPHP\Shared — Process-wide concurrent primitives
// ═══════════════════════════════════════════════════════════════
namespace OxPHP\Shared {
/**
* Marker interface implemented by every Shared\* type.
*
* Values that implement Shareable may be stored inside container
* Shared types (Map, Channel) as nested refcount-managed references
* without being serialised. Plain PHP objects cannot — the runtime
* rejects them with {@see TypeException}.
*
* Implemented internally by Counter, Flag, Once, Mutex, Channel, Map,
* Pool. User code cannot implement this interface directly.
*/
interface Shareable {}
/**
* Atomic signed 64-bit counter, visible from every PHP worker thread.
*
* All operations are lock-free (`SeqCst`). Use Counter for rate
* counters, progress trackers, sequence generators, or any shared
* integer state that would otherwise require Redis INCR.
*
* @link docs/en/features/shared-counter.md
*/
final class Counter implements Shareable
{
public function __construct(int $initial = 0) {}
/** Current value. */
public function get(): int {}
/** Set to `$value`, returning the previous value. */
public function set(int $value): int {}
/** Atomically add `$by` (default 1), returning the new value. */
public function inc(int $by = 1): int {}
/** Atomically subtract `$by` (default 1), returning the new value. */
public function dec(int $by = 1): int {}
/** Atomically add `$delta` (may be negative), returning the new value. */
public function add(int $delta): int {}
/**
* Compare-and-set. Returns true if the swap succeeded (current
* value was `$expect`), false otherwise.
*/
public function compareAndSet(int $expect, int $new): bool {}
/** Atomic sum of `$deltas`, returning the new value. */
public function addBatch(array $deltas): int {}
/** Reset to `$newValue` (default 0), returning the previous value. */
public function reset(int $newValue = 0): int {}
/** Registry ID for this instance. */
public function id(): int {}
}
/**
* Atomic boolean flag, visible from every PHP worker thread.
*
* Use Flag for feature toggles, circuit-breaker state, shutdown
* signals — anything that fits a single yes/no switch.
*
* @link docs/en/features/shared-flag.md
*/
final class Flag implements Shareable
{
public function __construct(bool $initial = false) {}
/** Current value. */
public function test(): bool {}
/** Atomically set to true, returning the previous value. */
public function set(): bool {}