. */ declare(strict_types=1); namespace App\EventSubscriber\LogSystem; use App\Entity\LogSystem\DatabaseUpdatedLogEntry; use App\Services\LogSystem\EventLogger; use Doctrine\Common\EventSubscriber; use Doctrine\Migrations\DependencyFactory; use Doctrine\Migrations\Event\MigrationsEventArgs; use Doctrine\Migrations\Events; /** * This subscriber logs databaseMigrations to Event log. */ class LogDBMigrationSubscriber implements EventSubscriber { protected ?string $old_version = null; protected ?string $new_version = null; protected EventLogger $eventLogger; protected DependencyFactory $dependencyFactory; public function __construct(EventLogger $eventLogger, DependencyFactory $dependencyFactory) { $this->eventLogger = $eventLogger; $this->dependencyFactory = $dependencyFactory; } public function onMigrationsMigrated(MigrationsEventArgs $args): void { //Dont do anything if this was a dry run if ($args->getMigratorConfiguration()->isDryRun()) { return; } $aliasResolver = $this->dependencyFactory->getVersionAliasResolver(); //Save the version after the migration //$this->new_version = $args->getMigratorConfiguration()->getCurrentVersion(); $this->new_version = (string) $aliasResolver->resolveVersionAlias('current'); //After everything is done, write the results to DB log $this->old_version = empty($this->old_version) ? 'legacy/empty' : $this->old_version; $this->new_version = empty($this->new_version) ? 'unknown' : $this->new_version; try { $log = new DatabaseUpdatedLogEntry($this->old_version, $this->new_version); //$this->eventLogger->logAndFlush($log); } catch (\Throwable $exception) { //Ignore any exception occuring here... } } public function onMigrationsMigrating(MigrationsEventArgs $args): void { // Save the version before any migration if (null === $this->old_version) { $aliasResolver = $this->dependencyFactory->getVersionAliasResolver(); //$this->old_version = $args->getConfiguration()->getCurrentVersion(); $this->old_version = (string) $aliasResolver->resolveVersionAlias('current'); } } public function getSubscribedEvents(): array { return [ Events::onMigrationsMigrated, Events::onMigrationsMigrating, ]; } }