Completed
Push — master ( 068ed2...1b73cd )
by Eugene
06:44
created

bootstrap.php ➔ requirement_exit()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 2
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
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
    if ($args) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $args of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
82
        $message = sprintf($message, ...$args);
83
    }
84
85
    echo "Unfulfilled requirement:\n$message\n";
86
    exit(0);
87
}
88