Skip to content

Commit 649bdfd

Browse files
Merge pull request #294 from hafijul233/master
Artisan command to export collection as JSON file
2 parents 4f47d1d + 4bff5ec commit 649bdfd

File tree

3 files changed

+145
-0
lines changed

3 files changed

+145
-0
lines changed

config/request-docs.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,4 +153,8 @@
153153
],
154154
],
155155
],
156+
157+
//export request docs as json file from terminal
158+
//from project root directory
159+
'export_path' => 'api.json'
156160
];
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
<?php
2+
3+
namespace Rakutentech\LaravelRequestDocs\Commands;
4+
5+
use ErrorException;
6+
use Exception;
7+
use Illuminate\Console\Command;
8+
use Illuminate\Support\Collection;
9+
use Rakutentech\LaravelRequestDocs\LaravelRequestDocs;
10+
use Rakutentech\LaravelRequestDocs\LaravelRequestDocsToOpenApi;
11+
12+
class ExportRequestDocsCommand extends Command
13+
{
14+
private LaravelRequestDocs $laravelRequestDocs;
15+
private LaravelRequestDocsToOpenApi $laravelRequestDocsToOpenApi;
16+
17+
public function __construct(LaravelRequestDocs $laravelRequestDoc, LaravelRequestDocsToOpenApi $laravelRequestDocsToOpenApi)
18+
{
19+
parent::__construct();
20+
21+
$this->laravelRequestDocs = $laravelRequestDoc;
22+
$this->laravelRequestDocsToOpenApi = $laravelRequestDocsToOpenApi;
23+
}
24+
25+
/**
26+
* The name and signature of the console command.
27+
*
28+
* @var string
29+
*/
30+
protected $signature = 'laravel-request-docs:export
31+
{path? : Export file location}
32+
{--force : Whether to overwrite existing file}';
33+
34+
/**
35+
* The console command description.
36+
*
37+
* @var string
38+
*/
39+
protected $description = 'Generate OpenAPI collection as json file';
40+
41+
private string $exportFilePath;
42+
43+
/**
44+
* Execute the console command.
45+
*/
46+
public function handle()
47+
{
48+
if (!$this->confirmFilePathAvailability()) {
49+
//silently stop command
50+
return self::SUCCESS;
51+
}
52+
53+
try {
54+
//get the excluded methods list from config
55+
$excludedMethods = config('request-docs.open_api.exclude_http_methods', []);
56+
$excludedMethods = array_map(fn($item) => strtolower($item), $excludedMethods);
57+
58+
//filter while method apis to export
59+
$showGet = !in_array('get', $excludedMethods);
60+
$showPost = !in_array('post', $excludedMethods);
61+
$showPut = !in_array('put', $excludedMethods);
62+
$showPatch = !in_array('patch', $excludedMethods);
63+
$showDelete = !in_array('delete', $excludedMethods);
64+
$showHead = !in_array('head', $excludedMethods);
65+
66+
// Get a list of Doc with route and rules information.
67+
$docs = $this->laravelRequestDocs->getDocs(
68+
$showGet,
69+
$showPost,
70+
$showPut,
71+
$showPatch,
72+
$showDelete,
73+
$showHead,
74+
);
75+
76+
// Loop and split Doc by the `methods` property.
77+
$docs = $this->laravelRequestDocs->splitByMethods($docs);
78+
$docs = $this->laravelRequestDocs->sortDocs($docs, 'default');
79+
$docs = $this->laravelRequestDocs->groupDocs($docs, 'default');
80+
81+
if (!$this->writeApiDocsToFile($docs)) {
82+
throw new ErrorException("Failed to write on [{$this->exportFilePath}] file.");
83+
}
84+
} catch (Exception $exception) {
85+
$this->error('Error : ' . $exception->getMessage());
86+
return self::FAILURE;
87+
}
88+
89+
return self::SUCCESS;
90+
}
91+
92+
/**
93+
* @return bool
94+
*/
95+
private function confirmFilePathAvailability(): bool
96+
{
97+
$path = $this->argument('path');
98+
99+
if (!$path) {
100+
$path = config('request-docs.export_path', 'api.json');
101+
}
102+
103+
$this->exportFilePath = base_path($path);
104+
105+
$path = str_replace(base_path('/'), '', $this->exportFilePath);
106+
107+
if (file_exists($this->exportFilePath)) {
108+
if (!$this->option('force')) {
109+
if ($this->confirm("File exists on [{$path}]. Overwrite?", false) == true) {
110+
return true;
111+
}
112+
return false;
113+
}
114+
}
115+
116+
return true;
117+
}
118+
119+
/**
120+
* @param $docs
121+
* @return false|int
122+
*/
123+
private function writeApiDocsToFile(Collection $docs): bool
124+
{
125+
$content = json_encode(
126+
$this->laravelRequestDocsToOpenApi->openApi($docs->all())->toArray(),
127+
JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE
128+
);
129+
130+
$targetDirectory = dirname($this->exportFilePath);
131+
132+
//create parent directory if not exists
133+
if (!is_dir($targetDirectory)) {
134+
mkdir($targetDirectory, 0755, true);
135+
}
136+
137+
return (bool)file_put_contents($this->exportFilePath, $content);
138+
}
139+
}

src/LaravelRequestDocsServiceProvider.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Rakutentech\LaravelRequestDocs;
44

55
use Illuminate\Support\Facades\Route;
6+
use Rakutentech\LaravelRequestDocs\Commands\ExportRequestDocsCommand;
67
use Spatie\LaravelPackageTools\Package;
78
use Spatie\LaravelPackageTools\PackageServiceProvider;
89

@@ -18,6 +19,7 @@ public function configurePackage(Package $package): void
1819
$package
1920
->name('laravel-request-docs')
2021
->hasConfigFile('request-docs')
22+
->hasCommand(ExportRequestDocsCommand::class)
2123
// ->hasAssets()
2224
->hasViews();
2325
// ->hasAssets();

0 commit comments

Comments
 (0)