@@ -97,15 +97,20 @@ class AppManager implements IAppManager {
9797 /** @var array<string, true> */
9898 private array $ loadedApps = [];
9999
100+ private ?AppConfig $ appConfig = null ;
101+ private ?IURLGenerator $ urlGenerator = null ;
102+
103+ /**
104+ * Be extremely careful when injecting classes here. The AppManager is used by the installer,
105+ * so it needs to work before installation. See how AppConfig and IURLGenerator are injected for reference
106+ */
100107 public function __construct (
101108 private IUserSession $ userSession ,
102109 private IConfig $ config ,
103- private AppConfig $ appConfig ,
104110 private IGroupManager $ groupManager ,
105111 private ICacheFactory $ memCacheFactory ,
106112 private IEventDispatcher $ dispatcher ,
107113 private LoggerInterface $ logger ,
108- private IURLGenerator $ urlGenerator ,
109114 ) {
110115 }
111116
@@ -114,7 +119,7 @@ public function getAppIcon(string $appId, bool $dark = false): ?string {
114119 $ icon = null ;
115120 foreach ($ possibleIcons as $ iconName ) {
116121 try {
117- $ icon = $ this ->urlGenerator ->imagePath ($ appId , $ iconName );
122+ $ icon = $ this ->getUrlGenerator () ->imagePath ($ appId , $ iconName );
118123 break ;
119124 } catch (\RuntimeException $ e ) {
120125 // ignore
@@ -123,12 +128,34 @@ public function getAppIcon(string $appId, bool $dark = false): ?string {
123128 return $ icon ;
124129 }
125130
131+ private function getAppConfig (): AppConfig {
132+ if ($ this ->appConfig !== null ) {
133+ return $ this ->appConfig ;
134+ }
135+ if (!$ this ->config ->getSystemValueBool ('installed ' , false )) {
136+ throw new \Exception ('Nextcloud is not installed yet, AppConfig is not available ' );
137+ }
138+ $ this ->appConfig = \OCP \Server::get (AppConfig::class);
139+ return $ this ->appConfig ;
140+ }
141+
142+ private function getUrlGenerator (): IURLGenerator {
143+ if ($ this ->urlGenerator !== null ) {
144+ return $ this ->urlGenerator ;
145+ }
146+ if (!$ this ->config ->getSystemValueBool ('installed ' , false )) {
147+ throw new \Exception ('Nextcloud is not installed yet, AppConfig is not available ' );
148+ }
149+ $ this ->urlGenerator = \OCP \Server::get (IURLGenerator::class);
150+ return $ this ->urlGenerator ;
151+ }
152+
126153 /**
127154 * @return string[] $appId => $enabled
128155 */
129156 private function getInstalledAppsValues (): array {
130157 if (!$ this ->installedAppsCache ) {
131- $ values = $ this ->appConfig ->getValues (false , 'enabled ' );
158+ $ values = $ this ->getAppConfig () ->getValues (false , 'enabled ' );
132159
133160 $ alwaysEnabledApps = $ this ->getAlwaysEnabledApps ();
134161 foreach ($ alwaysEnabledApps as $ appId ) {
@@ -253,7 +280,7 @@ public function isType(string $app, array $types): bool {
253280 private function getAppTypes (string $ app ): array {
254281 //load the cache
255282 if (count ($ this ->appTypes ) === 0 ) {
256- $ this ->appTypes = $ this ->appConfig ->getValues (false , 'types ' ) ?: [];
283+ $ this ->appTypes = $ this ->getAppConfig () ->getValues (false , 'types ' ) ?: [];
257284 }
258285
259286 if (isset ($ this ->appTypes [$ app ])) {
@@ -541,7 +568,7 @@ public function enableApp(string $appId, bool $forceEnable = false): void {
541568 }
542569
543570 $ this ->installedAppsCache [$ appId ] = 'yes ' ;
544- $ this ->appConfig ->setValue ($ appId , 'enabled ' , 'yes ' );
571+ $ this ->getAppConfig () ->setValue ($ appId , 'enabled ' , 'yes ' );
545572 $ this ->dispatcher ->dispatchTyped (new AppEnableEvent ($ appId ));
546573 $ this ->dispatcher ->dispatch (ManagerEvent::EVENT_APP_ENABLE , new ManagerEvent (
547574 ManagerEvent::EVENT_APP_ENABLE , $ appId
@@ -595,7 +622,7 @@ public function enableAppForGroups(string $appId, array $groups, bool $forceEnab
595622 }, $ groups );
596623
597624 $ this ->installedAppsCache [$ appId ] = json_encode ($ groupIds );
598- $ this ->appConfig ->setValue ($ appId , 'enabled ' , json_encode ($ groupIds ));
625+ $ this ->getAppConfig () ->setValue ($ appId , 'enabled ' , json_encode ($ groupIds ));
599626 $ this ->dispatcher ->dispatchTyped (new AppEnableEvent ($ appId , $ groupIds ));
600627 $ this ->dispatcher ->dispatch (ManagerEvent::EVENT_APP_ENABLE_FOR_GROUPS , new ManagerEvent (
601628 ManagerEvent::EVENT_APP_ENABLE_FOR_GROUPS , $ appId , $ groups
@@ -616,15 +643,15 @@ public function disableApp($appId, $automaticDisabled = false) {
616643 }
617644
618645 if ($ automaticDisabled ) {
619- $ previousSetting = $ this ->appConfig ->getValue ($ appId , 'enabled ' , 'yes ' );
646+ $ previousSetting = $ this ->getAppConfig () ->getValue ($ appId , 'enabled ' , 'yes ' );
620647 if ($ previousSetting !== 'yes ' && $ previousSetting !== 'no ' ) {
621648 $ previousSetting = json_decode ($ previousSetting , true );
622649 }
623650 $ this ->autoDisabledApps [$ appId ] = $ previousSetting ;
624651 }
625652
626653 unset($ this ->installedAppsCache [$ appId ]);
627- $ this ->appConfig ->setValue ($ appId , 'enabled ' , 'no ' );
654+ $ this ->getAppConfig () ->setValue ($ appId , 'enabled ' , 'no ' );
628655
629656 // run uninstall steps
630657 $ appData = $ this ->getAppInfo ($ appId );
@@ -689,7 +716,7 @@ public function getAppsNeedingUpgrade($version) {
689716 $ apps = $ this ->getInstalledApps ();
690717 foreach ($ apps as $ appId ) {
691718 $ appInfo = $ this ->getAppInfo ($ appId );
692- $ appDbVersion = $ this ->appConfig ->getValue ($ appId , 'installed_version ' );
719+ $ appDbVersion = $ this ->getAppConfig () ->getValue ($ appId , 'installed_version ' );
693720 if ($ appDbVersion
694721 && isset ($ appInfo ['version ' ])
695722 && version_compare ($ appInfo ['version ' ], $ appDbVersion , '> ' )
0 commit comments