-
-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Maxence Lange <maxence@artificial-owl.com>
- Loading branch information
1 parent
2b8e50a
commit adb01b2
Showing
5 changed files
with
245 additions
and
121 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors | ||
* SPDX-License-Identifier: AGPL-3.0-only | ||
*/ | ||
namespace OC\Migration; | ||
|
||
use OC\DB\Connection; | ||
use OC\DB\MigrationService; | ||
use OCP\App\IAppManager; | ||
use OCP\Migration\Attributes\GenericMigrationAttribute; | ||
use OCP\Migration\Attributes\MigrationAttribute; | ||
use OCP\Migration\Exceptions\AttributeException; | ||
use Psr\Log\LoggerInterface; | ||
use ReflectionClass; | ||
|
||
class MetadataManager { | ||
public function __construct( | ||
private readonly IAppManager $appManager, | ||
private readonly Connection $connection, | ||
private readonly LoggerInterface $logger, | ||
) { | ||
} | ||
|
||
/** | ||
* We get all migrations from an app, and for each migration we extract attributes | ||
* | ||
* @param string $appId | ||
* | ||
* @return array | ||
* @throws \Exception | ||
*/ | ||
public function extractMigrationAttributes(string $appId): array { | ||
$ms = new MigrationService($appId, $this->connection); | ||
|
||
$metadata = []; | ||
foreach($ms->getAvailableVersions() as $version) { | ||
$metadata[$version] = []; | ||
$class = new ReflectionClass($ms->createInstance($version)); | ||
$attributes = $class->getAttributes(); | ||
foreach ($attributes as $attribute) { | ||
$metadata[$version][] = $attribute->newInstance(); | ||
} | ||
} | ||
|
||
return $metadata; | ||
} | ||
|
||
/** | ||
* @param string $version | ||
* @param bool $filterKnownMigrations | ||
* | ||
* @return array | ||
*/ | ||
public function getMigrationsAttributesFromReleaseMetadata( | ||
array $metadata, | ||
bool $filterKnownMigrations = false | ||
): array { | ||
$appsAttributes = []; | ||
foreach (array_keys($metadata['apps']) as $appId) { | ||
if ($filterKnownMigrations && !$this->appManager->isInstalled($appId)) { | ||
continue; | ||
} | ||
$done = ($filterKnownMigrations) ? $this->getKnownMigrations($appId) : []; | ||
$appsAttributes[$appId] = $this->parseMigrations($metadata['apps'][$appId] ?? [], $done); | ||
Check failure on line 68 in lib/private/Migration/MetadataManager.php GitHub Actions / static-code-analysisInvalidArrayOffset
Check failure Code scanning / Psalm InvalidArrayOffset Error
Cannot access value on variable $appsAttributes[$appId] using a key-of<array<array-key, mixed>> offset, expecting array-key
|
||
} | ||
|
||
$done = ($filterKnownMigrations) ? $this->getKnownMigrations('core') : []; | ||
return [ | ||
'core' => $this->parseMigrations($metadata['core'] ?? [], $done), | ||
'apps' => $appsAttributes | ||
]; | ||
} | ||
|
||
private function parseMigrations(array $migrations, array $ignoreMigrations = []): array { | ||
$parsed = []; | ||
foreach (array_keys($migrations) as $entry) { | ||
if (in_array($entry, $ignoreMigrations)) { | ||
continue; | ||
} | ||
|
||
$parsed[$entry] = []; | ||
foreach ($migrations[$entry] as $item) { | ||
try { | ||
$parsed[$entry][] = $this->createAttribute($item); | ||
} catch (AttributeException $e) { | ||
$this->logger->warning('exception while trying to create attribute', ['exception' => $e, 'item' => json_encode($item)]); | ||
$parsed[$entry][] = new GenericMigrationAttribute($item); | ||
} | ||
} | ||
} | ||
|
||
return $parsed; | ||
} | ||
|
||
private function getKnownMigrations(string $appId): array { | ||
$ms = new MigrationService($appId, $this->connection); | ||
return $ms->getMigratedVersions(); | ||
} | ||
|
||
|
||
/** | ||
* @param array $item | ||
* | ||
* @return MigrationAttribute|null | ||
* @throws AttributeException | ||
*/ | ||
private function createAttribute(array $item): ?MigrationAttribute { | ||
$class = $item['class'] ?? ''; | ||
$namespace = 'OCP\Migration\Attributes\\'; | ||
if (!str_starts_with($class, $namespace) | ||
|| !ctype_alpha(substr($class, strlen($namespace)))) { | ||
throw new AttributeException('class name does not looks valid'); | ||
} | ||
|
||
try { | ||
$attribute = new $class(); | ||
return $attribute->import($item); | ||
} catch (\Error) { | ||
throw new AttributeException('cannot import Attribute'); | ||
} | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
lib/private/Updater/Exceptions/ReleaseMetadataException.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors | ||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||
*/ | ||
namespace OC\Updater\Exceptions; | ||
|
||
use Exception; | ||
|
||
/** | ||
* @since 30.0.0 | ||
*/ | ||
class ReleaseMetadataException extends Exception { | ||
} |
Oops, something went wrong.