ILIAS  trunk Revision v13.0_alpha-62-g3b72d83f2e5
HttpService.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
25use ILIAS\HTTP\Services as IliasHttpServices;
28use Psr\Http\Message\ServerRequestInterface;
29
31{
32 public const string KEY_REF_ID = 'ref_id';
33
34 public const string ALL_OBJECTS = 'ALL_OBJECTS';
35
36 public function __construct(
37 private readonly IliasHttpServices $http,
38 private readonly Refinery $refinery
39 ) {
40 }
41
42 public function getRequest(): ServerRequestInterface
43 {
44 return $this->http->request();
45 }
46
47 public function getRefId(): int
48 {
49 return $this->get(self::KEY_REF_ID, $this->refinery->kindlyTo()->int());
50 }
51
52 public function resolveRowParameter(string $key): string|int
53 {
54 return $this->get(
55 $key,
56 $this->refinery->byTrying(
57 [
58 $this->refinery->kindlyTo()->int(),
59 $this->refinery->kindlyTo()->string(),
60 $this->refinery->custom()->transformation(static fn(array $value): string|int => $value[0])
61 ]
62 )
63 );
64 }
65
66 public function resolveRowParameters(string $key): array|string
67 {
68 return $this->get($key, $this->refinery->custom()->transformation(
69 static fn(array|string $value): array|string => $value === self::ALL_OBJECTS || $value[0] === self::ALL_OBJECTS
70 ? self::ALL_OBJECTS
71 : array_map('intval', $value)
72 )) ?? [];
73 }
74
75 public function get(string $key, Transformation $t): mixed
76 {
77 $wrapper = $this->http->wrapper();
78
79 return match(true) {
80 $wrapper->post()->has($key) => $wrapper->post()->retrieve($key, $t),
81 $wrapper->query()->has($key) => $wrapper->query()->retrieve($key, $t),
82 default => null,
83 };
84 }
85
89 public function sendAsync(mixed $response): void
90 {
91 $response = match(true) {
94 default => $response,
95 };
96
97 $this->http->saveResponse($this->http->response()->withBody($response));
98 $this->http->sendResponse();
99 $this->http->close();
100 }
101
102 public function has(string $key): bool
103 {
104 return $this->http->wrapper()->query()->has($key) || $this->http->wrapper()->post()->has($key);
105 }
106}
Builds data types.
Definition: Factory.php:36
Stream factory which enables the user to create streams without the knowledge of the concrete class.
Definition: Streams.php:32
static ofString(string $string)
Creates a new stream with an initial value.
Definition: Streams.php:41
static ofResource($resource)
Wraps an already created resource with the stream abstraction.
Definition: Streams.php:64
Class Services.
Definition: Services.php:43
__construct(private readonly IliasHttpServices $http, private readonly Refinery $refinery)
Definition: HttpService.php:36
sendAsync(mixed $response)
Definition: HttpService.php:89
A transformation is a function from one datatype to another.
static http()
Fetches the global http state from ILIAS.
$response
Definition: xapitoken.php:92