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 Composer\InstalledVersions; |
18
|
|
|
use ErrorException; |
19
|
|
|
use Isolated\Symfony\Component\Finder\Finder as IsolatedFinder; |
20
|
|
|
use Phar; |
21
|
|
|
use Symfony\Component\Console\Helper\Helper; |
22
|
|
|
use Symfony\Component\Finder\Finder as SymfonyFinder; |
23
|
|
|
use Webmozart\Assert\Assert; |
24
|
|
|
use function bin2hex; |
25
|
|
|
use function class_alias; |
26
|
|
|
use function class_exists; |
27
|
|
|
use function floor; |
28
|
|
|
use function is_float; |
29
|
|
|
use function is_int; |
30
|
|
|
use function log; |
31
|
|
|
use function number_format; |
32
|
|
|
use function random_bytes; |
33
|
|
|
use function sprintf; |
34
|
|
|
use function str_replace; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @private |
38
|
|
|
*/ |
39
|
|
|
function get_box_version(): string |
40
|
|
|
{ |
41
|
|
|
// Load manually the InstalledVersions class. |
42
|
|
|
// Indeed, this class is registered to the autoloader by Composer itself which |
43
|
|
|
// results an incorrect classmap entry in the scoped code. |
44
|
|
|
// This strategy avoids having to exclude completely the file from the scoping. |
45
|
|
|
foreach ([__DIR__.'/../vendor/composer/InstalledVersions.php', __DIR__.'/../../../composer/InstalledVersions.php'] as $file) { |
46
|
|
|
if (file_exists($file)) { |
47
|
|
|
require_once $file; |
48
|
|
|
break; |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
$prettyVersion = InstalledVersions::getPrettyVersion('humbug/box'); |
53
|
|
|
$commitHash = InstalledVersions::getReference('humbug/box'); |
54
|
|
|
|
55
|
|
|
if (null === $commitHash) { |
56
|
|
|
return $prettyVersion; |
|
|
|
|
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
return $prettyVersion.'@'.mb_substr($commitHash, 0, 7); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @deprecated since 4.3.0. Use \KevinGH\Box\Phar\CompressionAlgorithm instead. |
64
|
|
|
* @private |
65
|
|
|
* |
66
|
|
|
* @return array<string,int> |
67
|
|
|
*/ |
68
|
|
|
function get_phar_compression_algorithms(): array |
69
|
|
|
{ |
70
|
|
|
static $algorithms = [ |
71
|
|
|
'GZ' => Phar::GZ, |
72
|
|
|
'BZ2' => Phar::BZ2, |
73
|
|
|
'NONE' => Phar::NONE, |
74
|
|
|
]; |
75
|
|
|
|
76
|
|
|
return $algorithms; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @deprecated Since 4.5.0. Use \KevinGH\Box\Phar\SigningAlgorithm instead. |
81
|
|
|
* |
82
|
|
|
* @private |
83
|
|
|
* |
84
|
|
|
* @return array<string,int> |
85
|
|
|
*/ |
86
|
|
|
function get_phar_signing_algorithms(): array |
87
|
|
|
{ |
88
|
|
|
static $algorithms = [ |
89
|
|
|
'MD5' => Phar::MD5, |
90
|
|
|
'SHA1' => Phar::SHA1, |
91
|
|
|
'SHA256' => Phar::SHA256, |
92
|
|
|
'SHA512' => Phar::SHA512, |
93
|
|
|
'OPENSSL' => Phar::OPENSSL, |
94
|
|
|
]; |
95
|
|
|
|
96
|
|
|
return $algorithms; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @private |
101
|
|
|
*/ |
102
|
|
|
function format_size(float|int $size, int $decimals = 2): string |
103
|
|
|
{ |
104
|
|
|
Assert::true(is_int($size) || is_float($size)); |
105
|
|
|
|
106
|
|
|
if (-1 === $size) { |
107
|
|
|
return '-1'; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
$units = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']; |
111
|
|
|
|
112
|
|
|
$power = $size > 0 ? (int) floor(log($size, 1024)) : 0; |
113
|
|
|
|
114
|
|
|
return sprintf( |
115
|
|
|
'%s%s', |
116
|
|
|
number_format( |
117
|
|
|
$size / (1024 ** $power), |
118
|
|
|
$decimals, |
119
|
|
|
), |
120
|
|
|
$units[$power], |
121
|
|
|
); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @private |
126
|
|
|
*/ |
127
|
|
|
function memory_to_bytes(string $value): float|int |
128
|
|
|
{ |
129
|
|
|
$unit = mb_strtolower($value[mb_strlen($value) - 1]); |
130
|
|
|
|
131
|
|
|
$bytes = (int) $value; |
132
|
|
|
|
133
|
|
|
switch ($unit) { |
134
|
|
|
case 'g': |
135
|
|
|
$bytes *= 1024; |
136
|
|
|
// no break (cumulative multiplier) |
137
|
|
|
case 'm': |
138
|
|
|
$bytes *= 1024; |
139
|
|
|
// no break (cumulative multiplier) |
140
|
|
|
case 'k': |
141
|
|
|
$bytes *= 1024; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
return $bytes; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @private |
149
|
|
|
*/ |
150
|
|
|
function format_time(float $secs): string |
151
|
|
|
{ |
152
|
|
|
return str_replace( |
153
|
|
|
' ', |
154
|
|
|
'', |
155
|
|
|
Helper::formatTime($secs), |
156
|
|
|
); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @private |
161
|
|
|
*/ |
162
|
|
|
function register_aliases(): void |
163
|
|
|
{ |
164
|
|
|
// Exposes the finder used by PHP-Scoper PHAR to allow its usage in the configuration file. |
165
|
|
|
if (false === class_exists(IsolatedFinder::class)) { |
166
|
|
|
class_alias(SymfonyFinder::class, IsolatedFinder::class); |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* @private |
172
|
|
|
* |
173
|
|
|
* @return string Random 12 characters long (plus the prefix) string composed of a-z characters and digits |
174
|
|
|
*/ |
175
|
|
|
function unique_id(string $prefix): string |
176
|
|
|
{ |
177
|
|
|
return $prefix.bin2hex(random_bytes(6)); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Converts errors to exceptions. |
182
|
|
|
* |
183
|
|
|
* @private |
184
|
|
|
*/ |
185
|
|
|
function register_error_handler(): void |
186
|
|
|
{ |
187
|
|
|
set_error_handler( |
188
|
|
|
static function (int $code, string $message, string $file = '', int $line = -1): void { |
189
|
|
|
if (error_reporting() & $code) { |
190
|
|
|
throw new ErrorException($message, 0, $code, $file, $line); |
191
|
|
|
} |
192
|
|
|
}, |
193
|
|
|
); |
194
|
|
|
} |
195
|
|
|
|