Skip to content

Commit 043fd38

Browse files
committed
chore(OC_App): move executeRepairSteps and checkAppDependencies to AppManager
Signed-off-by: Ferdinand Thiessen <opensource@fthiessen.de>
1 parent c4c6254 commit 043fd38

3 files changed

Lines changed: 73 additions & 57 deletions

File tree

lib/private/App/AppManager.php

Lines changed: 64 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
use OC\Config\ConfigManager;
1313
use OC\DB\MigrationService;
1414
use OC\Migration\BackgroundRepair;
15+
use OC\NeedsUpdateException;
16+
use OC\Repair;
17+
use OC\Repair\Events\RepairErrorEvent;
1518
use OCP\Activity\IManager as IActivityManager;
1619
use OCP\App\AppPathNotFoundException;
1720
use OCP\App\Events\AppDisableEvent;
@@ -258,7 +261,7 @@ public function loadApps(array $types = []): bool {
258261
return false;
259262
}
260263
// Load the enabled apps here
261-
$apps = \OC_App::getEnabledApps();
264+
$apps = $this->getEnabledApps();
262265

263266
// Add each apps' folder as allowed class path
264267
foreach ($apps as $app) {
@@ -708,7 +711,7 @@ public function disableApp($appId, $automaticDisabled = false): void {
708711
// run uninstall steps
709712
$appData = $this->getAppInfo($appId);
710713
if (!is_null($appData)) {
711-
\OC_App::executeRepairSteps($appId, $appData['repair-steps']['uninstall']);
714+
$this->executeRepairSteps($appId, $appData['repair-steps']['uninstall']);
712715
}
713716

714717
$this->dispatcher->dispatchTyped(new AppDisableEvent($appId));
@@ -1109,30 +1112,24 @@ public function upgradeApp(string $appId): bool {
11091112
$appPath = $this->getAppPath($appId, true);
11101113

11111114
$this->clearAppsCache();
1112-
$l = \OC::$server->getL10N('core');
1113-
$appData = $this->getAppInfo($appId, false, $l->getLanguageCode());
1114-
if ($appData === null) {
1115+
$appInfo = $this->getAppInfo($appId);
1116+
if ($appInfo === null) {
11151117
throw new AppPathNotFoundException('Could not find ' . $appId);
11161118
}
11171119

11181120
$ignoreMaxApps = $this->config->getSystemValue('app_install_overwrite', []);
11191121
$ignoreMax = in_array($appId, $ignoreMaxApps, true);
1120-
\OC_App::checkAppDependencies(
1121-
$this->config,
1122-
$l,
1123-
$appData,
1124-
$ignoreMax
1125-
);
1122+
$this->checkAppDependencies($appId, $ignoreMax);
11261123

11271124
\OC_App::registerAutoloading($appId, $appPath, true);
1128-
\OC_App::executeRepairSteps($appId, $appData['repair-steps']['pre-migration']);
1125+
$this->executeRepairSteps($appId, $appInfo['repair-steps']['pre-migration']);
11291126

11301127
$ms = new MigrationService($appId, Server::get(\OC\DB\Connection::class));
11311128
$ms->migrate();
11321129

1133-
\OC_App::executeRepairSteps($appId, $appData['repair-steps']['post-migration']);
1130+
$this->executeRepairSteps($appId, $appInfo['repair-steps']['post-migration']);
11341131
$queue = Server::get(IJobList::class);
1135-
foreach ($appData['repair-steps']['live-migration'] as $step) {
1132+
foreach ($appInfo['repair-steps']['live-migration'] as $step) {
11361133
$queue->add(BackgroundRepair::class, [
11371134
'app' => $appId,
11381135
'step' => $step]);
@@ -1143,19 +1140,19 @@ public function upgradeApp(string $appId): bool {
11431140
$this->getAppVersion($appId, false);
11441141

11451142
// Setup background jobs
1146-
foreach ($appData['background-jobs'] as $job) {
1143+
foreach ($appInfo['background-jobs'] as $job) {
11471144
$queue->add($job);
11481145
}
11491146

11501147
//set remote/public handlers
1151-
foreach ($appData['remote'] as $name => $path) {
1148+
foreach ($appInfo['remote'] as $name => $path) {
11521149
$this->config->setAppValue('core', 'remote_' . $name, $appId . '/' . $path);
11531150
}
1154-
foreach ($appData['public'] as $name => $path) {
1151+
foreach ($appInfo['public'] as $name => $path) {
11551152
$this->config->setAppValue('core', 'public_' . $name, $appId . '/' . $path);
11561153
}
11571154

1158-
$this->setAppTypes($appId, $appData);
1155+
$this->setAppTypes($appId, $appInfo);
11591156

11601157
$version = $this->getAppVersion($appId);
11611158
$this->config->setAppValue($appId, 'installed_version', $version);
@@ -1197,4 +1194,53 @@ public function isUpgradeRequired(string $appId): bool {
11971194
public function isAppCompatible(string $serverVersion, array $appInfo, bool $ignoreMax = false): bool {
11981195
return count($this->dependencyAnalyzer->analyzeServerVersion($serverVersion, $appInfo, $ignoreMax)) === 0;
11991196
}
1197+
1198+
/**
1199+
* Check if all dependencies of an app are satisfied.
1200+
*
1201+
* @param string $appId - The app to check
1202+
* @param bool $ignoreMax - Whether to ignore the Nextcloud max version requirement
1203+
* @throws \Exception - If there are missing dependencies
1204+
*/
1205+
public function checkAppDependencies(string $appId, bool $ignoreMax = false): void {
1206+
$info = $this->getAppInfo($appId);
1207+
$missing = $this->dependencyAnalyzer->analyze($info, $ignoreMax);
1208+
if (!empty($missing)) {
1209+
$l = \OCP\Server::get(\OCP\L10N\IFactory::class)->get('core');
1210+
$missingMsg = implode(PHP_EOL, $missing);
1211+
throw new \Exception(
1212+
$l->t('App "%1$s" cannot be installed because the following dependencies are not fulfilled: %2$s',
1213+
[$info['name'], $missingMsg]
1214+
)
1215+
);
1216+
}
1217+
}
1218+
1219+
/**
1220+
* Run repair steps for an app.
1221+
*
1222+
* @param string $appId - The app to run the repair steps for
1223+
* @param string[] $steps - The repair steps to run
1224+
* @throws NeedsUpdateException
1225+
*/
1226+
public function executeRepairSteps(string $appId, array $steps): void {
1227+
if (empty($steps)) {
1228+
return;
1229+
}
1230+
1231+
$this->loadApp($appId);
1232+
1233+
// load the steps
1234+
$r = Server::get(Repair::class);
1235+
foreach ($steps as $step) {
1236+
try {
1237+
$r->addStep($step);
1238+
} catch (\Exception $ex) {
1239+
$this->dispatcher->dispatchTyped(new RepairErrorEvent($ex->getMessage()));
1240+
$this->logger->error('Failed to add app migration step ' . $step, ['exception' => $ex]);
1241+
}
1242+
}
1243+
// run the steps
1244+
$r->run();
1245+
}
12001246
}

lib/private/Installer.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public function installApp(string $appId, bool $forceEnable = false): string {
8787
}
8888

8989
// check for required dependencies
90-
\OC_App::checkAppDependencies($this->config, $l, $info, $ignoreMax);
90+
$this->appManager->checkAppDependencies($appId, $ignoreMax);
9191
$coordinator = Server::get(Coordinator::class);
9292
$coordinator->runLazyRegistration($appId);
9393

@@ -541,13 +541,13 @@ private function installAppLastSteps(string $appPath, array $info, ?IOutput $out
541541
$ms->setOutput($output);
542542
}
543543
if ($previousVersion !== '') {
544-
\OC_App::executeRepairSteps($info['id'], $info['repair-steps']['pre-migration']);
544+
$this->appManager->executeRepairSteps($info['id'], $info['repair-steps']['pre-migration']);
545545
}
546546

547547
$ms->migrate('latest', $previousVersion === '');
548548

549549
if ($previousVersion !== '') {
550-
\OC_App::executeRepairSteps($info['id'], $info['repair-steps']['post-migration']);
550+
$this->appManager->executeRepairSteps($info['id'], $info['repair-steps']['post-migration']);
551551
}
552552

553553
if ($output instanceof IOutput) {
@@ -569,7 +569,7 @@ private function installAppLastSteps(string $appPath, array $info, ?IOutput $out
569569
self::includeAppScript($appInstallScriptPath);
570570
}
571571

572-
\OC_App::executeRepairSteps($info['id'], $info['repair-steps']['install']);
572+
$this->appManager->executeRepairSteps($info['id'], $info['repair-steps']['install']);
573573

574574
// Set the installed version
575575
$this->config->setAppValue($info['id'], 'installed_version', $this->appManager->getAppVersion($info['id'], false));

lib/private/legacy/OC_App.php

Lines changed: 5 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,16 @@
88
* SPDX-License-Identifier: AGPL-3.0-only
99
*/
1010
use OC\App\AppManager;
11-
use OC\App\DependencyAnalyzer;
1211
use OC\AppFramework\App;
1312
use OC\AppFramework\Bootstrap\Coordinator;
1413
use OC\Installer;
1514
use OC\NeedsUpdateException;
16-
use OC\Repair;
17-
use OC\Repair\Events\RepairErrorEvent;
1815
use OC\SystemConfig;
1916
use OCP\App\AppPathNotFoundException;
2017
use OCP\App\IAppManager;
2118
use OCP\Authentication\IAlternativeLogin;
2219
use OCP\Authentication\IAlternativeLoginProvider;
2320
use OCP\BackgroundJob\IJobList;
24-
use OCP\EventDispatcher\IEventDispatcher;
2521
use OCP\IAppConfig;
2622
use OCP\IConfig;
2723
use OCP\IGroup;
@@ -34,7 +30,6 @@
3430
use OCP\Support\Subscription\IRegistry;
3531
use Psr\Container\ContainerExceptionInterface;
3632
use Psr\Log\LoggerInterface;
37-
use function OCP\Log\logger;
3833

3934
/**
4035
* This class manages the apps. It allows them to register and integrate in the
@@ -146,6 +141,7 @@ public static function isType(string $app, array $types): bool {
146141
* @param bool $all whether to return apps for all users, not only the
147142
* currently logged in one
148143
* @return list<string>
144+
* @deprecated 32.0.0 - use {@see \OCP\App\IAppManager::getEnabledAppsForUser} or {@see \OC\OCP\AppIAppManager::getEnabledApps} instead
149145
*/
150146
public static function getEnabledApps(bool $forceRefresh = false, bool $all = false): array {
151147
if (!Server::get(SystemConfig::class)->getValue('installed', false)) {
@@ -532,28 +528,10 @@ public static function updateApp(string $appId): bool {
532528
* @param string $appId
533529
* @param string[] $steps
534530
* @throws NeedsUpdateException
531+
* @deprecated 34.0.0 Use {@see \OC\App\AppManager::upgradeApp}
535532
*/
536533
public static function executeRepairSteps(string $appId, array $steps) {
537-
if (empty($steps)) {
538-
return;
539-
}
540-
// load the app
541-
self::loadApp($appId);
542-
543-
$dispatcher = Server::get(IEventDispatcher::class);
544-
545-
// load the steps
546-
$r = Server::get(Repair::class);
547-
foreach ($steps as $step) {
548-
try {
549-
$r->addStep($step);
550-
} catch (Exception $ex) {
551-
$dispatcher->dispatchTyped(new RepairErrorEvent($ex->getMessage()));
552-
logger('core')->error('Failed to add app migration step ' . $step, ['exception' => $ex]);
553-
}
554-
}
555-
// run the steps
556-
$r->run();
534+
Server::get(AppManager::class)->executeRepairSteps($appId, $steps);
557535
}
558536

559537
/**
@@ -568,17 +546,9 @@ public static function setupBackgroundJobs(array $jobs): void {
568546

569547
/**
570548
* @throws \Exception
549+
* @deprecated 34.0.0 Use {@see \OC\App\AppManager::checkAppDependencies} instead
571550
*/
572551
public static function checkAppDependencies(IConfig $config, IL10N $l, array $info, bool $ignoreMax): void {
573-
$dependencyAnalyzer = Server::get(DependencyAnalyzer::class);
574-
$missing = $dependencyAnalyzer->analyze($info, $ignoreMax);
575-
if (!empty($missing)) {
576-
$missingMsg = implode(PHP_EOL, $missing);
577-
throw new \Exception(
578-
$l->t('App "%1$s" cannot be installed because the following dependencies are not fulfilled: %2$s',
579-
[$info['name'], $missingMsg]
580-
)
581-
);
582-
}
552+
Server::get(AppManager::class)->checkAppDependencies($info['id'], $ignoreMax);
583553
}
584554
}

0 commit comments

Comments
 (0)