-
Notifications
You must be signed in to change notification settings - Fork 138
Expand file tree
/
Copy pathutils.php
More file actions
117 lines (98 loc) · 3.35 KB
/
utils.php
File metadata and controls
117 lines (98 loc) · 3.35 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<?php
define('EXTENSIONS_INSTALL_DIR', '/usr/local/lib/thecodingmachine-php/extensions/current/');
/**
* Returns the list of compiled and available extensions as .so files.
*
* @return string[]
*/
function getAvailableExtensions(): array
{
//return array_map(function(string $fileName) { return basename($fileName);}, glob('/var/lib/php/modules/'.getenv('PHP_VERSION').'/registry/*'));
return array_map(function(string $fileName) { return preg_replace('/\.ini$/i', '', basename($fileName));}, glob('/etc/php/'.getenv('PHP_VERSION').'/mods-available/*.ini'));
}
/**
* Returns the list of extensions that can be compiled
*
* @return string[]
*/
function getCompilableExtensions(): array
{
return array_map('basename', glob(EXTENSIONS_INSTALL_DIR.'*', GLOB_ONLYDIR));
}
/**
* Returns the list of extensions to enable based on the PHP_EXTENSIONS env variable
*
* @return array<int, string>
*/
function getExtensions(): array
{
// Note: we cannot check the PHP_EXTENSION_XXX variables because it would create ~60 ONBUILD ARG PHP_EXTENSION_XXX lines
// that create too many layers.
//$toCheckExtensions = array_merge(getDeclaredEnvVars(), getPhpExtensionsEnvVar());
//return array_filter($toCheckExtensions, 'enableExtension');
return array_filter(getPhpExtensionsEnvVar(), 'enableExtension');
}
/**
* Returns a list of extensions available in PHP_EXTENSION_XXX env variables.
*
* @return array<int, string>
*/
function getDeclaredEnvVars(): array
{
$array = [];
foreach ($_SERVER as $key => $value) {
if (strpos($key, 'PHP_EXTENSION_') === 0) {
$array[] = strtolower(substr($key, 14));
}
}
return $array;
}
/**
* Returns a list of extensions available in the PHP_EXTENSIONS environment variable
*
* @return array<int, string>
*/
function getPhpExtensionsEnvVar(): array
{
static $phpExtensions = null;
if ($phpExtensions !== null) {
return $phpExtensions;
}
$delimiter = [',', '|', ';', ':'];
$replace = str_replace($delimiter, ' ', getenv('PHP_EXTENSIONS'));
$phpExtensions = explode(' ', $replace);
$phpExtensions = array_map('trim', $phpExtensions);
$phpExtensions = array_map('strtolower', $phpExtensions);
$phpExtensions = array_filter($phpExtensions);
return $phpExtensions;
}
function getPhpVersionEnvVar()
{
static $phpVersion = null;
if ($phpVersion !== null) {
return $phpVersion;
}
$phpVersion = getenv('PHP_VERSION');
return $phpVersion;
}
function enableExtension(string $extensionName): bool {
$phpExtensions = getPhpExtensionsEnvVar();
// If an extension name is set explicitly to "0" or "false" or "no", then it is not enabled.
// This has priority
$envName = 'PHP_EXTENSION_'.strtoupper($extensionName);
$env = strtolower(trim(getenv($envName)));
if ($env === '0' || $env === 'false' || $env === 'no' || $env === 'off') {
return false;
}
if (in_array($extensionName, $phpExtensions, true)) {
return true;
}
if ($env == '') {
return false;
}
if ($env === '1' || $env === 'true' || $env === 'yes' || $env === 'on') {
return true;
}
file_put_contents('php://stderr', 'Invalid environment variable value found for '.$envName.'. Value: "'.$env.'". Valid values are "0", "1", "yes", "no", "true", "false", "on", "off".'."\n");
exit(1);
}