]> source.dussan.org Git - nextcloud-server.git/commitdiff
Make the subscription registry lazy
authorRoeland Jago Douma <roeland@famdouma.nl>
Wed, 8 Jul 2020 17:38:26 +0000 (19:38 +0200)
committerRoeland Jago Douma <roeland@famdouma.nl>
Thu, 9 Jul 2020 10:21:06 +0000 (12:21 +0200)
This will allow to do lazy registration here which should allow for
loading less (or at least only when needed!).

Signed-off-by: Roeland Jago Douma <roeland@famdouma.nl>
lib/private/Support/Subscription/Registry.php
lib/public/Support/Subscription/IRegistry.php
tests/lib/Support/Subscription/RegistryTest.php

index f0d946a29115c1cc077bce4ae6425e99975f3895..9f5d78bebb29037302ab6b70a9c1d825bc4312eb 100644 (file)
@@ -27,7 +27,9 @@ declare(strict_types=1);
 
 namespace OC\Support\Subscription;
 
+use OCP\AppFramework\QueryException;
 use OCP\IConfig;
+use OCP\IServerContainer;
 use OCP\Support\Subscription\Exception\AlreadyRegisteredException;
 use OCP\Support\Subscription\IRegistry;
 use OCP\Support\Subscription\ISubscription;
@@ -38,11 +40,30 @@ class Registry implements IRegistry {
        /** @var ISubscription */
        private $subscription = null;
 
+       /** @var string */
+       private $subscriptionService = null;
+
        /** @var IConfig */
        private $config;
 
-       public function __construct(IConfig $config) {
+       /** @var IServerContainer */
+       private $container;
+
+       public function __construct(IConfig $config, IServerContainer $container) {
                $this->config = $config;
+               $this->container = $container;
+       }
+
+       private function getSubscription(): ?ISubscription {
+               if ($this->subscription === null && $this->subscriptionService !== null) {
+                       try {
+                               $this->subscription = $this->container->query($this->subscriptionService);
+                       } catch (QueryException $e) {
+                               // Ignore this
+                       }
+               }
+
+               return $this->subscription;
        }
 
        /**
@@ -55,20 +76,29 @@ class Registry implements IRegistry {
         * @since 17.0.0
         */
        public function register(ISubscription $subscription): void {
-               if ($this->subscription !== null) {
+               if ($this->subscription !== null || $this->subscriptionService !== null) {
                        throw new AlreadyRegisteredException();
                }
                $this->subscription = $subscription;
        }
 
+       public function registerService(string $subscriptionService): void {
+               if ($this->subscription !== null || $this->subscriptionService !== null) {
+                       throw new AlreadyRegisteredException();
+               }
+
+               $this->subscriptionService = $subscriptionService;
+       }
+
+
        /**
         * Fetches the list of app IDs that are supported by the subscription
         *
         * @since 17.0.0
         */
        public function delegateGetSupportedApps(): array {
-               if ($this->subscription instanceof ISupportedApps) {
-                       return $this->subscription->getSupportedApps();
+               if ($this->getSubscription() instanceof ISupportedApps) {
+                       return $this->getSubscription()->getSupportedApps();
                }
                return [];
        }
@@ -84,8 +114,8 @@ class Registry implements IRegistry {
                        return true;
                }
 
-               if ($this->subscription instanceof ISubscription) {
-                       return $this->subscription->hasValidSubscription();
+               if ($this->getSubscription() instanceof ISubscription) {
+                       return $this->getSubscription()->hasValidSubscription();
                }
                return false;
        }
@@ -96,8 +126,8 @@ class Registry implements IRegistry {
         * @since 17.0.0
         */
        public function delegateHasExtendedSupport(): bool {
-               if ($this->subscription instanceof ISubscription && method_exists($this->subscription, 'hasExtendedSupport')) {
-                       return $this->subscription->hasExtendedSupport();
+               if ($this->getSubscription() instanceof ISubscription && method_exists($this->subscription, 'hasExtendedSupport')) {
+                       return $this->getSubscription()->hasExtendedSupport();
                }
                return false;
        }
index ec491c370f1c6513682bcd3a1e56e7841c458ea6..430ddbcf8d1ded76bac33b35fe09eaa24c751eb5 100644 (file)
@@ -35,16 +35,28 @@ use OCP\Support\Subscription\Exception\AlreadyRegisteredException;
 interface IRegistry {
 
        /**
-        * Register a subscription instance. In case it is called multiple times the
-        * first one is used.
+        * Register a subscription instance. In case it is called multiple times an
+        * exception is thrown
         *
         * @param ISubscription $subscription
         * @throws AlreadyRegisteredException
         *
         * @since 17.0.0
+        * @deprecated 20.0.0 use registerService
         */
        public function register(ISubscription $subscription): void;
 
+       /**
+        * Register a subscription handler. The service has to implement the ISubscription interface.
+        * In case this is called multiple times an exception is thrown.
+        *
+        * @param string $subscriptionService
+        * @throws AlreadyRegisteredException
+        *
+        * @since 20.0.0
+        */
+       public function registerService(string $subscriptionService): void;
+
        /**
         * Fetches the list of app IDs that are supported by the subscription
         *
index 3e316792682443bd36e0490360747916e2202221..2548cfe817f0f59f20f62b424a039db049b46a38 100644 (file)
@@ -24,6 +24,7 @@ namespace Test\Support\Subscription;
 
 use OC\Support\Subscription\Registry;
 use OCP\IConfig;
+use OCP\IServerContainer;
 use OCP\Support\Subscription\ISubscription;
 use OCP\Support\Subscription\ISupportedApps;
 use PHPUnit\Framework\MockObject\MockObject;
@@ -37,11 +38,15 @@ class RegistryTest extends TestCase {
        /** @var MockObject|IConfig */
        private $config;
 
+       /** @var MockObject|IServerContainer */
+       private $serverContainer;
+
        protected function setUp(): void {
                parent::setUp();
 
                $this->config = $this->createMock(IConfig::class);
-               $this->registry = new Registry($this->config);
+               $this->serverContainer = $this->createMock(IServerContainer::class);
+               $this->registry = new Registry($this->config, $this->serverContainer);
        }
 
        /**
@@ -52,7 +57,7 @@ class RegistryTest extends TestCase {
                $this->addToAssertionCount(1);
        }
 
-       
+
        public function testDoubleRegistration() {
                $this->expectException(\OCP\Support\Subscription\Exception\AlreadyRegisteredException::class);