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\PackerFactory; |
19
|
|
|
use Tarantool\Client\Packer\PurePacker; |
20
|
|
|
|
21
|
|
|
return require __DIR__.'/../vendor/autoload.php'; |
22
|
|
|
|
23
|
|
|
function create_client(?Packer $packer = null) : Client |
24
|
|
|
{ |
25
|
|
|
$connection = isset($_SERVER['argv'][1]) |
26
|
|
|
? StreamConnection::create($_SERVER['argv'][1]) |
27
|
|
|
: StreamConnection::createTcp(); |
28
|
|
|
|
29
|
|
|
return new Client(new DefaultHandler($connection, $packer ?? PackerFactory::create())); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
function server_version_at_least(string $version, Client $client) : bool |
33
|
|
|
{ |
34
|
|
|
[$info] = $client->call('box.info'); |
35
|
|
|
$actualVersion = preg_replace('/-[^-]+$/', '', $info['version']); |
36
|
|
|
|
37
|
|
|
return version_compare($actualVersion, $version, '>='); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
function ensure_server_version_at_least(string $version, Client $client) : void |
41
|
|
|
{ |
42
|
|
|
if (server_version_at_least($version, $client)) { |
43
|
|
|
return; |
44
|
|
|
} |
45
|
|
|
|
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
|
|
|
requirement_exit('PHP extension "%s" is required to run "%s"', $name, $_SERVER['SCRIPT_FILENAME']); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
function ensure_class(string $className, string $requireMessage = '') : void |
59
|
|
|
{ |
60
|
|
|
if (class_exists($className)) { |
61
|
|
|
return; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
$errorMessage = $requireMessage ?: sprintf('Class "%s" is required', $className); |
65
|
|
|
|
66
|
|
|
requirement_exit('%s to run "%s"', $errorMessage, $_SERVER['SCRIPT_FILENAME']); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
function ensure_pure_packer(Client $client) : void |
70
|
|
|
{ |
71
|
|
|
$packer = $client->getHandler()->getPacker(); |
72
|
|
|
if ($packer instanceof PurePacker) { |
73
|
|
|
return; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
requirement_exit('Client needs to be configured to use pure packer to run "%s"', $_SERVER['SCRIPT_FILENAME']); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
function requirement_exit(string $message, ...$args) : void |
80
|
|
|
{ |
81
|
|
|
echo "Unfulfilled requirement:\n"; |
82
|
|
|
echo $args ? sprintf($message, ...$args) : $message, "\n"; |
83
|
|
|
exit(1); |
|
|
|
|
84
|
|
|
} |
85
|
|
|
|
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.