feat: add OpenAPI auto-generate dari routes - Tambah OpenAPIGenerator class untuk scan routes dan generate spec - Tambah CLI command bin/generate-openapi.php - Support auto-generate on request via OPENAPI_AUTO_GENERATE env - Update public/index.php untuk auto-generate saat request /docs/openapi.json - Tambah dokumentasi OPENAPI_AUTO_GENERATE.md
This commit is contained in:
65
bin/generate-openapi.php
Normal file
65
bin/generate-openapi.php
Normal file
@@ -0,0 +1,65 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* CLI script to generate OpenAPI spec from routes
|
||||
*
|
||||
* Usage:
|
||||
* php bin/generate-openapi.php
|
||||
* php bin/generate-openapi.php --output public/docs/openapi.json
|
||||
*
|
||||
* Note: This script requires database connection to register routes.
|
||||
* For auto-generation without DB, use OPENAPI_AUTO_GENERATE=true in .env
|
||||
*/
|
||||
|
||||
require __DIR__ . '/../vendor/autoload.php';
|
||||
|
||||
use App\Bootstrap\AppBootstrap;
|
||||
use App\Config\AppConfig;
|
||||
use App\Modules\Auth\AuthRoutes;
|
||||
use App\Modules\Health\HealthRoutes;
|
||||
use App\Modules\Retribusi\Dashboard\DashboardRoutes;
|
||||
use App\Modules\Retribusi\Realtime\RealtimeRoutes;
|
||||
use App\Modules\Retribusi\RetribusiRoutes;
|
||||
use App\Modules\Retribusi\Summary\SummaryRoutes;
|
||||
use App\Support\OpenAPIGenerator;
|
||||
|
||||
// Load environment variables
|
||||
AppConfig::loadEnv(__DIR__ . '/..');
|
||||
|
||||
// Parse command line arguments
|
||||
$outputFile = $argv[1] ?? __DIR__ . '/../public/docs/openapi.json';
|
||||
if (isset($argv[1]) && $argv[1] === '--output' && isset($argv[2])) {
|
||||
$outputFile = $argv[2];
|
||||
}
|
||||
|
||||
try {
|
||||
// Bootstrap application
|
||||
$app = AppBootstrap::create();
|
||||
|
||||
// Register all routes (same as public/index.php)
|
||||
// Note: This requires database connection
|
||||
HealthRoutes::register($app);
|
||||
AuthRoutes::register($app);
|
||||
RetribusiRoutes::register($app);
|
||||
SummaryRoutes::register($app);
|
||||
DashboardRoutes::register($app);
|
||||
RealtimeRoutes::register($app);
|
||||
|
||||
// Generate OpenAPI spec
|
||||
$generator = new OpenAPIGenerator($app);
|
||||
$spec = $generator->generate();
|
||||
|
||||
// Write to file
|
||||
$json = json_encode($spec, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
|
||||
file_put_contents($outputFile, $json);
|
||||
|
||||
echo "✅ OpenAPI spec generated successfully!\n";
|
||||
echo "📄 Output: {$outputFile}\n";
|
||||
echo "📊 Total paths: " . count($spec['paths']) . "\n";
|
||||
} catch (\Exception $e) {
|
||||
echo "❌ Error generating OpenAPI spec: " . $e->getMessage() . "\n";
|
||||
echo "💡 Tip: Make sure database is configured in .env file\n";
|
||||
exit(1);
|
||||
}
|
||||
Reference in New Issue
Block a user