diff options
author | Roeland Jago Douma <rullzer@users.noreply.github.com> | 2020-07-09 15:43:48 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-07-09 15:43:48 +0200 |
commit | 10d862e243a00b5ad0d540ade3dbabc6b543da8c (patch) | |
tree | de850d7db58f87817262a42c3b3aed6cd1e8090c | |
parent | 18acb137d3b973f04ee039fd4a4e8ed1bca25127 (diff) | |
parent | d9f5fdb67a1f06f19d505d7ee76e05163af7e6ce (diff) | |
download | nextcloud-server-10d862e243a00b5ad0d540ade3dbabc6b543da8c.tar.gz nextcloud-server-10d862e243a00b5ad0d540ade3dbabc6b543da8c.zip |
Merge pull request #21759 from nextcloud/enh/lazy_subscription
Make the subscription registry lazy
-rw-r--r-- | lib/private/Support/Subscription/Registry.php | 46 | ||||
-rw-r--r-- | lib/public/Support/Subscription/IRegistry.php | 16 | ||||
-rw-r--r-- | tests/lib/Support/Subscription/DummySubscription.php | 58 | ||||
-rw-r--r-- | tests/lib/Support/Subscription/RegistryTest.php | 19 |
4 files changed, 127 insertions, 12 deletions
diff --git a/lib/private/Support/Subscription/Registry.php b/lib/private/Support/Subscription/Registry.php index f0d946a2911..9f5d78bebb2 100644 --- a/lib/private/Support/Subscription/Registry.php +++ b/lib/private/Support/Subscription/Registry.php @@ -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; } diff --git a/lib/public/Support/Subscription/IRegistry.php b/lib/public/Support/Subscription/IRegistry.php index ec491c370f1..430ddbcf8d1 100644 --- a/lib/public/Support/Subscription/IRegistry.php +++ b/lib/public/Support/Subscription/IRegistry.php @@ -35,17 +35,29 @@ 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 * * @since 17.0.0 diff --git a/tests/lib/Support/Subscription/DummySubscription.php b/tests/lib/Support/Subscription/DummySubscription.php new file mode 100644 index 00000000000..e1f7f5c6b61 --- /dev/null +++ b/tests/lib/Support/Subscription/DummySubscription.php @@ -0,0 +1,58 @@ +<?php + +declare(strict_types=1); + +/** + * @author Daniel Kesselberg <mail@danielkesselberg.de> + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ + +namespace Test\Support\Subscription; + +class DummySubscription implements \OCP\Support\Subscription\ISubscription { + + /** @var bool */ + private $hasValidSubscription; + /** @var bool */ + private $hasExtendedSupport; + + /** + * DummySubscription constructor. + * + * @param bool $hasValidSubscription + * @param bool $hasExtendedSupport + */ + public function __construct(bool $hasValidSubscription, bool $hasExtendedSupport) { + $this->hasValidSubscription = $hasValidSubscription; + $this->hasExtendedSupport = $hasExtendedSupport; + } + + /** + * @inheritDoc + */ + public function hasValidSubscription(): bool { + return $this->hasValidSubscription; + } + + /** + * @inheritDoc + */ + public function hasExtendedSupport(): bool { + return $this->hasExtendedSupport; + } +} diff --git a/tests/lib/Support/Subscription/RegistryTest.php b/tests/lib/Support/Subscription/RegistryTest.php index 3e316792682..68159aa1b2b 100644 --- a/tests/lib/Support/Subscription/RegistryTest.php +++ b/tests/lib/Support/Subscription/RegistryTest.php @@ -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); @@ -112,4 +117,14 @@ class RegistryTest extends TestCase { $this->assertSame(['abc'], $this->registry->delegateGetSupportedApps()); } + + public function testSubscriptionService() { + $this->serverContainer->method('query') + ->with(DummySubscription::class) + ->willReturn(new DummySubscription(true, false)); + $this->registry->registerService(DummySubscription::class); + + $this->assertTrue($this->registry->delegateHasValidSubscription()); + $this->assertFalse($this->registry->delegateHasExtendedSupport()); + } } |