thesis/protobuf-known-types

Google protobuf known types collection.

Fund package maintenance!
www.tinkoff.ru/cf/5MqZQas2dk7

Installs: 163

Dependents: 2

Suggesters: 0

Security: 0

Stars: 0

Watchers: 0

Forks: 0

Open Issues: 0

pkg:composer/thesis/protobuf-known-types

0.1.2 2026-02-17 16:35 UTC

This package is auto-updated.

Last update: 2026-02-17 16:37:59 UTC


README

Installation

composer require thesis/protobuf-known-types

Usage

To encode or decode google.protobuf.Any type use encodeAny/decodeAny respectively.

use Google\Protobuf;
use Thesis\Protobuf\Encoder;

$any = Protobuf\encodeAny(X::class, Encoder\Builder::buildDefault());

Note that in this case, X should be autoloaded to the descriptor pool (Thesis\Protobuf\Pool\Registry) using autoload.metadata.php in composer.json or any other custom mechanism. Otherwise, an RuntimeException will be thrown.

If you strongly understand what you are doing and your types are not registered in the Pool\Registry, pass your own object type name resolver:

use Google\Protobuf;
use Thesis\Protobuf\Encoder;

$any = Protobuf\encodeAny(
    X::class,
    Encoder\Builder::buildDefault(),
    static fn(X $x) => 'my.own.types/x',
);

To decode google.protobuf.Any do the opposite using decodeAny:

use Google\Protobuf;
use Thesis\Protobuf\Decoder;

$x = Protobuf\decodeAny(
    new Protobuf\Any('type.googleapis.com/x', '...'),
    Decoder\Builder::buildDefault(),
);

Again, if you understand what you are doing, you can use custom class resolver:

use Google\Protobuf;
use Thesis\Protobuf\Decoder;

$x = Protobuf\decodeAny(
    new Protobuf\Any('my.own.types/x', '...'),
    Decoder\Builder::buildDefault(),
    static fn(string $type) => X::class,
);

In both cases, in encodeAny and decodeAny, your resolvers may return null to fall back to default type resolution using Pool\Registry.