{"id":33960089,"url":"https://github.com/php-kafka/php-avro-schema-generator","last_synced_at":"2025-12-12T21:41:12.021Z","repository":{"id":40299038,"uuid":"298851491","full_name":"php-kafka/php-avro-schema-generator","owner":"php-kafka","description":"PHP avro subschema merger and experimental PHP Class avro schema generator","archived":false,"fork":false,"pushed_at":"2025-03-07T11:06:26.000Z","size":127,"stargazers_count":6,"open_issues_count":11,"forks_count":6,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-12-03T16:30:31.045Z","etag":null,"topics":["avro","avro-schema","php","schema","sub","subschema"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/php-kafka.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-09-26T16:10:21.000Z","updated_at":"2025-03-07T11:15:24.000Z","dependencies_parsed_at":"2025-03-07T11:36:51.412Z","dependency_job_id":null,"html_url":"https://github.com/php-kafka/php-avro-schema-generator","commit_stats":null,"previous_names":[],"tags_count":24,"template":false,"template_full_name":null,"purl":"pkg:github/php-kafka/php-avro-schema-generator","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/php-kafka%2Fphp-avro-schema-generator","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/php-kafka%2Fphp-avro-schema-generator/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/php-kafka%2Fphp-avro-schema-generator/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/php-kafka%2Fphp-avro-schema-generator/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/php-kafka","download_url":"https://codeload.github.com/php-kafka/php-avro-schema-generator/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/php-kafka%2Fphp-avro-schema-generator/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":27692466,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-12-12T02:00:06.775Z","response_time":129,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["avro","avro-schema","php","schema","sub","subschema"],"created_at":"2025-12-12T21:41:09.990Z","updated_at":"2025-12-12T21:41:12.008Z","avatar_url":"https://github.com/php-kafka.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Avro schema generator for PHP\n[![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)\n[![Maintainability](https://api.codeclimate.com/v1/badges/41aecf21566d7e9bfb69/maintainability)](https://codeclimate.com/github/php-kafka/php-avro-schema-generator/maintainability)\n[![Test Coverage](https://api.codeclimate.com/v1/badges/41aecf21566d7e9bfb69/test_coverage)](https://codeclimate.com/github/php-kafka/php-avro-schema-generator/test_coverage)\n![Supported PHP versions: ^8.2](https://img.shields.io/badge/php-%5E8.2-blue.svg)\n[![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)\n\n## Installation\n```\ncomposer require php-kafka/php-avro-schema-generator \"^3.0\"\n```\nNote: For PHP7.x please use the 2.x version.\n\n## Description\nThis library enables you to:\n- Manage your embedded schema as separate files\n- The library is able to merge those files\n- The library is able to generate avsc schema templates from PHP classes\n\n### Merging subschemas / schemas\nSchema template directories: directories containing avsc template files (with subschema)  \nOutput directory: output directory for the merged schema files  \n\n**Console example**\n```bash\n./vendor/bin/avro-cli avro:subschema:merge ./example/schemaTemplates ./example/schema\n```\n\n**PHP example**\n```php\n\u003c?php\n\nuse PhpKafka\\PhpAvroSchemaGenerator\\Registry\\SchemaRegistry;\nuse PhpKafka\\PhpAvroSchemaGenerator\\Merger\\SchemaMerger;\n\n$registry = (new SchemaRegistry())\n    -\u003eaddSchemaTemplateDirectory('./schemaTemplates')\n    -\u003eload();\n\n$merger = new SchemaMerger('./schema');\n$merger-\u003esetSchemaRegistry($registry);\n\n$merger-\u003emerge();\n\n```\n\n### Merge optimizers\nThere are optimizers that you can enable for merging schema:  \n- FullNameOptimizer: removes unneeded namespaces\n- FieldOrderOptimizer: the first fields of a record schema will be: type, name, namespace (if present)\n- PrimitiveSchemaOptimizer: Optimizes primitive schema e.g. `{\"type\": \"string\"}` to `\"string\"`\n\nHow to enable optimizer:  \n\n**Console example**\n```bash\n./bin/avro-cli  --optimizeFullNames --optimizeFieldOrder --optimizePrimitiveSchemas avro:subschema:merge ./example/schemaTemplates ./example/schema\n```\n**PHP Example**\n```php\n\u003c?php\n\nuse PhpKafka\\PhpAvroSchemaGenerator\\Registry\\SchemaRegistry;\nuse PhpKafka\\PhpAvroSchemaGenerator\\Merger\\SchemaMerger;\nuse PhpKafka\\PhpAvroSchemaGenerator\\Optimizer\\FieldOrderOptimizer;\nuse PhpKafka\\PhpAvroSchemaGenerator\\Optimizer\\FullNameOptimizer;\nuse PhpKafka\\PhpAvroSchemaGenerator\\Optimizer\\PrimitiveSchemaOptimizer;\n\n$registry = (new SchemaRegistry())\n    -\u003eaddSchemaTemplateDirectory('./schemaTemplates')\n    -\u003eload();\n\n$merger = new SchemaMerger('./schema');\n$merger-\u003esetSchemaRegistry($registry);\n$merger-\u003eaddOptimizer(new FieldOrderOptimizer());\n$merger-\u003eaddOptimizer(new FullNameOptimizer());\n$merger-\u003eaddOptimizer(new PrimitiveSchemaOptimizer());\n\n$merger-\u003emerge();\n\n```\n\n### Generating schemas from classes\nYou will need to adjust the generated templates, but it gives you a good starting point to work with.  \nClass directories: Directories containing the classes you want to generate schemas from  \nOutput directory: output directory for your generated schema templates  \nAfter you have reviewed and adjusted your templates you will need to merge them (see above)  \n\n**Console example**\n```bash\n./bin/avro-cli  avro:schema:generate ./example/classes ./example/schemaTemplates\n```\n\n**PHP Example**\n```php\n\u003c?php\n\nuse PhpKafka\\PhpAvroSchemaGenerator\\Converter\\PhpClassConverter;\nuse PhpKafka\\PhpAvroSchemaGenerator\\Parser\\ClassParser;\nuse PhpKafka\\PhpAvroSchemaGenerator\\Parser\\DocCommentParser;\nuse PhpKafka\\PhpAvroSchemaGenerator\\Registry\\ClassRegistry;\nuse PhpKafka\\PhpAvroSchemaGenerator\\Parser\\ClassPropertyParser;\nuse PhpKafka\\PhpAvroSchemaGenerator\\Generator\\SchemaGenerator;\nuse PhpParser\\ParserFactory;\n\n$parser = (new ParserFactory())-\u003ecreate(ParserFactory::PREFER_PHP7);\n$classPropertyParser = new ClassPropertyParser(new DocCommentParser());\n$classParser = new ClassParser($parser, $classPropertyParser);\n\n$converter = new PhpClassConverter($classParser);\n$registry = (new ClassRegistry($converter))-\u003eaddClassDirectory('./classes')-\u003eload();\n\n$generator = new SchemaGenerator('./schema');\n$generator-\u003esetClassRegistry($registry);\n$schemas = $generator-\u003egenerate();\n$generator-\u003eexportSchemas($schemas);\n```\nThe generator is able to detect types from:\n- doc comments\n- property types\n- doc annotations\n  - @avro-type to set a fixed type instead of calculating one\n  - @avro-default set a default for this property in your schema\n  - @avro-doc to set schema doc comment\n  - @avro-logical-type set logical type for your property (decimal is not yet supported, since it has additional parameters)\n\n## Disclaimer\nIn `v1.3.0` the option `--optimizeSubSchemaNamespaces` was added. It was not working fully  \nin the `1.x` version and we had some discussions ([#13](https://github.com/php-kafka/php-avro-schema-generator/issues/13)) about it.  \nUltimately the decision was to adapt this behaviour fully in `v2.0.0` so you might want to  \nupgrade if you rely on that behaviour.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fphp-kafka%2Fphp-avro-schema-generator","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fphp-kafka%2Fphp-avro-schema-generator","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fphp-kafka%2Fphp-avro-schema-generator/lists"}