Sorted, deduped table names referenced by the * SQL statement. Empty when the statement is * DDL, empty, or unparseable. */ public static function fromSql(string $sql): array { $trimmed = ltrim($sql); if ($trimmed === '') { return []; } $prefix = strtolower(substr($trimmed, 0, 6)); $matched = false; foreach (self::DML_PREFIXES as $dml) { if (str_starts_with($prefix, $dml)) { $matched = true; break; } } if (! $matched) { return []; } // Match `from`, `into`, `update`, `join` and capture the // following identifier, tolerating the common quoting // styles: "double", `back`, [bracket], or bare. $pattern = '/(?:\bfrom|\binto|\bupdate|\bjoin)\s+(?:"([^"]+)"|`([^`]+)`|\[([^\]]+)\]|(\w+))/i'; if (preg_match_all($pattern, $sql, $matches) === false) { return []; } $tables = []; for ($i = 0, $n = count($matches[0]); $i < $n; $i++) { $name = $matches[1][$i] !== '' ? $matches[1][$i] : ($matches[2][$i] !== '' ? $matches[2][$i] : ($matches[3][$i] !== '' ? $matches[3][$i] : $matches[4][$i])); if ($name === '') { continue; } if (self::isSchemaMeta($name)) { continue; } $tables[strtolower($name)] = true; } $out = array_keys($tables); sort($out); return $out; } /** * @return list Table names referenced by `Schema::` calls * in the given migration file contents. Empty * when nothing matches — callers treat that * as "fall back to the broad watch pattern". */ public static function fromMigrationSource(string $php): array { $pattern = '/Schema::\s*(?:create|table|drop|dropIfExists|dropColumns|rename)\s*\(\s*[\'"]([^\'"]+)[\'"](?:\s*,\s*[\'"]([^\'"]+)[\'"])?/'; if (preg_match_all($pattern, $php, $matches) === false) { return []; } $tables = []; foreach ($matches[1] as $i => $primary) { // Group 1 always captures at least one char per the regex. $tables[strtolower($primary)] = true; // Group 2 (`Schema::rename('old', 'new')`) is optional and // absent from non-rename matches. $secondary = $matches[2][$i] ?? ''; if ($secondary !== '') { $tables[strtolower($secondary)] = true; } } $out = array_keys($tables); sort($out); return $out; } /** * Filters out driver-internal tables that show up as DB::listen * targets without representing user schema: SQLite's master * catalogue, Laravel's own `migrations` metadata. */ private static function isSchemaMeta(string $name): bool { $lower = strtolower($name); return in_array($lower, ['sqlite_master', 'sqlite_sequence', 'migrations'], true) || str_starts_with($lower, 'pg_') || str_starts_with($lower, 'information_schema'); } }