Passed
Push — master ( 813742...26b3f4 )
by Théo
02:16
created

memory_to_bytes()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.2
c 0
b 0
f 0
cc 4
eloc 10
nc 8
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the box project.
7
 *
8
 * (c) Kevin Herrera <[email protected]>
9
 *     Théo Fidry <[email protected]>
10
 *
11
 * This source file is subject to the MIT license that is bundled
12
 * with this source code in the file LICENSE.
13
 */
14
15
namespace KevinGH\Box;
16
17
use Assert\Assertion;
18
use Phar;
19
use function array_key_exists;
20
use function constant;
21
use function define;
22
use function defined;
23
24
/**
25
 * @internal
26
 * @private
27
 */
28
const NO_PARALLEL_PROCESSING = 'KevinGH\Box\BOX_NO_PARALLEL_PROCESSING';
29
30
/**
31
 * TODO: this function should be pushed down to the PHAR extension.
32
 *
33
 * @private
34
 */
35
function get_phar_compression_algorithms(): array
36
{
37
    static $algorithms = [
38
        'GZ' => Phar::GZ,
39
        'BZ2' => Phar::BZ2,
40
        'NONE' => Phar::NONE,
41
    ];
42
43
    return $algorithms;
44
}
45
46
/**
47
 * @private
48
 */
49
function get_phar_compression_algorithm_extension(int $algorithm): ?string
50
{
51
    static $extensions = [
52
        Phar::GZ => 'zlib',
53
        Phar::BZ2 => 'bz2',
54
        Phar::NONE => null,
55
    ];
56
57
    Assertion::true(array_key_exists($algorithm, $extensions));
58
59
    return $extensions[$algorithm];
60
}
61
62
/**
63
 * @private
64
 */
65
function formatted_filesize(string $path): string
66
{
67
    Assertion::file($path);
68
69
    $size = filesize($path);
70
    $units = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
71
    $power = $size > 0 ? (int) floor(log($size, 1024)) : 0;
72
73
    return sprintf(
74
        '%s%s',
75
        number_format(
76
            $size / (1024 ** $power),
77
            2,
78
            '.',
79
            ','
80
        ),
81
        $units[$power]
82
    );
83
}
84
85
/**
86
 * @private Converts a memory string, e.g. '2000M' to bytes
87
 */
88
function memory_to_bytes(string $value): int
89
{
90
    $unit = strtolower($value[strlen($value) - 1]);
91
92
    $value = (int) $value;
93
    switch ($unit) {
94
        case 'g':
95
            $value *= 1024;
96
        // no break (cumulative multiplier)
97
        case 'm':
98
            $value *= 1024;
99
        // no break (cumulative multiplier)
100
        case 'k':
101
            $value *= 1024;
102
    }
103
104
    return $value;
105
}
106
107
/**
108
 * @private
109
 */
110
function register_aliases(): void
111
{
112
    // Exposes the finder used by PHP-Scoper PHAR to allow its usage in the configuration file.
113
    if (false === class_exists(\Isolated\Symfony\Component\Finder\Finder::class)) {
114
        class_alias(\Symfony\Component\Finder\Finder::class, \Isolated\Symfony\Component\Finder\Finder::class);
115
    }
116
117
    // Register compactors aliases
118
    if (false === class_exists(\Herrera\Box\Compactor\Json::class, false)) {
119
        class_alias(\KevinGH\Box\Compactor\Json::class, \Herrera\Box\Compactor\Json::class);
120
    }
121
122
    if (false === class_exists(\Herrera\Box\Compactor\Php::class, false)) {
123
        class_alias(\KevinGH\Box\Compactor\Php::class, \Herrera\Box\Compactor\Php::class);
124
    }
125
}
126
127
/**
128
 * @private
129
 */
130
function disable_parallel_processing(): void
131
{
132
    define(NO_PARALLEL_PROCESSING, true);
133
}
134
135
/**
136
 * @private
137
 */
138
function is_parallel_processing_enabled(): bool
139
{
140
    return false === defined(NO_PARALLEL_PROCESSING) || false === constant(NO_PARALLEL_PROCESSING);
141
}
142