https://github.com/php-kafka/php-avro-schema-generator
PHP avro subschema merger and experimental PHP Class avro schema generator
https://github.com/php-kafka/php-avro-schema-generator
avro avro-schema php schema sub subschema
Last synced: about 1 month ago
JSON representation
PHP avro subschema merger and experimental PHP Class avro schema generator
- Host: GitHub
- URL: https://github.com/php-kafka/php-avro-schema-generator
- Owner: php-kafka
- License: bsd-3-clause
- Created: 2020-09-26T16:10:21.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2025-03-07T11:06:26.000Z (10 months ago)
- Last Synced: 2025-12-03T16:30:31.045Z (about 1 month ago)
- Topics: avro, avro-schema, php, schema, sub, subschema
- Language: PHP
- Homepage:
- Size: 124 KB
- Stars: 6
- Watchers: 2
- Forks: 6
- Open Issues: 11
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Avro schema generator for PHP
[](https://github.com/php-kafka/php-avro-schema-generator/workflows/CI/badge.svg)
[](https://codeclimate.com/github/php-kafka/php-avro-schema-generator/maintainability)
[](https://codeclimate.com/github/php-kafka/php-avro-schema-generator/test_coverage)

[](https://packagist.org/packages/php-kafka/php-avro-schema-generator)
## Installation
```
composer require php-kafka/php-avro-schema-generator "^3.0"
```
Note: For PHP7.x please use the 2.x version.
## Description
This library enables you to:
- Manage your embedded schema as separate files
- The library is able to merge those files
- The library is able to generate avsc schema templates from PHP classes
### Merging subschemas / schemas
Schema template directories: directories containing avsc template files (with subschema)
Output directory: output directory for the merged schema files
**Console example**
```bash
./vendor/bin/avro-cli avro:subschema:merge ./example/schemaTemplates ./example/schema
```
**PHP example**
```php
addSchemaTemplateDirectory('./schemaTemplates')
->load();
$merger = new SchemaMerger('./schema');
$merger->setSchemaRegistry($registry);
$merger->merge();
```
### Merge optimizers
There are optimizers that you can enable for merging schema:
- FullNameOptimizer: removes unneeded namespaces
- FieldOrderOptimizer: the first fields of a record schema will be: type, name, namespace (if present)
- PrimitiveSchemaOptimizer: Optimizes primitive schema e.g. `{"type": "string"}` to `"string"`
How to enable optimizer:
**Console example**
```bash
./bin/avro-cli --optimizeFullNames --optimizeFieldOrder --optimizePrimitiveSchemas avro:subschema:merge ./example/schemaTemplates ./example/schema
```
**PHP Example**
```php
addSchemaTemplateDirectory('./schemaTemplates')
->load();
$merger = new SchemaMerger('./schema');
$merger->setSchemaRegistry($registry);
$merger->addOptimizer(new FieldOrderOptimizer());
$merger->addOptimizer(new FullNameOptimizer());
$merger->addOptimizer(new PrimitiveSchemaOptimizer());
$merger->merge();
```
### Generating schemas from classes
You will need to adjust the generated templates, but it gives you a good starting point to work with.
Class directories: Directories containing the classes you want to generate schemas from
Output directory: output directory for your generated schema templates
After you have reviewed and adjusted your templates you will need to merge them (see above)
**Console example**
```bash
./bin/avro-cli avro:schema:generate ./example/classes ./example/schemaTemplates
```
**PHP Example**
```php
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);
```
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
in the `1.x` version and we had some discussions ([#13](https://github.com/php-kafka/php-avro-schema-generator/issues/13)) about it.
Ultimately the decision was to adapt this behaviour fully in `v2.0.0` so you might want to
upgrade if you rely on that behaviour.