diff options
Diffstat (limited to 'lib')
4 files changed, 73 insertions, 16 deletions
diff --git a/lib/private/AppFramework/Bootstrap/RegistrationContext.php b/lib/private/AppFramework/Bootstrap/RegistrationContext.php index c98f968c999..3ade98e334f 100644 --- a/lib/private/AppFramework/Bootstrap/RegistrationContext.php +++ b/lib/private/AppFramework/Bootstrap/RegistrationContext.php @@ -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 */ @@ -692,6 +707,13 @@ class RegistrationContext { } /** + * @return ServiceRegistration<IReferenceProvider>[] + */ + public function getReferenceProviders(): array { + return $this->referenceProviders; + } + + /** * @return ServiceRegistration<ILinkAction>[] */ public function getProfileLinkActions(): array { diff --git a/lib/private/Collaboration/Reference/ReferenceManager.php b/lib/private/Collaboration/Reference/ReferenceManager.php index 20a5d2eb5b7..1bd9bfac0b8 100644 --- a/lib/private/Collaboration/Reference/ReferenceManager.php +++ b/lib/private/Collaboration/Reference/ReferenceManager.php @@ -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; } } diff --git a/lib/public/AppFramework/Bootstrap/IRegistrationContext.php b/lib/public/AppFramework/Bootstrap/IRegistrationContext.php index 6b10d7bfc0f..0f398c13979 100644 --- a/lib/public/AppFramework/Bootstrap/IRegistrationContext.php +++ b/lib/public/AppFramework/Bootstrap/IRegistrationContext.php @@ -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; @@ -255,6 +256,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 * diff --git a/lib/public/Collaboration/Reference/IReferenceManager.php b/lib/public/Collaboration/Reference/IReferenceManager.php index dda0d030785..8d2110911d3 100644 --- a/lib/public/Collaboration/Reference/IReferenceManager.php +++ b/lib/public/Collaboration/Reference/IReferenceManager.php @@ -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; } |