Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 19 additions & 14 deletions lib/private/AppFramework/Bootstrap/Coordinator.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@

namespace OC\AppFramework\Bootstrap;

use OC\Search\SearchComposer;
use OC\Support\CrashReport\Registry;
use OC_App;
use OCP\AppFramework\App;
Expand All @@ -34,6 +33,7 @@
use OCP\EventDispatcher\IEventDispatcher;
use OCP\ILogger;
use OCP\IServerContainer;
use RuntimeException;
use Throwable;
use function class_exists;
use function class_implements;
Expand All @@ -50,26 +50,28 @@ class Coordinator {
/** @var IEventDispatcher */
private $eventDispatcher;

/** @var SearchComposer */
private $searchComposer;

/** @var ILogger */
private $logger;

/** @var RegistrationContext|null */
private $registrationContext;

public function __construct(IServerContainer $container,
Registry $registry,
IEventDispatcher $eventListener,
SearchComposer $searchComposer,
ILogger $logger) {
$this->serverContainer = $container;
$this->registry = $registry;
$this->eventDispatcher = $eventListener;
$this->searchComposer = $searchComposer;
$this->logger = $logger;
}

public function runRegistration(): void {
$context = new RegistrationContext($this->logger);
if ($this->registrationContext !== null) {
throw new RuntimeException('Registration has already been run');
}

$this->registrationContext = new RegistrationContext($this->logger);
$apps = [];
foreach (OC_App::getEnabledApps() as $appId) {
/*
Expand Down Expand Up @@ -99,7 +101,7 @@ public function runRegistration(): void {
continue;
}
try {
$application->register($context->for($appId));
$application->register($this->registrationContext->for($appId));
} catch (Throwable $e) {
$this->logger->logException($e, [
'message' => 'Error during app service registration: ' . $e->getMessage(),
Expand All @@ -113,12 +115,15 @@ public function runRegistration(): void {
* Now that all register methods have been called, we can delegate the registrations
* to the actual services
*/
$context->delegateCapabilityRegistrations($apps);
$context->delegateCrashReporterRegistrations($apps, $this->registry);
$context->delegateEventListenerRegistrations($this->eventDispatcher);
$context->delegateContainerRegistrations($apps);
$context->delegateMiddlewareRegistrations($apps);
$context->delegateSearchProviderRegistration($apps, $this->searchComposer);
$this->registrationContext->delegateCapabilityRegistrations($apps);
$this->registrationContext->delegateCrashReporterRegistrations($apps, $this->registry);
$this->registrationContext->delegateEventListenerRegistrations($this->eventDispatcher);
$this->registrationContext->delegateContainerRegistrations($apps);
$this->registrationContext->delegateMiddlewareRegistrations($apps);
}

public function getRegistrationContext(): ?RegistrationContext {
return $this->registrationContext;
}

public function bootApp(string $appId): void {
Expand Down
17 changes: 3 additions & 14 deletions lib/private/AppFramework/Bootstrap/RegistrationContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
namespace OC\AppFramework\Bootstrap;

use Closure;
use OC\Search\SearchComposer;
use OC\Support\CrashReport\Registry;
use OCP\AppFramework\App;
use OCP\AppFramework\Bootstrap\IRegistrationContext;
Expand Down Expand Up @@ -347,19 +346,9 @@ public function delegateMiddlewareRegistrations(array $apps): void {
}

/**
* @param App[] $apps
* @return array[]
*/
public function delegateSearchProviderRegistration(array $apps, SearchComposer $searchComposer): void {
foreach ($this->searchProviders as $registration) {
try {
$searchComposer->registerProvider($registration['class']);
} catch (Throwable $e) {
$appId = $registration['appId'];
$this->logger->logException($e, [
'message' => "Error during search provider registration of $appId: " . $e->getMessage(),
'level' => ILogger::ERROR,
]);
}
}
public function getSearchProviders(): array {
return $this->searchProviders;
}
}
40 changes: 16 additions & 24 deletions lib/private/Search/SearchComposer.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
namespace OC\Search;

use InvalidArgumentException;
use OCP\AppFramework\Bootstrap\IRegistrationContext;
use OC\AppFramework\Bootstrap\Coordinator;
use OCP\AppFramework\QueryException;
use OCP\ILogger;
use OCP\IServerContainer;
Expand Down Expand Up @@ -57,37 +57,24 @@
*/
class SearchComposer {

/** @var string[] */
private $lazyProviders = [];

/** @var IProvider[] */
private $providers = [];

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

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

/** @var ILogger */
private $logger;

public function __construct(IServerContainer $container,
public function __construct(Coordinator $bootstrapCoordinator,
IServerContainer $container,
ILogger $logger) {
$this->container = $container;
$this->logger = $logger;
}

/**
* Register a search provider lazily
*
* Registers the fully-qualified class name of an implementation of an
* IProvider. The service will only be queried on demand. Apps will register
* the providers through the registration context object.
*
* @see IRegistrationContext::registerSearchProvider()
*
* @param string $class
*/
public function registerProvider(string $class): void {
$this->lazyProviders[] = $class;
$this->bootstrapCoordinator = $bootstrapCoordinator;
}

/**
Expand All @@ -96,11 +83,17 @@ public function registerProvider(string $class): void {
* If a provider can't be loaded we log it but the operation continues nevertheless
*/
private function loadLazyProviders(): void {
$classes = $this->lazyProviders;
foreach ($classes as $class) {
$context = $this->bootstrapCoordinator->getRegistrationContext();
if ($context === null) {
// Too early, nothing registered yet
return;
}

$registrations = $context->getSearchProviders();
foreach ($registrations as $registration) {
try {
/** @var IProvider $provider */
$provider = $this->container->query($class);
$provider = $this->container->query($registration['class']);
$this->providers[$provider->getId()] = $provider;
} catch (QueryException $e) {
// Log an continue. We can be fault tolerant here.
Expand All @@ -110,7 +103,6 @@ private function loadLazyProviders(): void {
]);
}
}
$this->lazyProviders = [];
}

/**
Expand Down
6 changes: 0 additions & 6 deletions tests/lib/AppFramework/Bootstrap/CoordinatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
namespace lib\AppFramework\Bootstrap;

use OC\AppFramework\Bootstrap\Coordinator;
use OC\Search\SearchComposer;
use OC\Support\CrashReport\Registry;
use OCP\App\IAppManager;
use OCP\AppFramework\App;
Expand Down Expand Up @@ -54,9 +53,6 @@ class CoordinatorTest extends TestCase {
/** @var IEventDispatcher|MockObject */
private $eventDispatcher;

/** @var SearchComposer|MockObject */
private $searchComposer;

/** @var ILogger|MockObject */
private $logger;

Expand All @@ -70,14 +66,12 @@ protected function setUp(): void {
$this->serverContainer = $this->createMock(IServerContainer::class);
$this->crashReporterRegistry = $this->createMock(Registry::class);
$this->eventDispatcher = $this->createMock(IEventDispatcher::class);
$this->searchComposer = $this->createMock(SearchComposer::class);
$this->logger = $this->createMock(ILogger::class);

$this->coordinator = new Coordinator(
$this->serverContainer,
$this->crashReporterRegistry,
$this->eventDispatcher,
$this->searchComposer,
$this->logger
);
}
Expand Down