-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMigrationStatus.php
More file actions
113 lines (97 loc) · 4.23 KB
/
Copy pathMigrationStatus.php
File metadata and controls
113 lines (97 loc) · 4.23 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
<?php
declare(strict_types=1);
/**
* @return array{html: string, count: int, error: bool, no_migrations?: bool, no_db?: bool}
*/
function getMigrationsStatus(string $targetDir): array
{
global $lang;
/** @var array<string, string> $langForMigrations */
$langForMigrations = (isset($lang) && is_array($lang)) ? $lang : [];
$console = rtrim($targetDir, '/').'/bin/console';
if (!file_exists($console)) {
return ['html' => 'bin/console not found', 'count' => 0, 'error' => true];
}
$migrationsDir = rtrim($targetDir, '/').'/migrations';
$foundMigrations = glob($migrationsDir.'/*.php');
$hasMigrations = is_dir($migrationsDir) && false !== $foundMigrations && count($foundMigrations) > 0;
if (!$hasMigrations) {
return [
'html' => '<span style="color:#6a737d;">'.resolveLangKey('no_migrations_found', $langForMigrations).'</span>',
'count' => 0,
'error' => false,
'no_migrations' => true,
];
}
$cmd = 'php '.escapeshellarg($console).' doctrine:migrations:status --no-interaction 2>&1';
$output = (string) shell_exec($cmd);
// Try to find the line with "New Migrations"
if (preg_match('/New Migrations:\s+(\d+)/i', $output, $matches)) {
$count = (int) $matches[1];
if ($count > 0) {
return [
'html' => '<span style="color:#d73a49; font-weight:bold;">'.$count.' pending</span>',
'count' => $count,
'error' => false,
];
}
return [
'html' => '<span style="color:#28a745; font-weight:bold;">'.resolveLangKey('no_migrations_to_execute', $langForMigrations).'</span>',
'count' => 0,
'error' => false,
];
}
// Handle errors: extract message from JSON if possible, or just take first line
$trimmedOutput = trim((string) $output);
if (str_starts_with($trimmedOutput, '{')) {
$json = json_decode($trimmedOutput, true);
if (is_array($json) && isset($json['message']) && is_scalar($json['message'])) {
$msg = (string) $json['message'];
// If it's a long message with "Message: ...", try to extract the inner message
if (preg_match('/Message: "(.*?)"/s', $msg, $m)) {
$msg = (string) $m[1];
}
if (str_contains($msg, 'could not find driver') || str_contains($msg, 'Connection refused')) {
return [
'html' => '<span style="color:#6a737d;">'.resolveLangKey('migrations_disabled_no_db', $langForMigrations).'</span>',
'count' => 0,
'error' => false,
'no_db' => true,
];
}
$errorMsg = (string) strtok($msg, "\n");
return [
'html' => '<span style="color:#d73a49; font-size:0.9em;">Error: '.htmlspecialchars($errorMsg).'</span>',
'count' => 0,
'error' => true,
];
}
}
// Fallback: take first non-empty line
$lines = explode("\n", $trimmedOutput);
foreach ($lines as $line) {
$line = trim($line);
if ('' !== $line && !str_contains($line, 'CRITICAL') && !str_contains($line, 'DEBUG')) {
// Check for common Doctrine/PDO errors to make them compact
if (str_contains($line, 'ExceptionConverter.php') || str_contains($line, 'Connection refused') || str_contains($line, 'could not find driver')) {
return [
'html' => '<span style="color:#6a737d;">'.resolveLangKey('migrations_disabled_no_db', $langForMigrations).'</span>',
'count' => 0,
'error' => false,
'no_db' => true,
];
}
return [
'html' => '<span style="color:#d73a49; font-size:0.9em;">Error: '.htmlspecialchars($line).'</span>',
'count' => 0,
'error' => true,
];
}
}
$errorMsgFallback = (string) strtok($trimmedOutput, "\n");
return [
'html' => '<span style="color:#d73a49; font-size:0.9em;">Error: '.htmlspecialchars($errorMsgFallback).'</span>',
'count' => 0,
'error' => true,
];
}