-
-
Notifications
You must be signed in to change notification settings - Fork 166
Expand file tree
/
Copy pathIniUtil.php
More file actions
56 lines (48 loc) · 1.48 KB
/
IniUtil.php
File metadata and controls
56 lines (48 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<?php
namespace React\Http\Io;
/**
* @internal
*/
final class IniUtil
{
/**
* Convert a ini like size to a numeric size in bytes.
*
* @param string $size
* @return int
*/
public static function iniSizeToBytes($size)
{
if (\is_numeric($size)) {
return (int)$size;
}
// ini size values might be specified via environment variables, e.g.: memory_limit=${PHP_MEMORY_LIMIT}
$matches = [];
$envVarUsed = preg_match('#^\$(?|(\w+)|\{(\w+)})$#', $size, $matches);
$envVarName = $matches[1] ?? false;
if ($envVarUsed && $envVarName && array_key_exists($envVarName, $_ENV)) {
$size = (string)$_ENV[$envVarName];
}
$suffix = \strtoupper(\substr($size, -1));
$strippedSize = \substr($size, 0, -1);
if (!\is_numeric($strippedSize)) {
throw new \InvalidArgumentException("$size is not a valid ini size");
}
if ($strippedSize <= 0) {
throw new \InvalidArgumentException("Expect $size to be higher isn't zero or lower");
}
if ($suffix === 'K') {
return $strippedSize * 1024;
}
if ($suffix === 'M') {
return $strippedSize * 1024 * 1024;
}
if ($suffix === 'G') {
return $strippedSize * 1024 * 1024 * 1024;
}
if ($suffix === 'T') {
return $strippedSize * 1024 * 1024 * 1024 * 1024;
}
return (int)$size;
}
}