-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathps_base.php
More file actions
108 lines (88 loc) · 3.03 KB
/
Copy pathps_base.php
File metadata and controls
108 lines (88 loc) · 3.03 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
<?php
namespace Deployer;
set('sitehost_client_id', '969806');
set('php_config', []);
/**
* Synchronise optional per-host PHP settings without rewriting php.ini when
* the configured values already match.
*
* Example host config:
* ->set('php_config', ['post_max_size' => '100M'])
*/
desc('Synchronise optional PHP configuration');
task('sitehost:sync-config', function () {
$phpConfig = get('php_config');
if (empty($phpConfig)) {
writeln('<comment>No php_config set - skipping PHP config sync</comment>');
return;
}
if (!is_array($phpConfig)) {
throw new \InvalidArgumentException('php_config must be an associative array.');
}
foreach ($phpConfig as $key => $value) {
if (!is_string($key) || !preg_match('/^[A-Za-z0-9_.-]+$/', $key)) {
throw new \InvalidArgumentException('php_config contains an invalid directive name.');
}
if (!is_scalar($value)) {
throw new \InvalidArgumentException("php_config value for {$key} must be a scalar.");
}
$phpConfig[$key] = (string) $value;
}
$iniPath = '/container/config/php/php.ini';
$encodedConfig = base64_encode(json_encode($phpConfig, JSON_THROW_ON_ERROR));
$syncScript = <<<'PHP'
$path = $argv[1];
$settings = json_decode(base64_decode($argv[2]), true);
$contents = is_file($path) ? file_get_contents($path) : '';
if ($contents === false) {
fwrite(STDERR, "Unable to read {$path}\n");
exit(1);
}
$updated = $contents;
$newline = strpos($contents, "\r\n") !== false ? "\r\n" : "\n";
foreach ($settings as $key => $value) {
$pattern = '/^[ \t]*' . preg_quote($key, '/') . '[ \t]*=[ \t]*(.*?)[ \t]*$/m';
preg_match_all($pattern, $updated, $matches);
$currentValue = empty($matches[1]) ? null : trim(end($matches[1]));
if ($currentValue === $value) {
continue;
}
$line = $key . '=' . $value;
if ($currentValue !== null) {
$replacement = $line . ($newline === "\r\n" ? "\r" : '');
$updated = preg_replace_callback($pattern, function () use ($replacement) {
return $replacement;
}, $updated);
} else {
if ($updated !== '' && substr($updated, -1) !== "\n") {
$updated .= $newline;
}
$updated .= $line . $newline;
}
}
if ($updated === $contents) {
echo 'unchanged';
exit(0);
}
$directory = dirname($path);
if (!is_dir($directory) && !mkdir($directory, 0777, true) && !is_dir($directory)) {
fwrite(STDERR, "Unable to create {$directory}\n");
exit(1);
}
if (file_put_contents($path, $updated, LOCK_EX) === false) {
fwrite(STDERR, "Unable to write {$path}\n");
exit(1);
}
echo 'changed';
PHP;
$result = run(
'php -r ' . escapeshellarg($syncScript)
. ' ' . escapeshellarg($iniPath)
. ' ' . escapeshellarg($encodedConfig)
);
if (trim($result) === 'changed') {
writeln('<info>PHP config updated in ' . $iniPath . '</info>');
} else {
writeln('<info>PHP config already matches - skipping update</info>');
}
});