Skip to content

Commit 6846dce

Browse files
authored
Merge pull request #9 from ensi-platform/ef-382
EF-382 экранирование description в enum
2 parents 966328e + 8f6fb16 commit 6846dce

File tree

5 files changed

+87
-63
lines changed

5 files changed

+87
-63
lines changed

config/openapi-client-generator.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,5 +91,10 @@
9191
* Options for disable patch section "require" composer.json
9292
*/
9393
'composer_disable_patch_require' => false,
94+
95+
/**
96+
* Options for disable patch enum
97+
*/
98+
'enum_disable_patch' => false,
9499
],
95100
];

src/Commands/GeneratePhpClient.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ class GeneratePhpClient extends GenerateClient
2929
protected string $laravelPackageConfigKey;
3030

3131
private bool $disableComposerPatchRequire;
32+
private bool $disableEnumPath;
3233

3334
public function __construct()
3435
{
@@ -44,11 +45,19 @@ public function __construct()
4445
"openapi-client-generator.{$this->client}_args.composer_disable_patch_require",
4546
false
4647
);
48+
49+
$this->disableEnumPath = (bool)config(
50+
"openapi-client-generator.{$this->client}_args.enum_disable_patch",
51+
false
52+
);
4753
}
4854

4955
protected function patchClientPackage(): void
5056
{
51-
// $this->patchEnums();
57+
if (!$this->disableEnumPath) {
58+
$this->patchEnums();
59+
}
60+
5261
$this->patchComposerPackage();
5362
$this->patchReadme();
5463
$this->generateProvider();

src/Core/Patchers/EnumPatcher.php

Lines changed: 0 additions & 41 deletions
This file was deleted.
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
namespace Ensi\LaravelOpenapiClientGenerator\Core\Patchers;
4+
5+
use Exception;
6+
7+
abstract class PhpClassPatcher
8+
{
9+
public function getMethod(string $name, string $returnType, string $content): string
10+
{
11+
$pattern = "/([public|protected|private]( static)? function {$name}\(\): {$returnType}.*})/sU";
12+
if (!preg_match($pattern, $content, $matches)) {
13+
throw new Exception("Не удалось найти метод $name");
14+
}
15+
16+
return $matches[1];
17+
}
18+
19+
public function escapingString(string $string): string
20+
{
21+
return match (true) {
22+
str_starts_with($string, "'") => $this->replacePatternWithinString($string, "/[^\\\\]\K'/", "\\'"),
23+
str_starts_with($string, '"') => $this->replacePatternWithinString($string, "/[^\\\\]\K\"/", '\\"'),
24+
default => $string
25+
};
26+
}
27+
28+
public function replaceValue(string $context, string $oldValue, string $newValue): string
29+
{
30+
return str_replace($oldValue, $newValue, $context);
31+
}
32+
33+
public function replacePattern(string $context, string $pattern, string $newValue): string
34+
{
35+
return preg_replace($pattern, $newValue, $context);
36+
}
37+
38+
public function replacePatternWithinString(string $context, string $pattern, string $newValue): string
39+
{
40+
$subString = substr($context, 1, -1);
41+
$replacedSubString = $this->replacePattern($subString, $pattern, $newValue);
42+
43+
return $this->replaceValue($context, $subString, $replacedSubString);
44+
}
45+
}

src/Core/Patchers/PhpEnumPatcher.php

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,44 +2,50 @@
22

33
namespace Ensi\LaravelOpenapiClientGenerator\Core\Patchers;
44

5-
use Illuminate\Support\Str;
5+
use Exception;
66

7-
class PhpEnumPatcher extends EnumPatcher
7+
class PhpEnumPatcher extends PhpClassPatcher
88
{
9-
protected function getSpecificationName(): string
9+
public function __construct(protected string $enumFile)
1010
{
11-
return $this->toSnakeCase(basename($this->enumFile, '.php'));
1211
}
1312

14-
protected function patchEnumFile(array $constants): void
13+
/** @throws Exception */
14+
public function patch(): void
1515
{
1616
if (!file_exists($this->enumFile)) {
17-
return;
17+
throw new Exception("$this->enumFile not exists");
1818
}
1919

20-
$enum = file_get_contents($this->enumFile);
20+
$content = file_get_contents($this->enumFile);
2121

22-
foreach ($constants as $constant) {
23-
$enum = $this->patchConstantProperties(
24-
$enum,
25-
$constant['value'],
26-
Str::upper($constant['name']),
27-
$constant['title']
28-
);
29-
}
22+
$content = $this->escapingResponseDescription($content);
3023

31-
file_put_contents($this->enumFile, $enum);
24+
file_put_contents($this->enumFile, $content);
3225
}
3326

34-
private function patchConstantProperties(string $enum, string $value, string $name, string $title): string
27+
/** @throws Exception */
28+
protected function escapingResponseDescription(string $content): string
3529
{
36-
// Do some preg replace here to change something
30+
$method = $this->getMethod('getDescriptions', 'array', $content);
31+
32+
$descriptions = $this->parsingResponseDescription($method);
33+
34+
$escapingMethod = $method;
35+
foreach ($descriptions as $description) {
36+
$escapedDescription = $this->escapingString($description);
37+
$escapingMethod = $this->replaceValue($escapingMethod, $description, $escapedDescription);
38+
}
3739

38-
return $enum;
40+
return $this->replaceValue($content, $method, $escapingMethod);
3941
}
4042

41-
private function toSnakeCase(string $str): string
43+
protected function parsingResponseDescription(string $context): array
4244
{
43-
return strtolower(preg_replace('/(?<!^)[A-Z]/', '_$0', $str));
45+
return preg_match_all(
46+
'/self::.*=> ([\'|\"].*[\'|\"]),\\n/smU',
47+
$context,
48+
$constants
49+
) ? $constants[1] : [];
4450
}
4551
}

0 commit comments

Comments
 (0)