|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Th3Mouk\OpenAPIGenerator\Command; |
|
6
|
|
|
|
|
7
|
|
|
use Symfony\Component\Console\Command\Command; |
|
8
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
|
9
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
10
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
11
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
12
|
|
|
use Symfony\Component\Finder\Finder; |
|
13
|
|
|
use Symfony\Component\Yaml\Yaml; |
|
14
|
|
|
use Th3Mouk\OpenAPIGenerator\PathHelper; |
|
15
|
|
|
use Traversable; |
|
16
|
|
|
|
|
17
|
|
|
final class GenerateCommand extends Command |
|
18
|
|
|
{ |
|
19
|
|
|
protected function configure(): void |
|
20
|
|
|
{ |
|
21
|
|
|
$this |
|
22
|
|
|
->setName('generate') |
|
23
|
|
|
->setDescription('Generate the openapi.json') |
|
24
|
|
|
->addArgument('path', InputArgument::OPTIONAL, 'The path where generate the openapi.json file', '') |
|
25
|
|
|
->addOption('pretty-json', 'p', InputOption::VALUE_NONE, 'Generate json file in pretty format'); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
protected function execute(InputInterface $input, OutputInterface $output): int |
|
29
|
|
|
{ |
|
30
|
|
|
$this->generateJson($input); |
|
31
|
|
|
echo 'generated' . PHP_EOL; |
|
32
|
|
|
|
|
33
|
|
|
return 0; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
private function generateJson(InputInterface $input): void |
|
37
|
|
|
{ |
|
38
|
|
|
$template_file = (new Finder()) |
|
39
|
|
|
->in(getRootPath() . PathHelper::ROOT) |
|
40
|
|
|
->files() |
|
41
|
|
|
->name('openapi.yaml'); |
|
42
|
|
|
|
|
43
|
|
|
if (!$template_file->hasResults()) { |
|
44
|
|
|
echo 'no openapi.yaml file found' . PHP_EOL; |
|
45
|
|
|
|
|
46
|
|
|
return; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
$template = $this->getFirstElementOfFileIterator($template_file); |
|
|
|
|
|
|
50
|
|
|
|
|
51
|
|
|
$template = Yaml::parse($template->getContents()); |
|
52
|
|
|
$components = $template['components'] ?? []; |
|
53
|
|
|
|
|
54
|
|
|
$node_compactor = static fn (Traversable $node_iterator) => array_reduce( |
|
55
|
|
|
iterator_to_array($node_iterator), |
|
56
|
|
|
static fn ($carry, $item) => array_merge($carry, $item), |
|
57
|
|
|
[], |
|
58
|
|
|
); |
|
59
|
|
|
|
|
60
|
|
|
$components['schemas'] = (object) $node_compactor($this->getContentGenerator(PathHelper::SCHEMAS)); |
|
61
|
|
|
$components['responses'] = (object) $node_compactor($this->getContentGenerator(PathHelper::RESPONSES)); |
|
62
|
|
|
$components['parameters'] = (object) $node_compactor($this->getContentGenerator(PathHelper::PARAMETERS)); |
|
63
|
|
|
$components['examples'] = (object) $node_compactor($this->getContentGenerator(PathHelper::EXAMPLES)); |
|
64
|
|
|
$components['requestBodies'] = (object) $node_compactor($this->getContentGenerator(PathHelper::REQUEST_BODIES)); |
|
65
|
|
|
$components['headers'] = (object) $node_compactor($this->getContentGenerator(PathHelper::HEADERS)); |
|
66
|
|
|
$components['securitySchemes'] = (object) $node_compactor($this->getContentGenerator(PathHelper::SECURITY_SCHEMES)); |
|
67
|
|
|
$components['links'] = (object) $node_compactor($this->getContentGenerator(PathHelper::LINKS)); |
|
68
|
|
|
$components['callbacks'] = (object) $node_compactor($this->getContentGenerator(PathHelper::CALLBACKS)); |
|
69
|
|
|
|
|
70
|
|
|
$template['paths'] = (object) $node_compactor($this->getContentGenerator(PathHelper::PATHS)); |
|
71
|
|
|
$template['components'] = (object) $components; |
|
72
|
|
|
|
|
73
|
|
|
$arg_path = $input->getArgument('path'); |
|
74
|
|
|
|
|
75
|
|
|
if (!is_string($arg_path)) { |
|
76
|
|
|
throw new \RuntimeException('Path argument must be a string'); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
$path = '/' !== substr($arg_path, 0, 1) ? '/' . $arg_path : $arg_path; |
|
80
|
|
|
|
|
81
|
|
|
$openapi_file_path = getRootPath() . $path . '/openapi.json'; |
|
82
|
|
|
if (!$file = fopen($openapi_file_path, 'w')) { |
|
83
|
|
|
echo 'error generating openapi.json file' . PHP_EOL; |
|
84
|
|
|
|
|
85
|
|
|
return; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
if ($input->getOption('pretty-json')) { |
|
89
|
|
|
fwrite($file, \json_encode($template, JSON_UNESCAPED_SLASHES | JSON_THROW_ON_ERROR | JSON_PRETTY_PRINT)); |
|
90
|
|
|
fclose($file); |
|
91
|
|
|
|
|
92
|
|
|
return; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
fwrite($file, \json_encode($template, JSON_UNESCAPED_SLASHES | JSON_THROW_ON_ERROR)); |
|
96
|
|
|
fclose($file); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @return \Generator<mixed> |
|
101
|
|
|
*/ |
|
102
|
|
|
private function getContentGenerator(string $path): \Generator |
|
103
|
|
|
{ |
|
104
|
|
|
foreach ((new Finder())->files()->in(getRootPath() . $path)->name('*.yaml') as $file) { |
|
105
|
|
|
yield Yaml::parse($file->getContents()); |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* @return mixed|null |
|
111
|
|
|
*/ |
|
112
|
|
|
private function getFirstElementOfFileIterator(Finder $iterator) |
|
113
|
|
|
{ |
|
114
|
|
|
foreach ($iterator as $element) { |
|
115
|
|
|
return $element; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
return null; |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
|
This check looks for function or method calls that always return null and whose return value is assigned to a variable.
The method
getObject()can return nothing but null, so it makes no sense to assign that value to a variable.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.