Files
2026-08-31 11:33:59 +08:00

62 lines
2.2 KiB
PHP

<?php
/**
* This file is part of webman.
* Licensed under The MIT License
* For full copyright and license information, please see the MIT-LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @author walkor<walkor@workerman.net>
* @copyright walkor<walkor@workerman.net>
* @link http://www.workerman.net/
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
use OpenApi\Annotations as OA;
use app\controller\OpenApiController;
use Webman\Route;
use WebmanTech\Swagger\DTO\ConfigOpenapiDocDTO;
use WebmanTech\Swagger\Integrations\RouteRegister;
use WebmanTech\Swagger\RouteAnnotation\Reader;
/**
* 注册API路由
*/
Route::group('/api', function () {
$openapiDocConfig = [
'format' => 'json',
'scan_path' => [
base_path('app/api'),
],
'modify' => function (OA\OpenApi $openapi) {
$openapi->info->title = config('app.name') . ' API';
$openapi->info->version = '1.0.0';
$openapi->servers = [
new OA\Server([
'url' => '/api',
'description' => request()->host(),
]),
];
/** @phpstan-ignore-next-line */
if (!$openapi->components instanceof OA\Components) {
$openapi->components = new OA\Components([]);
}
$openapi->components->securitySchemes = [
new OA\SecurityScheme([
'securityScheme' => 'api_key',
'type' => 'apiKey',
'name' => config('core.jwt.app.token_name', 'Authorization'),
'in' => 'header',
]),
];
},
];
$openApiController = new OpenApiController();
Route::get('/openapi', static fn () => $openApiController->index('/api/openapi.json'));
Route::get('/openapi.json', static fn () => $openApiController->document($openapiDocConfig));
$config = ConfigOpenapiDocDTO::fromConfig($openapiDocConfig);
RouteRegister::create()->register((new Reader())->getData($config->getScanSources()));
});