]> source.dussan.org Git - nextcloud-server.git/commitdiff
Move registration to IBootstrap
authorJulius Härtl <jus@bitgrid.net>
Mon, 15 Aug 2022 08:02:14 +0000 (10:02 +0200)
committerJulius Härtl <jus@bitgrid.net>
Wed, 31 Aug 2022 14:20:06 +0000 (16:20 +0200)
Signed-off-by: Julius Härtl <jus@bitgrid.net>
lib/private/AppFramework/Bootstrap/RegistrationContext.php
lib/private/Collaboration/Reference/ReferenceManager.php
lib/public/AppFramework/Bootstrap/IRegistrationContext.php
lib/public/Collaboration/Reference/IReferenceManager.php

index c98f968c999854db9fe3ffa29a0b7ff12753eb18..3ade98e334f4de52f1cf2a11e37b5ef14fad6d09 100644 (file)
@@ -32,6 +32,7 @@ namespace OC\AppFramework\Bootstrap;
 use Closure;
 use OCP\Calendar\Resource\IBackend as IResourceBackend;
 use OCP\Calendar\Room\IBackend as IRoomBackend;
+use OCP\Collaboration\Reference\IReferenceProvider;
 use OCP\Talk\ITalkBackend;
 use RuntimeException;
 use function array_shift;
@@ -121,6 +122,9 @@ class RegistrationContext {
        /** @var ServiceRegistration<ICalendarProvider>[] */
        private $calendarProviders = [];
 
+       /** @var ServiceRegistration<IReferenceProvider>[] */
+       private array $referenceProviders = [];
+
        /** @var ParameterRegistration[] */
        private $sensitiveMethods = [];
 
@@ -273,6 +277,13 @@ class RegistrationContext {
                                );
                        }
 
+                       public function registerReferenceProvider(string $class): void {
+                               $this->context->registerReferenceProvider(
+                                       $this->appId,
+                                       $class
+                               );
+                       }
+
                        public function registerProfileLinkAction(string $actionClass): void {
                                $this->context->registerProfileLinkAction(
                                        $this->appId,
@@ -398,6 +409,10 @@ class RegistrationContext {
                $this->calendarProviders[] = new ServiceRegistration($appId, $class);
        }
 
+       public function registerReferenceProvider(string $appId, string $class): void {
+               $this->referenceProviders[] = new ServiceRegistration($appId, $class);
+       }
+
        /**
         * @psalm-param class-string<ILinkAction> $actionClass
         */
@@ -691,6 +706,13 @@ class RegistrationContext {
                return $this->calendarProviders;
        }
 
+       /**
+        * @return ServiceRegistration<IReferenceProvider>[]
+        */
+       public function getReferenceProviders(): array {
+               return $this->referenceProviders;
+       }
+
        /**
         * @return ServiceRegistration<ILinkAction>[]
         */
index 20a5d2eb5b74cc15df4e990776e74f678eea9771..1bd9bfac0b83ddcc248ffc60fc87e23245343133 100644 (file)
@@ -24,26 +24,31 @@ declare(strict_types=1);
 
 namespace OC\Collaboration\Reference;
 
+use OC\AppFramework\Bootstrap\Coordinator;
 use OCP\Collaboration\Reference\IReference;
 use OCP\Collaboration\Reference\IReferenceManager;
 use OCP\Collaboration\Reference\IReferenceProvider;
 use OCP\ICache;
 use OCP\ICacheFactory;
+use Psr\Container\ContainerInterface;
+use Throwable;
+use function OCP\Log\logger;
 
 class ReferenceManager implements IReferenceManager {
        public const URL_PATTERN = '/(\s|\n|^)(https?:\/\/)?((?:[-A-Z0-9+_]+\.)+[-A-Z]+(?:\/[-A-Z0-9+&@#%?=~_|!:,.;()]*)*)(\s|\n|$)/mi';
        public const CACHE_TTL = 60;
 
-       /** @var IReferenceProvider[] */
-       private array $providers = [];
+       /** @var IReferenceProvider[]|null */
+       private ?array $providers = null;
        private ICache $cache;
+       private Coordinator $coordinator;
+       private ContainerInterface $container;
 
-       private LinkReferenceProvider $linkReferenceProvider;
-
-       public function __construct(LinkReferenceProvider $linkReferenceProvider, FileReferenceProvider $fileReferenceProvider, ICacheFactory $cacheFactory) {
-               $this->registerReferenceProvider($fileReferenceProvider);
+       public function __construct(LinkReferenceProvider $linkReferenceProvider, ICacheFactory $cacheFactory, Coordinator $coordinator, ContainerInterface $container) {
                $this->linkReferenceProvider = $linkReferenceProvider;
                $this->cache = $cacheFactory->createDistributed('reference');
+               $this->coordinator = $coordinator;
+               $this->container = $container;
        }
 
        public function extractReferences(string $text): array {
@@ -87,7 +92,7 @@ class ReferenceManager implements IReferenceManager {
 
        private function getMatchedProvider(string $referenceId): ?IReferenceProvider {
                $matchedProvider = null;
-               foreach ($this->providers as $provider) {
+               foreach ($this->getProviders() as $provider) {
                        $matchedProvider = $provider->matchReference($referenceId) ? $provider : null;
                }
 
@@ -121,7 +126,34 @@ class ReferenceManager implements IReferenceManager {
                $this->cache->remove($this->getCacheKey($matchedProvider, $referenceId));
        }
 
-       public function registerReferenceProvider(IReferenceProvider $provider): void {
-               $this->providers[] = $provider;
+       /**
+        * @return IReferenceProvider[]
+        */
+       public function getProviders(): array {
+               if ($this->providers === null) {
+                       $context = $this->coordinator->getRegistrationContext();
+                       if ($context === null) {
+                               return [];
+                       }
+
+                       $this->providers = array_filter(array_map(function ($registration): ?IReferenceProvider {
+                               try {
+                                       /** @var IReferenceProvider $provider */
+                                       $provider = $this->container->get($registration->getService());
+                               } catch (Throwable $e) {
+                                       logger()->error('Could not load reference provider ' . $registration->getService() . ': ' . $e->getMessage(), [
+                                               'exception' => $e,
+                                       ]);
+                                       return null;
+                               }
+
+                               return $provider;
+                       }, $context->getReferenceProviders()));
+
+                       // TODO: Move to files app
+                       $this->providers[] = $this->container->get(FileReferenceProvider::class);
+               }
+
+               return $this->providers;
        }
 }
index 6b10d7bfc0fe993635e0dfe0ebd551fa43a1b296..0f398c13979bb2016da87d60edaceb64f97a1ed6 100644 (file)
@@ -33,6 +33,7 @@ use OCP\AppFramework\IAppContainer;
 use OCP\Authentication\TwoFactorAuth\IProvider;
 use OCP\Calendar\ICalendarProvider;
 use OCP\Capabilities\ICapability;
+use OCP\Collaboration\Reference\IReferenceProvider;
 use OCP\EventDispatcher\IEventDispatcher;
 use OCP\Files\Template\ICustomTemplateProvider;
 use OCP\IContainer;
@@ -254,6 +255,15 @@ interface IRegistrationContext {
         */
        public function registerCalendarProvider(string $class): void;
 
+       /**
+        * Register a reference provider
+        *
+        * @param string $class
+        * @psalm-param class-string<IReferenceProvider> $class
+        * @since 25.0.0
+        */
+       public function registerReferenceProvider(string $class): void;
+
        /**
         * Register an implementation of \OCP\Profile\ILinkAction that
         * will handle the implementation of a profile link action
index dda0d0307852dc97b510be02944acdbb081a4965..8d2110911d33bbf8157053f0fa021c899bc1778c 100644 (file)
@@ -41,11 +41,4 @@ interface IReferenceManager {
         * @since 25.0.0
         */
        public function resolveReference(string $reference): ?IReference;
-
-       /**
-        * Register a new reference provider
-        *
-        * @since 25.0.0
-        */
-       public function registerReferenceProvider(IReferenceProvider $provider): void;
 }