Skip to content

Commit dd2ed64

Browse files
DerManoMannclaude
andcommitted
refactor: use Builder in CLI command
Migrate GenerateCommand to use Builder with withGenerator() hook for processor/config customization. Add saveAs() to Result for file output. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 74a4a49 commit dd2ed64

3 files changed

Lines changed: 52 additions & 26 deletions

File tree

src/Builder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public function setLogger(LoggerInterface $logger): static
6464
* The callable receives a default Generator and may either modify it in-place
6565
* or return a fully configured instance.
6666
*
67-
* @param callable(Generator): ?Generator $hook
67+
* @param callable(Generator): (Generator|void) $hook
6868
*/
6969
public function withGenerator(callable $hook): static
7070
{

src/Builder/Result.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
namespace OpenApi\Builder;
88

99
use OpenApi\Annotations\OpenApi;
10+
use OpenApi\OpenApiException;
1011

1112
/**
1213
* Result container for a build operation.
@@ -86,4 +87,17 @@ public function toYaml(int $inline = 10, int $indent = 4): string
8687

8788
return $this->openApi->toYaml();
8889
}
90+
91+
public function saveAs(string $filename, string $format = 'auto'): void
92+
{
93+
if ($format === 'auto') {
94+
$format = strtolower(substr($filename, -5)) === '.json' ? 'json' : 'yaml';
95+
}
96+
97+
$content = strtolower($format) === 'json' ? $this->toJson() : $this->toYaml();
98+
99+
if (file_put_contents($filename, $content) === false) {
100+
throw new OpenApiException('Failed to save to "' . $filename . '"');
101+
}
102+
}
89103
}

src/Console/GenerateCommand.php

Lines changed: 37 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66

77
namespace OpenApi\Console;
88

9-
use OpenApi\Annotations as OA;
9+
use OpenApi\Builder;
10+
use OpenApi\Builder\Result;
1011
use OpenApi\Generator;
1112
use OpenApi\Utils\SourceFinder;
1213
use Symfony\Component\Console\Attribute\AsCommand;
@@ -45,50 +46,61 @@ public function __invoke(#[MapInput] GenerateInput $input, SymfonyStyle $io): in
4546
return 0;
4647
}
4748

48-
$openapi = $this->generate($input);
49+
$result = $this->generate($input);
4950

5051
if (!$input->output) {
5152
if ($input->format->isJson()) {
52-
echo $openapi->toJson();
53+
echo $result->toJson();
5354
} else {
54-
echo $openapi->toYaml();
55+
echo $result->toYaml();
5556
}
5657
echo "\n";
5758
} else {
5859
$outputPath = $input->output;
5960
if (is_dir($outputPath)) {
6061
$outputPath .= '/openapi.yaml';
6162
}
62-
$openapi->saveAs($outputPath, $input->format->value);
63+
$result->saveAs($outputPath, $input->format->value);
6364
}
6465

6566
return $this->logger->hasErrored() ? 1 : 0;
6667
}
6768

68-
private function generate(GenerateInput $input): OA\OpenApi
69+
protected function generate(GenerateInput $input): Result
6970
{
70-
$generator = new Generator($this->logger);
71-
72-
foreach ($input->addProcessor as $processor) {
73-
$class = '\OpenApi\Processors\\' . ucfirst((string) $processor);
74-
if (class_exists($class)) {
75-
$processor = new $class();
76-
} elseif (class_exists($processor)) {
77-
$processor = new $processor();
78-
}
79-
$generator->getProcessorPipeline()->add($processor);
71+
$builder = (new Builder())
72+
->addSource(new SourceFinder($input->paths, $input->exclude, $input->pattern))
73+
->setLogger($this->logger);
74+
75+
if ($input->version !== null) {
76+
$builder->setVersion($input->version);
8077
}
8178

82-
foreach ($input->removeProcessor as $processor) {
83-
$class = class_exists($processor)
84-
? $processor
85-
: '\OpenApi\Processors\\' . ucfirst((string) $processor);
86-
$generator->getProcessorPipeline()->remove($class);
79+
if ($input->config || $input->addProcessor || $input->removeProcessor) {
80+
$builder->withGenerator(function (Generator $generator) use ($input): void {
81+
if ($input->config) {
82+
$generator->setConfig($input->config);
83+
}
84+
85+
foreach ($input->addProcessor as $processor) {
86+
$class = '\OpenApi\Processors\\' . ucfirst((string) $processor);
87+
if (class_exists($class)) {
88+
$processor = new $class();
89+
} elseif (class_exists($processor)) {
90+
$processor = new $processor();
91+
}
92+
$generator->getProcessorPipeline()->add($processor);
93+
}
94+
95+
foreach ($input->removeProcessor as $processor) {
96+
$class = class_exists($processor)
97+
? $processor
98+
: '\OpenApi\Processors\\' . ucfirst((string) $processor);
99+
$generator->getProcessorPipeline()->remove($class);
100+
}
101+
});
87102
}
88103

89-
return $generator
90-
->setVersion($input->version)
91-
->setConfig($input->config)
92-
->generate(new SourceFinder($input->paths, $input->exclude, $input->pattern));
104+
return $builder->build();
93105
}
94106
}

0 commit comments

Comments
 (0)