|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Nowo\BeaconBundle\Context; |
| 6 | + |
| 7 | +use PDOException; |
| 8 | +use Throwable; |
| 9 | + |
| 10 | +use function is_array; |
| 11 | +use function is_int; |
| 12 | +use function is_object; |
| 13 | +use function is_string; |
| 14 | +use function mb_substr; |
| 15 | +use function method_exists; |
| 16 | +use function preg_match; |
| 17 | +use function preg_replace; |
| 18 | +use function strtolower; |
| 19 | +use function strtoupper; |
| 20 | +use function trim; |
| 21 | + |
| 22 | +/** |
| 23 | + * Best-effort SQL/SQLSTATE facts from a throwable chain for Envelope {@code contexts.db}. |
| 24 | + * |
| 25 | + * Does not require doctrine/dbal; uses {@see PDOException} and duck-typed DBAL APIs. |
| 26 | + */ |
| 27 | +final class DatabaseExceptionContext |
| 28 | +{ |
| 29 | + public const int MAX_SQL_LENGTH = 8192; |
| 30 | + |
| 31 | + /** |
| 32 | + * @return array<string, mixed>|null |
| 33 | + */ |
| 34 | + public static function fromThrowable(Throwable $throwable): ?array |
| 35 | + { |
| 36 | + $merged = []; |
| 37 | + $current = $throwable; |
| 38 | + while ($current instanceof Throwable) { |
| 39 | + $merged = self::merge($merged, self::fromOne($current)); |
| 40 | + $current = $current->getPrevious(); |
| 41 | + } |
| 42 | + |
| 43 | + if ($merged === []) { |
| 44 | + return null; |
| 45 | + } |
| 46 | + |
| 47 | + $out = ['type' => 'sql']; |
| 48 | + foreach (['sqlstate', 'code', 'driver', 'sql', 'sql_mode'] as $key) { |
| 49 | + if (isset($merged[$key]) && is_string($merged[$key]) && $merged[$key] !== '') { |
| 50 | + $out[$key] = $merged[$key]; |
| 51 | + } |
| 52 | + } |
| 53 | + if (isset($merged['bindings']) && is_array($merged['bindings']) && $merged['bindings'] !== []) { |
| 54 | + $out['bindings'] = $merged['bindings']; |
| 55 | + } |
| 56 | + |
| 57 | + if (!isset($out['sqlstate']) && !isset($out['code']) && !isset($out['sql'])) { |
| 58 | + return null; |
| 59 | + } |
| 60 | + |
| 61 | + return $out; |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * @return array<string, mixed> |
| 66 | + */ |
| 67 | + private static function fromOne(Throwable $throwable): array |
| 68 | + { |
| 69 | + $parsed = self::parseMessage($throwable->getMessage()); |
| 70 | + |
| 71 | + if ($throwable instanceof PDOException) { |
| 72 | + $info = $throwable->errorInfo; |
| 73 | + if (is_array($info)) { |
| 74 | + if (isset($info[0]) && is_string($info[0]) && $info[0] !== '' && $info[0] !== '00000') { |
| 75 | + $parsed['sqlstate'] = strtoupper($info[0]); |
| 76 | + } |
| 77 | + if (isset($info[1]) && (is_int($info[1]) || is_string($info[1]))) { |
| 78 | + $code = trim((string) $info[1]); |
| 79 | + if ($code !== '') { |
| 80 | + $parsed['code'] = $code; |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + $parsed['driver'] = $parsed['driver'] ?? 'pdo'; |
| 85 | + } |
| 86 | + |
| 87 | + if (method_exists($throwable, 'getSQLState')) { |
| 88 | + $state = $throwable->getSQLState(); |
| 89 | + if (is_string($state) && $state !== '') { |
| 90 | + $parsed['sqlstate'] = strtoupper($state); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + if (method_exists($throwable, 'getQuery')) { |
| 95 | + $query = $throwable->getQuery(); |
| 96 | + if (is_string($query) && $query !== '') { |
| 97 | + $parsed['sql'] = self::scrubSql($query); |
| 98 | + } elseif (is_object($query) && method_exists($query, 'getSQL')) { |
| 99 | + $sql = $query->getSQL(); |
| 100 | + if (is_string($sql) && $sql !== '') { |
| 101 | + $parsed['sql'] = self::scrubSql($sql); |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + return $parsed; |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * @return array<string, mixed> |
| 111 | + */ |
| 112 | + private static function parseMessage(string $message): array |
| 113 | + { |
| 114 | + $out = []; |
| 115 | + if (preg_match('/SQLSTATE\[([A-Z0-9]{5})\]/i', $message, $m) === 1) { |
| 116 | + $out['sqlstate'] = strtoupper($m[1]); |
| 117 | + } |
| 118 | + if (preg_match('/SQLSTATE\[[A-Z0-9]{5}\]\s*:[^:]*:\s*(\d+)/i', $message, $m) === 1) { |
| 119 | + $out['code'] = $m[1]; |
| 120 | + } elseif (preg_match('/\((\d{3,5}),\s*[\'"]([^\'"]+)[\'"]\)/', $message, $m) === 1) { |
| 121 | + $out['code'] = $m[1]; |
| 122 | + } |
| 123 | + if (preg_match('/sql_mode\s*=\s*[\'"]?([^\s,;\'")]+)/i', $message, $m) === 1) { |
| 124 | + $out['sql_mode'] = $m[1]; |
| 125 | + } |
| 126 | + if (preg_match('/Connection:\s*([a-z0-9_]+)/i', $message, $m) === 1) { |
| 127 | + $out['driver'] = strtolower($m[1]); |
| 128 | + } |
| 129 | + if (preg_match('/\(SQL:\s*(.+)\)\s*$/s', $message, $m) === 1 || preg_match('/,\s*SQL:\s*(.+)\)\s*$/s', $message, $m) === 1) { |
| 130 | + $out['sql'] = self::scrubSql(trim($m[1])); |
| 131 | + } |
| 132 | + |
| 133 | + return $out; |
| 134 | + } |
| 135 | + |
| 136 | + public static function scrubSql(string $sql): string |
| 137 | + { |
| 138 | + $collapsed = preg_replace('/\s+/', ' ', trim($sql)); |
| 139 | + $sql = is_string($collapsed) ? $collapsed : trim($sql); |
| 140 | + $scrubbed = preg_replace("/'([^'\\\\]|\\\\.)*'/", "'?'", $sql); |
| 141 | + $sql = is_string($scrubbed) ? $scrubbed : $sql; |
| 142 | + |
| 143 | + return mb_substr($sql, 0, self::MAX_SQL_LENGTH); |
| 144 | + } |
| 145 | + |
| 146 | + /** |
| 147 | + * @param array<string, mixed> $base |
| 148 | + * @param array<string, mixed> $overlay |
| 149 | + * |
| 150 | + * @return array<string, mixed> |
| 151 | + */ |
| 152 | + private static function merge(array $base, array $overlay): array |
| 153 | + { |
| 154 | + foreach ($overlay as $key => $value) { |
| 155 | + if ($value === null || $value === '') { |
| 156 | + continue; |
| 157 | + } |
| 158 | + $base[$key] = $value; |
| 159 | + } |
| 160 | + |
| 161 | + return $base; |
| 162 | + } |
| 163 | +} |
0 commit comments