Download the PHP package mateusjunges/avro-serde-php without Composer
On this page you can find all versions of the php package mateusjunges/avro-serde-php. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download mateusjunges/avro-serde-php
More information about mateusjunges/avro-serde-php
Files in mateusjunges/avro-serde-php
Package avro-serde-php
Short Description A library to serialize and deserialize Avro records making use of the confluent schema registry
License MIT
Informations about the package avro-serde-php
Avro SerDe for PHP 7.3+ and 8.0
Motivation
When serializing and deserializing messages using the Avro serialization format, especially when integrating with the Confluent Platform, you want to make sure that schemas are evolved in a way that downstream consumers are not affected.
Hence Confluent developed the Schema Registry which has the responsibility to validate a given schema evolution against a configurable compatibility policy.
Unfortunately Confluent is not providing an official Avro SerDe package for PHP. This library aims to provide an Avro SerDe library for PHP that implements the Confluent wire format and integrates FlixTech's Schema Registry Client.
Installation
This library is using the composer package manager for PHP.
Quickstart
NOTE
You should always use a cached schema registry client, since otherwise you'd make an HTTP request for every message serialized or deserialized.
1. Create a cached Schema Registry client
See the Schema Registry client documentation on caching for more detailed information.
2. Build the RecordSerializer
instance
The RecordSerializer
is the main way you interact with this library. It provides the encodeRecord
and
decodeMessage
methods for SerDe operations.
3. Encoding records
This is a simple example on how you can use the RecordSerializer
to encode messages in the Confluent Avro wire format.
4. Decoding messages
This is a simple example on how you can use the RecordSerializer
to decode messages.
Schema Resolvers
Schema Resolvers are responsible to know which Avro schema belongs to which type of record. This is especially useful if you want to manage your Avro schemas in separate files. Schema Resolvers enable you to integrate with whatever schema management concept you may have outside of the scope of this library.
Schema Resolvers take a $record
of any type and try to resolve a matching [AvroSchema
]() instance for it.
FileResolver
In even moderately complicated applications you want to manage your schemas within the VCS, most probably as .avsc
files. These files contain JSON that is describing the Avro schema.
The resolver takes a $baseDir
in which you want to manage the files and an inflector callable
, which is a simple
function that takes the record as first parameter, and a second boolean $isKey
parameter indicating if the inflection
is targeting a key schema.
CallableResolver
This is the simplest but also most flexible resolver. It just takes two callables
that are responsible to fetch either
value- or key-schemas respectively. A key schema resolver is optional.
DefinitionInterfaceResolver
This library also provides a HasSchemaDefinitionInterface
that exposes two static methods:
HasSchemaDefinitionInterface::valueSchemaJson
returns the schema definition for the value as JSON stringHasSchemaDefinitionInterface::keySchemaJson
returns eitherNULL
or the schema definition for the key as JSON string.
The DefinitionInterfaceResolver
checks if a given record implements that interface (if not it will throw an
InvalidArgumentException
) and resolves the schemas via the static methods.
ChainResolver
The chain resolver is a useful tool for composing multiple resolvers. The first resolver to be able to resolve a schema
will win. If none of the resolvers in the chain is able to determine a schema, an InvalidArgumentException
is thrown.
Symfony Serializer Integration
This library provides integrations with the Symfony Serializer component.
Name converter
Sometimes your property names may differ from the names of the fields in your schema. One option to solve this is by using custom Serializer annotations. However, if you're using the annotations provided by this library, you may use our name converter that parses these annotations and maps between the schema field names and the property names.
Schema builder
This library also provides means of defining schemas using php, very similar to the SchemaBuilder API provided by the Java SDK:
Schema generator
Besides providing a fluent api for defining schemas, we also provide means of generating schema from
class metadata (annotations). For this to work, you have to install the doctrine/annotations
package.
Further examples on the possible annotations can be seen in the test case.
Examples
This library provides a few executable examples in the examples folder. You should have a look to get an understanding how this library works.
All versions of avro-serde-php with dependencies
beberlei/assert Version ^2.9.9|~3.0
mateusjunges/confluent-schema-registry-api Version ^9.0
guzzlehttp/promises Version ^2.0
flix-tech/avro-php Version ^5.0
widmogrod/php-functional Version ^6.0