Completed
Branch master (00332a)
by Eugene
05:11
created

server_version_at_least()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 8
rs 10
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
use Tarantool\Client\Tests\Integration\ExamplesTest;
21
22
return require __DIR__.'/../vendor/autoload.php';
23
24
function create_client(?Packer $packer = null) : Client
25
{
26
    $connection = isset($_SERVER['argv'][1])
27
        ? StreamConnection::create($_SERVER['argv'][1])
28
        : StreamConnection::createTcp();
29
30
    return new Client(new DefaultHandler($connection, $packer ?? PackerFactory::create()));
31
}
32
33
function server_version_at_least(string $version, Client $client) : bool
34
{
35
    [$info] = $client->call('box.info');
36
    $actualVersion = preg_replace('/-[^-]+$/', '', $info['version']);
37
38
    return version_compare($actualVersion, $version, '>=');
39
}
40
41
function ensure_server_version_at_least(string $version, Client $client) : void
42
{
43
    if (server_version_at_least($version, $client)) {
44
        return;
45
    }
46
47
    printf('Tarantool version >= %s is required to run "%s"%s', $version, $_SERVER['SCRIPT_FILENAME'], PHP_EOL);
48
    exit(ExamplesTest::EXIT_CODE_SKIP);
49
}
50
51
function ensure_extension(string $name) : void
52
{
53
    if (extension_loaded($name)) {
54
        return;
55
    }
56
57
    printf('PHP extension "%s" is required to run "%s"%s', $name, $_SERVER['SCRIPT_FILENAME'], PHP_EOL);
58
    exit(ExamplesTest::EXIT_CODE_SKIP);
59
}
60
61
function ensure_pure_packer(Client $client) : void
62
{
63
    $packer = $client->getHandler()->getPacker();
64
    if ($packer instanceof PurePacker) {
65
        return;
66
    }
67
68
    printf('Client needs to be configured to use pure packer to run "%s"%s', $_SERVER['SCRIPT_FILENAME'], PHP_EOL);
69
    exit(ExamplesTest::EXIT_CODE_SKIP);
70
}
71