Skip to content

Commit cb7c03a

Browse files
committed
artisan command to export collection as file
1 parent 4f47d1d commit cb7c03a

File tree

3 files changed

+140
-0
lines changed

3 files changed

+140
-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: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
<?php
2+
3+
namespace Rakutentech\LaravelRequestDocs\Commands;
4+
5+
use Illuminate\Console\Command;
6+
use Rakutentech\LaravelRequestDocs\LaravelRequestDocs;
7+
use Rakutentech\LaravelRequestDocs\LaravelRequestDocsToOpenApi;
8+
9+
class ExportRequestDocsCommand extends Command
10+
{
11+
private LaravelRequestDocs $laravelRequestDocs;
12+
private LaravelRequestDocsToOpenApi $laravelRequestDocsToOpenApi;
13+
14+
public function __construct(LaravelRequestDocs $laravelRequestDoc, LaravelRequestDocsToOpenApi $laravelRequestDocsToOpenApi)
15+
{
16+
parent::__construct();
17+
18+
$this->laravelRequestDocs = $laravelRequestDoc;
19+
$this->laravelRequestDocsToOpenApi = $laravelRequestDocsToOpenApi;
20+
}
21+
22+
/**
23+
* The name and signature of the console command.
24+
*
25+
* @var string
26+
*/
27+
protected $signature = 'laravel-request-docs:export
28+
{path? : Export file location}
29+
{--force : Whether to overwrite existing file}';
30+
31+
/**
32+
* The console command description.
33+
*
34+
* @var string
35+
*/
36+
protected $description = 'Generate OpenAPI collection as json file';
37+
38+
private string $exportFilePath;
39+
40+
/**
41+
* Execute the console command.
42+
*/
43+
public function handle()
44+
{
45+
if ($this->confirmFilePath()) {
46+
try {
47+
$excludedMethods = config('request-docs.open_api.exclude_http_methods', []);
48+
49+
$excludedMethods = array_map(fn($item) => strtolower($item), $excludedMethods);
50+
51+
$showGet = !in_array('get', $excludedMethods);
52+
$showPost = !in_array('post', $excludedMethods);
53+
$showPut = !in_array('put', $excludedMethods);
54+
$showPatch = !in_array('patch', $excludedMethods);
55+
$showDelete = !in_array('delete', $excludedMethods);
56+
$showHead = !in_array('head', $excludedMethods);
57+
58+
// Get a list of Doc with route and rules information.
59+
// If user defined `Route::match(['get', 'post'], 'uri', ...)`,
60+
// only a single Doc will be generated.
61+
$docs = $this->laravelRequestDocs->getDocs(
62+
$showGet,
63+
$showPost,
64+
$showPut,
65+
$showPatch,
66+
$showDelete,
67+
$showHead,
68+
);
69+
70+
// Loop and split Doc by the `methods` property.
71+
// `Route::match([...n], 'uri', ...)` will generate n number of Doc.
72+
$docs = $this->laravelRequestDocs->splitByMethods($docs);
73+
$docs = $this->laravelRequestDocs->sortDocs($docs, 'default');
74+
$docs = $this->laravelRequestDocs->groupDocs($docs, 'default');
75+
76+
$content = json_encode(
77+
$this->laravelRequestDocsToOpenApi->openApi($docs->all())->toArray(),
78+
JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE
79+
);
80+
81+
if (!$this->writeFile($content)) {
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+
90+
return self::SUCCESS;
91+
}
92+
93+
/**
94+
* @return bool
95+
*/
96+
private function confirmFilePath(): bool
97+
{
98+
$path = $this->argument('path');
99+
100+
if (!$path) {
101+
$path = config('request-docs.export_path', 'api.json');
102+
}
103+
104+
$this->exportFilePath = base_path($path);
105+
106+
$path = str_replace(base_path('/'), '', $this->exportFilePath);
107+
108+
if (file_exists($this->exportFilePath)) {
109+
if (!$this->option('force')) {
110+
if ($this->confirm("File exists on [{$path}]. Overwrite?", false) == true) {
111+
return true;
112+
}
113+
return false;
114+
}
115+
}
116+
117+
return true;
118+
}
119+
120+
/**
121+
* @param $content
122+
* @return false|int
123+
*/
124+
private function writeFile($content)
125+
{
126+
$targetDirectory = dirname($this->exportFilePath);
127+
128+
if (!is_dir($targetDirectory)) {
129+
mkdir($targetDirectory, 0755, true);
130+
}
131+
132+
return file_put_contents($this->exportFilePath, $content);
133+
}
134+
}

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)