99use Ibexa \Contracts \Core \Repository \Values \Content \ContentInfo ;
1010use Psr \Log \LoggerInterface ;
1111use Symfony \Component \DependencyInjection \Attribute \Autowire ;
12+ use Symfony \Component \DependencyInjection \ContainerInterface ;
1213use Symfony \Component \EventDispatcher \Attribute \AsEventListener ;
1314use vardumper \IbexaAutomaticMigrationsBundle \Helper \Helper ;
1415use vardumper \IbexaAutomaticMigrationsBundle \Process \MigrationRunnerInterface ;
@@ -22,14 +23,18 @@ final class ContentListener
2223 private string $ destination ;
2324 private array $ consoleCommand ;
2425 private MigrationRunnerInterface $ migrationRunner ;
26+ private ContainerInterface $ container ;
2527
2628 public function __construct (
2729 private readonly LoggerInterface $ logger ,
2830 private readonly \vardumper \IbexaAutomaticMigrationsBundle \Service \SettingsService $ settingsService ,
2931 #[Autowire('%kernel.project_dir% ' )]
3032 string $ projectDir ,
33+ #[Autowire(service: 'service_container ' )]
34+ ContainerInterface $ container ,
3135 ?MigrationRunnerInterface $ migrationRunner = null ,
3236 ) {
37+ $ this ->container = $ container ;
3338 $ this ->migrationRunner = $ migrationRunner ?? new SymfonyProcessRunner ();
3439 $ this ->projectDir = rtrim ($ projectDir , DIRECTORY_SEPARATOR );
3540 $ this ->isCli = PHP_SAPI === 'cli ' && ($ _SERVER ['APP_ENV ' ] ?? $ _ENV ['APP_ENV ' ] ?? null ) !== 'testing ' ;
@@ -129,6 +134,59 @@ private function generateMigration(ContentInfo $contentInfo, string $mode): void
129134 $ this ->migrationRunner ->run ($ cmd , $ this ->projectDir );
130135 $ code = $ this ->migrationRunner ->getExitCode ();
131136 $ this ->logger ->info ('Content migration generate process finished ' , ['name ' => $ name , 'code ' => $ code , 'output ' => $ this ->migrationRunner ->getOutput (), 'error ' => $ this ->migrationRunner ->getErrorOutput ()]);
137+
138+ if ($ code == 0 ) {
139+ if ($ this ->mode === 'ibexa ' ) {
140+ $ fileName = $ inputArray ['--file ' ];
141+ } else {
142+ $ files = glob ($ this ->destination . DIRECTORY_SEPARATOR . '*.{yml,yaml} ' , GLOB_BRACE );
143+ $ latestFile = '' ;
144+ $ latestTime = 0 ;
145+ foreach ($ files as $ file ) {
146+ $ mtime = filemtime ($ file );
147+ if ($ mtime > $ latestTime ) {
148+ $ latestTime = $ mtime ;
149+ $ latestFile = $ file ;
150+ }
151+ }
152+ if ($ latestFile ) {
153+ $ fileName = basename ($ latestFile );
154+ } else {
155+ $ this ->logger ->warning ('No migration files found after generation ' );
156+ return ;
157+ }
158+ }
159+
160+ $ fullPath = $ this ->destination . DIRECTORY_SEPARATOR . $ fileName ;
161+
162+ try {
163+ $ conn = $ this ->container ->get ('doctrine.dbal.default_connection ' );
164+ if ($ this ->mode === 'ibexa ' ) {
165+ $ data = ['executed_at ' => new \DateTime (), 'execution_time ' => null ];
166+ $ identifier = ['name ' => $ fileName ];
167+ $ affected = $ conn ->update ('ibexa_migrations ' , $ data , $ identifier );
168+ if ($ affected === 0 ) {
169+ $ conn ->insert ('ibexa_migrations ' , array_merge ($ identifier , $ data ));
170+ }
171+ } elseif ($ this ->mode === 'kaliop ' ) {
172+ $ data = ['execution_date ' => time (), 'status ' => 2 ];
173+ $ identifier = ['migration ' => $ fileName ];
174+ $ affected = $ conn ->update ('kaliop_migrations ' , $ data , $ identifier );
175+ if ($ affected === 0 ) {
176+ $ conn ->insert ('kaliop_migrations ' , array_merge ($ identifier , $ data , [
177+ 'md5 ' => md5_file ($ fullPath ),
178+ 'path ' => $ fullPath ,
179+ 'execution_error ' => null ,
180+ ]));
181+ }
182+ }
183+ $ this ->logger ->info ('Content migration marked as executed ' , ['filename ' => $ fileName , 'mode ' => $ this ->mode ]);
184+ } catch (\Throwable $ e ) {
185+ $ this ->logger ->warning ('Failed to mark content migration as executed ' , ['exception ' => $ e ->getMessage ()]);
186+ }
187+ } else {
188+ $ this ->logger ->error ('Content migration generation failed ' , ['code ' => $ code , 'error ' => $ this ->migrationRunner ->getErrorOutput ()]);
189+ }
132190 } catch (\Throwable $ e ) {
133191 $ this ->logger ->error ('Failed to generate content migration programmatically ' , ['exception ' => $ e ->getMessage ()]);
134192 }
0 commit comments