File size: 1,441 Bytes
cf14c12
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
<?php

declare(strict_types=1);

namespace Presentation\RequestHandlers\Admin;

use Easy\Container\Attributes\Inject;
use Easy\Http\Message\RequestMethod;
use Easy\Router\Attributes\Route;
use Override;
use Presentation\Response\JsonResponse;
use Presentation\Response\ViewResponse;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Server\RequestHandlerInterface;

#[Route(path: '/api-[docs|specs:type]', method: RequestMethod::GET)]
class ApiDocsView extends AbstractAdminViewRequestHandler implements
    RequestHandlerInterface
{
    public function __construct(
        #[Inject('config.dirs.root')]
        private string $rootDir
    ) {
    }

    #[Override]
    public function handle(ServerRequestInterface $request): ResponseInterface
    {
        $type = $request->getAttribute('type');

        if ($type == 'specs') {
            $json = json_decode(file_get_contents($this->rootDir . '/docs/admin-openapi.json'));

            $uri = $request->getUri();
            $uri = $uri->withPath('/admin/api');

            $json->servers[0] = (object) [
                'url' => (string) $uri,
                'description' => 'Production server'
            ];

            return new JsonResponse($json);
        }

        return new ViewResponse(
            '/templates/app/api.twig',
            [
                'specs' => '/admin/api-specs'
            ]
        );
    }
}