diff --git a/.gitignore b/.gitignore index 6ec77e4e..63dcf8ad 100644 --- a/.gitignore +++ b/.gitignore @@ -9,3 +9,4 @@ phpstan.neon testbench.yaml vendor node_modules +*-test.json diff --git a/config/scramble.php b/config/scramble.php index dfdae5a1..7f7fa17f 100644 --- a/config/scramble.php +++ b/config/scramble.php @@ -15,6 +15,11 @@ */ 'api_domain' => null, + /** + * The path where your OpenAPI specification will be exported. + */ + 'export_path' => 'api.json', + /* * Define the theme of the documentation. * Available options are `light` and `dark`. diff --git a/src/Console/Commands/ExportSpecifications.php b/src/Console/Commands/ExportSpecifications.php new file mode 100644 index 00000000..fc6c5e28 --- /dev/null +++ b/src/Console/Commands/ExportSpecifications.php @@ -0,0 +1,42 @@ +option('path') ?? config('scramble.export_path', 'api.json'); + + File::put($filename, $specifications); + + $this->info("OpenAPI specifications exported to {$filename}."); + } +} diff --git a/src/ScrambleServiceProvider.php b/src/ScrambleServiceProvider.php index 1e194230..0a156f60 100644 --- a/src/ScrambleServiceProvider.php +++ b/src/ScrambleServiceProvider.php @@ -2,6 +2,7 @@ namespace Dedoc\Scramble; +use Dedoc\Scramble\Console\Commands\ExportSpecifications; use Dedoc\Scramble\Extensions\ExceptionToResponseExtension; use Dedoc\Scramble\Extensions\OperationExtension; use Dedoc\Scramble\Extensions\TypeToSchemaExtension; @@ -50,6 +51,7 @@ public function configurePackage(Package $package): void ->name('scramble') ->hasConfigFile() ->hasRoute('web') + ->hasCommand(ExportSpecifications::class) ->hasViews('scramble'); $this->app->singleton(FileParser::class, function () { diff --git a/tests/Console/Commands/ExportSpecificationsCommandTest.php b/tests/Console/Commands/ExportSpecificationsCommandTest.php new file mode 100644 index 00000000..792d3a3e --- /dev/null +++ b/tests/Console/Commands/ExportSpecificationsCommandTest.php @@ -0,0 +1,17 @@ +assertExitCode(0); + + $json_data = json_decode(file_get_contents($filepath), true); + + expect($json_data)->toBe($generator()); +});