tarantool-php /
client
| 1 | <?php |
||
| 2 | |||
| 3 | /** |
||
| 4 | * This file is part of the tarantool/client package. |
||
| 5 | * |
||
| 6 | * (c) Eugene Leonovich <[email protected]> |
||
| 7 | * |
||
| 8 | * For the full copyright and license information, please view the LICENSE |
||
| 9 | * file that was distributed with this source code. |
||
| 10 | */ |
||
| 11 | |||
| 12 | declare(strict_types=1); |
||
| 13 | |||
| 14 | use Tarantool\Client\Client; |
||
| 15 | use Tarantool\Client\Connection\StreamConnection; |
||
| 16 | use Tarantool\Client\Handler\DefaultHandler; |
||
| 17 | use Tarantool\Client\Packer\Packer; |
||
| 18 | use Tarantool\Client\Packer\PurePacker; |
||
| 19 | |||
| 20 | return require __DIR__.'/../vendor/autoload.php'; |
||
| 21 | |||
| 22 | function create_client(?Packer $packer = null) : Client |
||
| 23 | { |
||
| 24 | $connection = isset($_SERVER['argv'][1]) |
||
| 25 | ? StreamConnection::create($_SERVER['argv'][1]) |
||
| 26 | : StreamConnection::createTcp(); |
||
| 27 | |||
| 28 | return new Client(new DefaultHandler($connection, $packer ?? PurePacker::fromAvailableExtensions())); |
||
| 29 | } |
||
| 30 | |||
| 31 | function server_version_at_least(string $version, Client $client) : bool |
||
| 32 | { |
||
| 33 | [$info] = $client->call('box.info'); |
||
| 34 | $actualVersion = preg_replace('/-[^-]+$/', '', $info['version']); |
||
| 35 | |||
| 36 | return version_compare($actualVersion, $version, '>='); |
||
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
| 37 | } |
||
| 38 | |||
| 39 | function ensure_server_version_at_least(string $version, Client $client) : void |
||
| 40 | { |
||
| 41 | if (server_version_at_least($version, $client)) { |
||
| 42 | return; |
||
| 43 | } |
||
| 44 | |||
| 45 | /** @psalm-suppress PossiblyUndefinedArrayOffset */ |
||
| 46 | requirement_exit('Tarantool version >= %s is required to run "%s"', $version, $_SERVER['SCRIPT_FILENAME']); |
||
| 47 | } |
||
| 48 | |||
| 49 | function ensure_extension(string $name) : void |
||
| 50 | { |
||
| 51 | if (extension_loaded($name)) { |
||
| 52 | return; |
||
| 53 | } |
||
| 54 | |||
| 55 | /** @psalm-suppress PossiblyUndefinedArrayOffset */ |
||
| 56 | requirement_exit('PHP extension "%s" is required to run "%s"', $name, $_SERVER['SCRIPT_FILENAME']); |
||
| 57 | } |
||
| 58 | |||
| 59 | function requirement_exit(string $message, ...$args) : void |
||
| 60 | { |
||
| 61 | echo "Unfulfilled requirement:\n"; |
||
| 62 | echo $args ? sprintf($message, ...$args) : $message, "\n"; |
||
| 63 | exit(1); |
||
| 64 | } |
||
| 65 |