Skip to content

Commit

Permalink
fix examples (php-kafka#36)
Browse files Browse the repository at this point in the history
* fix examples

* update readme

* update doc
  • Loading branch information
nick-zh authored Dec 18, 2021
1 parent 8b3ed64 commit 32d5b78
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 12 deletions.
34 changes: 25 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -36,7 +36,8 @@ $registry = (new SchemaRegistry())
->addSchemaTemplateDirectory('./schemaTemplates')
->load();

$merger = new SchemaMerger($registry, './schema');
$merger = new SchemaMerger('./schema');
$merger->setSchemaRegistry($registry);

$merger->merge();

Expand Down Expand Up @@ -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());
Expand All @@ -91,20 +93,34 @@ Output directory: output directory for your generated schema templates
```php
<?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('./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
Expand Down
16 changes: 14 additions & 2 deletions example/generate.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
3 changes: 2 additions & 1 deletion example/merge.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
->addSchemaTemplateDirectory('./schemaTemplates')
->load();

$merger = new SchemaMerger($registry, './schema');
$merger = new SchemaMerger('./schema');
$merger->setSchemaRegistry($registry);

$merger->merge();

0 comments on commit 32d5b78

Please sign in to comment.