From 32d5b78be50ba2ef6fb89334cea8646cce895c12 Mon Sep 17 00:00:00 2001 From: Nick Date: Sat, 18 Dec 2021 05:19:35 +0100 Subject: [PATCH] fix examples (#36) * fix examples * update readme * update doc --- README.md | 34 +++++++++++++++++++++++++--------- example/generate.php | 16 ++++++++++++++-- example/merge.php | 3 ++- 3 files changed, 41 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index ce4f1be..26be7fb 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ ## Installation ``` -composer require php-kafka/php-avro-schema-generator "^2.0" +composer require php-kafka/php-avro-schema-generator "^3.0" ``` ## Description @@ -36,7 +36,8 @@ $registry = (new SchemaRegistry()) ->addSchemaTemplateDirectory('./schemaTemplates') ->load(); -$merger = new SchemaMerger($registry, './schema'); +$merger = new SchemaMerger('./schema'); +$merger->setSchemaRegistry($registry); $merger->merge(); @@ -68,7 +69,8 @@ $registry = (new SchemaRegistry()) ->addSchemaTemplateDirectory('./schemaTemplates') ->load(); -$merger = new SchemaMerger($registry, './schema'); +$merger = new SchemaMerger('./schema'); +$merger->setSchemaRegistry($registry); $merger->addOptimizer(new FieldOrderOptimizer()); $merger->addOptimizer(new FullNameOptimizer()); $merger->addOptimizer(new PrimitiveSchemaOptimizer()); @@ -91,20 +93,34 @@ Output directory: output directory for your generated schema templates ```php addClassDirectory('./example/classes') - ->load(); +$parser = (new ParserFactory())->create(ParserFactory::PREFER_PHP7); +$classPropertyParser = new ClassPropertyParser(new DocCommentParser()); +$classParser = new ClassParser($parser, $classPropertyParser); -$generator = new SchemaGenerator($registry, './example/schemaTemplates'); +$converter = new PhpClassConverter($classParser); +$registry = (new ClassRegistry($converter))->addClassDirectory('./classes')->load(); +$generator = new SchemaGenerator('./schema'); +$generator->setClassRegistry($registry); $schemas = $generator->generate(); - $generator->exportSchemas($schemas); - ``` +The generator is able to detect types from: +- doc comments +- property types +- doc annotations + - @avro-type to set a fixed type instead of calculating one + - @avro-default set a default for this property in your schema + - @avro-doc to set schema doc comment + - @avro-logical-type set logical type for your property (decimal is not yet supported, since it has additional parameters) ## Disclaimer In `v1.3.0` the option `--optimizeSubSchemaNamespaces` was added. It was not working fully diff --git a/example/generate.php b/example/generate.php index 4c59175..8caf056 100644 --- a/example/generate.php +++ b/example/generate.php @@ -4,10 +4,22 @@ require_once '../vendor/autoload.php'; +use PhpKafka\PhpAvroSchemaGenerator\Converter\PhpClassConverter; +use PhpKafka\PhpAvroSchemaGenerator\Parser\ClassParser; +use PhpKafka\PhpAvroSchemaGenerator\Parser\DocCommentParser; use PhpKafka\PhpAvroSchemaGenerator\Registry\ClassRegistry; +use PhpKafka\PhpAvroSchemaGenerator\Parser\ClassPropertyParser; use PhpKafka\PhpAvroSchemaGenerator\Generator\SchemaGenerator; +use PhpParser\ParserFactory; -$registry = (new ClassRegistry())->addClassDirectory('./classes')->load(); -$generator = new SchemaGenerator($registry); +$parser = (new ParserFactory())->create(ParserFactory::PREFER_PHP7); +$classPropertyParser = new ClassPropertyParser(new DocCommentParser()); +$classParser = new ClassParser($parser, $classPropertyParser); + +$converter = new PhpClassConverter($classParser); +$registry = (new ClassRegistry($converter))->addClassDirectory('./classes')->load(); + +$generator = new SchemaGenerator('./schema'); +$generator->setClassRegistry($registry); $schemas = $generator->generate(); $generator->exportSchemas($schemas); diff --git a/example/merge.php b/example/merge.php index 3026fe1..78b0b8e 100644 --- a/example/merge.php +++ b/example/merge.php @@ -11,6 +11,7 @@ ->addSchemaTemplateDirectory('./schemaTemplates') ->load(); -$merger = new SchemaMerger($registry, './schema'); +$merger = new SchemaMerger('./schema'); +$merger->setSchemaRegistry($registry); $merger->merge();