1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Swaggest\JsonCli; |
4
|
|
|
|
5
|
|
|
use Swaggest\JsonCli\JsonSchema\ResolverMux; |
6
|
|
|
use Swaggest\JsonSchema\Context; |
7
|
|
|
use Swaggest\JsonSchema\RemoteRef\BasicFetcher; |
8
|
|
|
use Swaggest\JsonSchema\RemoteRef\Preloaded; |
9
|
|
|
use Swaggest\JsonSchema\Schema; |
10
|
|
|
use Swaggest\PhpCodeBuilder\App\PhpApp; |
11
|
|
|
use Swaggest\PhpCodeBuilder\JsonSchema\ClassHookCallback; |
12
|
|
|
use Swaggest\PhpCodeBuilder\JsonSchema\PhpBuilder; |
13
|
|
|
use Swaggest\PhpCodeBuilder\PhpClass; |
14
|
|
|
use Swaggest\PhpCodeBuilder\PhpCode; |
15
|
|
|
use Yaoi\Command; |
16
|
|
|
|
17
|
|
|
class GenPhp extends Base |
18
|
|
|
{ |
19
|
|
|
/** @var string */ |
20
|
|
|
public $ns; |
21
|
|
|
/** @var string */ |
22
|
|
|
public $nsPath; |
23
|
|
|
/** @var string */ |
24
|
|
|
public $rootName = 'Structure'; |
25
|
|
|
/** @var bool */ |
26
|
|
|
public $setters = false; |
27
|
|
|
/** @var bool */ |
28
|
|
|
public $getters = false; |
29
|
|
|
/** @var bool */ |
30
|
|
|
public $noEnumConst = false; |
31
|
|
|
|
32
|
|
|
/** @var bool */ |
33
|
|
|
public $declarePropertyDefaults = false; |
34
|
|
|
|
35
|
|
|
/** @var bool */ |
36
|
|
|
public $buildAdditionalPropertiesAccessors = false; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param Command\Definition $definition |
40
|
|
|
* @param \stdClass|static $options |
41
|
|
|
*/ |
42
|
|
|
public static function setUpDefinition(Command\Definition $definition, $options) |
43
|
|
|
{ |
44
|
|
|
$definition->description = 'Generate PHP code from JSON schema'; |
45
|
|
|
Base::setupGenOptions($definition, $options); |
46
|
|
|
|
47
|
|
|
$options->ns = Command\Option::create() |
|
|
|
|
48
|
|
|
->setDescription('Namespace to use for generated classes, example \MyClasses')->setType()->setIsRequired(); |
49
|
|
|
|
50
|
|
|
$options->nsPath = Command\Option::create() |
|
|
|
|
51
|
|
|
->setDescription('Path to store generated classes, example ./src/MyClasses') |
52
|
|
|
->setType() |
53
|
|
|
->setIsRequired(); |
54
|
|
|
|
55
|
|
|
$options->rootName = Command\Option::create()->setType() |
|
|
|
|
56
|
|
|
->setDescription('Go root struct name, default "Structure", only used for # pointer'); |
57
|
|
|
|
58
|
|
|
$options->setters = Command\Option::create()->setDescription('Build setters'); |
|
|
|
|
59
|
|
|
$options->getters = Command\Option::create()->setDescription('Build getters'); |
|
|
|
|
60
|
|
|
$options->noEnumConst = Command\Option::create() |
|
|
|
|
61
|
|
|
->setDescription('Do not create constants for enum/const values'); |
62
|
|
|
|
63
|
|
|
$options->declarePropertyDefaults = Command\Option::create() |
|
|
|
|
64
|
|
|
->setDescription('Use default values to initialize properties'); |
65
|
|
|
|
66
|
|
|
$options->buildAdditionalPropertiesAccessors = Command\Option::create() |
|
|
|
|
67
|
|
|
->setDescription('Build accessors for additionalProperties'); |
68
|
|
|
|
69
|
|
|
Base::setupGenOptions($definition, $options); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
|
73
|
|
|
public function performAction() |
74
|
|
|
{ |
75
|
|
|
try { |
76
|
|
|
$skipRoot = false; |
77
|
|
|
$baseName = null; |
78
|
|
|
$schema = $this->loadSchema($skipRoot, $baseName); |
79
|
|
|
|
80
|
|
|
$appPath = realpath($this->nsPath); |
81
|
|
|
if (!$appPath) { |
82
|
|
|
$this->response->error('Could not find directory ' . $this->nsPath); |
83
|
|
|
throw new ExitCode('', 1); |
84
|
|
|
} |
85
|
|
|
$appNs = $this->ns; |
86
|
|
|
|
87
|
|
|
$app = new PhpApp(); |
88
|
|
|
$app->setNamespaceRoot($appNs, '.'); |
89
|
|
|
|
90
|
|
|
$builder = new PhpBuilder(); |
91
|
|
|
$builder->buildSetters = $this->setters; |
92
|
|
|
$builder->buildGetters = $this->getters; |
93
|
|
|
|
94
|
|
|
$builder->makeEnumConstants = !$this->noEnumConst; |
95
|
|
|
$builder->declarePropertyDefaults = $this->declarePropertyDefaults; |
96
|
|
|
$builder->buildAdditionalPropertyMethodsOnTrue = $this->buildAdditionalPropertiesAccessors; |
97
|
|
|
|
98
|
|
|
$builder->classCreatedHook = new ClassHookCallback(function (PhpClass $class, $path, $schema) |
99
|
|
|
use ($app, $appNs, $skipRoot, $baseName) { |
100
|
|
|
if ($skipRoot && '#' === $path) { |
101
|
|
|
return; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
$desc = ''; |
105
|
|
|
if ($schema->title) { |
106
|
|
|
$desc = $schema->title; |
107
|
|
|
} |
108
|
|
|
if ($schema->description) { |
109
|
|
|
$desc .= "\n" . $schema->description; |
110
|
|
|
} |
111
|
|
|
if ($fromRefs = $schema->getFromRefs()) { |
112
|
|
|
$desc .= "\nBuilt from " . implode("\n" . ' <- ', $fromRefs); |
113
|
|
|
} |
114
|
|
|
$class->setDescription(trim($desc)); |
115
|
|
|
|
116
|
|
|
$class->setNamespace($appNs); |
117
|
|
|
if ('#' === $path) { |
118
|
|
|
$class->setName($this->rootName); |
119
|
|
|
} else { |
120
|
|
|
if (!empty($fromRefs)) { |
121
|
|
|
$path = $fromRefs[0]; |
122
|
|
|
} |
123
|
|
|
foreach ($this->defPtr as $defPtr) { |
124
|
|
|
if (isset($baseName)) { |
125
|
|
|
$baseNameDefPtr = $baseName . $defPtr; |
126
|
|
|
if ($baseNameDefPtr === substr($path, 0, strlen($baseNameDefPtr))) { |
127
|
|
|
$path = substr($path, strlen($baseNameDefPtr)); |
128
|
|
|
$className = PhpCode::makePhpClassName($path); |
129
|
|
|
$class->setName($className); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
if ($defPtr === substr($path, 0, strlen($defPtr))) { |
134
|
|
|
$className = PhpCode::makePhpClassName(substr($path, strlen($defPtr))); |
135
|
|
|
$class->setName($className); |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
$app->addClass($class); |
140
|
|
|
}); |
141
|
|
|
|
142
|
|
|
if (!$schema instanceof Schema) { |
143
|
|
|
$this->response->error('failed to assert Schema type, ' . get_class($schema) . ' received'); |
144
|
|
|
throw new ExitCode('', 1); |
145
|
|
|
} |
146
|
|
|
$builder->getType($schema); |
147
|
|
|
$app->store($appPath); |
148
|
|
|
$this->response->success("Classes are generated in " . $appPath); |
149
|
|
|
} catch (\Exception $e) { |
150
|
|
|
$this->response->error($e->getMessage()); |
151
|
|
|
throw new ExitCode('', 1); |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
} |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..