forked from onmoon/openapi-server-bundle
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodeGenerator.php
More file actions
100 lines (82 loc) · 3.15 KB
/
CodeGenerator.php
File metadata and controls
100 lines (82 loc) · 3.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<?php
declare(strict_types=1);
namespace OnMoon\OpenApiServerBundle\CodeGenerator\PhpParserGenerators;
use Exception;
use OnMoon\OpenApiServerBundle\CodeGenerator\Definitions\PropertyDefinition;
use OnMoon\OpenApiServerBundle\Types\ScalarTypesResolver;
use PhpParser\BuilderFactory;
use PhpParser\Node\Scalar\LNumber;
use PhpParser\Node\Stmt\Declare_;
use PhpParser\Node\Stmt\DeclareDeclare;
use PhpParser\PrettyPrinter\Standard;
use function array_map;
use function count;
use function implode;
use function sprintf;
use function trim;
use const PHP_EOL;
abstract class CodeGenerator
{
protected const AUTOGENERATED_WARNING = '/**
* This %s was automatically generated
* You should not change it manually as it will be overwritten
*/';
protected BuilderFactory $factory;
protected ScalarTypesResolver $typeResolver;
protected string $languageLevel;
protected bool $fullDocs;
public function __construct(BuilderFactory $factory, ScalarTypesResolver $typeResolver, string $languageLevel, bool $fullDocs)
{
$this->factory = $factory;
$this->typeResolver = $typeResolver;
$this->languageLevel = $languageLevel;
$this->fullDocs = $fullDocs;
}
public function getTypeDocBlock(FileBuilder $builder, PropertyDefinition $definition): string
{
return $this->getTypeName($builder, $definition) .
($definition->isArray() ? '[]' : '') .
($definition->isNullable() ? '|null' : '');
}
public function getTypePhp(FileBuilder $builder, PropertyDefinition $definition): string
{
return ($definition->isNullable() ? '?' : '') .
($definition->isArray() ? 'array' : $this->getTypeName($builder, $definition));
}
public function getTypeName(FileBuilder $builder, PropertyDefinition $definition): string
{
$objectType = $definition->getObjectTypeDefinition();
$scalarType = $definition->getScalarTypeId();
if ($objectType !== null) {
return $builder->getReference($objectType);
}
if ($definition->getSpecProperty()->getOutputType() !== null) {
return $definition->getSpecProperty()->getOutputType();
}
if ($scalarType === null) {
throw new Exception('One of ObjectTypeDefinition and ScalarTypeId should not be null');
}
return $this->typeResolver->getPhpType($scalarType);
}
/** @param string[] $lines */
public function getDocComment(array $lines): string
{
if (count($lines) === 1) {
$glued = ' ' . trim($lines[0]);
} else {
$asteriskLines = array_map(
static fn (string $line): string => ' * ' . trim($line),
$lines
);
$glued = PHP_EOL . implode(PHP_EOL, $asteriskLines) . PHP_EOL;
}
return sprintf('/**%s */', $glued);
}
public function printFile(FileBuilder $fileBuilder): string
{
return (new Standard())->prettyPrintFile([
new Declare_([new DeclareDeclare('strict_types', new LNumber(1))]),
$fileBuilder->getNamespace()->getNode(),
]);
}
}