定制 php-kafka/php-avro-schema-generator 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

php-kafka/php-avro-schema-generator

Composer 安装命令:

composer require php-kafka/php-avro-schema-generator

包简介

PHP avro schema generator for subschema

README 文档

README

Actions Status Maintainability Test Coverage Supported PHP versions: ^8.2 Latest Stable Version

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

./vendor/bin/avro-cli avro:subschema:merge ./example/schemaTemplates ./example/schema

PHP example

<?php

use PhpKafka\PhpAvroSchemaGenerator\Registry\SchemaRegistry;
use PhpKafka\PhpAvroSchemaGenerator\Merger\SchemaMerger;

$registry = (new SchemaRegistry())
    ->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

./bin/avro-cli  --optimizeFullNames --optimizeFieldOrder --optimizePrimitiveSchemas avro:subschema:merge ./example/schemaTemplates ./example/schema

PHP Example

<?php

use PhpKafka\PhpAvroSchemaGenerator\Registry\SchemaRegistry;
use PhpKafka\PhpAvroSchemaGenerator\Merger\SchemaMerger;
use PhpKafka\PhpAvroSchemaGenerator\Optimizer\FieldOrderOptimizer;
use PhpKafka\PhpAvroSchemaGenerator\Optimizer\FullNameOptimizer;
use PhpKafka\PhpAvroSchemaGenerator\Optimizer\PrimitiveSchemaOptimizer;

$registry = (new SchemaRegistry())
    ->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

./bin/avro-cli  avro:schema:generate ./example/classes ./example/schemaTemplates

PHP Example

<?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;

$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);

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) 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.

php-kafka/php-avro-schema-generator 适用场景与选型建议

php-kafka/php-avro-schema-generator 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 22.35k 次下载、GitHub Stars 达 6, 最近一次更新时间为 2020 年 10 月 05 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「avro」 「avro-schema」 「subschema」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 php-kafka/php-avro-schema-generator 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 php-kafka/php-avro-schema-generator 我们能提供哪些服务?
定制开发 / 二次开发

基于 php-kafka/php-avro-schema-generator 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 22.35k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 6
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 6
  • Watchers: 2
  • Forks: 5
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-10-05