77use AvroSchemaParseException ;
88use PhpKafka \PhpAvroSchemaGenerator \Avro \Avro ;
99use PhpKafka \PhpAvroSchemaGenerator \Exception \SchemaMergerException ;
10+ use PhpKafka \PhpAvroSchemaGenerator \Optimizer \OptimizerInterface ;
1011use PhpKafka \PhpAvroSchemaGenerator \Registry \SchemaRegistryInterface ;
1112use PhpKafka \PhpAvroSchemaGenerator \Schema \SchemaTemplateInterface ;
1213
@@ -22,6 +23,11 @@ final class SchemaMerger implements SchemaMergerInterface
2223 */
2324 private $ schemaRegistry ;
2425
26+ /**
27+ * @var OptimizerInterface[]
28+ */
29+ private $ optimizers = [];
30+
2531 public function __construct (SchemaRegistryInterface $ schemaRegistry , string $ outputDirectory = '/tmp ' )
2632 {
2733 $ this ->schemaRegistry = $ schemaRegistry ;
@@ -45,23 +51,20 @@ public function getOutputDirectory(): string
4551 }
4652
4753 /**
48- * @param SchemaTemplateInterface $schemaTemplate
49- * @param bool $optimizeSubSchemaNamespaces
54+ * @param SchemaTemplateInterface $rootSchemaTemplate
5055 * @return SchemaTemplateInterface
5156 * @throws AvroSchemaParseException
5257 * @throws SchemaMergerException
5358 */
54- public function getResolvedSchemaTemplate (
55- SchemaTemplateInterface $ schemaTemplate ,
56- bool $ optimizeSubSchemaNamespaces = false
57- ): SchemaTemplateInterface {
58- $ definition = $ schemaTemplate ->getSchemaDefinition ();
59+ public function getResolvedSchemaTemplate (SchemaTemplateInterface $ rootSchemaTemplate ): SchemaTemplateInterface
60+ {
61+ $ rootDefinition = $ rootSchemaTemplate ->getSchemaDefinition ();
5962
6063 do {
6164 $ exceptionThrown = false ;
6265
6366 try {
64- \AvroSchema::parse ($ definition );
67+ \AvroSchema::parse ($ rootDefinition );
6568 } catch (AvroSchemaParseException $ e ) {
6669 if (false === strpos ($ e ->getMessage (), ' is not a schema we know about. ' )) {
6770 throw $ e ;
@@ -75,16 +78,15 @@ public function getResolvedSchemaTemplate(
7578 );
7679 }
7780
78- $ definition = $ this ->replaceSchemaIdWithDefinition (
79- $ definition ,
81+ $ rootDefinition = $ this ->replaceSchemaIdWithDefinition (
82+ $ rootDefinition ,
8083 $ schemaId ,
81- $ embeddedTemplate ->getSchemaDefinition (),
82- $ optimizeSubSchemaNamespaces
84+ $ embeddedTemplate ->getSchemaDefinition ()
8385 );
8486 }
8587 } while (true === $ exceptionThrown );
8688
87- return $ schemaTemplate ->withSchemaDefinition ($ definition );
89+ return $ rootSchemaTemplate ->withSchemaDefinition ($ rootDefinition );
8890 }
8991
9092 private function getSchemaIdFromExceptionMessage (string $ exceptionMessage ): string
@@ -93,51 +95,46 @@ private function getSchemaIdFromExceptionMessage(string $exceptionMessage): stri
9395 }
9496
9597 private function replaceSchemaIdWithDefinition (
96- string $ definition ,
98+ string $ rootDefinition ,
9799 string $ schemaId ,
98- string $ embeddedDefinition ,
99- bool $ optimizeSubSchemaNamespaces = false
100+ string $ embeddedDefinition
100101 ): string {
101102 $ idString = '" ' . $ schemaId . '" ' ;
103+ $ pos = (int ) strpos ($ rootDefinition , $ idString );
102104
103- if (true === $ optimizeSubSchemaNamespaces ) {
104- $ embeddedDefinition = $ this ->excludeNamespacesForEmbeddedSchema ($ definition , $ embeddedDefinition );
105- }
106-
107- $ pos = strpos ($ definition , $ idString );
108-
109- return substr_replace ($ definition , $ embeddedDefinition , $ pos , strlen ($ idString ));
105+ return substr_replace ($ rootDefinition , $ embeddedDefinition , $ pos , strlen ($ idString ));
110106 }
111107
112108 /**
113109 * @param bool $prefixWithNamespace
114110 * @param bool $useTemplateName
115- * @param bool $optimizeSubSchemaNamespaces
111+ * @param bool $optimizeFullNames
116112 * @return integer
117113 * @throws AvroSchemaParseException
118114 * @throws SchemaMergerException
119115 */
120116 public function merge (
121117 bool $ prefixWithNamespace = false ,
122118 bool $ useTemplateName = false ,
123- bool $ optimizeSubSchemaNamespaces = false
119+ bool $ optimizeFullNames = false
124120 ): int {
125121 $ mergedFiles = 0 ;
126122 $ registry = $ this ->getSchemaRegistry ();
127123
128- /** @var SchemaTemplateInterface $schemaTemplate */
129- foreach ($ registry ->getRootSchemas () as $ schemaTemplate ) {
124+ /** @var SchemaTemplateInterface $rootSchemaTemplate */
125+ foreach ($ registry ->getRootSchemas () as $ rootSchemaTemplate ) {
130126 try {
131- $ resolvedTemplate = $ this ->getResolvedSchemaTemplate ($ schemaTemplate , $ optimizeSubSchemaNamespaces );
127+ $ resolvedTemplate = $ this ->getResolvedSchemaTemplate ($ rootSchemaTemplate );
128+ foreach ($ this ->optimizers as $ optimizer ) {
129+ $ resolvedTemplate = $ resolvedTemplate ->withSchemaDefinition (
130+ $ optimizer ->optimize ($ resolvedTemplate ->getSchemaDefinition ())
131+ );
132+ }
132133 } catch (SchemaMergerException $ e ) {
133134 throw $ e ;
134135 }
135- $ this ->exportSchema (
136- $ resolvedTemplate ,
137- $ prefixWithNamespace ,
138- $ useTemplateName ,
139- $ optimizeSubSchemaNamespaces
140- );
136+ $ this ->exportSchema ($ resolvedTemplate , $ prefixWithNamespace , $ useTemplateName );
137+
141138 ++$ mergedFiles ;
142139 }
143140
@@ -153,8 +150,7 @@ public function merge(
153150 public function exportSchema (
154151 SchemaTemplateInterface $ rootSchemaTemplate ,
155152 bool $ prefixWithNamespace = false ,
156- bool $ useTemplateName = false ,
157- bool $ optimizeSubSchemaNamespaces = false
153+ bool $ useTemplateName = false
158154 ): void {
159155 $ rootSchemaDefinition = $ this ->transformExportSchemaDefinition (
160156 json_decode ($ rootSchemaTemplate ->getSchemaDefinition (), true , JSON_THROW_ON_ERROR )
@@ -179,11 +175,6 @@ public function exportSchema(
179175 /** @var string $fileContents */
180176 $ fileContents = json_encode ($ rootSchemaDefinition );
181177
182- if (true === $ optimizeSubSchemaNamespaces ) {
183- $ embeddedSchemaNamespace = $ rootSchemaDefinition ['namespace ' ] . '. ' ;
184- $ fileContents = str_replace ($ embeddedSchemaNamespace , '' , $ fileContents );
185- }
186-
187178 file_put_contents ($ this ->getOutputDirectory () . '/ ' . $ schemaFilename , $ fileContents );
188179 }
189180
@@ -199,24 +190,10 @@ public function transformExportSchemaDefinition(array $schemaDefinition): array
199190 }
200191
201192 /**
202- * @param string $definition
203- * @param string $embeddedDefinition
204- * @return string
193+ * @param OptimizerInterface $optimizer
205194 */
206- private function excludeNamespacesForEmbeddedSchema ( string $ definition , string $ embeddedDefinition ): string
195+ public function addOptimizer ( OptimizerInterface $ optimizer ): void
207196 {
208- $ decodedRootDefinition = json_decode ($ definition , true , JSON_THROW_ON_ERROR );
209- $ decodedEmbeddedDefinition = json_decode ($ embeddedDefinition , true , JSON_THROW_ON_ERROR );
210-
211- if (
212- isset ($ decodedRootDefinition ['namespace ' ]) && isset ($ decodedEmbeddedDefinition ['namespace ' ]) &&
213- $ decodedRootDefinition ['namespace ' ] === $ decodedEmbeddedDefinition ['namespace ' ]
214- ) {
215- unset($ decodedEmbeddedDefinition ['namespace ' ]);
216- /** @var string $embeddedDefinition */
217- $ embeddedDefinition = json_encode ($ decodedEmbeddedDefinition );
218- }
219-
220- return $ embeddedDefinition ;
197+ $ this ->optimizers [] = $ optimizer ;
221198 }
222199}
0 commit comments