fix: Lazy load IURLGenerator from AppManager to avoid installation crash

Signed-off-by: Côme Chilliet <come.chilliet@nextcloud.com>
This commit is contained in:
Côme Chilliet 2024-04-22 15:50:06 +02:00
parent 61621ee1d0
commit ac1d868d18
No known key found for this signature in database
GPG Key ID: A3E2F658B28C760A
3 changed files with 19 additions and 10 deletions

View File

@ -98,7 +98,12 @@ class AppManager implements IAppManager {
private array $loadedApps = []; private array $loadedApps = [];
private ?AppConfig $appConfig = null; private ?AppConfig $appConfig = null;
private ?IURLGenerator $urlGenerator = null;
/**
* Be extremely careful when injecting classes here. The AppManager is used by the installer,
* so it needs to work before installation. See how AppConfig and IURLGenerator are injected for reference
*/
public function __construct( public function __construct(
private IUserSession $userSession, private IUserSession $userSession,
private IConfig $config, private IConfig $config,
@ -106,7 +111,6 @@ class AppManager implements IAppManager {
private ICacheFactory $memCacheFactory, private ICacheFactory $memCacheFactory,
private IEventDispatcher $dispatcher, private IEventDispatcher $dispatcher,
private LoggerInterface $logger, private LoggerInterface $logger,
private IURLGenerator $urlGenerator,
) { ) {
} }
@ -115,7 +119,7 @@ class AppManager implements IAppManager {
$icon = null; $icon = null;
foreach ($possibleIcons as $iconName) { foreach ($possibleIcons as $iconName) {
try { try {
$icon = $this->urlGenerator->imagePath($appId, $iconName); $icon = $this->getUrlGenerator()->imagePath($appId, $iconName);
break; break;
} catch (\RuntimeException $e) { } catch (\RuntimeException $e) {
// ignore // ignore
@ -135,6 +139,17 @@ class AppManager implements IAppManager {
return $this->appConfig; return $this->appConfig;
} }
private function getUrlGenerator(): IURLGenerator {
if ($this->urlGenerator !== null) {
return $this->urlGenerator;
}
if (!$this->config->getSystemValueBool('installed', false)) {
throw new \Exception('Nextcloud is not installed yet, AppConfig is not available');
}
$this->urlGenerator = \OCP\Server::get(IURLGenerator::class);
return $this->urlGenerator;
}
/** /**
* @return string[] $appId => $enabled * @return string[] $appId => $enabled
*/ */

View File

@ -897,7 +897,6 @@ class Server extends ServerContainer implements IServerContainer {
$c->get(ICacheFactory::class), $c->get(ICacheFactory::class),
$c->get(IEventDispatcher::class), $c->get(IEventDispatcher::class),
$c->get(LoggerInterface::class), $c->get(LoggerInterface::class),
$c->get(IURLGenerator::class),
); );
}); });
/** @deprecated 19.0.0 */ /** @deprecated 19.0.0 */

View File

@ -99,7 +99,7 @@ class AppManagerTest extends TestCase {
/** @var LoggerInterface|MockObject */ /** @var LoggerInterface|MockObject */
protected $logger; protected $logger;
protected IURLGenerator|MockObject $urlGenerator; protected IURLGenerator&MockObject $urlGenerator;
/** @var IAppManager */ /** @var IAppManager */
protected $manager; protected $manager;
@ -118,6 +118,7 @@ class AppManagerTest extends TestCase {
$this->urlGenerator = $this->createMock(IURLGenerator::class); $this->urlGenerator = $this->createMock(IURLGenerator::class);
$this->overwriteService(AppConfig::class, $this->appConfig); $this->overwriteService(AppConfig::class, $this->appConfig);
$this->overwriteService(IURLGenerator::class, $this->urlGenerator);
$this->cacheFactory->expects($this->any()) $this->cacheFactory->expects($this->any())
->method('createDistributed') ->method('createDistributed')
@ -136,7 +137,6 @@ class AppManagerTest extends TestCase {
$this->cacheFactory, $this->cacheFactory,
$this->eventDispatcher, $this->eventDispatcher,
$this->logger, $this->logger,
$this->urlGenerator,
); );
} }
@ -279,7 +279,6 @@ class AppManagerTest extends TestCase {
$this->cacheFactory, $this->cacheFactory,
$this->eventDispatcher, $this->eventDispatcher,
$this->logger, $this->logger,
$this->urlGenerator,
]) ])
->onlyMethods([ ->onlyMethods([
'getAppPath', 'getAppPath',
@ -333,7 +332,6 @@ class AppManagerTest extends TestCase {
$this->cacheFactory, $this->cacheFactory,
$this->eventDispatcher, $this->eventDispatcher,
$this->logger, $this->logger,
$this->urlGenerator,
]) ])
->onlyMethods([ ->onlyMethods([
'getAppPath', 'getAppPath',
@ -395,7 +393,6 @@ class AppManagerTest extends TestCase {
$this->cacheFactory, $this->cacheFactory,
$this->eventDispatcher, $this->eventDispatcher,
$this->logger, $this->logger,
$this->urlGenerator,
]) ])
->onlyMethods([ ->onlyMethods([
'getAppPath', 'getAppPath',
@ -598,7 +595,6 @@ class AppManagerTest extends TestCase {
$this->cacheFactory, $this->cacheFactory,
$this->eventDispatcher, $this->eventDispatcher,
$this->logger, $this->logger,
$this->urlGenerator,
]) ])
->onlyMethods(['getAppInfo']) ->onlyMethods(['getAppInfo'])
->getMock(); ->getMock();
@ -657,7 +653,6 @@ class AppManagerTest extends TestCase {
$this->cacheFactory, $this->cacheFactory,
$this->eventDispatcher, $this->eventDispatcher,
$this->logger, $this->logger,
$this->urlGenerator,
]) ])
->onlyMethods(['getAppInfo']) ->onlyMethods(['getAppInfo'])
->getMock(); ->getMock();