Skip to content

Commit

Permalink
Merge pull request #36298 from nextcloud/perf/app-framework/lazy-midd…
Browse files Browse the repository at this point in the history
…leware-registration

perf(app-framework): Make app middleware registration even lazier
  • Loading branch information
ChristophWurst authored Jan 25, 2023
2 parents 919a840 + 907ff68 commit b58d4f7
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 41 deletions.
1 change: 0 additions & 1 deletion lib/private/AppFramework/Bootstrap/Coordinator.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,6 @@ private function registerApps(array $appIds): void {
$this->registrationContext->delegateDashboardPanelRegistrations($this->dashboardManager);
$this->registrationContext->delegateEventListenerRegistrations($this->eventDispatcher);
$this->registrationContext->delegateContainerRegistrations($apps);
$this->registrationContext->delegateMiddlewareRegistrations($apps);
}

public function getRegistrationContext(): ?RegistrationContext {
Expand Down
25 changes: 3 additions & 22 deletions lib/private/AppFramework/Bootstrap/RegistrationContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -617,29 +617,10 @@ public function delegateContainerRegistrations(array $apps): void {
}

/**
* @param App[] $apps
* @return ServiceRegistration<Middleware>[]
*/
public function delegateMiddlewareRegistrations(array $apps): void {
while (($middleware = array_shift($this->middlewares)) !== null) {
$appId = $middleware->getAppId();
if (!isset($apps[$appId])) {
// If we land here something really isn't right. But at least we caught the
// notice that is otherwise emitted for the undefined index
$this->logger->error("App $appId not loaded for the container middleware registration");

continue;
}

try {
$apps[$appId]
->getContainer()
->registerMiddleWare($middleware->getService());
} catch (Throwable $e) {
$this->logger->error("Error during capability registration of $appId: " . $e->getMessage(), [
'exception' => $e,
]);
}
}
public function getMiddlewareRegistrations(): array {
return $this->middlewares;
}

/**
Expand Down
11 changes: 11 additions & 0 deletions lib/private/AppFramework/DependencyInjection/DIContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,17 @@ public function __construct(string $appName, array $urlParams = [], ServerContai
$c->get(\OC\AppFramework\Middleware\AdditionalScriptsMiddleware::class)
);

/** @var \OC\AppFramework\Bootstrap\Coordinator $coordinator */
$coordinator = $c->get(\OC\AppFramework\Bootstrap\Coordinator::class);
$registrationContext = $coordinator->getRegistrationContext();
if ($registrationContext !== null) {
$appId = $this->getAppName();
foreach ($registrationContext->getMiddlewareRegistrations() as $middlewareRegistration) {
if ($middlewareRegistration->getAppId() === $appId) {
$dispatcher->registerMiddleware($c->get($middlewareRegistration->getService()));
}
}
}
foreach ($this->middleWares as $middleWare) {
$dispatcher->registerMiddleware($c->get($middleWare));
}
Expand Down
29 changes: 11 additions & 18 deletions tests/lib/AppFramework/Bootstrap/RegistrationContextTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

use OC\AppFramework\Bootstrap\RegistrationContext;
use OC\AppFramework\Bootstrap\ServiceRegistration;
use OC\Core\Middleware\TwoFactorMiddleware;
use OCP\AppFramework\App;
use OCP\AppFramework\IAppContainer;
use OCP\EventDispatcher\IEventDispatcher;
Expand Down Expand Up @@ -145,24 +146,6 @@ public function testRegisterParameter(): void {
]);
}

public function testRegisterMiddleware(): void {
$app = $this->createMock(App::class);
$name = 'abc';
$container = $this->createMock(IAppContainer::class);
$app->method('getContainer')
->willReturn($container);
$container->expects($this->once())
->method('registerMiddleware')
->with($name);
$this->logger->expects($this->never())
->method('error');

$this->context->for('myapp')->registerMiddleware($name);
$this->context->delegateMiddlewareRegistrations([
'myapp' => $app,
]);
}

public function testRegisterUserMigrator(): void {
$appIdA = 'myapp';
$migratorClassA = 'OCA\App\UserMigration\AppMigrator';
Expand Down Expand Up @@ -195,4 +178,14 @@ public function dataProvider_TrueFalse() {
[false]
];
}

public function testGetMiddlewareRegistrations(): void {
$this->context->registerMiddleware('core', TwoFactorMiddleware::class);

$registrations = $this->context->getMiddlewareRegistrations();

self::assertNotEmpty($registrations);
self::assertSame('core', $registrations[0]->getAppId());
self::assertSame(TwoFactorMiddleware::class, $registrations[0]->getService());
}
}
36 changes: 36 additions & 0 deletions tests/lib/AppFramework/DependencyInjection/DIContainerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,13 @@

namespace Test\AppFramework\DependencyInjection;

use OC\AppFramework\Bootstrap\Coordinator;
use OC\AppFramework\Bootstrap\RegistrationContext;
use OC\AppFramework\Bootstrap\ServiceRegistration;
use OC\AppFramework\DependencyInjection\DIContainer;
use OC\AppFramework\Http\Request;
use OC\AppFramework\Middleware\Security\SecurityMiddleware;
use OCP\AppFramework\Middleware;
use OCP\AppFramework\QueryException;
use OCP\IConfig;
use OCP\IRequestId;
Expand Down Expand Up @@ -84,6 +88,38 @@ public function testMiddlewareDispatcherIncludesSecurityMiddleware() {
$this->assertTrue($found);
}

public function testMiddlewareDispatcherIncludesBootstrapMiddlewares(): void {
$coordinator = $this->createMock(Coordinator::class);
$this->container[Coordinator::class] = $coordinator;
$this->container['Request'] = $this->createMock(Request::class);
$registrationContext = $this->createMock(RegistrationContext::class);
$registrationContext->method('getMiddlewareRegistrations')
->willReturn([
new ServiceRegistration($this->container['appName'], 'foo'),
new ServiceRegistration('otherapp', 'bar'),
]);
$this->container['foo'] = new class extends Middleware {
};
$this->container['bar'] = new class extends Middleware {
};
$coordinator->method('getRegistrationContext')->willReturn($registrationContext);

$dispatcher = $this->container['MiddlewareDispatcher'];

$middlewares = $dispatcher->getMiddlewares();
self::assertNotEmpty($middlewares);
foreach ($middlewares as $middleware) {
if ($middleware === $this->container['bar']) {
$this->fail('Container must not register this middleware');
}
if ($middleware === $this->container['foo']) {
// It is done
return;
}
}
$this->fail('Bootstrap registered middleware not found');
}

public function testInvalidAppClass() {
$this->expectException(QueryException::class);
$this->container->query('\OCA\Name\Foo');
Expand Down

0 comments on commit b58d4f7

Please sign in to comment.