diff --git a/apps/dav/lib/CalDAV/Activity/Provider/Event.php b/apps/dav/lib/CalDAV/Activity/Provider/Event.php index d3cfd00b74295..41b610542df0f 100644 --- a/apps/dav/lib/CalDAV/Activity/Provider/Event.php +++ b/apps/dav/lib/CalDAV/Activity/Provider/Event.php @@ -5,7 +5,6 @@ */ namespace OCA\DAV\CalDAV\Activity\Provider; -use OC_App; use OCP\Activity\Exceptions\UnknownActivityException; use OCP\Activity\IEvent; use OCP\Activity\IEventMerger; @@ -67,7 +66,7 @@ protected function generateObjectParameter(array $eventData, string $affectedUse if (isset($eventData['link']) && is_array($eventData['link']) && $this->appManager->isEnabledForUser('calendar')) { try { // The calendar app needs to be manually loaded for the routes to be loaded - OC_App::loadApp('calendar'); + $this->appManager->loadApp('calendar'); $linkData = $eventData['link']; $calendarUri = $this->urlencodeLowerHex($linkData['calendar_uri']); if ($affectedUser === $linkData['owner']) { diff --git a/apps/files/tests/Controller/ViewControllerTest.php b/apps/files/tests/Controller/ViewControllerTest.php index 56cc7d7c6f85a..dd76e81405432 100644 --- a/apps/files/tests/Controller/ViewControllerTest.php +++ b/apps/files/tests/Controller/ViewControllerTest.php @@ -94,6 +94,9 @@ protected function setUp(): void { $this->appManager->expects($this->any()) ->method('getAppPath') ->willReturnCallback(fn (string $appid): string => \OC::$SERVERROOT . '/apps/' . $appid); + $this->appManager->expects($this->any()) + ->method('isAppLoaded') + ->willReturn(true); $this->cacheFactory = $this->createMock(ICacheFactory::class); $this->logger = $this->createMock(LoggerInterface::class); diff --git a/apps/files_sharing/tests/UpdaterTest.php b/apps/files_sharing/tests/UpdaterTest.php index 70e644f920799..efdbeebdf3f58 100644 --- a/apps/files_sharing/tests/UpdaterTest.php +++ b/apps/files_sharing/tests/UpdaterTest.php @@ -59,8 +59,9 @@ protected function tearDown(): void { * that the mount point doesn't end up at the trash bin */ public function testDeleteParentFolder(): void { - $status = Server::get(IAppManager::class)->isEnabledForUser('files_trashbin'); - (new \OC_App())->enable('files_trashbin'); + $appManager = Server::get(IAppManager::class); + $status = $appManager->isEnabledForUser('files_trashbin'); + $appManager->enableApp('files_trashbin'); // register trashbin hooks $trashbinApp = new Application(); @@ -116,7 +117,7 @@ public function testDeleteParentFolder(): void { $rootView->deleteAll('files_trashin'); if ($status === false) { - Server::get(IAppManager::class)->disableApp('files_trashbin'); + $appManager->disableApp('files_trashbin'); } Filesystem::getLoader()->removeStorageWrapper('oc_trashbin'); diff --git a/core/Command/Maintenance/Repair.php b/core/Command/Maintenance/Repair.php index 35f4ef7a8d557..f0c88f6811b7a 100644 --- a/core/Command/Maintenance/Repair.php +++ b/core/Command/Maintenance/Repair.php @@ -70,7 +70,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int if (!is_array($info)) { continue; } - \OC_App::loadApp($app); + $this->appManager->loadApp($app); $steps = $info['repair-steps']['post-migration']; foreach ($steps as $step) { try { diff --git a/lib/base.php b/lib/base.php index 546a119479bb1..811055c982b3b 100644 --- a/lib/base.php +++ b/lib/base.php @@ -675,7 +675,10 @@ public static function init(): void { throw new \OCP\HintException('The PHP SimpleXML/PHP-XML extension is not installed.', 'Install the extension or make sure it is enabled.'); } - OC_App::loadApps(['session']); + $appManager = Server::get(\OCP\App\IAppManager::class); + if ($systemConfig->getValue('installed', false)) { + $appManager->loadApps(['session']); + } if (!self::$CLI) { self::initSession(); } @@ -759,7 +762,7 @@ public static function init(): void { // Make sure that the application class is not loaded before the database is setup if ($systemConfig->getValue('installed', false)) { - OC_App::loadApp('settings'); + $appManager->loadApp('settings'); /* Build core application to make sure that listeners are registered */ Server::get(\OC\Core\Application::class); } @@ -1002,19 +1005,21 @@ public static function handleRequest(): void { } } + $appManager = Server::get(\OCP\App\IAppManager::class); + // Always load authentication apps - OC_App::loadApps(['authentication']); - OC_App::loadApps(['extended_authentication']); + $appManager->loadApps(['authentication']); + $appManager->loadApps(['extended_authentication']); // Load minimum set of apps if (!\OCP\Util::needUpgrade() && !((bool)$systemConfig->getValue('maintenance', false))) { // For logged-in users: Load everything if (Server::get(IUserSession::class)->isLoggedIn()) { - OC_App::loadApps(); + $appManager->loadApps(); } else { // For guests: Load only filesystem and logging - OC_App::loadApps(['filesystem', 'logging']); + $appManager->loadApps(['filesystem', 'logging']); // Don't try to login when a client is trying to get a OAuth token. // OAuth needs to support basic auth too, so the login is not valid @@ -1027,9 +1032,9 @@ public static function handleRequest(): void { if (!self::$CLI) { try { - if (!((bool)$systemConfig->getValue('maintenance', false)) && !\OCP\Util::needUpgrade()) { - OC_App::loadApps(['filesystem', 'logging']); - OC_App::loadApps(); + if (!\OCP\Util::needUpgrade()) { + $appManager->loadApps(['filesystem', 'logging']); + $appManager->loadApps(); } Server::get(\OC\Route\Router::class)->match($request->getRawPathInfo()); return; diff --git a/lib/private/Migration/BackgroundRepair.php b/lib/private/Migration/BackgroundRepair.php index cbc21e4fe12fd..d542b82d5e1f7 100644 --- a/lib/private/Migration/BackgroundRepair.php +++ b/lib/private/Migration/BackgroundRepair.php @@ -7,9 +7,8 @@ */ namespace OC\Migration; -use OC\NeedsUpdateException; use OC\Repair; -use OC_App; +use OCP\App\IAppManager; use OCP\AppFramework\Utility\ITimeFactory; use OCP\BackgroundJob\IJobList; use OCP\BackgroundJob\TimedJob; @@ -26,6 +25,7 @@ public function __construct( ITimeFactory $time, private LoggerInterface $logger, private IJobList $jobList, + private IAppManager $appManager, ) { parent::__construct($time); $this->setInterval(15 * 60); @@ -34,7 +34,6 @@ public function __construct( /** * @param array $argument * @throws \Exception - * @throws \OC\NeedsUpdateException */ protected function run($argument): void { if (!isset($argument['app']) || !isset($argument['step'])) { @@ -44,13 +43,7 @@ protected function run($argument): void { } $app = $argument['app']; - try { - $this->loadApp($app); - } catch (NeedsUpdateException $ex) { - // as long as the app is not yet done with it's offline migration - // we better not start with the live migration - return; - } + $this->appManager->loadApp($app); $step = $argument['step']; $this->repair->setRepairSteps([]); @@ -73,13 +66,4 @@ protected function run($argument): void { // remove the job once executed successfully $this->jobList->remove($this, $this->argument); } - - /** - * @codeCoverageIgnore - * @param $app - * @throws NeedsUpdateException - */ - protected function loadApp($app): void { - OC_App::loadApp($app); - } } diff --git a/lib/private/Route/Router.php b/lib/private/Route/Router.php index 3277b983d0f31..d073132516de5 100644 --- a/lib/private/Route/Router.php +++ b/lib/private/Route/Router.php @@ -142,7 +142,7 @@ public function loadRoutes($app = null) { foreach ($routingFiles as $app => $file) { if (!isset($this->loadedApps[$app])) { - if (!\OC_App::isAppLoaded($app)) { + if (!$this->appManager->isAppLoaded($app)) { // app MUST be loaded before app routes // try again next time loadRoutes() is called $this->loaded = false; @@ -257,8 +257,8 @@ public function findMatchingRoute(string $url): array { $this->loadRoutes('settings'); } elseif (str_starts_with($url, '/core/')) { \OC::$REQUESTEDAPP = $url; - if (!$this->config->getSystemValueBool('maintenance') && !Util::needUpgrade()) { - \OC_App::loadApps(); + if ($this->config->getSystemValueBool('installed', false) && !Util::needUpgrade()) { + $this->appManager->loadApps(); } $this->loadRoutes('core'); } else { diff --git a/lib/private/Server.php b/lib/private/Server.php index 77759de30c5d1..8c5ec8ed25222 100644 --- a/lib/private/Server.php +++ b/lib/private/Server.php @@ -838,7 +838,7 @@ public function __construct($webRoot, \OC\Config $config) { if ($busClass) { [$app, $class] = explode('::', $busClass, 2); if ($c->get(IAppManager::class)->isEnabledForUser($app)) { - \OC_App::loadApp($app); + $c->get(IAppManager::class)->loadApp($app); return $c->get($class); } else { throw new ServiceUnavailableException("The app providing the command bus ($app) is not enabled"); diff --git a/public.php b/public.php index fc0e78695c981..9443367eb1284 100644 --- a/public.php +++ b/public.php @@ -1,18 +1,22 @@ 'dav/appinfo/v1/publicwebdav.php', @@ -22,7 +26,7 @@ function resolveService(string $service): string { return $services[$service]; } - return \OC::$server->getConfig()->getAppValue('core', 'remote_' . $service); + return Server::get(IConfig::class)->getAppValue('core', 'remote_' . $service); } try { @@ -34,13 +38,13 @@ function resolveService(string $service): string { header("Content-Security-Policy: default-src 'none';"); // Check if Nextcloud is in maintenance mode - if (\OCP\Util::needUpgrade()) { + if (Util::needUpgrade()) { // since the behavior of apps or remotes are unpredictable during // an upgrade, return a 503 directly throw new \Exception('Service unavailable', 503); } - $request = \OC::$server->getRequest(); + $request = Server::get(IRequest::class); $pathInfo = $request->getPathInfo(); if ($pathInfo === false || $pathInfo === '') { throw new \Exception('Path not found', 404); @@ -64,18 +68,19 @@ function resolveService(string $service): string { $app = $parts[0]; // Load all required applications + $appManager = Server::get(IAppManager::class); \OC::$REQUESTEDAPP = $app; - OC_App::loadApps(['authentication']); - OC_App::loadApps(['extended_authentication']); - OC_App::loadApps(['filesystem', 'logging']); + $appManager->loadApps(['authentication']); + $appManager->loadApps(['extended_authentication']); + $appManager->loadApps(['filesystem', 'logging']); // Check if the app is enabled - if (!\OC::$server->getAppManager()->isEnabledForUser($app)) { + if (!$appManager->isEnabledForUser($app)) { throw new \Exception('App not installed: ' . $app); } // Load the app - OC_App::loadApp($app); + $appManager->loadApp($app); OC_User::setIncognitoMode(true); $baseuri = OC::$WEBROOT . '/public.php/' . $service . '/'; @@ -86,10 +91,10 @@ function resolveService(string $service): string { $status = 503; } //show the user a detailed error page - \OCP\Server::get(LoggerInterface::class)->error($ex->getMessage(), ['app' => 'public', 'exception' => $ex]); + Server::get(LoggerInterface::class)->error($ex->getMessage(), ['app' => 'public', 'exception' => $ex]); OC_Template::printExceptionErrorPage($ex, $status); } catch (Error $ex) { //show the user a detailed error page - \OCP\Server::get(LoggerInterface::class)->error($ex->getMessage(), ['app' => 'public', 'exception' => $ex]); + Server::get(LoggerInterface::class)->error($ex->getMessage(), ['app' => 'public', 'exception' => $ex]); OC_Template::printExceptionErrorPage($ex, 500); } diff --git a/tests/lib/Migration/BackgroundRepairTest.php b/tests/lib/Migration/BackgroundRepairTest.php index 585ebc420ad88..25ea60880729a 100644 --- a/tests/lib/Migration/BackgroundRepairTest.php +++ b/tests/lib/Migration/BackgroundRepairTest.php @@ -9,9 +9,9 @@ use OC\BackgroundJob\JobList; use OC\Migration\BackgroundRepair; -use OC\NeedsUpdateException; use OC\Repair; use OC\Repair\Events\RepairStepEvent; +use OCP\App\IAppManager; use OCP\AppFramework\Utility\ITimeFactory; use OCP\EventDispatcher\IEventDispatcher; use OCP\Migration\IOutput; @@ -48,6 +48,7 @@ class BackgroundRepairTest extends TestCase { private LoggerInterface $logger; private IEventDispatcher $dispatcher; private ITimeFactory $time; + private IAppManager $appManager; private Repair $repair; protected function setUp(): void { @@ -63,9 +64,10 @@ protected function setUp(): void { $this->time = $this->createMock(ITimeFactory::class); $this->time->method('getTime') ->willReturn(999999); + $this->appManager = $this->createMock(IAppManager::class); $this->repair = new Repair($this->dispatcher, $this->logger); $this->job = $this->getMockBuilder(BackgroundRepair::class) - ->setConstructorArgs([$this->repair, $this->time, $this->logger, $this->jobList]) + ->setConstructorArgs([$this->repair, $this->time, $this->logger, $this->jobList, $this->appManager]) ->setMethods(['loadApp']) ->getMock(); } @@ -75,16 +77,6 @@ public function testNoArguments(): void { $this->job->start($this->jobList); } - public function testAppUpgrading(): void { - $this->jobList->expects($this->never())->method('remove'); - $this->job->expects($this->once())->method('loadApp')->with('test')->willThrowException(new NeedsUpdateException()); - $this->job->setArgument([ - 'app' => 'test', - 'step' => 'j' - ]); - $this->job->start($this->jobList); - } - public function testUnknownStep(): void { $this->dispatcher->expects($this->never())->method('dispatchTyped'); @@ -103,6 +95,9 @@ public function testWorkingStep(): void { ->with($this->equalTo(new RepairStepEvent('A test repair step'))); $this->jobList->expects($this->once())->method('remove'); + $this->appManager->expects(self::once()) + ->method('loadApp') + ->with('test'); $this->job->setArgument([ 'app' => 'test', diff --git a/tests/lib/Route/RouterTest.php b/tests/lib/Route/RouterTest.php index 6f253d9121f0a..f99ebe4767ffe 100644 --- a/tests/lib/Route/RouterTest.php +++ b/tests/lib/Route/RouterTest.php @@ -63,6 +63,9 @@ public function testGenerateConsecutively(): void { $this->appManager->expects(self::atLeastOnce()) ->method('getAppPath') ->willReturnCallback(fn (string $appid): string => \OC::$SERVERROOT . '/apps/' . $appid); + $this->appManager->expects(self::atLeastOnce()) + ->method('isAppLoaded') + ->willReturn(true); $this->assertEquals('/index.php/apps/files/', $this->router->generate('files.view.index'));