From 8bc25e33243a914a44e86ef4ba060ab6a8bd3237 Mon Sep 17 00:00:00 2001 From: Roeland Jago Douma Date: Sun, 4 Oct 2020 14:41:08 +0200 Subject: [PATCH] Move preview provider registration to bootstrap Signed-off-by: Roeland Jago Douma Signed-off-by: Christoph Wurst --- lib/composer/composer/autoload_classmap.php | 1 + lib/composer/composer/autoload_static.php | 1 + .../Bootstrap/PreviewProviderRegistration.php | 47 ++++++++++++++++ .../Bootstrap/RegistrationContext.php | 22 ++++++++ lib/private/PreviewManager.php | 53 ++++++++++++++++++- lib/private/Server.php | 5 +- .../Bootstrap/IRegistrationContext.php | 13 +++++ lib/public/IPreview.php | 3 ++ 8 files changed, 143 insertions(+), 2 deletions(-) create mode 100644 lib/private/AppFramework/Bootstrap/PreviewProviderRegistration.php diff --git a/lib/composer/composer/autoload_classmap.php b/lib/composer/composer/autoload_classmap.php index 72e3ff8a8e4..f40f3c92be7 100644 --- a/lib/composer/composer/autoload_classmap.php +++ b/lib/composer/composer/autoload_classmap.php @@ -611,6 +611,7 @@ return array( 'OC\\AppFramework\\Bootstrap\\EventListenerRegistration' => $baseDir . '/lib/private/AppFramework/Bootstrap/EventListenerRegistration.php', 'OC\\AppFramework\\Bootstrap\\FunctionInjector' => $baseDir . '/lib/private/AppFramework/Bootstrap/FunctionInjector.php', 'OC\\AppFramework\\Bootstrap\\ParameterRegistration' => $baseDir . '/lib/private/AppFramework/Bootstrap/ParameterRegistration.php', + 'OC\\AppFramework\\Bootstrap\\PreviewProviderRegistration' => $baseDir . '/lib/private/AppFramework/Bootstrap/PreviewProviderRegistration.php', 'OC\\AppFramework\\Bootstrap\\RegistrationContext' => $baseDir . '/lib/private/AppFramework/Bootstrap/RegistrationContext.php', 'OC\\AppFramework\\Bootstrap\\ServiceAliasRegistration' => $baseDir . '/lib/private/AppFramework/Bootstrap/ServiceAliasRegistration.php', 'OC\\AppFramework\\Bootstrap\\ServiceFactoryRegistration' => $baseDir . '/lib/private/AppFramework/Bootstrap/ServiceFactoryRegistration.php', diff --git a/lib/composer/composer/autoload_static.php b/lib/composer/composer/autoload_static.php index c689a1c011e..13fffdd53ad 100644 --- a/lib/composer/composer/autoload_static.php +++ b/lib/composer/composer/autoload_static.php @@ -640,6 +640,7 @@ class ComposerStaticInit53792487c5a8370acc0b06b1a864ff4c 'OC\\AppFramework\\Bootstrap\\EventListenerRegistration' => __DIR__ . '/../../..' . '/lib/private/AppFramework/Bootstrap/EventListenerRegistration.php', 'OC\\AppFramework\\Bootstrap\\FunctionInjector' => __DIR__ . '/../../..' . '/lib/private/AppFramework/Bootstrap/FunctionInjector.php', 'OC\\AppFramework\\Bootstrap\\ParameterRegistration' => __DIR__ . '/../../..' . '/lib/private/AppFramework/Bootstrap/ParameterRegistration.php', + 'OC\\AppFramework\\Bootstrap\\PreviewProviderRegistration' => __DIR__ . '/../../..' . '/lib/private/AppFramework/Bootstrap/PreviewProviderRegistration.php', 'OC\\AppFramework\\Bootstrap\\RegistrationContext' => __DIR__ . '/../../..' . '/lib/private/AppFramework/Bootstrap/RegistrationContext.php', 'OC\\AppFramework\\Bootstrap\\ServiceAliasRegistration' => __DIR__ . '/../../..' . '/lib/private/AppFramework/Bootstrap/ServiceAliasRegistration.php', 'OC\\AppFramework\\Bootstrap\\ServiceFactoryRegistration' => __DIR__ . '/../../..' . '/lib/private/AppFramework/Bootstrap/ServiceFactoryRegistration.php', diff --git a/lib/private/AppFramework/Bootstrap/PreviewProviderRegistration.php b/lib/private/AppFramework/Bootstrap/PreviewProviderRegistration.php new file mode 100644 index 00000000000..47c25d39300 --- /dev/null +++ b/lib/private/AppFramework/Bootstrap/PreviewProviderRegistration.php @@ -0,0 +1,47 @@ + + * + * @author 2021 Christoph Wurst + * + * @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 . + */ + +namespace OC\AppFramework\Bootstrap; + +/** + * @psalm-immutable + * @template-extends ServiceRegistration<\OCP\Preview\IProviderV2> + */ +class PreviewProviderRegistration extends ServiceRegistration { + + /** @var string */ + private $mimeTypeRegex; + + public function __construct(string $appId, + string $service, + string $mimeTypeRegex) { + parent::__construct($appId, $service); + $this->mimeTypeRegex = $mimeTypeRegex; + } + + public function getMimeTypeRegex(): string { + return $this->mimeTypeRegex; + } +} diff --git a/lib/private/AppFramework/Bootstrap/RegistrationContext.php b/lib/private/AppFramework/Bootstrap/RegistrationContext.php index c638af94c84..256d3e2346b 100644 --- a/lib/private/AppFramework/Bootstrap/RegistrationContext.php +++ b/lib/private/AppFramework/Bootstrap/RegistrationContext.php @@ -107,6 +107,9 @@ class RegistrationContext { /** @var LoggerInterface */ private $logger; + /** @var PreviewProviderRegistration[] */ + private $previewProviders = []; + public function __construct(LoggerInterface $logger) { $this->logger = $logger; } @@ -235,6 +238,14 @@ class RegistrationContext { ); } + public function registerPreviewProvider(string $previewProviderClass, string $mimeTypeRegex): void { + $this->context->registerPreviewProvider( + $this->appId, + $previewProviderClass, + $mimeTypeRegex + ); + } + public function registerCalendarProvider(string $class): void { $this->context->registerCalendarProvider( $this->appId, @@ -323,6 +334,10 @@ class RegistrationContext { $this->twoFactorProviders[] = new ServiceRegistration($appId, $class); } + public function registerPreviewProvider(string $appId, string $class, string $mimeTypeRegex): void { + $this->previewProviders[] = new PreviewProviderRegistration($appId, $class, $mimeTypeRegex); + } + public function registerCalendarProvider(string $appId, string $class): void { $this->calendarProviders[] = new ServiceRegistration($appId, $class); } @@ -565,6 +580,13 @@ class RegistrationContext { return $this->twoFactorProviders; } + /** + * @return PreviewProviderRegistration[] + */ + public function getPreviewProviders(): array { + return $this->previewProviders; + } + /** * @return ServiceRegistration[] */ diff --git a/lib/private/PreviewManager.php b/lib/private/PreviewManager.php index a381ec8c23d..02d017a5017 100644 --- a/lib/private/PreviewManager.php +++ b/lib/private/PreviewManager.php @@ -30,8 +30,10 @@ */ namespace OC; +use OC\AppFramework\Bootstrap\Coordinator; use OC\Preview\Generator; use OC\Preview\GeneratorHelper; +use OCP\AppFramework\QueryException; use OCP\Files\File; use OCP\Files\IAppData; use OCP\Files\IRootFolder; @@ -39,8 +41,10 @@ use OCP\Files\NotFoundException; use OCP\Files\SimpleFS\ISimpleFile; use OCP\IConfig; use OCP\IPreview; +use OCP\IServerContainer; use OCP\Preview\IProviderV2; use Symfony\Component\EventDispatcher\EventDispatcherInterface; +use function array_key_exists; class PreviewManager implements IPreview { /** @var IConfig */ @@ -79,6 +83,20 @@ class PreviewManager implements IPreview { /** @var string */ protected $userId; + /** @var Coordinator */ + private $bootstrapCoordinator; + + /** + * Hash map (without value) of loaded bootstrap providers + * + * @var null[] + * @psalm-var array + */ + private $loadedBootstrapProviders = []; + + /** @var IServerContainer */ + private $container; + /** * PreviewManager constructor. * @@ -93,13 +111,17 @@ class PreviewManager implements IPreview { IAppData $appData, EventDispatcherInterface $eventDispatcher, GeneratorHelper $helper, - $userId) { + $userId, + Coordinator $bootstrapCoordinator, + IServerContainer $container) { $this->config = $config; $this->rootFolder = $rootFolder; $this->appData = $appData; $this->eventDispatcher = $eventDispatcher; $this->helper = $helper; $this->userId = $userId; + $this->bootstrapCoordinator = $bootstrapCoordinator; + $this->container = $container; } /** @@ -134,6 +156,7 @@ class PreviewManager implements IPreview { } $this->registerCoreProviders(); + $this->registerBootstrapProviders(); if ($this->providerListDirty) { $keys = array_map('strlen', array_keys($this->providers)); array_multisort($keys, SORT_DESC, $this->providers); @@ -220,6 +243,7 @@ class PreviewManager implements IPreview { } $this->registerCoreProviders(); + $this->registerBootstrapProviders(); $providerMimeTypes = array_keys($this->providers); foreach ($providerMimeTypes as $supportedMimeType) { if (preg_match($supportedMimeType, $mimeType)) { @@ -431,4 +455,31 @@ class PreviewManager implements IPreview { } } } + + private function registerBootstrapProviders(): void { + $context = $this->bootstrapCoordinator->getRegistrationContext(); + + if ($context === null) { + // Just ignore for now + return; + } + + $providers = $context->getPreviewProviders(); + foreach ($providers as $provider) { + $key = $provider->getMimeTypeRegex() . '-' . $provider->getService(); + if (array_key_exists($key, $this->loadedBootstrapProviders)) { + // Do not load the provider more than once + continue; + } + $this->loadedBootstrapProviders[$key] = null; + + $this->registerProvider($provider->getMimeTypeRegex(), function () use ($provider) { + try { + return $this->container->query($provider->getService()); + } catch (QueryException $e) { + return null; + } + }); + } + } } diff --git a/lib/private/Server.php b/lib/private/Server.php index a9ea07c27d3..529e2eacfc3 100644 --- a/lib/private/Server.php +++ b/lib/private/Server.php @@ -58,6 +58,7 @@ use OC\App\AppManager; use OC\App\AppStore\Bundles\BundleFetcher; use OC\App\AppStore\Fetcher\AppFetcher; use OC\App\AppStore\Fetcher\CategoryFetcher; +use OC\AppFramework\Bootstrap\Coordinator; use OC\AppFramework\Http\Request; use OC\AppFramework\Utility\TimeFactory; use OC\Authentication\Events\LoginFailed; @@ -318,7 +319,9 @@ class Server extends ServerContainer implements IServerContainer { ), $c->get(SymfonyAdapter::class), $c->get(GeneratorHelper::class), - $c->get(ISession::class)->get('user_id') + $c->get(ISession::class)->get('user_id'), + $c->get(Coordinator::class), + $c->get(IServerContainer::class) ); }); /** @deprecated 19.0.0 */ diff --git a/lib/public/AppFramework/Bootstrap/IRegistrationContext.php b/lib/public/AppFramework/Bootstrap/IRegistrationContext.php index f75cdb18cfb..de391050ec1 100644 --- a/lib/public/AppFramework/Bootstrap/IRegistrationContext.php +++ b/lib/public/AppFramework/Bootstrap/IRegistrationContext.php @@ -36,6 +36,7 @@ use OCP\EventDispatcher\IEventDispatcher; use OCP\Files\Template\ICustomTemplateProvider; use OCP\IContainer; use OCP\Notification\INotifier; +use OCP\Preview\IProviderV2; /** * The context object passed to IBootstrap::register @@ -231,6 +232,18 @@ interface IRegistrationContext { */ public function registerTwoFactorProvider(string $twoFactorProviderClass): void; + /** + * Register a preview provider + * + * It is allowed to register more than one provider per app. + * + * @param string $previewProviderClass + * @param string $mimeTypeRegex + * @psalm-param class-string $previewProviderClass + * @since 23.0.0 + */ + public function registerPreviewProvider(string $previewProviderClass, string $mimeTypeRegex): void; + /** * Register a calendar provider * diff --git a/lib/public/IPreview.php b/lib/public/IPreview.php index c4e3c557012..a8a798a526c 100644 --- a/lib/public/IPreview.php +++ b/lib/public/IPreview.php @@ -58,6 +58,9 @@ interface IPreview { * @param \Closure $callable * @return void * @since 8.1.0 + * @see \OCP\AppFramework\Bootstrap\IRegistrationContext::registerPreviewProvider + * + * @deprecated 23.0.0 Register your provider via the IRegistrationContext when booting the app */ public function registerProvider($mimeTypeRegex, \Closure $callable); -- 2.39.5