|
| 1 | +<?php |
| 2 | +header('Content-Type: application/json'); |
| 3 | +header('Access-Control-Allow-Origin: *'); // Allow calls from your WP site |
| 4 | +header('Access-Control-Allow-Methods: POST'); |
| 5 | +header('Access-Control-Allow-Headers: Content-Type'); |
| 6 | + |
| 7 | +if ($_SERVER['REQUEST_METHOD'] === 'POST') { |
| 8 | + $input = json_decode(file_get_contents('php://input'), true); |
| 9 | + $code = $input['code'] ?? ''; |
| 10 | + $rules = $input['rules'] ?? ''; |
| 11 | + |
| 12 | + if (empty($code)) { |
| 13 | + http_response_code(400); |
| 14 | + echo json_encode(['error' => 'No code provided']); |
| 15 | + exit; |
| 16 | + } |
| 17 | + |
| 18 | + // Save code to temp file |
| 19 | + $tempCodeFile = sys_get_temp_dir() . '/' . uniqid() . '.java'; |
| 20 | + file_put_contents($tempCodeFile, $code); |
| 21 | + |
| 22 | + // Save rules to temp XML |
| 23 | + $tempRulesFile = sys_get_temp_dir() . '/' . uniqid() . '.xml'; |
| 24 | + file_put_contents($tempRulesFile, $rules ?: 'rulesets/java/quickstart.xml'); // Fallback to basic rules if none provided |
| 25 | + |
| 26 | + // Run PMD via shell_exec |
| 27 | + $pmdOutput = shell_exec('pmd check -d ' . escapeshellarg($tempCodeFile) . ' -R ' . escapeshellarg($tempRulesFile) . ' -f json 2>&1'); |
| 28 | + |
| 29 | + // Cleanup |
| 30 | + unlink($tempCodeFile); |
| 31 | + unlink($tempRulesFile); |
| 32 | + |
| 33 | + if (empty($pmdOutput)) { |
| 34 | + echo json_encode(['violations' => []]); |
| 35 | + } else { |
| 36 | + $issues = json_decode($pmdOutput, true); |
| 37 | + $violations = $issues['files'][0]['violations'] ?? []; |
| 38 | + echo json_encode(['violations' => $violations]); |
| 39 | + } |
| 40 | +} else { |
| 41 | + http_response_code(405); |
| 42 | + echo json_encode(['error' => 'Method not allowed']); |
| 43 | +} |
| 44 | +?> |
0 commit comments