-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtrafic.php
More file actions
95 lines (80 loc) · 2.62 KB
/
trafic.php
File metadata and controls
95 lines (80 loc) · 2.62 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
<?php
// Parcourt récursivement le dossier `logo/` et fusionne tous les
// fichiers nommés `trafic.json` en une seule sortie JSON.
// Définir le répertoire de base (par défaut : dossier `logo` à côté du script)
$baseDir = __DIR__ . DIRECTORY_SEPARATOR . 'logo';
if (php_sapi_name() === 'cli') {
global $argv;
if (isset($argv[1]) && $argv[1] !== '') {
$baseDir = $argv[1];
}
}
if (!is_dir($baseDir)) {
$msg = "Directory not found: $baseDir\n";
if (php_sapi_name() === 'cli') {
fwrite(STDERR, $msg);
} else {
header('HTTP/1.1 500 Internal Server Error');
echo $msg;
}
exit(2);
}
// If run from a web server, send JSON headers for download
if (php_sapi_name() !== 'cli') {
header('Content-Type: application/json; charset=utf-8');
header('Content-Disposition: attachment; filename="trafic.json"');
}
$it = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($baseDir, RecursiveDirectoryIterator::SKIP_DOTS));
$merged = [];
function is_list_array(array $arr): bool {
return array_keys($arr) === range(0, count($arr) - 1);
}
foreach ($it as $file) {
if (!$file->isFile()) continue;
if (strtolower($file->getFilename()) !== 'trafic.json') continue;
$path = $file->getPathname();
$contents = file_get_contents($path);
if ($contents === false) continue;
// Remove UTF-8 BOM if present
$contents = preg_replace('/^\xEF\xBB\xBF/', '', $contents);
$data = json_decode($contents, true);
if (json_last_error() !== JSON_ERROR_NONE) {
// Skip files with invalid JSON
if (php_sapi_name() === 'cli') {
fwrite(STDERR, "Warning: skipping invalid JSON file: $path\n");
}
continue;
}
if (is_array($data)) {
// If the decoded value is a list (array of items), merge its elements
if (is_list_array($data)) {
foreach ($data as $item) {
$merged[] = $item;
}
} else {
// Associative array / object -> add as single entry
$merged[] = $data;
}
} else {
// Scalars (unlikely) -> add as-is
$merged[] = $data;
}
}
// Sort merged entries by `companyId` ascending to have deterministic output order
usort($merged, function ($a, $b) {
$aId = (is_array($a) && isset($a['companyId'])) ? $a['companyId'] : '';
$bId = (is_array($b) && isset($b['companyId'])) ? $b['companyId'] : '';
return strcmp($aId, $bId);
});
$output = json_encode($merged, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT);
if ($output === false) {
if (php_sapi_name() === 'cli') {
fwrite(STDERR, "Error encoding merged JSON: " . json_last_error_msg() . "\n");
exit(3);
} else {
header('HTTP/1.1 500 Internal Server Error');
echo "Error encoding merged JSON: " . json_last_error_msg();
exit(3);
}
}
echo $output;