-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.php
More file actions
244 lines (192 loc) · 7.48 KB
/
test.php
File metadata and controls
244 lines (192 loc) · 7.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
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
<?php
declare(strict_types=1);
const PRODUCTION_EXTENSIONS = ['mysqli', 'pdo_mysql', 'pgsql', 'pdo_pgsql', 'bcmath', 'gd', 'imagick', 'imap', 'pcntl', 'zip', 'intl', 'exif', 'ftp', 'xml', 'pdo_sqlsrv', 'sqlsrv', 'sockets'];
const PRODUCTION_ONLY_EXTENSIONS = [];
const DEV_ONLY_EXTENSIONS = ['xdebug'];
const DEV_EXTENSIONS = [
...PRODUCTION_EXTENSIONS,
...DEV_ONLY_EXTENSIONS,
];
const PRODUCTION_BINARIES = ['php', 'composer', 'node', 'npm', 'pnpm', 'jpegoptim', 'optipng', 'pngquant', 'gifsicle', 'ffmpeg', 'svgo', 'avifenc', 'zsh'];
const PRODUCTION_ONLY_BINARIES = [];
const DEV_ONLY_BINARIES = ['gh', 'htop', 'nano', 'fzf', 'zoxide'];
const DEV_BINARIES = [
...PRODUCTION_BINARIES,
...DEV_ONLY_BINARIES,
];
class Colors
{
// Control codes
public const RESET = "\033[0m";
public const BOLD = "\033[1m";
public const DIM = "\033[2m";
// Standard colors
public const RED = "\033[31m";
public const GREEN = "\033[32m";
public const YELLOW = "\033[33m";
public const BLUE = "\033[34m";
public const MAGENTA = "\033[35m";
public const CYAN = "\033[36m";
public const WHITE = "\033[37m";
// Bright colors
public const BRIGHT_RED = "\033[91m";
public const BRIGHT_GREEN = "\033[92m";
public const BRIGHT_YELLOW = "\033[93m";
public const BRIGHT_BLUE = "\033[94m";
public const BRIGHT_MAGENTA = "\033[95m";
public const BRIGHT_CYAN = "\033[96m";
// Background colors
public const BG_RED = "\033[41m";
public const BG_GREEN = "\033[42m";
}
class Runner
{
private array $missingExtensions = [];
private array $missingBinaries = [];
private array $missingSanity = [];
public function __construct(public string $environment = 'production', private int $failures = 0) {}
public function printHeader(string $emoji, string $title): void
{
$envText = paint("[{$this->environment}]", Colors::BOLD . Colors::MAGENTA);
$header = paint("\n{$emoji} {$title}...", Colors::BOLD . Colors::BRIGHT_BLUE);
echo "{$envText} {$header}\n";
}
public function warn(string $message): void
{
$warnText = paint("[WARN]", Colors::BOLD . Colors::BRIGHT_YELLOW);
$messageText = paint($message, Colors::MAGENTA);
echo "{$warnText} {$messageText}\n";
}
public function check(string $name, bool $passed, ?string $hint = null): void
{
if ($passed) {
$statusText = paint("[OK]", Colors::BOLD . Colors::BRIGHT_GREEN);
$nameText = paint($name, Colors::CYAN);
echo "{$statusText} {$nameText}\n";
return;
}
$statusText = paint("[FAIL]", Colors::BOLD . Colors::BRIGHT_RED);
$nameText = paint($name, Colors::YELLOW);
$hintText = $hint ? paint(" - {$hint}", Colors::DIM . Colors::WHITE) : "";
echo "{$statusText} {$nameText}{$hintText}\n";
$this->failures++;
if (str_starts_with($name, 'extension:')) {
$this->missingExtensions[] = substr($name, 10);
} elseif (str_starts_with($name, 'binary:')) {
$this->missingBinaries[] = substr($name, 7);
} else {
$this->missingSanity[] = $name;
}
}
public function commandExists(string $command): bool
{
$output = @shell_exec('command -v ' . escapeshellarg($command) . ' 2>/dev/null');
if ($output === null) {
$output = @shell_exec('which ' . escapeshellarg($command) . ' 2>/dev/null');
}
return is_string($output) && trim($output) !== '';
}
public function finish(): never
{
if ($this->failures > 0) {
echo "\n";
if (!empty($this->missingExtensions)) {
$extList = paint(implode(', ', $this->missingExtensions), Colors::YELLOW);
echo paint("Missing extensions: ", Colors::BOLD . Colors::RED) . "[{$extList}]\n";
}
if (!empty($this->missingBinaries)) {
$binList = paint(implode(', ', $this->missingBinaries), Colors::YELLOW);
echo paint("Missing binaries: ", Colors::BOLD . Colors::RED) . "[{$binList}]\n";
}
if (!empty($this->missingSanity)) {
$sanityList = paint(implode(', ', $this->missingSanity), Colors::YELLOW);
echo paint("Failed sanity checks: ", Colors::BOLD . Colors::RED) . "[{$sanityList}]\n";
}
$message = "❌ Total failures: {$this->failures}";
echo "\n" . paint($message, Colors::BOLD . Colors::BG_RED . Colors::WHITE) . "\n";
exit(1);
}
echo "\n" . paint("🎉 All checks passed!", Colors::BOLD . Colors::BRIGHT_GREEN) . "\n";
exit(0);
}
}
function paint(string $text, string $color): string
{
if (!posix_isatty(STDOUT) && getenv('FORCE_COLOR') !== '1') {
return $text;
}
return $color . $text . Colors::RESET;
}
function extensions(Runner $runner): void
{
$runner->printHeader("🔍", "Checking PHP Extensions");
$extensions = $runner->environment === 'dev' ? DEV_EXTENSIONS : PRODUCTION_EXTENSIONS;
foreach ($extensions as $extension) {
$runner->check("extension:{$extension}", extension_loaded($extension));
}
if ($runner->environment === 'production') {
foreach (DEV_ONLY_EXTENSIONS as $extension) {
if (extension_loaded($extension)) {
$runner->warn("extension:{$extension} present (unexpected in production)");
}
}
} else {
foreach (PRODUCTION_ONLY_EXTENSIONS as $extension) {
if (extension_loaded($extension)) {
$runner->warn("extension:{$extension} present (unexpected in dev)");
}
}
}
}
function binaries(Runner $runner): void
{
$runner->printHeader("🛠️", "Checking CLI Tools & Binaries");
$binaries = $runner->environment === 'dev' ? DEV_BINARIES : PRODUCTION_BINARIES;
foreach ($binaries as $binary) {
$runner->check("binary:{$binary}", $runner->commandExists($binary));
}
if ($runner->environment === 'production') {
foreach (DEV_ONLY_BINARIES as $binary) {
if ($runner->commandExists($binary)) {
$runner->warn("binary:{$binary} present (unexpected in production)");
}
}
} else {
foreach (PRODUCTION_ONLY_BINARIES as $binary) {
if ($runner->commandExists($binary)) {
$runner->warn("binary:{$binary} present (unexpected in dev)");
}
}
}
}
function sanity(Runner $runner): void
{
$runner->printHeader("✅", "Running Sanity Checks");
$modules = @shell_exec('php -m 2>/dev/null');
$runner->check('php-cli works', is_string($modules) && trim($modules) !== '');
if ($runner->environment === 'dev') {
$pnpmStorePath = trim(@shell_exec('pnpm store path 2>/dev/null') ?? '');
$expectedPath = '/home/deploy/.pnpm-store';
$runner->check(
'pnpm store path',
str_starts_with($pnpmStorePath, $expectedPath),
"expected {$expectedPath}, got {$pnpmStorePath}"
);
}
}
function go(): never
{
global $argv;
$environment = $argv[1] ?? getenv('ENV') ?? 'production';
if (!in_array($environment, ['production', 'dev'], true)) {
echo "Usage: php test.php [production|dev]\n";
echo "Or set ENV environment variable\n";
exit(1);
}
$runner = new Runner($environment);
extensions($runner);
binaries($runner);
sanity($runner);
$runner->finish();
}
go();