| Total Complexity | 5 |
| Total Lines | 31 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 1 |
| 1 | <?php |
||
| 8 | class DumpOpenApiSpecCommand extends Command |
||
| 9 | { |
||
| 10 | protected $signature = 'apie:dump-open-api {filename}'; |
||
| 11 | |||
| 12 | protected $description = 'Dumps openapi spec as JSON or yaml to a file.'; |
||
| 13 | |||
| 14 | public function handle(OpenApiSpecGenerator $generator) |
||
| 15 | { |
||
| 16 | $filename = $this->argument('filename'); |
||
| 17 | if (preg_match('/\.y[a]{0,1}ml$/i', $filename)) { |
||
|
|
|||
| 18 | $content = $this->createYamlContent($generator); |
||
| 19 | } else { |
||
| 20 | $content = $this->createJsonContent($generator); |
||
| 21 | } |
||
| 22 | if (@file_put_contents($this->argument('filename'), $content)) { |
||
| 23 | $this->output->writeln('Created file "' . $this->argument('filename') . '" successfully!'); |
||
| 24 | } else { |
||
| 25 | $this->output->error('Could not write file "' . $this->argument('filename') . '"!'); |
||
| 26 | return false; |
||
| 27 | } |
||
| 28 | return true; |
||
| 29 | } |
||
| 30 | |||
| 31 | private function createYamlContent(OpenApiSpecGenerator $generator): string |
||
| 32 | { |
||
| 33 | return $generator->getOpenApiSpec()->toYaml(20, 2); |
||
| 34 | } |
||
| 35 | |||
| 36 | private function createJsonContent(OpenApiSpecGenerator $generator): string |
||
| 39 | } |
||
| 40 | } |
||
| 41 |