7
7
use AvroSchemaParseException ;
8
8
use PhpKafka \PhpAvroSchemaGenerator \Avro \Avro ;
9
9
use PhpKafka \PhpAvroSchemaGenerator \Exception \SchemaMergerException ;
10
- use PhpKafka \PhpAvroSchemaGenerator \Exception \SchemaGenerationException ;
11
- use PhpKafka \PhpAvroSchemaGenerator \Exception \UnknownSchemaTypeException ;
12
10
use PhpKafka \PhpAvroSchemaGenerator \Registry \SchemaRegistryInterface ;
13
11
use PhpKafka \PhpAvroSchemaGenerator \Schema \SchemaTemplateInterface ;
14
12
15
13
final class SchemaMerger implements SchemaMergerInterface
16
14
{
17
-
18
15
/**
19
16
* @var string
20
17
*/
@@ -49,12 +46,15 @@ public function getOutputDirectory(): string
49
46
50
47
/**
51
48
* @param SchemaTemplateInterface $schemaTemplate
49
+ * @param bool $optimizeSubSchemaNamespaces
52
50
* @return SchemaTemplateInterface
53
51
* @throws AvroSchemaParseException
54
52
* @throws SchemaMergerException
55
53
*/
56
- public function getResolvedSchemaTemplate (SchemaTemplateInterface $ schemaTemplate ): SchemaTemplateInterface
57
- {
54
+ public function getResolvedSchemaTemplate (
55
+ SchemaTemplateInterface $ schemaTemplate ,
56
+ bool $ optimizeSubSchemaNamespaces = false
57
+ ): SchemaTemplateInterface {
58
58
$ definition = $ schemaTemplate ->getSchemaDefinition ();
59
59
60
60
do {
@@ -78,7 +78,8 @@ public function getResolvedSchemaTemplate(SchemaTemplateInterface $schemaTemplat
78
78
$ definition = $ this ->replaceSchemaIdWithDefinition (
79
79
$ definition ,
80
80
$ schemaId ,
81
- $ embeddedTemplate ->getSchemaDefinition ()
81
+ $ embeddedTemplate ->getSchemaDefinition (),
82
+ $ optimizeSubSchemaNamespaces
82
83
);
83
84
}
84
85
} while (true === $ exceptionThrown );
@@ -94,32 +95,40 @@ private function getSchemaIdFromExceptionMessage(string $exceptionMessage): stri
94
95
private function replaceSchemaIdWithDefinition (
95
96
string $ definition ,
96
97
string $ schemaId ,
97
- string $ embeddedDefinition
98
+ string $ embeddedDefinition ,
99
+ bool $ optimizeSubSchemaNamespaces = false
98
100
): string {
99
101
$ idString = '" ' . $ schemaId . '" ' ;
100
102
103
+ if (true === $ optimizeSubSchemaNamespaces ) {
104
+ $ embeddedDefinition = $ this ->excludeNamespacesForEmbeddedSchema ($ definition , $ embeddedDefinition );
105
+ }
106
+
101
107
$ pos = strpos ($ definition , $ idString );
102
108
103
109
return substr_replace ($ definition , $ embeddedDefinition , $ pos , strlen ($ idString ));
104
110
}
105
111
106
-
107
112
/**
108
- * @param boolean $prefixWithNamespace
109
- * @param boolean $useTemplateName
113
+ * @param bool $prefixWithNamespace
114
+ * @param bool $useTemplateName
115
+ * @param bool $optimizeSubSchemaNamespaces
110
116
* @return integer
111
117
* @throws AvroSchemaParseException
112
118
* @throws SchemaMergerException
113
119
*/
114
- public function merge (bool $ prefixWithNamespace = false , bool $ useTemplateName = false ): int
115
- {
120
+ public function merge (
121
+ bool $ prefixWithNamespace = false ,
122
+ bool $ useTemplateName = false ,
123
+ bool $ optimizeSubSchemaNamespaces = false
124
+ ): int {
116
125
$ mergedFiles = 0 ;
117
126
$ registry = $ this ->getSchemaRegistry ();
118
127
119
128
/** @var SchemaTemplateInterface $schemaTemplate */
120
129
foreach ($ registry ->getRootSchemas () as $ schemaTemplate ) {
121
130
try {
122
- $ resolvedTemplate = $ this ->getResolvedSchemaTemplate ($ schemaTemplate );
131
+ $ resolvedTemplate = $ this ->getResolvedSchemaTemplate ($ schemaTemplate, $ optimizeSubSchemaNamespaces );
123
132
} catch (SchemaMergerException $ e ) {
124
133
throw $ e ;
125
134
}
@@ -176,4 +185,26 @@ public function transformExportSchemaDefinition(array $schemaDefinition): array
176
185
177
186
return $ schemaDefinition ;
178
187
}
188
+
189
+ /**
190
+ * @param string $definition
191
+ * @param string $embeddedDefinition
192
+ * @return string
193
+ */
194
+ private function excludeNamespacesForEmbeddedSchema (string $ definition , string $ embeddedDefinition ): string
195
+ {
196
+ $ decodedRootDefinition = json_decode ($ definition , true );
197
+ $ decodedEmbeddedDefinition = json_decode ($ embeddedDefinition , true );
198
+
199
+ if (
200
+ isset ($ decodedRootDefinition ['namespace ' ]) && isset ($ decodedEmbeddedDefinition ['namespace ' ]) &&
201
+ $ decodedRootDefinition ['namespace ' ] === $ decodedEmbeddedDefinition ['namespace ' ]
202
+ ) {
203
+ unset($ decodedEmbeddedDefinition ['namespace ' ]);
204
+ /** @var string $embeddedDefinition */
205
+ $ embeddedDefinition = json_encode ($ decodedEmbeddedDefinition );
206
+ }
207
+
208
+ return $ embeddedDefinition ;
209
+ }
179
210
}
0 commit comments