Download the PHP package tarantool/client without Composer
On this page you can find all versions of the php package tarantool/client. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package client
PHP client for Tarantool
A pure PHP client for Tarantool 1.7.1 and later, including the 2.x and 3.x series.
Features
- Written in pure PHP, no extensions are required
- Supports Unix domain sockets
- Supports SQL protocol
- Supports user-defined types (decimals and UUIDs are included)
- Highly customizable
- Thoroughly tested
- Being used in a number of projects, including Queue, Mapper, Web Admin and others.
Table of contents
- Installation
- Creating a client
- Handlers
- Middleware
- Data manipulation
- Binary protocol
- SQL protocol
- User-defined types
- Tests
- Benchmarks
- License
Installation
This release requires PHP 8.2 or newer and rybakit/msgpack 0.11.2 or newer.
The recommended way to install the library is through Composer:
To use the Decimal type added in Tarantool 2.3, install the decimal extension. To improve performance when working with the UUID type, available since Tarantool 2.4, you can also install the uuid extension.
Creating a client
The easiest way to create a client is by using the default configuration:
The client connects to 127.0.0.1 on port 3301 using the default stream connection options.
It also selects the best available MessagePack package automatically. You can configure the client in several ways,
as described below.
DSN string
The client supports the following Data Source Name formats:
Some examples:
If the username, password, path or options include special characters such as @, :, / or %,
they must be encoded according to RFC 3986
(for example, with the rawurlencode() function).
Array of options
It is also possible to create the client from an array of configuration options:
The following options are available:
| Name | Type | Default | Description |
|---|---|---|---|
| uri | string | 'tcp://127.0.0.1:3301' | The connection uri that is used to create a StreamConnection object. |
| connect_timeout | float | 5.0 | The number of seconds that the client waits to connect to a Tarantool server before throwing a ConnectionFailed exception. |
| socket_timeout | float | 5.0 | The number of seconds that the client waits for a response from a Tarantool server before throwing a CommunicationFailed exception. |
| tcp_nodelay | boolean | true | Whether the Nagle algorithm is disabled on a TCP connection. |
| persistent | boolean | false | Whether to use a persistent connection. |
| username | string | The username for the user being authenticated. | |
| password | string | '' | The password for the user being authenticated. If the username is not set, this option will be ignored. |
| max_retries | integer | 0 | The number of times the client retries an unsuccessful request. If set to 0, the client does not retry after the initial unsuccessful attempt. |
Authentication and persistent connections
A persistent connection may reuse a socket opened by a previous client instance. Since authentication is tied to the socket, avoid combining persistent=true with username when a socket may be reused across client instances.
Custom build
For more extensive customization, you can build a client from the ground up:
Handlers
A handler transforms a request into a response. Once you have created a handler object, you can use it to send requests to Tarantool, for example:
The library ships with two handlers:
DefaultHandleris used for handling low-level communication with a Tarantool serverMiddlewareHandleris used as an extension point for an underlying handler via middleware
Middleware
Middleware is the recommended way to extend the client with custom functionality. The library includes middleware
for common use cases, such as authentication and logging. See the full list in src/Middleware.
Usage is straightforward:
You can also add multiple middleware components to the client. They execute in FIFO order:
The order in which you add middleware matters. The same middleware, placed in a different order, can produce different or sometimes unexpected behavior. To illustrate, consider the following configurations:
In this example, $client1 will retry an unsuccessful operation and in case of connection
problems may initiate reconnection with follow-up re-authentication. However, $client2
and $client3 will perform reconnection without doing any re-authentication.
You may wonder why
$client3behaves like$client2in this case. This is because specifying some options (via array or DSN string) may implicitly register middleware. Thus, theusername/passwordoptions will be turned intoAuthenticationMiddlewareunder the hood, making the two configurations identical.
To make sure your middleware runs first, use the withPrependedMiddleware() method:
Data manipulation
Binary protocol
The following are examples of binary protocol requests. For more detailed information and examples please see the official documentation.
Select
*Fixtures* *Code* *Output*
Insert
*Fixtures* *Code* *Output* *Space data*
Update
*Fixtures* *Code* *Output* *Space data*
Upsert
*Fixtures* *Code* *Space data*
Replace
*Fixtures* *Code* *Output* *Space data*
Delete
*Fixtures* *Code* *Output* *Space data*
Call
*Fixtures* *Code* *Output*
Evaluate
*Code* *Output*
SQL protocol
The following are examples of SQL protocol requests. For more detailed information and examples, please see the official documentation. SQL is supported starting with Tarantool 2.0.
Execute
*Code* *Output* If you need to execute a dynamic SQL statement and do not know its type, use the generic `execute()` method. It returns a `Response` object whose body contains either an array of result-set rows or an array with information about the changed rows:
Prepare
*Note that the `prepare` request is supported only as of Tarantool 2.3.2.* *Code* *Output*
User-defined types
To store complex structures inside a tuple you may want to use objects:
You can do this by extending the MessagePack type system with your own types. Write a MessagePack extension that converts your objects to and from MessagePack structures (see the msgpack.php README for details). Once you have implemented the extension, register it with the packer object:
A working example of using the user-defined types can be found in the examples folder.
Tests
To run unit tests:
To run integration tests:
Make sure to start client.lua first.
To run all tests:
If you already have Docker installed, you can run the tests in a Docker container. First, build the Docker image:
The command above builds an image named client with the PHP 8.3 runtime.
You may change the default runtime by defining the PHP_IMAGE environment variable:
See a list of various images here.
Then run a Tarantool instance (needed for integration tests):
And then run both unit and integration tests:
Benchmarks
The benchmarks can be found in the dedicated repository.
License
The library is released under the MIT License. See the bundled LICENSE file for details.