Repository navigation
Expand file tree
/
Copy pathPcreRuntimeInfo.php
More file actions
98 lines (84 loc) · 2.44 KB
/
Copy pathPcreRuntimeInfo.php
File metadata and controls
98 lines (84 loc) · 2.44 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
<?php
declare(strict_types=1);
/*
* This file is part of the PHPRegex package.
*
* (c) Younes ENNAJI <younes.ennaji.pro@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PHPRegex\Cli;
use PHPRegex\Parser\Engine\IniFlag;
use PHPRegex\Parser\Text\Ascii;
/**
* @internal
*/
final class PcreRuntimeInfo implements \JsonSerializable
{
public function __construct(
public readonly string $version,
public readonly ?string $jitSetting,
public readonly ?int $backtrackLimit,
public readonly ?int $recursionLimit,
) {}
public static function fromIni(): self
{
$version = 'unknown';
if (\defined('PCRE_VERSION')) {
$version = (string) \PCRE_VERSION;
} elseif (false !== ($pcreVersion = phpversion('pcre'))) {
$version = (string) $pcreVersion;
}
return new self(
$version,
self::iniValue('pcre.jit'),
self::iniInt('pcre.backtrack_limit'),
self::iniInt('pcre.recursion_limit'),
);
}
/**
* @return array{
* version: string,
* jit: bool|null,
* backtrack_limit: int|null,
* recursion_limit: int|null,
* }
*/
public function jsonSerialize(): array
{
return [
'version' => $this->version,
'jit' => null === $this->jitSetting ? null : IniFlag::isOn($this->jitSetting),
'backtrack_limit' => $this->backtrackLimit,
'recursion_limit' => $this->recursionLimit,
];
}
/**
* The setting, or null when it is unknown or ini_get() is disabled.
*/
private static function iniValue(string $key): ?string
{
if (!\function_exists('ini_get')) {
// Reached only where ini_get() is disabled; the tests run that case in a child PHP process.
return null;
}
$value = ini_get($key);
if (false === $value) {
return null;
}
return (string) $value;
}
private static function iniInt(string $key): ?int
{
$value = self::iniValue($key);
if (null === $value) {
return null;
}
$value = trim((string) $value, " \t\n\r\0\x0B");
if ('' === $value || !Ascii::isDigit($value)) {
return null;
}
return (int) $value;
}
}