Skip to content

Commit 04a3cf8

Browse files
committed
only enums mode and support templates
1 parent b24802e commit 04a3cf8

File tree

2 files changed

+43
-7
lines changed

2 files changed

+43
-7
lines changed

config/openapi-server-generator.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
return [
44

55
/**
6-
* Path to the directory where index.yaml openapi file located
6+
* Path to the directory where v{N}/index.yaml openapi files are located.
77
*/
88
'apidoc_dir' => public_path('api-docs'),
99

@@ -18,5 +18,15 @@
1818
* Path relative to the app/ directory where DTO files will be located.
1919
* Old name: `app_dir`
2020
*/
21-
'destination_dir' => 'Http/Api{version}/OpenApiGenerated'
21+
'destination_dir' => 'Http/Api{version}/OpenApiGenerated',
22+
23+
/**
24+
* Directory where you can place templates to override default ones. . Used in -t
25+
*/
26+
'template_dir' => '',
27+
28+
/*
29+
* Preserve only enums - *Enum.php
30+
*/
31+
'only_enums_mode' => true,
2232
];

src/Commands/GenerateServerVersion.php

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,29 @@ class GenerateServerVersion extends Command {
3434
*/
3535
private $tempDir;
3636

37+
/**
38+
* @var boolean
39+
*/
40+
private $onlyEnumsMode;
41+
3742
/**
3843
* @var string
3944
*/
4045
private $destinationDir;
4146

47+
/**
48+
* @var string
49+
*/
50+
private $templateDir;
51+
4252
public function __construct()
4353
{
4454
parent::__construct();
4555

4656
$this->tempDir = config('openapi-server-generator.temp_dir', config('openapi-server-generator.output_dir'));
4757
$this->destinationDir = config('openapi-server-generator.destination_dir', config('openapi-server-generator.app_dir'));
58+
$this->templateDir = config("openapi-server-generator.template_dir", '');
59+
$this->onlyEnumsMode = config('openapi-server-generator.only_enums_mode', false);
4860
}
4961

5062
/**
@@ -75,7 +87,11 @@ private function generateDto(): void
7587

7688
$command = "$bin generate -i $inputFile -g php -p 'invokerPackage=$invokerPackage,modelPackage=$modelPackage' -o $this->tempDir";
7789

78-
$this->info("Execute command: $command");
90+
if ($this->templateDir) {
91+
$command .= " -t " . escapeshellarg($this->templateDir);
92+
}
93+
94+
$this->info("Executing command: $command");
7995

8096
shell_exec($command);
8197
}
@@ -102,9 +118,14 @@ private function clearAppDir(): void {
102118

103119
private function copyDto(): void
104120
{
105-
shell_exec("cp -rf $this->tempDir/lib/Dto " . $this->getAppPathToDto());
106-
shell_exec("cp -f $this->tempDir/lib/Configuration.php " . $this->getAppPathToDto());
107-
shell_exec("cp -n $this->tempDir/lib/ObjectSerializer.php " . $this->getAppPathToDto());
121+
if ($this->onlyEnumsMode) {
122+
$modelsPath = $this->getAppPathToModels();
123+
shell_exec("find $this->tempDir/lib/Dto -name \*Enum.php -exec cp {} $modelsPath \;");
124+
} else {
125+
shell_exec("cp -rf $this->tempDir/lib/Dto " . $this->getAppPathToDto());
126+
shell_exec("cp -f $this->tempDir/lib/Configuration.php " . $this->getAppPathToDto());
127+
shell_exec("cp -n $this->tempDir/lib/ObjectSerializer.php " . $this->getAppPathToDto());
128+
}
108129
}
109130

110131
private function removeGeneratedDto(): void
@@ -138,7 +159,12 @@ private function patchSerializer(): void
138159
{
139160
$file = $this->getAppPathToDto() . DIRECTORY_SEPARATOR . 'ObjectSerializer.php';
140161

141-
$this->info("Patch serializer: $file");
162+
if (!file_exists($file)) {
163+
$this->info("Skipping serializer patching, no such file: $file");
164+
return;
165+
}
166+
167+
$this->info("Patching serializer: $file");
142168

143169
$patcher = new SerializerPatcher($file);
144170

0 commit comments

Comments
 (0)