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.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package msgpack

msgpack.php

Quality Assurance Code Coverage Mentioned in Awesome PHP

A pure PHP implementation of the MessagePack serialization format.

Features

Table of contents

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 and Bin. 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.


All versions of msgpack with dependencies

PHP Build Version
Package Version
Requires php Version ^7.1.1|^8
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package rybakit/msgpack contains the following files

Loading the files please wait ....