|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Swaggest\JsonCli; |
|
4
|
|
|
|
|
5
|
|
|
use Swaggest\JsonCli\Json\LoadFile; |
|
6
|
|
|
use Swaggest\JsonCli\JsonSchema\ResolverMux; |
|
7
|
|
|
use Swaggest\JsonDiff\Exception; |
|
8
|
|
|
use Swaggest\JsonDiff\JsonMergePatch; |
|
9
|
|
|
use Swaggest\JsonDiff\JsonPatch; |
|
10
|
|
|
use Swaggest\JsonSchema\Context; |
|
11
|
|
|
use Swaggest\JsonSchema\RemoteRef\BasicFetcher; |
|
12
|
|
|
use Swaggest\JsonSchema\RemoteRef\Preloaded; |
|
13
|
|
|
use Swaggest\JsonSchema\Schema; |
|
14
|
|
|
use Symfony\Component\Yaml\Yaml; |
|
15
|
|
|
use Yaoi\Command; |
|
16
|
|
|
use Yaoi\Io\Response; |
|
17
|
|
|
|
|
18
|
|
|
abstract class Base extends Command |
|
19
|
1 |
|
{ |
|
20
|
|
|
use LoadFile; |
|
21
|
1 |
|
|
|
22
|
1 |
|
public $pretty; |
|
23
|
1 |
|
public $toYaml; |
|
24
|
1 |
|
public $toSerialized; |
|
25
|
1 |
|
public $output; |
|
26
|
1 |
|
|
|
27
|
1 |
|
/** |
|
28
|
|
|
* @param Command\Definition $definition |
|
29
|
|
|
* @param \stdClass|static $options |
|
30
|
|
|
*/ |
|
31
|
|
|
static function setUpDefinition(Command\Definition $definition, $options) |
|
32
|
6 |
|
{ |
|
33
|
|
|
$options->pretty = Command\Option::create() |
|
34
|
6 |
|
->setDescription('Pretty-print result JSON'); |
|
35
|
|
|
$options->output = Command\Option::create()->setType() |
|
36
|
|
|
->setDescription('Path to output result, default STDOUT'); |
|
37
|
|
|
$options->toYaml = Command\Option::create()->setDescription('Output in YAML format'); |
|
38
|
6 |
|
$options->toSerialized = Command\Option::create()->setDescription('Output in PHP serialized format'); |
|
39
|
6 |
|
} |
|
40
|
|
|
|
|
41
|
|
|
|
|
42
|
|
|
protected $out; |
|
43
|
6 |
|
|
|
44
|
|
|
/** |
|
45
|
6 |
|
* @param string $path |
|
46
|
|
|
* @return mixed |
|
47
|
|
|
* @throws ExitCode |
|
48
|
6 |
|
*/ |
|
49
|
|
|
protected function readData($path) |
|
50
|
|
|
{ |
|
51
|
6 |
|
return self::readJsonOrYaml($path, $this->response); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
8 |
|
/** |
|
55
|
|
|
* @param string $path |
|
56
|
8 |
|
* @param Response $response |
|
57
|
8 |
|
* @return mixed |
|
58
|
5 |
|
* @throws ExitCode |
|
59
|
|
|
*/ |
|
60
|
|
|
public static function readJsonOrYaml($path, $response) |
|
61
|
8 |
|
{ |
|
62
|
|
|
$fileData = file_get_contents($path); |
|
63
|
8 |
|
if (!$fileData) { |
|
64
|
|
|
$response->error('Unable to read ' . $path); |
|
65
|
|
|
throw new ExitCode('', 1); |
|
66
|
8 |
|
} |
|
67
|
|
|
if (substr($path, -5) === '.yaml' || substr($path, -4) === '.yml') { |
|
68
|
|
|
$jsonData = Yaml::parse($fileData, Yaml::PARSE_OBJECT + Yaml::PARSE_OBJECT_FOR_MAP); |
|
69
|
8 |
|
} elseif (substr($path, -11) === '.serialized') { |
|
70
|
|
|
$jsonData = unserialize($fileData); |
|
71
|
|
|
} else { |
|
72
|
8 |
|
$jsonData = json_decode($fileData); |
|
73
|
|
|
} |
|
74
|
8 |
|
|
|
75
|
|
|
return $jsonData; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
|
|
79
|
|
|
protected function postPerform() |
|
80
|
|
|
{ |
|
81
|
|
|
$options = JSON_UNESCAPED_SLASHES + JSON_UNESCAPED_UNICODE; |
|
82
|
|
|
if ($this->pretty) { |
|
83
|
|
|
$options += JSON_PRETTY_PRINT; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
if ($this->toYaml) { |
|
87
|
|
|
$result = Yaml::dump($this->out, 2, 2, Yaml::DUMP_OBJECT_AS_MAP); |
|
88
|
|
|
} elseif ($this->toSerialized) { |
|
89
|
|
|
$result = serialize($this->out); |
|
90
|
|
|
} else { |
|
91
|
|
|
$result = json_encode($this->out, $options); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
if ($this->output) { |
|
95
|
|
|
file_put_contents($this->output, $result); |
|
96
|
|
|
} else { |
|
97
|
|
|
echo $result; |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
|
|
102
|
|
|
/** @var string */ |
|
103
|
|
|
public $schema; |
|
104
|
|
|
/** @var string[] */ |
|
105
|
|
|
public $defPtr = ['#/definitions']; |
|
106
|
|
|
/** @var string[] */ |
|
107
|
|
|
public $ptrInSchema; |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* @param Command\Definition $definition |
|
111
|
|
|
* @param static|mixed $options |
|
112
|
|
|
*/ |
|
113
|
|
|
protected static function setupGenOptions(Command\Definition $definition, $options) |
|
|
|
|
|
|
114
|
|
|
{ |
|
115
|
|
|
$options->schema = Command\Option::create() |
|
|
|
|
|
|
116
|
|
|
->setDescription('Path to JSON schema file')->setIsUnnamed()->setIsRequired(); |
|
117
|
|
|
|
|
118
|
|
|
$options->ptrInSchema = Command\Option::create()->setType()->setIsVariadic() |
|
|
|
|
|
|
119
|
|
|
->setDescription('JSON pointers to structure in in root schema, default #'); |
|
120
|
|
|
|
|
121
|
|
|
$options->defPtr = Command\Option::create()->setType()->setIsVariadic() |
|
|
|
|
|
|
122
|
|
|
->setDescription('Definitions pointers to strip from symbol names, default #/definitions'); |
|
123
|
|
|
|
|
124
|
|
|
static::setupLoadFileOptions($options); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* @param bool $skipRoot |
|
129
|
|
|
* @param string $baseName |
|
130
|
|
|
* @return \Swaggest\JsonSchema\SchemaContract |
|
131
|
|
|
* @throws Exception |
|
132
|
|
|
* @throws ExitCode |
|
133
|
|
|
* @throws \Swaggest\JsonSchema\Exception |
|
134
|
|
|
* @throws \Swaggest\JsonSchema\InvalidValue |
|
135
|
|
|
*/ |
|
136
|
|
|
protected function loadSchema(&$skipRoot, &$baseName) |
|
137
|
|
|
{ |
|
138
|
|
|
$resolver = new ResolverMux(); |
|
139
|
|
|
|
|
140
|
|
|
$dataValue = $this->loadFile(); |
|
141
|
|
|
$data = $dataValue; |
|
142
|
|
|
|
|
143
|
|
|
if (!empty($this->ptrInSchema)) { |
|
144
|
|
|
$baseName = basename($this->schema); |
|
145
|
|
|
$skipRoot = true; |
|
146
|
|
|
$preloaded = new Preloaded(); |
|
147
|
|
|
$preloaded->setSchemaData($baseName, $dataValue); |
|
148
|
|
|
$resolver->resolvers[] = $preloaded; |
|
149
|
|
|
$data = new \stdClass(); |
|
150
|
|
|
foreach ($this->ptrInSchema as $i => $ptr) { |
|
151
|
|
|
$data->oneOf[$i] = (object)[Schema::PROP_REF => $baseName . $ptr]; |
|
152
|
|
|
} |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
$resolver->resolvers[] = new BasicFetcher(); |
|
156
|
|
|
return Schema::import($data, new Context($resolver)); |
|
157
|
|
|
} |
|
158
|
|
|
} |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.