diff --git a/lib/ExportDestination.php b/lib/ExportDestination.php index 16f9b15c..ced3ed2b 100644 --- a/lib/ExportDestination.php +++ b/lib/ExportDestination.php @@ -92,6 +92,13 @@ public function copyFolder(Folder $folder, string $destinationPath): bool { return true; } + /** + * {@inheritDoc} + */ + public function setMigratorVersions(array $versions): bool { + return $this->addFileContents("migrator_versions.json", json_encode($versions)); + } + /** * {@inheritDoc} */ diff --git a/lib/ImportSource.php b/lib/ImportSource.php index ae16fc4f..3a978ce0 100644 --- a/lib/ImportSource.php +++ b/lib/ImportSource.php @@ -36,6 +36,11 @@ class ImportSource implements IImportSource { private string $path; + /** + * @var ?array + */ + private ?array $migratorVersions = null; + public function __construct(string $path) { $this->path = $path; $this->archive = new ZIP($this->path); @@ -84,6 +89,16 @@ public function copyToFolder(Folder $destination, string $sourcePath): bool { return true; } + /** + * {@inheritDoc} + */ + public function getMigratorVersions(): array { + if ($this->migratorVersions === null) { + $this->migratorVersions = json_decode($this->getFileContents("migrator_versions.json"), true, 512, JSON_THROW_ON_ERROR); + } + return $this->migratorVersions; + } + /** * {@inheritDoc} */ diff --git a/lib/Service/UserMigrationService.php b/lib/Service/UserMigrationService.php index 6364dd51..364e042d 100644 --- a/lib/Service/UserMigrationService.php +++ b/lib/Service/UserMigrationService.php @@ -50,6 +50,8 @@ class UserMigrationService { use TMigratorBasicVersionHandling; + protected bool $mandatory = true; + protected IRootFolder $root; protected IConfig $config; @@ -145,7 +147,7 @@ public function export(IUser $user, ?OutputInterface $output = null): string { $migrator->export($user, $exportDestination, $output); $migratorVersions[$migrator::class] = $migrator->getVersion(); } - if ($exportDestination->addFileContents("migrator_versions.json", json_encode($migratorVersions)) === false) { + if ($exportDestination->setMigratorVersions($migratorVersions) === false) { throw new UserMigrationException("Could not export user information."); } @@ -166,25 +168,18 @@ public function import(string $path, ?OutputInterface $output = null): void { if ($context === null) { throw new UserMigrationException("Failed to get context"); } - // TODO move this to ImportSource so that migrators can query the versions as well - $migrationVersions = json_decode($importSource->getFileContents("migrator_versions.json"), true, 512, JSON_THROW_ON_ERROR); + $migratorVersions = $importSource->getMigratorVersions(); - if (!isset($migrationVersions[static::class])) { - throw new UserMigrationException("Cannot find version for main class ".static::class); - } elseif (!$this->canImport($migrationVersions[static::class])) { - throw new UserMigrationException("Version ${migrationVersions[static::class]} for main class ".static::class." is not compatible"); + if (!$this->canImport($importSource, $migratorVersions[static::class] ?? null)) { + throw new UserMigrationException("Version ${$migratorVersions[static::class]} for main class ".static::class." is not compatible"); } // Check versions foreach ($context->getUserMigrators() as $migratorRegistration) { /** @var IMigrator $migrator */ $migrator = $this->container->get($migratorRegistration->getService()); - if (isset($migrationVersions[$migrator::class])) { - if (!$migrator->canImport($migrationVersions[$migrator::class])) { - throw new UserMigrationException("Version ${migrationVersions[$migrator::class]} for migrator ".$migrator::class." is not compatible"); - } - } else { - $output->writeln("No input data for migrator ".$migrator::class.", ignoring"); + if (!$migrator->canImport($importSource, $migratorVersions[$migrator::class] ?? null)) { + throw new UserMigrationException("Version ${$migratorVersions[$migrator::class]} for migrator ".$migrator::class." is not compatible"); } } @@ -197,9 +192,7 @@ public function import(string $path, ?OutputInterface $output = null): void { foreach ($context->getUserMigrators() as $migratorRegistration) { /** @var IMigrator $migrator */ $migrator = $this->container->get($migratorRegistration->getService()); - if (isset($migrationVersions[$migrator::class])) { - $migrator->import($user, $importSource, $output); - } + $migrator->import($user, $importSource, $output, $migratorVersions[$migrator::class] ?? null); } $uid = $user->getUID();