1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Swaggest\JsonCli; |
4
|
|
|
|
5
|
|
|
use Swaggest\JsonCli\GenPhp\BuilderOptions; |
6
|
|
|
use Swaggest\JsonSchema\Schema; |
7
|
|
|
use Swaggest\PhpCodeBuilder\App\PhpApp; |
8
|
|
|
use Swaggest\PhpCodeBuilder\JsonSchema\ClassHookCallback; |
9
|
|
|
use Swaggest\PhpCodeBuilder\JsonSchema\PhpBuilder; |
10
|
|
|
use Swaggest\PhpCodeBuilder\PhpClass; |
11
|
|
|
use Swaggest\PhpCodeBuilder\PhpCode; |
12
|
|
|
use Yaoi\Command; |
13
|
|
|
|
14
|
|
|
class GenPhp extends Base |
15
|
|
|
{ |
16
|
|
|
use BuilderOptions; |
17
|
|
|
|
18
|
|
|
/** @var string */ |
19
|
|
|
public $ns; |
20
|
|
|
/** @var string */ |
21
|
|
|
public $nsPath; |
22
|
|
|
/** @var string */ |
23
|
|
|
public $rootName = 'Structure'; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param Command\Definition $definition |
27
|
|
|
* @param \stdClass|static $options |
28
|
|
|
*/ |
29
|
|
|
public static function setUpDefinition(Command\Definition $definition, $options) |
30
|
|
|
{ |
31
|
|
|
$definition->description = 'Generate PHP code from JSON schema'; |
32
|
|
|
Base::setupGenOptions($definition, $options); |
33
|
|
|
|
34
|
|
|
$options->ns = Command\Option::create() |
|
|
|
|
35
|
|
|
->setDescription('Namespace to use for generated classes, example \MyClasses')->setType()->setIsRequired(); |
36
|
|
|
|
37
|
|
|
$options->nsPath = Command\Option::create() |
|
|
|
|
38
|
|
|
->setDescription('Path to store generated classes, example ./src/MyClasses') |
39
|
|
|
->setType() |
40
|
|
|
->setIsRequired(); |
41
|
|
|
|
42
|
|
|
$options->rootName = Command\Option::create()->setType() |
|
|
|
|
43
|
|
|
->setDescription('Root class name, default "Structure", only used for # pointer'); |
44
|
|
|
|
45
|
|
|
static::setupBuilderOptions($options); |
46
|
|
|
Base::setupGenOptions($definition, $options); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
|
50
|
|
|
public function performAction() |
51
|
|
|
{ |
52
|
|
|
try { |
53
|
|
|
$skipRoot = false; |
54
|
|
|
$baseName = null; |
55
|
|
|
$schema = $this->loadSchema($skipRoot, $baseName); |
56
|
|
|
|
57
|
|
|
$appPath = realpath($this->nsPath); |
58
|
|
|
if (!$appPath) { |
59
|
|
|
$this->response->error('Could not find directory ' . $this->nsPath); |
60
|
|
|
throw new ExitCode('', 1); |
61
|
|
|
} |
62
|
|
|
$appNs = $this->ns; |
63
|
|
|
|
64
|
|
|
$app = new PhpApp(); |
65
|
|
|
$app->setNamespaceRoot($appNs, '.'); |
66
|
|
|
|
67
|
|
|
$builder = new PhpBuilder(); |
68
|
|
|
$this->setupBuilder($builder); |
69
|
|
|
|
70
|
|
|
$builder->classCreatedHook = new ClassHookCallback(function (PhpClass $class, $path, $schema) |
71
|
|
|
use ($app, $appNs, $skipRoot, $baseName) { |
72
|
|
|
if ($skipRoot && '#' === $path) { |
|
|
|
|
73
|
|
|
return; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
$desc = ''; |
77
|
|
|
if ($schema->title) { |
78
|
|
|
$desc = $schema->title; |
79
|
|
|
} |
80
|
|
|
if ($schema->description) { |
81
|
|
|
$desc .= "\n" . $schema->description; |
82
|
|
|
} |
83
|
|
|
if ($fromRefs = $schema->getFromRefs()) { |
84
|
|
|
$desc .= "\nBuilt from " . implode("\n" . ' <- ', $fromRefs); |
85
|
|
|
} |
86
|
|
|
$class->setDescription(trim($desc)); |
87
|
|
|
|
88
|
|
|
$class->setNamespace($appNs); |
89
|
|
|
if ('#' === $path) { |
90
|
|
|
$class->setName($this->rootName); |
91
|
|
|
} else { |
92
|
|
|
if (!empty($fromRefs)) { |
93
|
|
|
$path = $fromRefs[0]; |
94
|
|
|
} |
95
|
|
|
foreach ($this->defPtr as $defPtr) { |
96
|
|
|
if (isset($baseName)) { |
97
|
|
|
$baseNameDefPtr = $baseName . $defPtr; |
98
|
|
|
if ($baseNameDefPtr === substr($path, 0, strlen($baseNameDefPtr))) { |
99
|
|
|
$path = substr($path, strlen($baseNameDefPtr)); |
100
|
|
|
$className = PhpCode::makePhpClassName($path); |
101
|
|
|
$class->setName($className); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
if ($defPtr === substr($path, 0, strlen($defPtr))) { |
106
|
|
|
$className = PhpCode::makePhpClassName(substr($path, strlen($defPtr))); |
107
|
|
|
$class->setName($className); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
$app->addClass($class); |
112
|
|
|
}); |
113
|
|
|
|
114
|
|
|
if (!$schema instanceof Schema) { |
115
|
|
|
$this->response->error('failed to assert Schema type, ' . get_class($schema) . ' received'); |
116
|
|
|
throw new ExitCode('', 1); |
117
|
|
|
} |
118
|
|
|
$builder->getType($schema); |
119
|
|
|
$app->store($appPath); |
120
|
|
|
$this->response->success("Classes are generated in " . $appPath); |
121
|
|
|
} catch (\Exception $e) { |
122
|
|
|
$this->response->error($e->getMessage()); |
123
|
|
|
throw new ExitCode('', 1); |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
} |
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..