Download the PHP package cnik87/msgpack2 without Composer

On this page you can find all versions of the php package cnik87/msgpack2. 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 msgpack2

msgpack.php

A pure PHP implementation of the MessagePack serialization format.

Build Status Code Coverage

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. But 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 "Type transformers" section below on how to pack arbitrary PHP objects.

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 Binary. Check the "Type transformers" 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:

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_EXCEPTION Throws an exception on integer overflow [1]
BIGINT_AS_GMP Converts overflowed integers to GMP objects [2]
BIGINT_AS_STR Converts overflowed integers to strings

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 that the GMP extension is enabled.

Examples:

Extensions

To define application-specific types use the Ext class:

Type transformers

In addition to the basic types, the library provides functionality to serialize and deserialize arbitrary types. In order to support a custom type you need to create and register a transformer. The transformer should either implement the Packable or the Extension interface.

The purpose of Packable transformers is to serialize a specific value to one of the basic MessagePack types. A good example of such a transformer is a MapTransformer that comes with the library. It serializes Map objects (which are simple wrappers around PHP arrays) to MessagePack maps. This is useful when you want to explicitly mark that a given PHP array must be packed as a MessagePack map, without triggering the type's auto-detection routine.

More types and type transformers can be found in src/Type and src/TypeTransformer directories.

The implementation is trivial:

Once MapTransformer is registered, you can pack Map objects:

Transformers implementing the Extension interface are intended for packing and unpacking application-specific types using the MessagePack's Extension type. For example, the code below shows how to create a transformer that allows you to work transparently with DateTime objects:

Register DateTimeTransformer for both the packer and the unpacker with a unique extension type (an integer from 0 to 127) and you are ready to go:

More type transformer examples can be found in the examples directory.

Exceptions

If an error occurs during packing/unpacking, a PackingFailedException or UnpackingFailedException will be thrown, respectively.

In addition, there are three more exceptions that 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 7.2 runtime. You may change the default runtime by defining the PHP_RUNTIME environment variable:

See a list of various runtimes here.

Then run the unit tests:

Performance

To check performance, run:

This command will output something like:

You may change default benchmark settings by defining the following environment variables:

For example:

Another example, benchmarking both the library and the msgpack pecl extension:

Note, that this is not a fair comparison as the msgpack extension (0.5.2+, 2.0) 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 msgpack2 with dependencies

PHP Build Version
Package Version
Requires php Version ^7.1.1
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 cnik87/msgpack2 contains the following files

Loading the files please wait ....