"I'm susperd the mountain zweist" - Lenka Dusilová
This is a routing tool for Slim Framework 4
to generate router
only by using zircote/swagger-php
attributes (or maybe annotations,
didn't test it) to generate the router.
composer require zrnik/zweist
class HelloWorldController
{
public function __construct(
private readonly \Zrnik\Zweist\Content\JsonContentFacade $jsonContentFacade,
) {}
/**
* @param array<string, string> $arguments
* @throws JsonException
*/
#[
Get(
path: '/api/hello/{name:.*}',
operationId: 'Say Hello',
description: 'says hello by the request parameter',
),
Response(
response: 200,
description: 'when ok',
content: new JsonContent(ref: TestResponse::class)
),
Middleware(ExampleMiddleware::class),
]
public function sayHello(
RequestInterface $request,
ResponseInterface $response,
array $arguments = []
): ResponseInterface
{
return $this->jsonContentFacade->updateResponse(
$response,
new TestResponse(
sprintf(
'Hello, %s :)',
$arguments['name']
)
)
);
}
}
$zweistConfiguration = new ZweistConfiguration(
// scan paths for openapi attributes (requests & schemas)
[
__DIR__ . '/../../Controllers',
__DIR__ . '/../../Model',
],
// generated (and committed) files
__DIR__ . '/openapi.json',
__DIR__ .'/router.json',
);
$zweistOpenApiGenerator = $container->get(ZweistOpenApiGenerator::class);
$zweistOpenApiGenerator->generate();
$zweistRouteService = $container->get(ZweistRouteService::class);
$zweistRouteService->applyRoutes($app);
You will need to create a class with openapi
description attributes.
(see ./tests/ExampleApplication/ExampleApplicationInfo.php)
You want to generate openapi.json
and router.json
locally when developing,
and then committing them with your code, because you do not want to scan all the files
for the router at runtime for every request.
You should check on the CI that you didn't forget to generate new files.