|
| 1 | +# Avro schema generator for PHP |
| 2 | +[](https://github.com/nick-zh/php-avro-schema-generator/workflows/CI/badge.svg) |
| 3 | +[](https://codeclimate.com/github/nick-zh/php-avro-schema-generator/maintainability) |
| 4 | +[](https://codeclimate.com/github/nick-zh/php-avro-schema-generator/test_coverage) |
| 5 | +[](https://packagist.org/packages/nick-zh/php-avro-schema-generator) |
| 6 | +[](https://packagist.org/packages/nick-zh/php-avro-schema-generator) |
| 7 | + |
| 8 | +## Installation |
| 9 | +``` |
| 10 | +composer require nick-zh/php-avro-schema-generator "^0.1.0" |
| 11 | +``` |
| 12 | + |
| 13 | +## 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!). |
| 16 | + |
| 17 | +### Merging subschemas / schemas |
| 18 | +Schema template directories: directories containing avsc template files (with subschema) |
| 19 | +Output directory: output directory for the unified schema files |
| 20 | + |
| 21 | +#### Merge subschemas (code) |
| 22 | +```php |
| 23 | +<?php |
| 24 | + |
| 25 | +use NickZh\PhpAvroSchemaGenerator\Registry\SchemaRegistry; |
| 26 | +use NickZh\PhpAvroSchemaGenerator\Merger\SchemaMerger; |
| 27 | + |
| 28 | +$registry = (new SchemaRegistry()) |
| 29 | + ->addSchemaTemplateDirectory('./schemaTemplates') |
| 30 | + ->load(); |
| 31 | + |
| 32 | +$merger = new SchemaMerger($registry, './schema'); |
| 33 | + |
| 34 | +$merger->merge(); |
| 35 | + |
| 36 | +``` |
| 37 | + |
| 38 | +#### Merge subschemas (command) |
| 39 | +```bash |
| 40 | +./vendor/bin/avro-cli avro:subschema:merge ./example/schemaTemplates ./example/schema |
| 41 | +``` |
| 42 | + |
| 43 | +### 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 tempalte to work with. |
| 46 | +Class direcotries: Directories containing the classes you want to generate schemas from |
| 47 | +Output directory: output directory for your generated schema templates |
| 48 | + |
| 49 | +#### Generate schemas (code) |
| 50 | +```php |
| 51 | +<?php |
| 52 | + |
| 53 | +use NickZh\PhpAvroSchemaGenerator\Registry\ClassRegistry; |
| 54 | +use NickZh\PhpAvroSchemaGenerator\Generator\SchemaGenerator; |
| 55 | + |
| 56 | +$registry = (new ClassRegistry()) |
| 57 | + ->addClassDirectory('./example/classes') |
| 58 | + ->load(); |
| 59 | + |
| 60 | +$generator = new SchemaGenerator($registry, './example/schemaTemplates'); |
| 61 | + |
| 62 | +$schemas = $generator->generate(); |
| 63 | + |
| 64 | +$generator->exportSchemas($schemas); |
| 65 | + |
| 66 | +``` |
| 67 | + |
| 68 | +#### Merge subschemas (command) |
| 69 | +```bash |
| 70 | +./vendor/bin/avro-cli avro:schema:generate ./example/classes ./example/schemaTemplates |
| 71 | +``` |
0 commit comments