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
|
<?php
declare(strict_types=1);
$state = getenv('CEPIS_TEST_STATE');
if ($state === false) {
http_response_code(500);
echo 'CEPIS_TEST_STATE is not set';
return true;
}
$uri = $_SERVER['REQUEST_URI'] ?? '/';
file_put_contents($state . '/calls.log', $uri . "\n", FILE_APPEND | LOCK_EX);
$routes = json_decode((string) file_get_contents($state . '/routes.json'), true);
$routes = is_array($routes) ? $routes : [];
$specs = $routes[$uri] ?? $routes['*'] ?? null;
if (!is_array($specs) || $specs === []) {
header('Content-Type: application/json; charset=utf-8');
http_response_code(404);
echo json_encode(['erro' => 'cep_nao_encontrado', 'mensagem' => 'no route registered']);
return true;
}
$calls = array_filter(explode("\n", (string) file_get_contents($state . '/calls.log')), 'strlen');
$seen = count(array_filter($calls, static fn (string $line): bool => $line === $uri));
$spec = $specs[min(max($seen - 1, 0), count($specs) - 1)];
if (($spec['kind'] ?? 'static') === 'csv_echo') {
parse_str((string) parse_url($uri, PHP_URL_QUERY), $query);
$ceps = array_filter(explode(',', (string) ($query['ceps'] ?? '')), 'strlen');
$rows = ['cep,logradouro,complemento,unidade,bairro,localidade,uf,estado,regiao,ibge,gia,ddd,siafi'];
foreach ($ceps as $cep) {
$rows[] = $cep . ',Rua Teste,,,Centro,Cidade,SP,São Paulo,Sudeste,1,,11,1';
}
header('Content-Type: text/csv; charset=utf-8');
http_response_code(200);
echo implode("\n", $rows) . "\n";
return true;
}
foreach (($spec['headers'] ?? []) as $name => $value) {
header($name . ': ' . $value);
}
header('Content-Type: ' . ($spec['content_type'] ?? 'text/plain; charset=utf-8'));
http_response_code((int) ($spec['status'] ?? 200));
echo (string) ($spec['body'] ?? '');
return true;
|