-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApiController.php
More file actions
77 lines (66 loc) · 2.41 KB
/
Copy pathApiController.php
File metadata and controls
77 lines (66 loc) · 2.41 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
<?php
// SPDX-FileCopyrightText: 2025 Icinga GmbH <https://icinga.com>
// SPDX-License-Identifier: GPL-3.0-or-later
namespace Icinga\Module\Notifications\Controllers;
use Exception;
use Icinga\Exception\Http\HttpBadRequestException;
use Icinga\Module\Notifications\Api\Middleware\DispatchMiddleware;
use Icinga\Module\Notifications\Api\Middleware\EndpointExecutionMiddleware;
use Icinga\Module\Notifications\Api\Middleware\ErrorHandlingMiddleware;
use Icinga\Module\Notifications\Api\Middleware\LegacyRequestConversionMiddleware;
use Icinga\Module\Notifications\Api\Middleware\MiddlewarePipeline;
use Icinga\Module\Notifications\Api\Middleware\RoutingMiddleware;
use Icinga\Module\Notifications\Api\Middleware\ValidationMiddleware;
use Icinga\Security\SecurityException;
use Icinga\Web\Request;
use ipl\Web\Compat\CompatController;
use Psr\Http\Message\ResponseInterface;
class ApiController extends CompatController
{
/**
* Handle API requests and route them to the appropriate endpoint class.
*
* Processes API requests for the Notifications module, serving as the main entry point for all API interactions.
*
* @return never
* @throws SecurityException
*/
public function indexAction(): never
{
$this->assertPermission('notifications/api');
$pipeline = new MiddlewarePipeline([
new ErrorHandlingMiddleware(),
new LegacyRequestConversionMiddleware($this->getRequest()),
new RoutingMiddleware(),
new DispatchMiddleware(),
new ValidationMiddleware(),
new EndpointExecutionMiddleware(),
]);
$this->emitResponse($pipeline->execute());
exit;
}
/**
* Emit the HTTP response to the client.
*
* @param ResponseInterface $response The response object to emit.
*
* @return void
*/
protected function emitResponse(ResponseInterface $response): void
{
do {
ob_end_clean();
} while (ob_get_level() > 0);
http_response_code($response->getStatusCode());
foreach ($response->getHeaders() as $name => $values) {
foreach ($values as $value) {
header(sprintf('%s: %s', $name, $value), false);
}
}
header('Content-Type: application/json');
$body = $response->getBody();
while (! $body->eof()) {
echo $body->read(8192);
}
}
}