From 75e212184eb46f65d785a8a968cd44e4281112e0 Mon Sep 17 00:00:00 2001 From: Venkata Chandra Sekhar Nainala Date: Fri, 24 May 2024 11:27:20 +0200 Subject: [PATCH] feat: added command to generate api docs --- app/Console/Commands/GenerateSwaggerDocs.php | 72 ++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 app/Console/Commands/GenerateSwaggerDocs.php diff --git a/app/Console/Commands/GenerateSwaggerDocs.php b/app/Console/Commands/GenerateSwaggerDocs.php new file mode 100644 index 00000000..ae96ce07 --- /dev/null +++ b/app/Console/Commands/GenerateSwaggerDocs.php @@ -0,0 +1,72 @@ +call('rest:documentation'); + $publicPaths = [ + '/api/v1/auth/login', + '/api/v1/auth/register' + ]; + $this->modifyOpenAPIJsonFile($publicPaths); + + } + + public function modifyOpenAPIJsonFile($filterPaths = [], $filePath = 'vendor/rest/openapi.json') + { + $jsonFilePath = public_path($filePath); + + if (! File::exists($jsonFilePath)) { + abort(404, 'File not found'); + } + + $jsonData = json_decode(File::get($jsonFilePath), true); + + if (isset($jsonData['paths'])) { + foreach ($jsonData['paths'] as $pathKey => &$path) { + if (! in_array($path, $filterPaths)) { + foreach ($jsonData['paths'][$pathKey] as $operationKey => &$operation) { + $operation['security'] = [['sanctum' => []]]; + } + } + } + } + + $jsonData['components']['securitySchemes'] = [ + 'sanctum' => [ + 'type' => 'apiKey', + 'scheme' => 'Bearer', + 'description' => 'Enter token in format (Bearer \)', + 'name' => 'Authorization', + 'in' => 'header', + ], + ]; + + $updatedJsonContents = json_encode($jsonData, JSON_PRETTY_PRINT); + + File::put($jsonFilePath, $updatedJsonContents); + } +}