diff --git a/composer.json b/composer.json index 37f279a..84e1541 100644 --- a/composer.json +++ b/composer.json @@ -16,9 +16,10 @@ "symfony/yaml": "^3", "salsify/json-streaming-parser": "^7.0", "swaggest/json-schema": "^0.12.21", - "swaggest/go-code-builder": "^0.4.6", + "swaggest/go-code-builder": "dev-master", "swaggest/php-code-builder": "^0.2.8", - "swaggest/code-builder": "^0.3.2" + "swaggest/code-builder": "^0.3.2", + "swaggest/json-schema-maker": "dev-master" }, "require-dev": { "phpunit/phpunit": "^5" diff --git a/src/App.php b/src/App.php index 528d83c..45d58c4 100644 --- a/src/App.php +++ b/src/App.php @@ -17,6 +17,7 @@ class App extends Command\Application public $resolve; public $resolvePos; public $validateSchema; + public $makeSchema; public $genGo; public $genPhp; @@ -40,6 +41,7 @@ static function setUpCommands(Definition $definition, $commandDefinitions) $commandDefinitions->resolve = Resolve::definition(); $commandDefinitions->resolvePos = ResolvePos::definition(); $commandDefinitions->validateSchema = ValidateSchema::definition(); + $commandDefinitions->makeSchema = MakeSchema::definition(); $commandDefinitions->genGo = GenGo::definition(); $commandDefinitions->genPhp = GenPhp::definition(); } diff --git a/src/MakeSchema.php b/src/MakeSchema.php new file mode 100644 index 0000000..41a7b02 --- /dev/null +++ b/src/MakeSchema.php @@ -0,0 +1,60 @@ +data = Command\Option::create()->setIsUnnamed()->setIsRequired() + ->setDescription('Path to data (JSON/YAML)'); + $options->schema = Command\Option::create()->setIsUnnamed() + ->setDescription('Path to schema, default JSON Schema'); + } + + + /** + * @throws ExitCode + * @throws \Swaggest\JsonSchema\Exception + */ + public function performAction() + { + if ($this->schema) { + $schemaData = $this->readData($this->schema); + try { + $schema = Schema::import($schemaData); + } catch (InvalidValue $e) { + $this->response->error('Invalid schema'); + $this->response->addContent($e->getMessage()); + throw new ExitCode('', 1); + } catch (\Exception $e) { + $this->response->error('Failed to import schema:' . $e->getMessage()); + throw new ExitCode('', 1); + } + } else { + $schema = new Schema(); + } + + $data = $this->readData($this->data); + + + $maker = new JsonSchemaFromInstance($schema); + $maker->addInstanceValue($data); + + print_r(json_encode($schema, JSON_PRETTY_PRINT)); + + $this->out = Schema::export($schema); + } +} \ No newline at end of file