111 lines
3.2 KiB
PHP
111 lines
3.2 KiB
PHP
<?php
|
|
|
|
/**
|
|
* ============================================================
|
|
*
|
|
* @project biiproject
|
|
*
|
|
* @author Andika Debi Putra
|
|
*
|
|
* @email andikadebiputra@gmail.com
|
|
*
|
|
* @website https://biiproject.com
|
|
*
|
|
* @copyright Copyright (c) 2026 Andika Debi Putra
|
|
* @license Proprietary - All Rights Reserved
|
|
*
|
|
* @version 1.0.0
|
|
*
|
|
* @created 2026-05-01
|
|
* ============================================================
|
|
*/
|
|
|
|
namespace App\Console\Commands\AI;
|
|
|
|
use App\Services\AI\AiService;
|
|
use Illuminate\Console\Command;
|
|
use Illuminate\Support\Facades\File;
|
|
|
|
class GenerateSwaggerAnnotations extends Command
|
|
{
|
|
/**
|
|
* The name and signature of the console command.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $signature = 'ai:swagger {controller : The path to the controller file}';
|
|
|
|
/**
|
|
* The console command description.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $description = 'Generate Swagger/OpenAPI annotations for a controller using AI';
|
|
|
|
public function __construct(
|
|
protected AiService $aiService
|
|
) {
|
|
parent::__construct();
|
|
}
|
|
|
|
/**
|
|
* Execute the console command.
|
|
*/
|
|
public function handle()
|
|
{
|
|
$path = $this->argument('controller');
|
|
|
|
if (! File::exists($path)) {
|
|
// Try to find it in app/Http/Controllers
|
|
$fullPath = app_path('Http/Controllers/'.ltrim($path, '/'));
|
|
if (! File::exists($fullPath)) {
|
|
$this->error("Controller not found at: {$path}");
|
|
|
|
return 1;
|
|
}
|
|
$path = $fullPath;
|
|
}
|
|
|
|
$content = File::get($path);
|
|
|
|
$this->info('Analyzing controller: '.basename($path));
|
|
|
|
$prompt = "Generate PHP Swagger (L5-Swagger) annotations for the following Laravel controller.
|
|
Focus on @OA\Get, @OA\Post, etc. with proper @OA\Response and @OA\Parameter.
|
|
|
|
Guidelines:
|
|
- Use modern OpenAPI 3.0 standards.
|
|
- Include common responses like 200, 401, 403, and 500.
|
|
- Identify request parameters from the code.
|
|
- OUTPUT ONLY THE PHP CODE FOR THE ANNOTATIONS, no extra explanation.
|
|
|
|
CONTROLLER CODE:
|
|
{$content}";
|
|
|
|
$result = $this->aiService->provider()->generate($prompt);
|
|
|
|
if (isset($result['success']) && $result['success']) {
|
|
$annotations = $result['response'];
|
|
|
|
$this->warn('AI Generated Annotations:');
|
|
$this->line($annotations);
|
|
|
|
if ($this->confirm('Do you want to prepend these annotations to the file?')) {
|
|
// Find where to insert (usually before the class declaration)
|
|
$pattern = '/class\s+'.basename($path, '.php').'/';
|
|
if (preg_match($pattern, $content)) {
|
|
$newContent = preg_replace($pattern, $annotations."\n".'$0', $content);
|
|
File::put($path, $newContent);
|
|
$this->success('Annotations added to '.basename($path));
|
|
} else {
|
|
$this->error('Could not find class declaration to insert annotations.');
|
|
}
|
|
}
|
|
} else {
|
|
$this->error('AI Error: '.($result['error'] ?? 'Unknown error'));
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
}
|