77use AvroSchemaParseException ;
88use PhpKafka \PhpAvroSchemaGenerator \Avro \Avro ;
99use PhpKafka \PhpAvroSchemaGenerator \Exception \SchemaMergerException ;
10- use PhpKafka \PhpAvroSchemaGenerator \Exception \SchemaGenerationException ;
11- use PhpKafka \PhpAvroSchemaGenerator \Exception \UnknownSchemaTypeException ;
1210use PhpKafka \PhpAvroSchemaGenerator \Registry \SchemaRegistryInterface ;
1311use PhpKafka \PhpAvroSchemaGenerator \Schema \SchemaTemplateInterface ;
1412
1513final class SchemaMerger implements SchemaMergerInterface
1614{
17-
1815 /**
1916 * @var string
2017 */
@@ -49,12 +46,15 @@ public function getOutputDirectory(): string
4946
5047 /**
5148 * @param SchemaTemplateInterface $schemaTemplate
49+ * @param bool $optimizeSubSchemaNamespaces
5250 * @return SchemaTemplateInterface
5351 * @throws AvroSchemaParseException
5452 * @throws SchemaMergerException
5553 */
56- public function getResolvedSchemaTemplate (SchemaTemplateInterface $ schemaTemplate ): SchemaTemplateInterface
57- {
54+ public function getResolvedSchemaTemplate (
55+ SchemaTemplateInterface $ schemaTemplate ,
56+ bool $ optimizeSubSchemaNamespaces = false
57+ ): SchemaTemplateInterface {
5858 $ definition = $ schemaTemplate ->getSchemaDefinition ();
5959
6060 do {
@@ -78,7 +78,8 @@ public function getResolvedSchemaTemplate(SchemaTemplateInterface $schemaTemplat
7878 $ definition = $ this ->replaceSchemaIdWithDefinition (
7979 $ definition ,
8080 $ schemaId ,
81- $ embeddedTemplate ->getSchemaDefinition ()
81+ $ embeddedTemplate ->getSchemaDefinition (),
82+ $ optimizeSubSchemaNamespaces
8283 );
8384 }
8485 } while (true === $ exceptionThrown );
@@ -94,32 +95,40 @@ private function getSchemaIdFromExceptionMessage(string $exceptionMessage): stri
9495 private function replaceSchemaIdWithDefinition (
9596 string $ definition ,
9697 string $ schemaId ,
97- string $ embeddedDefinition
98+ string $ embeddedDefinition ,
99+ bool $ optimizeSubSchemaNamespaces = false
98100 ): string {
99101 $ idString = '" ' . $ schemaId . '" ' ;
100102
103+ if (true === $ optimizeSubSchemaNamespaces ) {
104+ $ embeddedDefinition = $ this ->excludeNamespacesForEmbeddedSchema ($ definition , $ embeddedDefinition );
105+ }
106+
101107 $ pos = strpos ($ definition , $ idString );
102108
103109 return substr_replace ($ definition , $ embeddedDefinition , $ pos , strlen ($ idString ));
104110 }
105111
106-
107112 /**
108- * @param boolean $prefixWithNamespace
109- * @param boolean $useTemplateName
113+ * @param bool $prefixWithNamespace
114+ * @param bool $useTemplateName
115+ * @param bool $optimizeSubSchemaNamespaces
110116 * @return integer
111117 * @throws AvroSchemaParseException
112118 * @throws SchemaMergerException
113119 */
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 {
116125 $ mergedFiles = 0 ;
117126 $ registry = $ this ->getSchemaRegistry ();
118127
119128 /** @var SchemaTemplateInterface $schemaTemplate */
120129 foreach ($ registry ->getRootSchemas () as $ schemaTemplate ) {
121130 try {
122- $ resolvedTemplate = $ this ->getResolvedSchemaTemplate ($ schemaTemplate );
131+ $ resolvedTemplate = $ this ->getResolvedSchemaTemplate ($ schemaTemplate, $ optimizeSubSchemaNamespaces );
123132 } catch (SchemaMergerException $ e ) {
124133 throw $ e ;
125134 }
@@ -176,4 +185,26 @@ public function transformExportSchemaDefinition(array $schemaDefinition): array
176185
177186 return $ schemaDefinition ;
178187 }
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+ }
179210}
0 commit comments