3030use SimpleXMLElement ;
3131
3232class ExAppService {
33- private ICache $ cache ;
33+ private ? ICache $ cache = null ;
3434
3535 public function __construct (
3636 private readonly LoggerInterface $ logger ,
@@ -54,7 +54,9 @@ public function __construct(
5454 private readonly ExAppEventsListenerService $ eventsListenerService ,
5555 private readonly ExAppOccService $ occService ,
5656 ) {
57- $ this ->cache = $ cacheFactory ->createDistributed (Application::APP_ID . '/service ' );
57+ if ($ cacheFactory ->isAvailable ()) {
58+ $ this ->cache = $ cacheFactory ->createDistributed (Application::APP_ID . '/service ' );
59+ }
5860 }
5961
6062 public function getExApp (string $ appId ): ?ExApp {
@@ -83,7 +85,7 @@ public function registerExApp(array $appInfo): ?ExApp {
8385 try {
8486 $ this ->exAppMapper ->insert ($ exApp );
8587 $ exApp = $ this ->exAppMapper ->findByAppId ($ appInfo ['id ' ]);
86- $ this ->cache ->remove ('/ex_apps ' );
88+ $ this ->cache ? ->remove('/ex_apps ' );
8789 if (isset ($ appInfo ['external-app ' ]['routes ' ])) {
8890 $ exApp ->setRoutes ($ this ->registerExAppRoutes ($ exApp , $ appInfo ['external-app ' ]['routes ' ])->getRoutes () ?? []);
8991 }
@@ -121,7 +123,7 @@ public function unregisterExApp(string $appId): bool {
121123 if ($ rmRoutes === null ) {
122124 $ this ->logger ->error (sprintf ('Error while unregistering %s ExApp routes from the database. ' , $ appId ));
123125 }
124- $ this ->cache ->remove ('/ex_apps ' );
126+ $ this ->cache ? ->remove('/ex_apps ' );
125127 return $ r === 1 && $ rmRoutes !== null ;
126128 }
127129
@@ -208,14 +210,14 @@ public function updateExAppInfo(ExApp $exApp, array $appInfo): bool {
208210 public function updateExApp (ExApp $ exApp , array $ fields = ['version ' , 'name ' , 'port ' , 'status ' , 'enabled ' , 'last_check_time ' , 'api_scopes ' ]): bool {
209211 try {
210212 $ this ->exAppMapper ->updateExApp ($ exApp , $ fields );
211- $ this ->cache ->remove ('/ex_apps ' );
213+ $ this ->cache ? ->remove('/ex_apps ' );
212214 if (in_array ('enabled ' , $ fields ) || in_array ('version ' , $ fields )) {
213215 $ this ->resetCaches ();
214216 }
215217 return true ;
216218 } catch (Exception $ e ) {
217219 $ this ->logger ->error (sprintf ('Failed to update "%s" ExApp info. ' , $ exApp ->getAppid ()), ['exception ' => $ e ]);
218- $ this ->cache ->remove ('/ex_apps ' );
220+ $ this ->cache ? ->remove('/ex_apps ' );
219221 $ this ->resetCaches ();
220222 }
221223 return false ;
@@ -372,14 +374,14 @@ public function setStatusError(ExApp $exApp, string $error): void {
372374 public function getExApps (): array {
373375 try {
374376 $ cacheKey = '/ex_apps ' ;
375- $ records = $ this ->cache ->get ($ cacheKey );
377+ $ records = $ this ->cache ? ->get($ cacheKey );
376378 if ($ records !== null ) {
377379 return array_map (function ($ record ) {
378380 return $ record instanceof ExApp ? $ record : new ExApp ($ record );
379381 }, $ records );
380382 }
381383 $ records = $ this ->exAppMapper ->findAll ();
382- $ this ->cache ->set ($ cacheKey , $ records );
384+ $ this ->cache ? ->set($ cacheKey , $ records );
383385 return $ records ;
384386 } catch (Exception ) {
385387 return [];
0 commit comments