Download the PHP package rybakit/msgpack without Composer
On this page you can find all versions of the php package rybakit/msgpack. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download rybakit/msgpack
More information about rybakit/msgpack
Files in rybakit/msgpack
Package msgpack
Short Description A pure PHP implementation of the MessagePack serialization format.
License MIT
Informations about the package msgpack
msgpack.php
A pure PHP implementation of the MessagePack serialization format.
Features
- Fully compliant with the latest MessagePack specification
- Supports streaming unpacking
- Supports unsigned 64-bit integers handling
- Supports object serialization
- Fully tested
- Relatively fast
Table of contents
- Installation
- Usage
- Packing
- Packing options
- Unpacking
- Unpacking options
- Packing
- Custom types
- Type objects
- Type transformers
- Extensions
- Timestamp
- Application-specific extensions
- Exceptions
- Tests
- Fuzzing
- Performance
- License
Installation
The recommended way to install the library is through Composer:
Usage
Packing
To pack values you can either use an instance of a Packer
:
or call a static method on the MessagePack
class:
In the examples above, the method pack
automatically packs a value depending on its type. However, not all PHP types
can be uniquely translated to MessagePack types. For example, the MessagePack format defines map
and array
types,
which are represented by a single array
type in PHP. By default, the packer will pack a PHP array as a MessagePack
array if it has sequential numeric keys, starting from 0
and as a MessagePack map otherwise:
However, sometimes you need to pack a sequential array as a MessagePack map.
To do this, use the packMap
method:
Here is a list of type-specific packing methods:
Check the "Custom types" section below on how to pack custom types.
Packing options
The Packer
object supports a number of bitmask-based options for fine-tuning
the packing process (defaults are in bold):
Name | Description |
---|---|
FORCE_STR |
Forces PHP strings to be packed as MessagePack UTF-8 strings |
FORCE_BIN |
Forces PHP strings to be packed as MessagePack binary data |
DETECT_STR_BIN |
Detects MessagePack str/bin type automatically |
FORCE_ARR |
Forces PHP arrays to be packed as MessagePack arrays |
FORCE_MAP |
Forces PHP arrays to be packed as MessagePack maps |
DETECT_ARR_MAP |
Detects MessagePack array/map type automatically |
FORCE_FLOAT32 |
Forces PHP floats to be packed as 32-bits MessagePack floats |
FORCE_FLOAT64 |
Forces PHP floats to be packed as 64-bits MessagePack floats |
The type detection mode (
DETECT_STR_BIN
/DETECT_ARR_MAP
) adds some overhead which can be noticed when you pack large (16- and 32-bit) arrays or strings. However, if you know the value type in advance (for example, you only work with UTF-8 strings or/and associative arrays), you can eliminate this overhead by forcing the packer to use the appropriate type, which will save it from running the auto-detection routine. Another option is to explicitly specify the value type. The library provides 2 auxiliary classes for this,Map
andBin
. Check the "Custom types" section below for details.
Examples:
Unpacking
To unpack data you can either use an instance of a BufferUnpacker
:
or call a static method on the MessagePack
class:
If the packed data is received in chunks (e.g. when reading from a stream), use the tryUnpack
method, which attempts
to unpack data and returns an array of unpacked messages (if any) instead of throwing an InsufficientDataException
:
If you want to unpack from a specific position in a buffer, use seek
:
To skip bytes from the current position, use skip
:
To get the number of remaining (unread) bytes in the buffer:
To check whether the buffer has unread data:
If needed, you can remove already read data from the buffer by calling:
With the read
method you can read raw (packed) data:
Besides the above methods BufferUnpacker
provides type-specific unpacking methods, namely:
Unpacking options
The BufferUnpacker
object supports a number of bitmask-based options for fine-tuning
the unpacking process (defaults are in bold):
Name | Description |
---|---|
BIGINT_AS_STR |
Converts overflowed integers to strings [1] |
BIGINT_AS_GMP |
Converts overflowed integers to GMP objects [2] |
BIGINT_AS_DEC |
Converts overflowed integers to Decimal\Decimal objects [3] |
1. The binary MessagePack format has unsigned 64-bit as its largest integer data type, but PHP does not support such integers, which means that an overflow can occur during unpacking.
2. Make sure the GMP extension is enabled.
3. Make sure the Decimal extension is enabled.
Examples:
Custom types
In addition to the basic types, the library provides functionality to serialize and deserialize arbitrary types. This can be done in several ways, depending on your use case. Let's take a look at them.
Type objects
If you need to serialize an instance of one of your classes into one of the basic MessagePack types, the best way
to do this is to implement the CanBePacked interface in the class. A good example of such
a class is the Map
type class that comes with the library. This type is useful when you want to explicitly specify
that a given PHP array should be packed as a MessagePack map without triggering an automatic type detection routine:
More type examples can be found in the src/Type directory.
Type transformers
As with type objects, type transformers are only responsible for serializing values. They should be used when you need to serialize a value that does not implement the CanBePacked interface. Examples of such values could be instances of built-in or third-party classes that you don't own, or non-objects such as resources.
A transformer class must implement the CanPack interface. To use a transformer,
it must first be registered in the packer. Here is an example of how to serialize PHP streams into
the MessagePack bin
format type using one of the supplied transformers, StreamTransformer
:
More type transformer examples can be found in the src/TypeTransformer directory.
Extensions
In contrast to the cases described above, extensions are intended to handle extension types and are responsible for both serialization and deserialization of values (types).
An extension class must implement the Extension interface. To use an extension, it must first be registered in the packer and the unpacker.
The MessagePack specification divides extension types into two groups: predefined and application-specific. Currently, there is only one predefined type in the specification, Timestamp.
Timestamp
The Timestamp extension type is a predefined
type. Support for this type in the library is done through the TimestampExtension
class. This class is responsible
for handling Timestamp
objects, which represent the number of seconds and optional adjustment in nanoseconds:
When using the MessagePack
class, the Timestamp extension is already registered:
Application-specific extensions
In addition, the format can be extended with your own types. For example, to make the built-in PHP DateTime
objects
first-class citizens in your code, you can create a corresponding extension, as shown in the example.
Please note, that custom extensions have to be registered with a unique extension ID (an integer from 0
to 127
).
More extension examples can be found in the examples/MessagePack directory.
To learn more about how extension types can be useful, check out this article.
Exceptions
If an error occurs during packing/unpacking, a PackingFailedException
or an UnpackingFailedException
will be thrown, respectively. In addition, an InsufficientDataException
can be thrown during unpacking.
An InvalidOptionException
will be thrown in case an invalid option (or a combination of mutually
exclusive options) is used.
Tests
Run tests as follows:
Also, if you already have Docker installed, you can run the tests in a docker container. First, create a container:
The command above will create a container named msgpack
with PHP 8.2 runtime. You may change the default runtime
by defining the PHP_IMAGE
environment variable:
See a list of various images here.
Then run the unit tests:
Fuzzing
To ensure that the unpacking works correctly with malformed/semi-malformed data, you can use a testing technique called Fuzzing. The library ships with a help file (target) for PHP-Fuzzer and can be used as follows:
Performance
To check performance, run:
Example output
With JIT:
Example output
You may change default benchmark settings by defining the following environment variables:
Name | Default |
---|---|
MP_BENCH_TARGETS | pure_p,pure_u , see a list of available targets |
MP_BENCH_ITERATIONS | 100_000 |
MP_BENCH_DURATION | not set |
MP_BENCH_ROUNDS | 3 |
MP_BENCH_TESTS | -@slow , see a list of available tests |
For example:
Another example, benchmarking both the library and the PECL extension:
Example output
With JIT:
Example output
Note that the msgpack extension (v2.1.2) doesn't support ext, bin and UTF-8 str types.
License
The library is released under the MIT License. See the bundled LICENSE file for details.