9
9
use Swaggest \JsonSchema \RemoteRef \BasicFetcher ;
10
10
use Swaggest \JsonSchema \RemoteRef \Preloaded ;
11
11
use Swaggest \JsonSchema \Schema ;
12
+ use Swaggest \PhpCodeBuilder \Markdown \TypeBuilder ;
12
13
use Symfony \Component \Yaml \Yaml ;
13
14
use Yaoi \Command ;
14
15
use Yaoi \Io \Response ;
@@ -21,6 +22,7 @@ abstract class Base extends Command
21
22
public $ toYaml ;
22
23
public $ toSerialized ;
23
24
public $ output ;
25
+ public $ schemaResolver ;
24
26
25
27
/**
26
28
* @param Command\Definition $definition
@@ -32,6 +34,13 @@ static function setUpDefinition(Command\Definition $definition, $options)
32
34
->setDescription ('Pretty-print result JSON ' );
33
35
$ options ->output = Command \Option::create ()->setType ()
34
36
->setDescription ('Path to output result, default STDOUT ' );
37
+ $ options ->schemaResolver = Command \Option::create ()->setType ()
38
+ ->setDescription ('Path to schema resolver JSON file. Schema: ' );
39
+ $ tb = new TypeBuilder ();
40
+ $ tb ->getTypeString (SchemaResolver::schema ()->exportSchema ());
41
+ $ options ->schemaResolver ->description .= "\n" . trim (substr ($ tb ->file , 77 )); // Stripping header.
42
+
43
+
35
44
$ options ->toYaml = Command \Option::create ()->setDescription ('Output in YAML format ' );
36
45
$ options ->toSerialized = Command \Option::create ()->setDescription ('Output in PHP serialized format ' );
37
46
}
@@ -119,6 +128,12 @@ protected static function setupGenOptions(Command\Definition $definition, $optio
119
128
$ options ->defPtr = Command \Option::create ()->setType ()->setIsVariadic ()
120
129
->setDescription ('Definitions pointers to strip from symbol names, default #/definitions ' );
121
130
131
+ $ options ->schemaResolver = Command \Option::create ()->setType ()
132
+ ->setDescription ('Path to schema resolver JSON file. Schema: ' );
133
+ $ tb = new TypeBuilder ();
134
+ $ tb ->getTypeString (SchemaResolver::schema ()->exportSchema ());
135
+ $ options ->schemaResolver ->description .= "\n" . trim (substr ($ tb ->file , 77 )); // Stripping header.
136
+
122
137
static ::setupLoadFileOptions ($ options );
123
138
}
124
139
@@ -139,6 +154,28 @@ protected function loadSchema(&$skipRoot, &$baseName)
139
154
140
155
$ resolver = new ResolverMux ();
141
156
$ preloaded = new Preloaded ();
157
+
158
+ if ($ this ->schemaResolver !== null ) {
159
+ $ data = file_get_contents ($ this ->schemaResolver );
160
+ if (empty ($ data )) {
161
+ throw new ExitCode ("empty or missing schema resolver file " , 1 );
162
+ }
163
+ $ json = json_decode ($ data );
164
+ if (empty ($ json )) {
165
+ throw new ExitCode ("invalid json in schema resolver file " , 1 );
166
+ }
167
+
168
+ $ schemaResolver = SchemaResolver::import ($ json );
169
+
170
+ foreach ($ schemaResolver ->schemaData as $ url => $ data ) {
171
+ $ preloaded ->setSchemaData ($ url , $ data );
172
+ }
173
+
174
+ foreach ($ schemaResolver ->schemaFiles as $ url => $ file ) {
175
+ $ preloaded ->setSchemaFile ($ url , $ file );
176
+ }
177
+ }
178
+
142
179
$ resolver ->resolvers [] = $ preloaded ;
143
180
144
181
$ dataValue = $ this ->loadFile ();
0 commit comments