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 <[email protected]>
Signed-off-by: Christoph Wurst <[email protected]>
  • Loading branch information
rullzer authored and skjnldsv committed Oct 15, 2021
1 parent 5645b2a commit 1f7568a
Show file tree
Hide file tree
Showing 8 changed files with 130 additions and 3 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 @@ -609,6 +609,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 @@ -638,6 +638,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 <[email protected]>
*
* @author 2021 Christoph Wurst <[email protected]>
*
* @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;
}
}
25 changes: 24 additions & 1 deletion lib/private/AppFramework/Bootstrap/RegistrationContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

namespace OC\AppFramework\Bootstrap;

use Closure;
use function array_shift;
use OC\Support\CrashReport\Registry;
use OCP\AppFramework\App;
use OCP\AppFramework\Bootstrap\IRegistrationContext;
Expand All @@ -47,7 +49,6 @@
use OCP\Support\CrashReport\IReporter;
use Psr\Log\LoggerInterface;
use Throwable;
use function array_shift;

class RegistrationContext {

Expand Down Expand Up @@ -102,6 +103,9 @@ class RegistrationContext {
/** @var LoggerInterface */
private $logger;

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

public function __construct(LoggerInterface $logger) {
$this->logger = $logger;
}
Expand Down Expand Up @@ -230,6 +234,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 @@ -311,6 +323,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 @@ -546,6 +562,13 @@ public function getTwoFactorProviders(): array {
return $this->twoFactorProviders;
}

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

/**
* @return ServiceRegistration<ICalendarProvider>[]
*/
Expand Down
37 changes: 36 additions & 1 deletion lib/private/PreviewManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,18 @@
*/
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;

Expand Down Expand Up @@ -79,6 +82,12 @@ class PreviewManager implements IPreview {
/** @var string */
protected $userId;

/** @var Coordinator */
private $bootstrapCoordinator;

/** @var IServerContainer */
private $container;

/**
* PreviewManager constructor.
*
Expand All @@ -93,13 +102,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 +147,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 +234,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 +446,24 @@ 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) {
$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
14 changes: 14 additions & 0 deletions lib/public/AppFramework/Bootstrap/IRegistrationContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

namespace OCP\AppFramework\Bootstrap;

use OCP\AppFramework\IAppContainer;
Expand All @@ -35,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 @@ -229,6 +231,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 1f7568a

Please sign in to comment.