-
-
Notifications
You must be signed in to change notification settings - Fork 102
/
Copy pathExportPostmanCommand.php
53 lines (42 loc) · 1.81 KB
/
ExportPostmanCommand.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<?php
namespace AndreasElia\PostmanGenerator\Commands;
use AndreasElia\PostmanGenerator\Exporter;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
class ExportPostmanCommand extends Command
{
/** @var string */
protected $signature = 'export:postman
{--bearer= : The bearer token to use on your endpoints}
{--basic= : The basic auth to use on your endpoints}';
/** @var string */
protected $description = 'Automatically generate a Postman collection for your API routes';
public function handle(Exporter $exporter): void
{
$filename = str_replace(
['{timestamp}', '{app}'],
[date('Y_m_d_His'), Str::snake(config('app.name'))],
config('api-postman.filename')
);
config()->set('api-postman.authentication', [
'method' => $this->option('bearer') ? 'bearer' : ($this->option('basic') ? 'basic' : null),
'token' => $this->option('bearer') ?? $this->option('basic') ?? null,
]);
$exporter
->to($filename)
->setAuthentication(value(function () {
if (filled($this->option('bearer'))) {
return new \AndreasElia\PostmanGenerator\Authentication\Bearer($this->option('bearer'));
}
if (filled($this->option('basic'))) {
return new \AndreasElia\PostmanGenerator\Authentication\Basic($this->option('basic'));
}
return null;
}))
->export();
Storage::disk(config('api-postman.disk'))
->put('postman/'.$filename, $exporter->getOutput());
$this->info('Postman Collection Exported: '.storage_path('app/postman/'.$filename));
}
}