Skip to content

Commit

Permalink
Allow to set config options on the command line (#1302)
Browse files Browse the repository at this point in the history
  • Loading branch information
DerManoMann authored Aug 26, 2022
1 parent 3ea1262 commit 1d08b7a
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 12 deletions.
12 changes: 9 additions & 3 deletions bin/openapi
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ error_reporting(E_ALL);

// Possible options and their default values.
$options = [
'config' => [],
'legacy' => false,
'output' => false,
'format' => 'auto',
Expand All @@ -34,6 +35,7 @@ $options = [
'version' => null,
];
$aliases = [
'c' => 'config',
'l' => 'legacy',
'o' => 'output',
'e' => 'exclude',
Expand All @@ -45,6 +47,7 @@ $aliases = [
'f' => 'format'
];
$needsArgument = [
'config',
'output',
'format',
'exclude',
Expand Down Expand Up @@ -119,6 +122,8 @@ if ($options['help']) {
Usage: openapi [--option value] [/path/to/project ...]
Options:
--config (-c) Generator config
ex: -c operationId.hash=false
--legacy (-l) Use legacy TokenAnalyser; default is the new ReflectionAnalyser
--output (-o) Path to store the generated documentation.
ex: --output openapi.yaml
Expand All @@ -128,9 +133,9 @@ Options:
ex: --pattern "*.php" or --pattern "/\.(phps|php)$/"
--bootstrap (-b) Bootstrap a php file for defining constants, etc.
ex: --bootstrap config/constants.php
--processor Register an additional processor.
--format Force yaml or json.
--debug Show additional error information.
--processor (-p) Register an additional processor.
--format (-f) Force yaml or json.
--debug (-d) Show additional error information.
--version The OpenAPI version; defaults to {$defaultVersion}.
--help (-h) Display this help message.
Expand Down Expand Up @@ -215,6 +220,7 @@ $analyser = $options['legacy']

$openapi = $generator
->setVersion($options['version'])
->setConfig($options['config'])
->setAnalyser($analyser)
->generate(Util::finder($paths, $exclude, $pattern));

Expand Down
12 changes: 7 additions & 5 deletions docs/guide/generating-openapi-documents.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,22 @@
```

::: tip Output Format
By default the output format is YAML. If a filename is given (via `--output` or `-o`)
By default the output format is YAML. If a filename is given (via `--output` or `-o`)
the tool will use the file extension to determine the format.

The `--format` option can be used to force a specific format.
:::

For a list of all available options use the `-h` option
For a list of all available options use the `-h` option

```shell
> ./bin/openapi -h

Usage: openapi [--option value] [/path/to/project ...]

Options:
--config (-c) Generator config
ex: -c operationId.hash=false
--legacy (-l) Use legacy TokenAnalyser; default is the new ReflectionAnalyser
--output (-o) Path to store the generated documentation.
ex: --output openapi.yaml
Expand All @@ -32,9 +34,9 @@ Options:
ex: --pattern "*.php" or --pattern "/\.(phps|php)$/"
--bootstrap (-b) Bootstrap a php file for defining constants, etc.
ex: --bootstrap config/constants.php
--processor Register an additional processor.
--format Force yaml or json.
--debug Show additional error information.
--processor (-p) Register an additional processor.
--format (-f) Force yaml or json.
--debug (-d) Show additional error information.
--version The OpenAPI version; defaults to 3.0.0.
--help (-h) Display this help message.
```
Expand Down
1 change: 1 addition & 0 deletions src/Analysis.php
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,7 @@ public function process($processors = null): void
if (is_array($processors) === false && is_callable($processors)) {
$processors = [$processors];
}

foreach ($processors as $processor) {
$processor($this);
}
Expand Down
30 changes: 29 additions & 1 deletion src/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -197,14 +197,42 @@ public function getConfig(): array
return $this->config + $this->getDefaultConfig();
}

protected function normaliseConfig(array $config): array
{
$normalised = [];
foreach ($config as $key => $value) {
if (is_numeric($key)) {
$token = explode('=', $value);
if (2 == count($token)) {
// 'operationId.hash=false'
[$key, $value] = $token;
}
}

if (in_array($value, ['true', 'false'])) {
$value = 'true' == $value;
}

$token = explode('.', $key);
if (2 == count($token)) {
// 'operationId.hash' => false
$normalised[$token[0]][$token[1]] = $value;
} else {
$normalised[$key] = $value;
}
}

return $normalised;
}

/**
* Set generator and/or processor config.
*
* @param array<string,mixed> $config
*/
public function setConfig(array $config): Generator
{
$this->config = $config + $this->config;
$this->config = $this->normaliseConfig($config) + $this->config;

return $this;
}
Expand Down
19 changes: 16 additions & 3 deletions tests/GeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,13 +124,26 @@ protected function assertOperationIdHash(Generator $generator, bool $expected):
}
}

public function testConfig(): void
public function configCases(): iterable
{
return [
'default' => [[], true],
'nested' => [['operationId' => ['hash' => false]], false],
'dots-kv' => [['operationId.hash' => false], false],
'dots-string' => [['operationId.hash=false'], false],
];
}

/**
* @dataProvider configCases
*/
public function testConfig(array $config, bool $expected)
{
$generator = new Generator();
$this->assertOperationIdHash($generator, true);

$generator->setConfig(['operationId' => ['hash' => false]]);
$this->assertOperationIdHash($generator, false);
$generator->setConfig($config);
$this->assertOperationIdHash($generator, $expected);
}

public function testCallableProcessor(): void
Expand Down

0 comments on commit 1d08b7a

Please sign in to comment.