ILIAS  trunk Revision v12.0_alpha-1613-gae4c99ebb18
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($key, $this->refinery->byTrying([
55 $this->refinery->kindlyTo()->int(),
56 $this->refinery->kindlyTo()->string(),
57 $this->refinery->custom()->transformation(static fn(array $value): string|int => $value[0])
58 ]));
59 }
60
61 public function resolveRowParameters(string $key): array|string
62 {
63 return $this->get($key, $this->refinery->custom()->transformation(
64 static fn(array|string $value): array|string => $value === self::ALL_OBJECTS || $value[0] === self::ALL_OBJECTS
65 ? self::ALL_OBJECTS
66 : array_map(
67 static fn(string $value): string|int => count(explode('_', $value)) > 1 ? $value : (int) $value,
68 $value
69 )
70 )) ?? [];
71 }
72
73 public function get(string $key, Transformation $t): mixed
74 {
75 $wrapper = $this->http->wrapper();
76
77 return match(true) {
78 $wrapper->post()->has($key) => $wrapper->post()->retrieve($key, $t),
79 $wrapper->query()->has($key) => $wrapper->query()->retrieve($key, $t),
80 default => null,
81 };
82 }
83
87 public function sendAsync(mixed $response): void
88 {
89 $response = match(true) {
92 default => $response,
93 };
94
95 $this->http->saveResponse($this->http->response()->withBody($response));
96 $this->http->sendResponse();
97 $this->http->close();
98 }
99
100 public function has(string $key): bool
101 {
102 return $this->http->wrapper()->query()->has($key) || $this->http->wrapper()->post()->has($key);
103 }
104}
__construct(private readonly IliasHttpServices $http, private readonly Refinery $refinery)
Definition: HttpService.php:36
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:38
A transformation is a function from one datatype to another.
static http()
Fetches the global http state from ILIAS.
$response
Definition: xapitoken.php:90