Skip to content

Commit

Permalink
Move preview provider registration to bootstrap
Browse files Browse the repository at this point in the history
Signed-off-by: Roeland Jago Douma <roeland@famdouma.nl>
Signed-off-by: Christoph Wurst <christoph@winzerhof-wurst.at>
  • Loading branch information
rullzer authored and ChristophWurst committed Oct 21, 2021
1 parent 732a057 commit 8bc25e3
Show file tree
Hide file tree
Showing 8 changed files with 143 additions and 2 deletions.
1 change: 1 addition & 0 deletions lib/composer/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -611,6 +611,7 @@
'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',
Expand Down
1 change: 1 addition & 0 deletions lib/composer/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
47 changes: 47 additions & 0 deletions lib/private/AppFramework/Bootstrap/PreviewProviderRegistration.php
Original file line number Diff line number Diff line change
@@ -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;
}
}
22 changes: 22 additions & 0 deletions lib/private/AppFramework/Bootstrap/RegistrationContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ class RegistrationContext {
/** @var LoggerInterface */
private $logger;

/** @var PreviewProviderRegistration[] */
private $previewProviders = [];

public function __construct(LoggerInterface $logger) {
$this->logger = $logger;
}
Expand Down Expand Up @@ -235,6 +238,14 @@ public function registerTwoFactorProvider(string $twoFactorProviderClass): void
);
}

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,
Expand Down Expand Up @@ -323,6 +334,10 @@ public function registerTwoFactorProvider(string $appId, string $class): void {
$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);
}
Expand Down Expand Up @@ -565,6 +580,13 @@ public function getTwoFactorProviders(): array {
return $this->twoFactorProviders;
}

/**
* @return PreviewProviderRegistration[]
*/
public function getPreviewProviders(): array {
return $this->previewProviders;
}

/**
* @return ServiceRegistration<ICalendarProvider>[]
*/
Expand Down
53 changes: 52 additions & 1 deletion lib/private/PreviewManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,21 @@
*/
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;
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 */
Expand Down Expand Up @@ -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.
*
Expand All @@ -93,13 +111,17 @@ public function __construct(IConfig $config,
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;
}

/**
Expand Down Expand Up @@ -134,6 +156,7 @@ public function getProviders() {
}

$this->registerCoreProviders();
$this->registerBootstrapProviders();
if ($this->providerListDirty) {
$keys = array_map('strlen', array_keys($this->providers));
array_multisort($keys, SORT_DESC, $this->providers);
Expand Down Expand Up @@ -220,6 +243,7 @@ public function isMimeSupported($mimeType = '*') {
}

$this->registerCoreProviders();
$this->registerBootstrapProviders();
$providerMimeTypes = array_keys($this->providers);
foreach ($providerMimeTypes as $supportedMimeType) {
if (preg_match($supportedMimeType, $mimeType)) {
Expand Down Expand Up @@ -431,4 +455,31 @@ protected function registerCoreProviders() {
}
}
}

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;
}
});
}
}
}
5 changes: 4 additions & 1 deletion lib/private/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
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;
Expand Down Expand Up @@ -318,7 +319,9 @@ public function __construct($webRoot, \OC\Config $config) {
),
$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 */
Expand Down
13 changes: 13 additions & 0 deletions lib/public/AppFramework/Bootstrap/IRegistrationContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
use OCP\Files\Template\ICustomTemplateProvider;
use OCP\IContainer;
use OCP\Notification\INotifier;
use OCP\Preview\IProviderV2;

/**
* The context object passed to IBootstrap::register
Expand Down Expand Up @@ -231,6 +232,18 @@ public function registerNotifierService(string $notifierClass): void;
*/
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
*
Expand Down
3 changes: 3 additions & 0 deletions lib/public/IPreview.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit 8bc25e3

Please sign in to comment.