Skip to content

Commit fc8355e

Browse files
authored
Merge pull request #2 from ensi-platform/configure-generators-with-option
Configure generators with option
2 parents baaa6bc + 89f6e1f commit fc8355e

File tree

10 files changed

+47
-24
lines changed

10 files changed

+47
-24
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,9 @@ Destination must be configured with array as namespace instead of string.
6969
E.g
7070

7171
```php
72-
'requests' => ["Controllers" => "Requests"],
72+
'requests' => [
73+
'namespace' => ["Controllers" => "Requests"]
74+
],
7375
```
7476

7577
This means "Get handler (x-lg-handler) namespace and replace Controllers with Requests in it"

config/openapi-server-generator.php

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,23 @@
1010
/**
1111
* You can use as many yaml/json openapi entrypoints as you want.
1212
* Key = openapi entrypoint file with path.
13-
* Value = one of
14-
* - string: fixed PSR-4 namespace where the result of generation should be placed.
15-
* - array: relative PSR-4 namespace. Base namespace is taken from `x-lg-handler` and selected parts are replaced.
13+
* Value = list of entities and their generator params.
1614
*/
1715
'api_docs_mappings' => [
1816
public_path('api-docs/v1/index.yaml') => [
1917
'controllers' => [],
20-
'enums' => "App\\Http\\ApiV1\\OpenApiGenerated\\Enums\\",
21-
'requests' => ["Controllers" => "Requests"],
22-
'routes' => "App\\Http\\ApiV1\\OpenApiGenerated\\",
23-
'pest_tests' => ["Controllers" => "Tests"],
18+
'enums' => [
19+
'namespace' => "App\\Http\\ApiV1\\OpenApiGenerated\\Enums\\",
20+
],
21+
'requests' => [
22+
'namespace' => ["Controllers" => "Requests"],
23+
],
24+
'routes' => [
25+
'namespace' => "App\\Http\\ApiV1\\OpenApiGenerated\\",
26+
],
27+
'pest_tests' => [
28+
'namespace' => ["Controllers" => "Tests"],
29+
],
2430
],
2531
],
2632

src/Commands/GenerateServer.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,17 @@ public function handle()
4040
return self::FAILURE;
4141
}
4242

43-
foreach ($this->config['api_docs_mappings'] as $sourcePath => $toNamespaces) {
43+
foreach ($this->config['api_docs_mappings'] as $sourcePath => $optionsPerEntity) {
4444
$this->info("Generating [" . implode(', ', $this->enabledEntities) . "] for specification file \"$sourcePath\"");
45-
if (self::FAILURE === $this->handleMapping($sourcePath, $toNamespaces)) {
45+
if (self::FAILURE === $this->handleMapping($sourcePath, $optionsPerEntity)) {
4646
return self::FAILURE;
4747
}
4848
}
4949

5050
return self::SUCCESS;
5151
}
5252

53-
public function handleMapping(string $sourcePath, array $toNamespaces)
53+
public function handleMapping(string $sourcePath, array $optionsPerEntity)
5454
{
5555
$specObject = $this->parseSpec($sourcePath);
5656

@@ -59,14 +59,14 @@ public function handleMapping(string $sourcePath, array $toNamespaces)
5959
continue;
6060
}
6161

62-
if (!isset($toNamespaces[$entity])) {
63-
$this->error("Namespace for entity \"$entity\" is not set in \"api_docs_mappings\" config for source \"$sourcePath\"");
62+
if (!isset($optionsPerEntity[$entity])) {
63+
$this->error("Options for entity \"$entity\" are not set in \"api_docs_mappings\" config for source \"$sourcePath\"");
6464

6565
return self::FAILURE;
6666
}
6767

6868
$this->infoIfVerbose("Generating files for entity \"$entity\" using generator \"$generatorClass\"");
69-
resolve($generatorClass)->generate($specObject, $toNamespaces[$entity]);
69+
resolve($generatorClass)->setOptions($optionsPerEntity[$entity])->generate($specObject);
7070
}
7171

7272
return self::SUCCESS;

src/Generators/BaseGenerator.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
class BaseGenerator
1313
{
14+
protected array $options = [];
15+
1416
public function __construct(
1517
protected Filesystem $filesystem,
1618
protected TemplatesManager $templatesManager,
@@ -20,6 +22,13 @@ public function __construct(
2022
) {
2123
}
2224

25+
public function setOptions(array $options): static
26+
{
27+
$this->options = $options;
28+
29+
return $this;
30+
}
31+
2332
protected function replacePlaceholders(string $content, array $placeholders): string
2433
{
2534
return str_replace(array_keys($placeholders), array_values($placeholders), $content);

src/Generators/ControllersGenerator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class ControllersGenerator extends BaseGenerator implements GeneratorInterface
99
{
1010
private array $methodsWithRequests = ['PATCH', 'POST', 'PUT', 'DELETE'];
1111

12-
public function generate(SpecObjectInterface $specObject, string|array $namespaceData): void
12+
public function generate(SpecObjectInterface $specObject): void
1313
{
1414
$controllers = $this->extractControllers($specObject);
1515
$this->createControllersFiles($controllers, $this->templatesManager->getTemplate('Controller.template'));

src/Generators/EnumsGenerator.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@
99

1010
class EnumsGenerator extends BaseGenerator implements GeneratorInterface
1111
{
12-
public function generate(SpecObjectInterface $specObject, string|array $namespaceData): void
12+
public function generate(SpecObjectInterface $specObject): void
1313
{
14+
$namespaceData = $this->options['namespace'] ?? null;
1415
if (!is_string($namespaceData)) {
15-
throw new InvalidArgumentException("EnumsGenerator supports only strings as namespaceData");
16+
throw new InvalidArgumentException("EnumsGenerator must be configured with string as 'namespace'");
1617
}
1718

1819
$namespace = rtrim($namespaceData, "\\");

src/Generators/GeneratorInterface.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,7 @@
66

77
interface GeneratorInterface
88
{
9-
public function generate(SpecObjectInterface $specObject, string|array $namespaceData): void;
9+
public function generate(SpecObjectInterface $specObject): void;
10+
11+
public function setOptions(array $options): static;
1012
}

src/Generators/RequestsGenerator.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@ class RequestsGenerator extends BaseGenerator implements GeneratorInterface
99
{
1010
private array $methods = ['PATCH', 'POST', 'PUT', 'DELETE'];
1111

12-
public function generate(SpecObjectInterface $specObject, string|array $namespaceData): void
12+
public function generate(SpecObjectInterface $specObject): void
1313
{
14+
$namespaceData = $this->options['namespace'] ?? null;
1415
if (!is_array($namespaceData)) {
15-
throw new InvalidArgumentException("RequestsGenerator supports only arrays as namespaceData");
16+
throw new InvalidArgumentException("RequestsGenerator must be configured with array as 'namespace'");
1617
}
1718

1819
$requests = $this->extractRequests($specObject, $namespaceData);

src/Generators/RoutesGenerator.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@
77

88
class RoutesGenerator extends BaseGenerator implements GeneratorInterface
99
{
10-
public function generate(SpecObjectInterface $specObject, string|array $namespaceData): void
10+
public function generate(SpecObjectInterface $specObject): void
1111
{
12+
$namespaceData = $this->options['namespace'] ?? null;
1213
if (!is_string($namespaceData)) {
13-
throw new InvalidArgumentException("RoutesGenerator supports only strings as namespaceData");
14+
throw new InvalidArgumentException("RoutesGenerator must be configured with string as 'namespace'");
1415
}
1516

1617
$namespace = rtrim($namespaceData, "\\");

src/Generators/TestsGenerator.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,11 @@ abstract protected function convertRoutesToImportsString(array $routes): string;
1414

1515
abstract protected function getTemplateName(): string;
1616

17-
public function generate(SpecObjectInterface $specObject, string|array $namespaceData): void
17+
public function generate(SpecObjectInterface $specObject): void
1818
{
19+
$namespaceData = $this->options['namespace'] ?? null;
1920
if (!is_array($namespaceData)) {
20-
throw new InvalidArgumentException("TestsGenerator supports only arrays as namespaceData");
21+
throw new InvalidArgumentException("TestsGenerator must be configured with array as 'namespace'");
2122
}
2223

2324
$openApiData = $specObject->getSerializableData();

0 commit comments

Comments
 (0)