99use  Jobcloud \SchemaConsole \Helper \SchemaFileHelper ;
1010use  Symfony \Component \Console \Input \InputArgument ;
1111use  Symfony \Component \Console \Input \InputInterface ;
12+ use  Symfony \Component \Console \Input \InputOption ;
1213use  Symfony \Component \Console \Output \OutputInterface ;
1314use  Symfony \Component \Console \Style \SymfonyStyle ;
1415
@@ -43,7 +44,13 @@ protected function configure(): void
4344            ->setName ('kafka-schema-registry:register:changed ' )
4445            ->setDescription ('Register all changed schemas from a path ' )
4546            ->setHelp ('Register all changed schemas from a path ' )
46-             ->addArgument ('schemaDirectory ' , InputArgument::REQUIRED , 'Path to avro schema directory ' );
47+             ->addArgument ('schemaDirectory ' , InputArgument::REQUIRED , 'Path to avro schema directory ' )
48+             ->addOption (
49+                 'useSchemaVersioning ' ,
50+                 null ,
51+                 InputOption::VALUE_NONE ,
52+                 'Register schemas with multiple versions (e.g. ch.jobcloud.namespace.schema.1.avsc) ' 
53+             );
4754    }
4855
4956    /** 
@@ -64,8 +71,16 @@ public function execute(InputInterface $input, OutputInterface $output): int
6471        $ failed
6572        $ succeeded
6673
74+         $ useSchemaVersioningbool ) $ inputgetOption ('useSchemaVersioning ' );
75+ 
76+         $ successMessage'%s with new version: %s ' ;
77+         if  ($ useSchemaVersioning
78+             natsort ($ avroFiles
79+             $ successMessage'%s with new versions, the latest being: %s ' ;
80+         }
81+ 
6782        while  (false  === $ this abortRegister ) {
68-             if  (false  === $ this registerFiles ($ avroFiles$ io$ failed$ succeeded
83+             if  (false  === $ this registerFiles ($ avroFiles$ io$ failed$ succeeded,  $ useSchemaVersioning 
6984                return  1 ;
7085            }
7186
@@ -79,8 +94,8 @@ public function execute(InputInterface $input, OutputInterface $output): int
7994
8095        if  (isset ($ succeeded0  !== count ($ succeeded
8196            $ iosuccess ('Succeeded registering the following schemas: ' );
82-             $ iolisting (array_map (static  function  ($ item
83-                 return  sprintf (' %s with new version: %s ' $ item'name ' ], $ item'version ' ]);
97+             $ iolisting (array_map (static  function  ($ itemuse  ( $ successMessage )  {
98+                 return  sprintf ($ successMessage $ item'name ' ], $ item'version ' ]);
8499            }, $ succeeded
85100        }
86101
@@ -92,13 +107,15 @@ public function execute(InputInterface $input, OutputInterface $output): int
92107     * @param SymfonyStyle $io 
93108     * @param array<string, mixed> $failed 
94109     * @param array<string, mixed> $succeeded 
110+      * @param bool $useSchemaVersioning 
95111     * @return boolean 
96112     */ 
97113    private  function  registerFiles (
98114        array  $ avroFiles
99115        SymfonyStyle $ io
100116        array  &$ failed
101-         array  &$ succeeded
117+         array  &$ succeeded
118+         bool  $ useSchemaVersioningfalse 
102119    ): bool  {
103120        foreach  ($ avroFilesas  $ schemaName$ avroFile
104121            /** @var string $fileContents */ 
@@ -110,6 +127,11 @@ private function registerFiles(
110127            /** @var string $localSchema */ 
111128            $ localSchemajson_encode ($ jsonDecoded
112129
130+             if  ($ useSchemaVersioning
131+                 /** @var string $schemaName */ 
132+                 $ schemaNamepreg_replace ('/[.0-9]*$/ ' , '' , $ schemaName
133+             }
134+ 
113135            try  {
114136                $ latestVersion$ this schemaRegistryApi ->getLatestSubjectVersion ($ schemaName
115137            } catch  (SubjectNotFoundException $ e
0 commit comments