11# Avro schema generator for PHP
22[ ![ Actions Status] ( https://github.com/php-kafka/php-avro-schema-generator/workflows/CI/badge.svg )] ( https://github.com/php-kafka/php-avro-schema-generator/workflows/CI/badge.svg )
3- [ ![ Maintainability] ( https://api.codeclimate.com/v1/badges/937e14c63beb08885c70 /maintainability )] ( https://codeclimate.com/github/php-kafka/php-avro-schema-generator/maintainability )
4- [ ![ Test Coverage] ( https://api.codeclimate.com/v1/badges/937e14c63beb08885c70 /test_coverage )] ( https://codeclimate.com/github/php-kafka/php-avro-schema-generator/test_coverage )
3+ [ ![ Maintainability] ( https://api.codeclimate.com/v1/badges/41aecf21566d7e9bfb69 /maintainability )] ( https://codeclimate.com/github/php-kafka/php-avro-schema-generator/maintainability )
4+ [ ![ Test Coverage] ( https://api.codeclimate.com/v1/badges/41aecf21566d7e9bfb69 /test_coverage )] ( https://codeclimate.com/github/php-kafka/php-avro-schema-generator/test_coverage )
55[ ![ Latest Stable Version] ( https://poser.pugx.org/php-kafka/php-avro-schema-generator/v/stable )] ( https://packagist.org/packages/php-kafka/php-avro-schema-generator )
6- [ ![ Latest Unstable Version] ( https://poser.pugx.org/php-kafka/php-avro-schema-generator/v/unstable )] ( https://packagist.org/packages/php-kafka/php-avro-schema-generator )
76
87## Installation
98```
10- composer require php-kafka/php-avro-schema-generator "^1 .0"
9+ composer require php-kafka/php-avro-schema-generator "^2 .0"
1110```
1211
1312## Description
14- Since avro does not support external subschemas, this is just a small
15- helper to unify your schemas and to create basic schemas from php classes (experimental!).
13+ This library enables you to:
14+ - Manage your embedded schema as separate files
15+ - The library is able to merge those files
16+ - The library is able to generate avsc schema from PHP classes
1617
1718### Merging subschemas / schemas
18- Schema template directories: directories containing avsc template files (with subschema)
19- Output directory: output directory for the unified schema files
19+ Schema template directories: directories containing avsc template files (with subschema)
20+ Output directory: output directory for the merged schema files
2021
21- #### Merge subschemas (code)
22+ ** Console example**
23+ ``` bash
24+ ./vendor/bin/avro-cli avro:subschema:merge ./example/schemaTemplates ./example/schema
25+ ```
26+
27+ ** PHP example**
2228``` php
2329<?php
2430
@@ -35,18 +41,49 @@ $merger->merge();
3541
3642```
3743
38- #### Merge subschemas (command)
44+ ### Merge optimizers
45+ There are optimizers that you can enable for merging schema:
46+ - FullNameOptimizer: removes unneeded namespaces
47+ - FieldOrderOptimizer: the first fields of a record schema will be: type, name, namespace (if present)
48+
49+ How to enable optimizer:
50+
51+ ** Console example**
3952``` bash
40- ./vendor/bin/avro-cli avro:subschema:merge ./example/schemaTemplates ./example/schema
53+ ./vendor/bin/avro-cli --optimizeFullNames --optimizeFieldOrder avro:subschema:merge ./example/schemaTemplates ./example/schema
54+ ```
55+ ** PHP Example**
56+ ``` php
57+ <?php
58+
59+ use PhpKafka\PhpAvroSchemaGenerator\Registry\SchemaRegistry;
60+ use PhpKafka\PhpAvroSchemaGenerator\Merger\SchemaMerger;
61+ use PhpKafka\PhpAvroSchemaGenerator\Optimizer\FieldOrderOptimizer;
62+ use PhpKafka\PhpAvroSchemaGenerator\Optimizer\FullNameOptimizer;
63+
64+ $registry = (new SchemaRegistry())
65+ ->addSchemaTemplateDirectory('./schemaTemplates')
66+ ->load();
67+
68+ $merger = new SchemaMerger($registry, './schema');
69+ $merger->addOptimizer(new FieldOrderOptimizer());
70+ $merger->addOptimizer(new FullNameOptimizer());
71+
72+ $merger->merge();
73+
4174```
4275
4376### Generating schemas from classes
44- Please note, that this feature is highly experimental.
45- You probably still need to adjust the generated templates, but it gives you a basic template to work with.
46- Class directories: Directories containing the classes you want to generate schemas from
47- Output directory: output directory for your generated schema templates
77+ You will need to adjust the generated templates, but it gives you a good starting point to work with.
78+ Class directories: Directories containing the classes you want to generate schemas from
79+ Output directory: output directory for your generated schema templates
4880
49- #### Generate schemas (code)
81+ ** Console example**
82+ ``` bash
83+ ./vendor/bin/avro-cli avro:schema:generate ./example/classes ./example/schemaTemplates
84+ ```
85+
86+ ** PHP Example**
5087``` php
5188<?php
5289
@@ -65,7 +102,8 @@ $generator->exportSchemas($schemas);
65102
66103```
67104
68- #### Merge subschemas (command)
69- ``` bash
70- ./vendor/bin/avro-cli avro:schema:generate ./example/classes ./example/schemaTemplates
71- ```
105+ ## Disclaimer
106+ In ` v1.3.0 ` the option ` --optimizeSubSchemaNamespaces ` was added. It was not working fully
107+ in the ` 1.x ` version and we had some discussions (#13 ) about it.
108+ Ultimately the decision was to adapt this behaviour fully in ` v2.0.0 ` so you might want to
109+ upgrade if you rely on that behaviour.
0 commit comments