Move preview provider registration to bootstrap

Signed-off-by: Roeland Jago Douma <roeland@famdouma.nl>
Signed-off-by: Christoph Wurst <christoph@winzerhof-wurst.at>
pull/23171/head
Roeland Jago Douma 4 years ago committed by Christoph Wurst
parent 732a057167
commit 8bc25e3324
No known key found for this signature in database
GPG Key ID: CC42AC2A7F0E56D8
  1. 1
      lib/composer/composer/autoload_classmap.php
  2. 1
      lib/composer/composer/autoload_static.php
  3. 47
      lib/private/AppFramework/Bootstrap/PreviewProviderRegistration.php
  4. 22
      lib/private/AppFramework/Bootstrap/RegistrationContext.php
  5. 53
      lib/private/PreviewManager.php
  6. 5
      lib/private/Server.php
  7. 13
      lib/public/AppFramework/Bootstrap/IRegistrationContext.php
  8. 3
      lib/public/IPreview.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',

@ -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',

@ -0,0 +1,47 @@
<?php
declare(strict_types=1);
/*
* @copyright 2021 Christoph Wurst <christoph@winzerhof-wurst.at>
*
* @author 2021 Christoph Wurst <christoph@winzerhof-wurst.at>
*
* @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 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;
}
}

@ -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<ICalendarProvider>[]
*/

@ -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<string, null>
*/
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;
}
});
}
}
}

@ -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 */

@ -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<IProviderV2> $previewProviderClass
* @since 23.0.0
*/
public function registerPreviewProvider(string $previewProviderClass, string $mimeTypeRegex): void;
/**
* Register a calendar provider
*

@ -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);

Loading…
Cancel
Save