-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsync-schema.php
More file actions
173 lines (141 loc) · 4.71 KB
/
sync-schema.php
File metadata and controls
173 lines (141 loc) · 4.71 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
<?php
declare(strict_types=1);
use Ecourier\GoblValidator\GoblValidator;
require __DIR__ . '/vendor/autoload.php';
$version = GoblValidator::$GOBL_VERSION;
$repo = "invopop/gobl";
$zipUrl = "https://github.com/$repo/archive/refs/tags/v$version.zip";
$tmpZip = __DIR__ . "/.gobl.zip";
$tmpDir = __DIR__ . "/.gobl-sync";
$dstDir = __DIR__ . "/schemas";
echo "Using GOBL version: $version\n";
echo "Downloading: $zipUrl ...\n";
// ------------------------------------------------------------
// 1. Download the ZIP archive
// ------------------------------------------------------------
$data = @file_get_contents($zipUrl);
if ($data === false) {
fwrite(STDERR, "Error: Could not download $zipUrl\n");
exit(1);
}
file_put_contents($tmpZip, $data);
// ------------------------------------------------------------
// 2. Extract ZIP
// ------------------------------------------------------------
$zip = new ZipArchive();
if ($zip->open($tmpZip) !== true) {
fwrite(STDERR, "Cannot open downloaded ZIP file\n");
exit(1);
}
$zip->extractTo($tmpDir);
$zip->close();
$goblRoot = glob("$tmpDir/gobl-$version")[0] ?? null;
$schemasPath = "$goblRoot/data/schemas";
if (!$goblRoot) {
fwrite(STDERR, "Could not locate extracted folder gobl-$version\n");
exit(1);
}
if (!is_dir($schemasPath)) {
fwrite(STDERR, "Schema directory not found: $schemasPath\n");
exit(1);
}
// ------------------------------------------------------------
// 3. Replace local schemas/
// ------------------------------------------------------------
if (is_dir($dstDir)) {
echo "Clearing existing schemas directory...\n";
deleteDirectory($dstDir);
}
mkdir($dstDir, 0777, true);
echo "Copying schemas...\n";
recurseCopy($schemasPath, $dstDir);
echo "Adding additionalProperties: false to all object definitions...\n";
addAdditionalPropertiesFalse($dstDir);
deleteDirectory($tmpDir);
unlink($tmpZip);
echo "Schemas updated successfully for GOBL version $version\n";
// ------------------------------------------------------------
// Helper functions
// ------------------------------------------------------------
function deleteDirectory(string $dir): void
{
if (!is_dir($dir)) return;
foreach (scandir($dir) as $item) {
if ($item === '.' || $item === '..') continue;
$path = "$dir/$item";
if (is_dir($path)) {
deleteDirectory($path);
} else {
unlink($path);
}
}
rmdir($dir);
}
function recurseCopy(string $src, string $dst): void
{
@mkdir($dst, 0777, true);
foreach (scandir($src) as $item) {
if ($item === '.' || $item === '..') continue;
$srcPath = "$src/$item";
$dstPath = "$dst/$item";
if (is_dir($srcPath)) {
recurseCopy($srcPath, $dstPath);
} else {
copy($srcPath, $dstPath);
}
}
}
/**
* Recursively process all JSON schema files in a directory,
* adding "additionalProperties": false to all object definitions
* that have "properties" defined.
*/
function addAdditionalPropertiesFalse(string $dir): void
{
foreach (scandir($dir) as $item) {
if ($item === '.' || $item === '..') continue;
$path = "$dir/$item";
if (is_dir($path)) {
addAdditionalPropertiesFalse($path);
} elseif (str_ends_with($item, '.json')) {
$content = file_get_contents($path);
$schema = json_decode($content, true);
if ($schema !== null) {
$modified = processSchemaNode($schema);
file_put_contents($path, json_encode($modified, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES) . "\n");
}
}
}
}
/**
* Recursively process a schema node, adding additionalProperties: false
* to any object that has "type": "object" and "properties" defined.
*/
function processSchemaNode(array $node): array
{
// If this node is an object type with properties, add additionalProperties: false
if (
isset($node['type']) &&
$node['type'] === 'object' &&
isset($node['properties']) &&
!isset($node['additionalProperties'])
) {
// Allow $schema property (used by GOBL to identify the schema type)
if (!isset($node['properties']['$schema'])) {
$node['properties']['$schema'] = [
'type' => 'string',
'format' => 'uri',
'title' => 'Schema',
'description' => 'Schema identifier for the GOBL document type.',
];
}
$node['additionalProperties'] = false;
}
// Recursively process all nested structures
foreach ($node as $key => $value) {
if (is_array($value)) {
$node[$key] = processSchemaNode($value);
}
}
return $node;
}