From 9913bdda90fd108fff32dac6430826f8af4a78c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=B4me=20Chilliet?= Date: Mon, 16 Jun 2025 18:00:28 +0200 Subject: [PATCH 01/10] chore: Cleanup DIContainer class MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Also removed deprecated tag from the class as this class will not be removed, only the interface IAppContainer and associated methods should be removed. Signed-off-by: Côme Chilliet --- .../tests/AppInfo/ApplicationTest.php | 2 +- lib/private/AppFramework/App.php | 4 +- .../DependencyInjection/DIContainer.php | 188 +++++------------- lib/private/AppFramework/Http/Dispatcher.php | 6 +- lib/private/AppFramework/Http/Output.php | 11 +- .../AppFramework/Utility/SimpleContainer.php | 18 +- lib/private/Server.php | 20 +- 7 files changed, 81 insertions(+), 168 deletions(-) diff --git a/apps/settings/tests/AppInfo/ApplicationTest.php b/apps/settings/tests/AppInfo/ApplicationTest.php index b09412d8387..3e895d87b06 100644 --- a/apps/settings/tests/AppInfo/ApplicationTest.php +++ b/apps/settings/tests/AppInfo/ApplicationTest.php @@ -39,7 +39,7 @@ class ApplicationTest extends TestCase { public function testContainerAppName(): void { $this->app = new Application(); - $this->assertEquals('settings', $this->container->getAppName()); + $this->assertEquals('settings', $this->container->get('appName')); } public static function dataContainerQuery(): array { diff --git a/lib/private/AppFramework/App.php b/lib/private/AppFramework/App.php index c5f61d7e938..e719ea19f90 100644 --- a/lib/private/AppFramework/App.php +++ b/lib/private/AppFramework/App.php @@ -101,7 +101,7 @@ class App { $profiler->setEnabled($profiler->isEnabled() && !is_null($urlParams) && isset($urlParams['_route']) && !str_starts_with($urlParams['_route'], 'profiler.')); if ($profiler->isEnabled()) { \OC::$server->get(IEventLogger::class)->activate(); - $profiler->add(new RoutingDataCollector($container['AppName'], $controllerName, $methodName)); + $profiler->add(new RoutingDataCollector($container['appName'], $controllerName, $methodName)); } $eventLogger->start('app:controller:params', 'Gather controller parameters'); @@ -115,7 +115,7 @@ class App { $request = $container->get(IRequest::class); $request->setUrlParameters($container['urlParams']); } - $appName = $container['AppName']; + $appName = $container['appName']; $eventLogger->end('app:controller:params'); diff --git a/lib/private/AppFramework/DependencyInjection/DIContainer.php b/lib/private/AppFramework/DependencyInjection/DIContainer.php index 87361a9d1ea..4d802b5c297 100644 --- a/lib/private/AppFramework/DependencyInjection/DIContainer.php +++ b/lib/private/AppFramework/DependencyInjection/DIContainer.php @@ -1,10 +1,13 @@ appName = $appName; - $this['appName'] = $appName; - $this['urlParams'] = $urlParams; + $this->registerParameter('appName', $appName); + $this->registerParameter('urlParams', $urlParams); - $this->registerAlias('Request', IRequest::class); + /** @deprecated 32.0.0 */ + $this->registerDeprecatedAlias('Request', IRequest::class); - /** @var \OC\ServerContainer $server */ if ($server === null) { $server = \OC::$server; } @@ -90,74 +72,66 @@ class DIContainer extends SimpleContainer implements IAppContainer { // aliases /** @deprecated 26.0.0 inject $appName */ - $this->registerAlias('AppName', 'appName'); + $this->registerDeprecatedAlias('AppName', 'appName'); /** @deprecated 26.0.0 inject $webRoot*/ - $this->registerAlias('WebRoot', 'webRoot'); + $this->registerDeprecatedAlias('WebRoot', 'webRoot'); /** @deprecated 26.0.0 inject $userId */ - $this->registerAlias('UserId', 'userId'); + $this->registerDeprecatedAlias('UserId', 'userId'); /** * Core services */ - $this->registerService(IOutput::class, function () { - return new Output($this->getServer()->getWebRoot()); - }); + /* Cannot be an alias because Output is not in OCA */ + $this->registerService(IOutput::class, fn (ContainerInterface $c): IOutput => new Output($c->get('webRoot'))); $this->registerService(Folder::class, function () { return $this->getServer()->getUserFolder(); }); - $this->registerService(IAppData::class, function (ContainerInterface $c) { - return $this->getServer()->getAppDataDir($c->get('AppName')); + $this->registerService(IAppData::class, function (ContainerInterface $c): IAppData { + return $c->get(\OCP\Files\AppData\IAppDataFactory::class)->get($c->get('appName')); }); $this->registerService(IL10N::class, function (ContainerInterface $c) { - return $this->getServer()->getL10N($c->get('AppName')); + return $this->getServer()->getL10N($c->get('appName')); }); // Log wrappers - $this->registerService(LoggerInterface::class, function (ContainerInterface $c) { - return new ScopedPsrLogger( - $c->get(PsrLoggerAdapter::class), - $c->get('AppName') - ); - }); + $this->registerAlias(LoggerInterface::class, ScopedPsrLogger::class); $this->registerService(IServerContainer::class, function () { return $this->getServer(); }); - $this->registerAlias('ServerContainer', IServerContainer::class); + /** @deprecated 32.0.0 */ + $this->registerDeprecatedAlias('ServerContainer', IServerContainer::class); - $this->registerService(\OCP\WorkflowEngine\IManager::class, function (ContainerInterface $c) { - return $c->get(Manager::class); - }); + $this->registerAlias(\OCP\WorkflowEngine\IManager::class, Manager::class); - $this->registerService(ContainerInterface::class, function (ContainerInterface $c) { - return $c; - }); - $this->registerAlias(IAppContainer::class, ContainerInterface::class); + $this->registerDeprecatedAlias(IAppContainer::class, ContainerInterface::class); // commonly used attributes - $this->registerService('userId', function (ContainerInterface $c) { + $this->registerService('userId', function (ContainerInterface $c): string { return $c->get(IUserSession::class)->getSession()->get('user_id'); }); - $this->registerService('webRoot', function (ContainerInterface $c) { + $this->registerService('webRoot', function (ContainerInterface $c): string { return $c->get(IServerContainer::class)->getWebRoot(); }); - $this->registerService('OC_Defaults', function (ContainerInterface $c) { + $this->registerService('OC_Defaults', function (ContainerInterface $c): object { return $c->get(IServerContainer::class)->get('ThemingDefaults'); }); - $this->registerService('Protocol', function (ContainerInterface $c) { - /** @var \OC\Server $server */ - $server = $c->get(IServerContainer::class); - $protocol = $server->getRequest()->getHttpProtocol(); + /** @deprecated 32.0.0 */ + $this->registerDeprecatedAlias('Protocol', Http::class); + $this->registerService(Http::class, function (ContainerInterface $c) { + $protocol = $c->get(IRequest::class)->getHttpProtocol(); return new Http($_SERVER, $protocol); }); - $this->registerService('Dispatcher', function (ContainerInterface $c) { + /** @deprecated 32.0.0 */ + $this->registerDeprecatedAlias('Dispatcher', Dispatcher::class); + $this->registerService(Dispatcher::class, function (ContainerInterface $c) { return new Dispatcher( $c->get('Protocol'), $c->get(MiddlewareDispatcher::class), @@ -181,7 +155,8 @@ class DIContainer extends SimpleContainer implements IAppContainer { /** * Middleware */ - $this->registerAlias('MiddlewareDispatcher', MiddlewareDispatcher::class); + /** @deprecated 32.0.0 */ + $this->registerDeprecatedAlias('MiddlewareDispatcher', MiddlewareDispatcher::class); $this->registerService(MiddlewareDispatcher::class, function (ContainerInterface $c) { $server = $this->getServer(); @@ -198,24 +173,13 @@ class DIContainer extends SimpleContainer implements IAppContainer { ); $dispatcher->registerMiddleware( - new OC\AppFramework\Middleware\Security\SameSiteCookieMiddleware( - $c->get(IRequest::class), - $c->get(IControllerMethodReflector::class) - ) + $c->get(OC\AppFramework\Middleware\Security\SameSiteCookieMiddleware::class) ); $dispatcher->registerMiddleware( - new CORSMiddleware( - $c->get(IRequest::class), - $c->get(IControllerMethodReflector::class), - $c->get(IUserSession::class), - $c->get(IThrottler::class), - $c->get(LoggerInterface::class) - ) + $c->get(CORSMiddleware::class) ); $dispatcher->registerMiddleware( - new OCSMiddleware( - $c->get(IRequest::class) - ) + $c->get(OCSMiddleware::class) ); $dispatcher->registerMiddleware($c->get(FlowV2EphemeralSessionsMiddleware::class)); @@ -225,75 +189,45 @@ class DIContainer extends SimpleContainer implements IAppContainer { $c->get(IControllerMethodReflector::class), $c->get(INavigationManager::class), $c->get(IURLGenerator::class), - $server->get(LoggerInterface::class), - $c->get('AppName'), + $c->get(LoggerInterface::class), + $c->get('appName'), $server->getUserSession()->isLoggedIn(), $c->get(IGroupManager::class), $c->get(ISubAdmin::class), $server->getAppManager(), $server->getL10N('lib'), $c->get(AuthorizedGroupMapper::class), - $server->get(IUserSession::class), + $c->get(IUserSession::class), $c->get(IRemoteAddress::class), ); $dispatcher->registerMiddleware($securityMiddleware); $dispatcher->registerMiddleware( - new OC\AppFramework\Middleware\Security\CSPMiddleware( - $server->query(OC\Security\CSP\ContentSecurityPolicyManager::class), - $server->query(OC\Security\CSP\ContentSecurityPolicyNonceManager::class), - ) + $c->get(OC\AppFramework\Middleware\Security\CSPMiddleware::class) ); $dispatcher->registerMiddleware( - $server->query(OC\AppFramework\Middleware\Security\FeaturePolicyMiddleware::class) + $c->get(OC\AppFramework\Middleware\Security\FeaturePolicyMiddleware::class) ); $dispatcher->registerMiddleware( - new OC\AppFramework\Middleware\Security\PasswordConfirmationMiddleware( - $c->get(IControllerMethodReflector::class), - $c->get(ISession::class), - $c->get(IUserSession::class), - $c->get(ITimeFactory::class), - $c->get(\OC\Authentication\Token\IProvider::class), - $c->get(LoggerInterface::class), - $c->get(IRequest::class), - $c->get(UserManager::class), - ) + $c->get(OC\AppFramework\Middleware\Security\PasswordConfirmationMiddleware::class) ); $dispatcher->registerMiddleware( - new TwoFactorMiddleware( - $c->get(OC\Authentication\TwoFactorAuth\Manager::class), - $c->get(IUserSession::class), - $c->get(ISession::class), - $c->get(IURLGenerator::class), - $c->get(IControllerMethodReflector::class), - $c->get(IRequest::class) - ) + $c->get(TwoFactorMiddleware::class) ); $dispatcher->registerMiddleware( - new OC\AppFramework\Middleware\Security\BruteForceMiddleware( - $c->get(IControllerMethodReflector::class), - $c->get(IThrottler::class), - $c->get(IRequest::class), - $c->get(LoggerInterface::class) - ) + $c->get(OC\AppFramework\Middleware\Security\BruteForceMiddleware::class) ); $dispatcher->registerMiddleware($c->get(RateLimitingMiddleware::class)); $dispatcher->registerMiddleware( - new OC\AppFramework\Middleware\PublicShare\PublicShareMiddleware( - $c->get(IRequest::class), - $c->get(ISession::class), - $c->get(IConfig::class), - $c->get(IThrottler::class) - ) + $c->get(OC\AppFramework\Middleware\PublicShare\PublicShareMiddleware::class) ); $dispatcher->registerMiddleware( $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(); + $appId = $this->get('appName'); foreach ($registrationContext->getMiddlewareRegistrations() as $middlewareRegistration) { if ($middlewareRegistration->getAppId() === $appId || $middlewareRegistration->isGlobal()) { @@ -306,27 +240,13 @@ class DIContainer extends SimpleContainer implements IAppContainer { } $dispatcher->registerMiddleware( - new SessionMiddleware( - $c->get(IControllerMethodReflector::class), - $c->get(ISession::class) - ) + $c->get(SessionMiddleware::class) ); return $dispatcher; }); - $this->registerService(IAppConfig::class, function (ContainerInterface $c) { - return new OC\AppFramework\Services\AppConfig( - $c->get(IConfig::class), - $c->get(\OCP\IAppConfig::class), - $c->get('AppName') - ); - }); - $this->registerService(IInitialState::class, function (ContainerInterface $c) { - return new OC\AppFramework\Services\InitialState( - $c->get(IInitialStateService::class), - $c->get('AppName') - ); - }); + $this->registerAlias(IAppConfig::class, \OC\AppFramework\Services\AppConfig::class); + $this->registerAlias(IInitialState::class, \OC\AppFramework\Services\InitialState::class); } /** @@ -338,13 +258,13 @@ class DIContainer extends SimpleContainer implements IAppContainer { /** * @param string $middleWare - * @return boolean|null */ - public function registerMiddleWare($middleWare) { + public function registerMiddleWare($middleWare): bool { if (in_array($middleWare, $this->middleWares, true) !== false) { return false; } $this->middleWares[] = $middleWare; + return true; } /** @@ -352,7 +272,7 @@ class DIContainer extends SimpleContainer implements IAppContainer { * @return string the name of your application */ public function getAppName() { - return $this->query('AppName'); + return $this->query('appName'); } /** @@ -372,7 +292,7 @@ class DIContainer extends SimpleContainer implements IAppContainer { return \OC_User::isAdminUser($uid); } - private function getUserId() { + private function getUserId(): string { return $this->getServer()->getSession()->get('user_id'); } diff --git a/lib/private/AppFramework/Http/Dispatcher.php b/lib/private/AppFramework/Http/Dispatcher.php index d129a7d770b..8d91ddf7502 100644 --- a/lib/private/AppFramework/Http/Dispatcher.php +++ b/lib/private/AppFramework/Http/Dispatcher.php @@ -64,7 +64,8 @@ class Dispatcher { * @param LoggerInterface $logger * @param IEventLogger $eventLogger */ - public function __construct(Http $protocol, + public function __construct( + Http $protocol, MiddlewareDispatcher $middlewareDispatcher, ControllerMethodReflector $reflector, IRequest $request, @@ -72,7 +73,8 @@ class Dispatcher { ConnectionAdapter $connection, LoggerInterface $logger, IEventLogger $eventLogger, - ContainerInterface $appContainer) { + ContainerInterface $appContainer, + ) { $this->protocol = $protocol; $this->middlewareDispatcher = $middlewareDispatcher; $this->reflector = $reflector; diff --git a/lib/private/AppFramework/Http/Output.php b/lib/private/AppFramework/Http/Output.php index ff0ef1c15ca..b4a8672fdc7 100644 --- a/lib/private/AppFramework/Http/Output.php +++ b/lib/private/AppFramework/Http/Output.php @@ -13,14 +13,9 @@ use OCP\AppFramework\Http\IOutput; * Very thin wrapper class to make output testable */ class Output implements IOutput { - /** @var string */ - private $webRoot; - - /** - * @param $webRoot - */ - public function __construct($webRoot) { - $this->webRoot = $webRoot; + public function __construct( + private string $webRoot, + ) { } /** diff --git a/lib/private/AppFramework/Utility/SimpleContainer.php b/lib/private/AppFramework/Utility/SimpleContainer.php index 1d77c277b02..ed26e75ec89 100644 --- a/lib/private/AppFramework/Utility/SimpleContainer.php +++ b/lib/private/AppFramework/Utility/SimpleContainer.php @@ -14,6 +14,7 @@ use OCP\IContainer; use Pimple\Container; use Psr\Container\ContainerExceptionInterface; use Psr\Container\ContainerInterface; +use Psr\Log\LoggerInterface; use ReflectionClass; use ReflectionException; use ReflectionNamedType; @@ -185,8 +186,21 @@ class SimpleContainer implements ArrayAccess, ContainerInterface, IContainer { * @param string $alias the alias that should be registered * @param string $target the target that should be resolved instead */ - public function registerAlias($alias, $target) { - $this->registerService($alias, function (ContainerInterface $container) use ($target) { + public function registerAlias($alias, $target): void { + $this->registerService($alias, function (ContainerInterface $container) use ($target): mixed { + return $container->get($target); + }, false); + } + + protected function registerDeprecatedAlias(string $alias, string $target): void { + $this->registerService($alias, function (ContainerInterface $container) use ($target, $alias): mixed { + try { + $logger = $container->get(LoggerInterface::class); + $logger->debug('The requested alias "' . $alias . '" is deprecated. Please request "' . $target . '" directly. This alias will be removed in a future Nextcloud version.', ['app' => 'serverDI']); + } catch (ContainerExceptionInterface $e) { + // Could not get logger. Continue + } + return $container->get($target); }, false); } diff --git a/lib/private/Server.php b/lib/private/Server.php index c78decd90cb..b6dbdf62bdc 100644 --- a/lib/private/Server.php +++ b/lib/private/Server.php @@ -238,7 +238,6 @@ use OCP\User\Events\UserLoggedInEvent; use OCP\User\Events\UserLoggedInWithCookieEvent; use OCP\User\Events\UserLoggedOutEvent; use OCP\User\IAvailabilityCoordinator; -use Psr\Container\ContainerExceptionInterface; use Psr\Container\ContainerInterface; use Psr\Log\LoggerInterface; @@ -268,9 +267,7 @@ class Server extends ServerContainer implements IServerContainer { $this->registerService(ContainerInterface::class, function (ContainerInterface $c) { return $c; }); - $this->registerService(\OCP\IServerContainer::class, function (ContainerInterface $c) { - return $c; - }); + $this->registerDeprecatedAlias(\OCP\IServerContainer::class, ContainerInterface::class); $this->registerAlias(\OCP\Calendar\IManager::class, \OC\Calendar\Manager::class); @@ -1682,7 +1679,6 @@ class Server extends ServerContainer implements IServerContainer { * @deprecated 20.0.0 Use get(\OCP\Files\AppData\IAppDataFactory::class)->get($app) instead */ public function getAppDataDir($app) { - /** @var \OC\Files\AppData\Factory $factory */ $factory = $this->get(\OC\Files\AppData\Factory::class); return $factory->get($app); } @@ -1694,18 +1690,4 @@ class Server extends ServerContainer implements IServerContainer { public function getCloudIdManager() { return $this->get(ICloudIdManager::class); } - - private function registerDeprecatedAlias(string $alias, string $target) { - $this->registerService($alias, function (ContainerInterface $container) use ($target, $alias) { - try { - /** @var LoggerInterface $logger */ - $logger = $container->get(LoggerInterface::class); - $logger->debug('The requested alias "' . $alias . '" is deprecated. Please request "' . $target . '" directly. This alias will be removed in a future Nextcloud version.', ['app' => 'serverDI']); - } catch (ContainerExceptionInterface $e) { - // Could not get logger. Continue - } - - return $container->get($target); - }, false); - } } From ab310ce93836b01c01d7e286d6ae6650d9148620 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=B4me=20Chilliet?= Date: Tue, 17 Jun 2025 15:00:01 +0200 Subject: [PATCH 02/10] fix: Fix issues and tests in DIContainer and friends MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Some tests related to MiddlewareDispatcher are still failing. Signed-off-by: Côme Chilliet --- .../Controller/DeletedShareAPIController.php | 4 +-- .../DependencyInjection/DIContainer.php | 8 ++++-- lib/private/Support/CrashReport/Registry.php | 26 ++++--------------- tests/lib/AppFramework/AppTest.php | 12 ++++----- .../DependencyInjection/DIContainerTest.php | 6 +++-- 5 files changed, 23 insertions(+), 33 deletions(-) diff --git a/apps/files_sharing/lib/Controller/DeletedShareAPIController.php b/apps/files_sharing/lib/Controller/DeletedShareAPIController.php index f4a2cd500e1..5abb8e69bdd 100644 --- a/apps/files_sharing/lib/Controller/DeletedShareAPIController.php +++ b/apps/files_sharing/lib/Controller/DeletedShareAPIController.php @@ -23,12 +23,12 @@ use OCP\Files\IRootFolder; use OCP\Files\NotFoundException; use OCP\IGroupManager; use OCP\IRequest; -use OCP\IServerContainer; use OCP\IUserManager; use OCP\Share\Exceptions\GenericShareException; use OCP\Share\Exceptions\ShareNotFound; use OCP\Share\IManager as ShareManager; use OCP\Share\IShare; +use Psr\Container\ContainerInterface; /** * @psalm-import-type Files_SharingDeletedShare from ResponseDefinitions @@ -44,7 +44,7 @@ class DeletedShareAPIController extends OCSController { private IGroupManager $groupManager, private IRootFolder $rootFolder, private IAppManager $appManager, - private IServerContainer $serverContainer, + private ContainerInterface $serverContainer, ) { parent::__construct($appName, $request); } diff --git a/lib/private/AppFramework/DependencyInjection/DIContainer.php b/lib/private/AppFramework/DependencyInjection/DIContainer.php index 4d802b5c297..01bb5a0674e 100644 --- a/lib/private/AppFramework/DependencyInjection/DIContainer.php +++ b/lib/private/AppFramework/DependencyInjection/DIContainer.php @@ -44,6 +44,7 @@ use OCP\IL10N; use OCP\INavigationManager; use OCP\IRequest; use OCP\IServerContainer; +use OCP\ISession; use OCP\IURLGenerator; use OCP\IUserSession; use OCP\Security\Ip\IRemoteAddress; @@ -110,8 +111,8 @@ class DIContainer extends SimpleContainer implements IAppContainer { $this->registerDeprecatedAlias(IAppContainer::class, ContainerInterface::class); // commonly used attributes - $this->registerService('userId', function (ContainerInterface $c): string { - return $c->get(IUserSession::class)->getSession()->get('user_id'); + $this->registerService('userId', function (ContainerInterface $c): ?string { + return $c->get(ISession::class)->get('user_id'); }); $this->registerService('webRoot', function (ContainerInterface $c): string { @@ -359,6 +360,9 @@ class DIContainer extends SimpleContainer implements IAppContainer { return parent::query($name); } elseif (str_starts_with($name, \OC\AppFramework\App::buildAppNamespace($this->appName) . '\\')) { return parent::query($name); + } elseif (str_starts_with($name, 'OC\\AppFramework\\Services\\')) { + /* AppFramework services are scoped to the application */ + return parent::query($name); } throw new QueryException('Could not resolve ' . $name . '!' diff --git a/lib/private/Support/CrashReport/Registry.php b/lib/private/Support/CrashReport/Registry.php index 77dd8163174..4fbbb8448ca 100644 --- a/lib/private/Support/CrashReport/Registry.php +++ b/lib/private/Support/CrashReport/Registry.php @@ -6,11 +6,12 @@ declare(strict_types=1); * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors * SPDX-License-Identifier: AGPL-3.0-or-later */ + namespace OC\Support\CrashReport; use Exception; use OCP\AppFramework\QueryException; -use OCP\IServerContainer; +use OCP\Server; use OCP\Support\CrashReport\ICollectBreadcrumbs; use OCP\Support\CrashReport\IMessageReporter; use OCP\Support\CrashReport\IRegistry; @@ -26,17 +27,8 @@ class Registry implements IRegistry { /** @var IReporter[] */ private $reporters = []; - /** @var IServerContainer */ - private $serverContainer; - - public function __construct(IServerContainer $serverContainer) { - $this->serverContainer = $serverContainer; - } - /** * Register a reporter instance - * - * @param IReporter $reporter */ public function register(IReporter $reporter): void { $this->reporters[] = $reporter; @@ -49,10 +41,6 @@ class Registry implements IRegistry { /** * Delegate breadcrumb collection to all registered reporters * - * @param string $message - * @param string $category - * @param array $context - * * @since 15.0.0 */ public function delegateBreadcrumb(string $message, string $category, array $context = []): void { @@ -69,7 +57,6 @@ class Registry implements IRegistry { * Delegate crash reporting to all registered reporters * * @param Exception|Throwable $exception - * @param array $context */ public function delegateReport($exception, array $context = []): void { $this->loadLazyProviders(); @@ -82,9 +69,6 @@ class Registry implements IRegistry { /** * Delegate a message to all reporters that implement IMessageReporter * - * @param string $message - * @param array $context - * * @return void */ public function delegateMessage(string $message, array $context = []): void { @@ -101,13 +85,13 @@ class Registry implements IRegistry { while (($class = array_shift($this->lazyReporters)) !== null) { try { /** @var IReporter $reporter */ - $reporter = $this->serverContainer->query($class); + $reporter = Server::get($class); } catch (QueryException $e) { /* * There is a circular dependency between the logger and the registry, so * we can not inject it. Thus the static call. */ - \OC::$server->get(LoggerInterface::class)->critical('Could not load lazy crash reporter: ' . $e->getMessage(), [ + Server::get(LoggerInterface::class)->critical('Could not load lazy crash reporter: ' . $e->getMessage(), [ 'exception' => $e, ]); return; @@ -123,7 +107,7 @@ class Registry implements IRegistry { * There is a circular dependency between the logger and the registry, so * we can not inject it. Thus the static call. */ - \OC::$server->get(LoggerInterface::class)->critical('Could not register lazy crash reporter: ' . $e->getMessage(), [ + Server::get(LoggerInterface::class)->critical('Could not register lazy crash reporter: ' . $e->getMessage(), [ 'exception' => $e, ]); } diff --git a/tests/lib/AppFramework/AppTest.php b/tests/lib/AppFramework/AppTest.php index c43cd8e3c54..f9b7cf50675 100644 --- a/tests/lib/AppFramework/AppTest.php +++ b/tests/lib/AppFramework/AppTest.php @@ -29,7 +29,7 @@ function rrmdir($directory) { class AppTest extends \Test\TestCase { - private $container; + private DIContainer $container; private $io; private $api; private $controller; @@ -55,8 +55,8 @@ class AppTest extends \Test\TestCase { $this->controllerMethod = 'method'; $this->container[$this->controllerName] = $this->controller; - $this->container['Dispatcher'] = $this->dispatcher; - $this->container['OCP\\AppFramework\\Http\\IOutput'] = $this->io; + $this->container[Dispatcher::class] = $this->dispatcher; + $this->container[IOutput::class] = $this->io; $this->container['urlParams'] = ['_route' => 'not-profiler']; $this->appPath = __DIR__ . '/../../../apps/namespacetestapp'; @@ -165,7 +165,7 @@ class AppTest extends \Test\TestCase { } public function testCoreApp(): void { - $this->container['AppName'] = 'core'; + $this->container['appName'] = 'core'; $this->container['OC\Core\Controller\Foo'] = $this->controller; $this->container['urlParams'] = ['_route' => 'not-profiler']; @@ -183,7 +183,7 @@ class AppTest extends \Test\TestCase { } public function testSettingsApp(): void { - $this->container['AppName'] = 'settings'; + $this->container['appName'] = 'settings'; $this->container['OCA\Settings\Controller\Foo'] = $this->controller; $this->container['urlParams'] = ['_route' => 'not-profiler']; @@ -201,7 +201,7 @@ class AppTest extends \Test\TestCase { } public function testApp(): void { - $this->container['AppName'] = 'bar'; + $this->container['appName'] = 'bar'; $this->container['OCA\Bar\Controller\Foo'] = $this->controller; $this->container['urlParams'] = ['_route' => 'not-profiler']; diff --git a/tests/lib/AppFramework/DependencyInjection/DIContainerTest.php b/tests/lib/AppFramework/DependencyInjection/DIContainerTest.php index e84b44db9a0..31188b12f14 100644 --- a/tests/lib/AppFramework/DependencyInjection/DIContainerTest.php +++ b/tests/lib/AppFramework/DependencyInjection/DIContainerTest.php @@ -18,13 +18,13 @@ use OCP\AppFramework\Middleware; use OCP\AppFramework\QueryException; use OCP\IConfig; use OCP\IRequestId; +use PHPUnit\Framework\MockObject\MockObject; /** * @group DB */ class DIContainerTest extends \Test\TestCase { - /** @var DIContainer|\PHPUnit\Framework\MockObject\MockObject */ - private $container; + private DIContainer&MockObject $container; protected function setUp(): void { parent::setUp(); @@ -45,11 +45,13 @@ class DIContainerTest extends \Test\TestCase { public function testProvidesAppName(): void { $this->assertTrue(isset($this->container['AppName'])); + $this->assertTrue(isset($this->container['appName'])); } public function testAppNameIsSetCorrectly(): void { $this->assertEquals('name', $this->container['AppName']); + $this->assertEquals('name', $this->container['appName']); } public function testMiddlewareDispatcherIncludesSecurityMiddleware(): void { From 5d65f14e608f5c3cdb8f8c212f4f49cada024a03 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=B4me=20Chilliet?= Date: Thu, 19 Jun 2025 11:40:53 +0200 Subject: [PATCH 03/10] chore: Fix CrashReport\Registry tests MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Côme Chilliet --- .../lib/Support/CrashReport/RegistryTest.php | 31 ++++++------------- tests/lib/TestCase.php | 17 +++++++--- 2 files changed, 23 insertions(+), 25 deletions(-) diff --git a/tests/lib/Support/CrashReport/RegistryTest.php b/tests/lib/Support/CrashReport/RegistryTest.php index b65c21b759e..adf7579b202 100644 --- a/tests/lib/Support/CrashReport/RegistryTest.php +++ b/tests/lib/Support/CrashReport/RegistryTest.php @@ -12,27 +12,18 @@ namespace Test\Support\CrashReport; use Exception; use OC\Support\CrashReport\Registry; use OCP\AppFramework\QueryException; -use OCP\IServerContainer; use OCP\Support\CrashReport\ICollectBreadcrumbs; use OCP\Support\CrashReport\IMessageReporter; use OCP\Support\CrashReport\IReporter; use Test\TestCase; class RegistryTest extends TestCase { - /** @var IServerContainer|\PHPUnit\Framework\MockObject\MockObject */ - private $serverContainer; - - /** @var Registry */ - private $registry; + private Registry $registry; protected function setUp(): void { parent::setUp(); - $this->serverContainer = $this->createMock(IServerContainer::class); - - $this->registry = new Registry( - $this->serverContainer - ); + $this->registry = new Registry(); } /** @@ -45,13 +36,10 @@ class RegistryTest extends TestCase { $this->addToAssertionCount(1); } - public function testRegisterLazyCantLoad(): void { + public function testRegisterLazy(): void { $reporterClass = '\OCA\MyApp\Reporter'; $reporter = $this->createMock(IReporter::class); - $this->serverContainer->expects($this->once()) - ->method('query') - ->with($reporterClass) - ->willReturn($reporter); + $this->overwriteService($reporterClass, $reporter); $reporter->expects($this->once()) ->method('report'); $exception = new Exception('test'); @@ -60,16 +48,17 @@ class RegistryTest extends TestCase { $this->registry->delegateReport($exception); } - public function testRegisterLazy(): void { + /** + * Doesn't assert anything, just checks whether anything "explodes" + */ + public function testRegisterLazyCantLoad(): void { $reporterClass = '\OCA\MyApp\Reporter'; - $this->serverContainer->expects($this->once()) - ->method('query') - ->with($reporterClass) - ->willThrowException(new QueryException()); + /* We do not register reporterClass in DI, so it will throw a QueryException queried */ $exception = new Exception('test'); $this->registry->registerLazy($reporterClass); $this->registry->delegateReport($exception); + $this->addToAssertionCount(1); } public function testDelegateBreadcrumbCollection(): void { diff --git a/tests/lib/TestCase.php b/tests/lib/TestCase.php index e91e847e859..cdeefde748c 100644 --- a/tests/lib/TestCase.php +++ b/tests/lib/TestCase.php @@ -21,6 +21,7 @@ use OC\Files\ObjectStore\PrimaryObjectStoreConfig; use OC\Files\SetupManager; use OC\Files\View; use OC\Template\Base; +use OCP\AppFramework\QueryException; use OCP\Command\IBus; use OCP\DB\QueryBuilder\IQueryBuilder; use OCP\Defaults; @@ -91,7 +92,11 @@ abstract class TestCase extends \PHPUnit\Framework\TestCase { return false; } - $this->services[$name] = Server::get($name); + try { + $this->services[$name] = Server::get($name); + } catch (QueryException $e) { + $this->services[$name] = false; + } $container = \OC::$server->getAppContainerForService($name); $container = $container ?? \OC::$server; @@ -113,9 +118,13 @@ abstract class TestCase extends \PHPUnit\Framework\TestCase { $container = \OC::$server->getAppContainerForService($name); $container = $container ?? \OC::$server; - $container->registerService($name, function () use ($oldService) { - return $oldService; - }); + if ($oldService !== false) { + $container->registerService($name, function () use ($oldService) { + return $oldService; + }); + } else { + unset($container[$oldService]); + } unset($this->services[$name]); From bbe766b07a74d218a6140d29bcaa7322c9483f30 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=B4me=20Chilliet?= Date: Thu, 19 Jun 2025 11:41:43 +0200 Subject: [PATCH 04/10] fix: Make sure Request class can be dependency injected to fix SameSiteCookieMiddleware injection MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Côme Chilliet --- .../Security/SameSiteCookieMiddleware.php | 16 +++++----------- lib/private/Server.php | 3 ++- 2 files changed, 7 insertions(+), 12 deletions(-) diff --git a/lib/private/AppFramework/Middleware/Security/SameSiteCookieMiddleware.php b/lib/private/AppFramework/Middleware/Security/SameSiteCookieMiddleware.php index ed3bb232023..097ed1b2b8f 100644 --- a/lib/private/AppFramework/Middleware/Security/SameSiteCookieMiddleware.php +++ b/lib/private/AppFramework/Middleware/Security/SameSiteCookieMiddleware.php @@ -14,16 +14,10 @@ use OCP\AppFramework\Http\Response; use OCP\AppFramework\Middleware; class SameSiteCookieMiddleware extends Middleware { - /** @var Request */ - private $request; - - /** @var ControllerMethodReflector */ - private $reflector; - - public function __construct(Request $request, - ControllerMethodReflector $reflector) { - $this->request = $request; - $this->reflector = $reflector; + public function __construct( + private Request $request, + private ControllerMethodReflector $reflector, + ) { } public function beforeController($controller, $methodName) { @@ -59,7 +53,7 @@ class SameSiteCookieMiddleware extends Middleware { throw $exception; } - protected function setSameSiteCookie() { + protected function setSameSiteCookie(): void { $cookieParams = $this->request->getCookieParams(); $secureCookie = ($cookieParams['secure'] === true) ? 'secure; ' : ''; $policies = [ diff --git a/lib/private/Server.php b/lib/private/Server.php index b6dbdf62bdc..4b88a446405 100644 --- a/lib/private/Server.php +++ b/lib/private/Server.php @@ -871,7 +871,7 @@ class Server extends ServerContainer implements IServerContainer { $c->get(IMimeTypeDetector::class) ); }); - $this->registerService(\OCP\IRequest::class, function (ContainerInterface $c) { + $this->registerService(Request::class, function (ContainerInterface $c) { if (isset($this['urlParams'])) { $urlParams = $this['urlParams']; } else { @@ -905,6 +905,7 @@ class Server extends ServerContainer implements IServerContainer { $stream ); }); + $this->registerAlias(\OCP\IRequest::class, Request::class); $this->registerService(IRequestId::class, function (ContainerInterface $c): IRequestId { return new RequestId( From 2240acec7f55235cd163e3fb71edd50703c3908f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=B4me=20Chilliet?= Date: Thu, 19 Jun 2025 14:02:30 +0200 Subject: [PATCH 05/10] fix: Put back ScopedPsrLogger service MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Cannot use an alias for this one, as it depends upon LoggerInterface so that creates an infinite loop. Signed-off-by: Côme Chilliet --- .../AppFramework/DependencyInjection/DIContainer.php | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/lib/private/AppFramework/DependencyInjection/DIContainer.php b/lib/private/AppFramework/DependencyInjection/DIContainer.php index 01bb5a0674e..b42abcf633c 100644 --- a/lib/private/AppFramework/DependencyInjection/DIContainer.php +++ b/lib/private/AppFramework/DependencyInjection/DIContainer.php @@ -25,6 +25,7 @@ use OC\AppFramework\ScopedPsrLogger; use OC\AppFramework\Utility\SimpleContainer; use OC\Core\Middleware\TwoFactorMiddleware; use OC\Diagnostics\EventLogger; +use OC\Log\PsrLoggerAdapter; use OC\ServerContainer; use OC\Settings\AuthorizedGroupMapper; use OCA\WorkflowEngine\Manager; @@ -34,6 +35,7 @@ use OCP\AppFramework\QueryException; use OCP\AppFramework\Services\IAppConfig; use OCP\AppFramework\Services\IInitialState; use OCP\AppFramework\Utility\IControllerMethodReflector; +use OCP\Files\AppData\IAppDataFactory; use OCP\Files\Folder; use OCP\Files\IAppData; use OCP\Group\ISubAdmin; @@ -90,7 +92,7 @@ class DIContainer extends SimpleContainer implements IAppContainer { }); $this->registerService(IAppData::class, function (ContainerInterface $c): IAppData { - return $c->get(\OCP\Files\AppData\IAppDataFactory::class)->get($c->get('appName')); + return $c->get(IAppDataFactory::class)->get($c->get('appName')); }); $this->registerService(IL10N::class, function (ContainerInterface $c) { @@ -98,7 +100,13 @@ class DIContainer extends SimpleContainer implements IAppContainer { }); // Log wrappers - $this->registerAlias(LoggerInterface::class, ScopedPsrLogger::class); + $this->registerService(LoggerInterface::class, function (ContainerInterface $c) { + /* Cannot be an alias because it uses LoggerInterface so it would infinite loop */ + return new ScopedPsrLogger( + $c->get(PsrLoggerAdapter::class), + $c->get('appName') + ); + }); $this->registerService(IServerContainer::class, function () { return $this->getServer(); From 3dd4ba854ffd010ca3b66dcec2e6c1b0f2746768 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=B4me=20Chilliet?= Date: Thu, 19 Jun 2025 16:37:32 +0200 Subject: [PATCH 06/10] fix: Add back ContainerInterface service to DIContainer MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Otherwise it gets resolved to \OC::$server. Signed-off-by: Côme Chilliet --- lib/private/AppFramework/DependencyInjection/DIContainer.php | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/private/AppFramework/DependencyInjection/DIContainer.php b/lib/private/AppFramework/DependencyInjection/DIContainer.php index b42abcf633c..04570621131 100644 --- a/lib/private/AppFramework/DependencyInjection/DIContainer.php +++ b/lib/private/AppFramework/DependencyInjection/DIContainer.php @@ -116,6 +116,7 @@ class DIContainer extends SimpleContainer implements IAppContainer { $this->registerAlias(\OCP\WorkflowEngine\IManager::class, Manager::class); + $this->registerService(ContainerInterface::class, fn (ContainerInterface $c) => $c); $this->registerDeprecatedAlias(IAppContainer::class, ContainerInterface::class); // commonly used attributes From 580328507fe3a7df24ba357b75d82eba129d9461 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=B4me=20Chilliet?= Date: Tue, 24 Jun 2025 12:14:32 +0200 Subject: [PATCH 07/10] fix: Do not use deprecated IServerContainer in EventDispatcher MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This caused a call to logger too soon in init phase Signed-off-by: Côme Chilliet --- .../EventDispatcher/EventDispatcher.php | 4 +-- .../EventDispatcher/ServiceEventListener.php | 27 ++++++------------- 2 files changed, 10 insertions(+), 21 deletions(-) diff --git a/lib/private/EventDispatcher/EventDispatcher.php b/lib/private/EventDispatcher/EventDispatcher.php index b7554c439ea..474c902013b 100644 --- a/lib/private/EventDispatcher/EventDispatcher.php +++ b/lib/private/EventDispatcher/EventDispatcher.php @@ -14,7 +14,7 @@ use OCP\Broadcast\Events\IBroadcastEvent; use OCP\EventDispatcher\ABroadcastedEvent; use OCP\EventDispatcher\Event; use OCP\EventDispatcher\IEventDispatcher; -use OCP\IServerContainer; +use Psr\Container\ContainerInterface; use Psr\Log\LoggerInterface; use Symfony\Component\EventDispatcher\EventDispatcher as SymfonyDispatcher; use function get_class; @@ -22,7 +22,7 @@ use function get_class; class EventDispatcher implements IEventDispatcher { public function __construct( private SymfonyDispatcher $dispatcher, - private IServerContainer $container, + private ContainerInterface $container, private LoggerInterface $logger, ) { // inject the event dispatcher into the logger diff --git a/lib/private/EventDispatcher/ServiceEventListener.php b/lib/private/EventDispatcher/ServiceEventListener.php index 03a986ec78c..f9cc85b4126 100644 --- a/lib/private/EventDispatcher/ServiceEventListener.php +++ b/lib/private/EventDispatcher/ServiceEventListener.php @@ -12,7 +12,7 @@ namespace OC\EventDispatcher; use OCP\AppFramework\QueryException; use OCP\EventDispatcher\Event; use OCP\EventDispatcher\IEventListener; -use OCP\IServerContainer; +use Psr\Container\ContainerInterface; use Psr\Log\LoggerInterface; use function sprintf; @@ -23,24 +23,13 @@ use function sprintf; * created by the service container */ final class ServiceEventListener { - /** @var IServerContainer */ - private $container; + private ?IEventListener $service = null; - /** @var string */ - private $class; - - /** @var LoggerInterface */ - private $logger; - - /** @var null|IEventListener */ - private $service; - - public function __construct(IServerContainer $container, - string $class, - LoggerInterface $logger) { - $this->container = $container; - $this->class = $class; - $this->logger = $logger; + public function __construct( + private ContainerInterface $container, + private string $class, + private LoggerInterface $logger, + ) { } public function __invoke(Event $event) { @@ -49,7 +38,7 @@ final class ServiceEventListener { // TODO: fetch from the app containers, otherwise any custom services, // parameters and aliases won't be resolved. // See https://github.com/nextcloud/server/issues/27793 for details. - $this->service = $this->container->query($this->class); + $this->service = $this->container->get($this->class); } catch (QueryException $e) { $this->logger->error( sprintf( From 7bdfd942463e55dd7553c279cae8bab15d24d23b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=B4me=20Chilliet?= Date: Tue, 24 Jun 2025 16:15:52 +0200 Subject: [PATCH 08/10] chore: update psalm baseline MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Côme Chilliet --- build/psalm-baseline.xml | 9 --------- 1 file changed, 9 deletions(-) diff --git a/build/psalm-baseline.xml b/build/psalm-baseline.xml index e40c10bfc67..72b6311b2fc 100644 --- a/build/psalm-baseline.xml +++ b/build/psalm-baseline.xml @@ -1581,9 +1581,6 @@ - - - @@ -3364,18 +3361,12 @@ - - - server]]> - - - From 90a2c77abf67672a517834e793c84bdaaf43cc43 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=B4me=20Chilliet?= Date: Tue, 1 Jul 2025 10:52:50 +0200 Subject: [PATCH 09/10] fix: Use Server::get in files_sharing DeletedShareAPIController MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit In this case we do not want the application DI container because we are requesting classes from other applications, so it’s better to ask the server container. We use \OCP\Server::get for this. Signed-off-by: Côme Chilliet --- .../lib/Controller/DeletedShareAPIController.php | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/apps/files_sharing/lib/Controller/DeletedShareAPIController.php b/apps/files_sharing/lib/Controller/DeletedShareAPIController.php index 5abb8e69bdd..2fa4d7c668f 100644 --- a/apps/files_sharing/lib/Controller/DeletedShareAPIController.php +++ b/apps/files_sharing/lib/Controller/DeletedShareAPIController.php @@ -24,11 +24,11 @@ use OCP\Files\NotFoundException; use OCP\IGroupManager; use OCP\IRequest; use OCP\IUserManager; +use OCP\Server; use OCP\Share\Exceptions\GenericShareException; use OCP\Share\Exceptions\ShareNotFound; use OCP\Share\IManager as ShareManager; use OCP\Share\IShare; -use Psr\Container\ContainerInterface; /** * @psalm-import-type Files_SharingDeletedShare from ResponseDefinitions @@ -44,7 +44,6 @@ class DeletedShareAPIController extends OCSController { private IGroupManager $groupManager, private IRootFolder $rootFolder, private IAppManager $appManager, - private ContainerInterface $serverContainer, ) { parent::__construct($appName, $request); } @@ -202,7 +201,7 @@ class DeletedShareAPIController extends OCSController { throw new QueryException(); } - return $this->serverContainer->get('\OCA\Talk\Share\Helper\DeletedShareAPIController'); + return Server::get('\OCA\Talk\Share\Helper\DeletedShareAPIController'); } /** @@ -219,7 +218,7 @@ class DeletedShareAPIController extends OCSController { throw new QueryException(); } - return $this->serverContainer->get('\OCA\Deck\Sharing\ShareAPIHelper'); + return Server::get('\OCA\Deck\Sharing\ShareAPIHelper'); } /** @@ -236,6 +235,6 @@ class DeletedShareAPIController extends OCSController { throw new QueryException(); } - return $this->serverContainer->get('\OCA\ScienceMesh\Sharing\ShareAPIHelper'); + return Server::get('\OCA\ScienceMesh\Sharing\ShareAPIHelper'); } } From 2346a528ba071f51cb0596c9a1724131ef0c6d6d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=B4me=20Chilliet?= Date: Tue, 1 Jul 2025 11:11:07 +0200 Subject: [PATCH 10/10] fix: Tidy up middleware registration code and scope them to application container MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This make sure that all middlewares get a logger scoped to the application id, among other things. Signed-off-by: Côme Chilliet --- .../DependencyInjection/DIContainer.php | 73 +++++++------------ 1 file changed, 28 insertions(+), 45 deletions(-) diff --git a/lib/private/AppFramework/DependencyInjection/DIContainer.php b/lib/private/AppFramework/DependencyInjection/DIContainer.php index 04570621131..5ccc1b7d348 100644 --- a/lib/private/AppFramework/DependencyInjection/DIContainer.php +++ b/lib/private/AppFramework/DependencyInjection/DIContainer.php @@ -10,15 +10,24 @@ declare(strict_types=1); namespace OC\AppFramework\DependencyInjection; -use OC; use OC\AppFramework\Http; use OC\AppFramework\Http\Dispatcher; use OC\AppFramework\Http\Output; +use OC\AppFramework\Middleware\AdditionalScriptsMiddleware; +use OC\AppFramework\Middleware\CompressionMiddleware; use OC\AppFramework\Middleware\FlowV2EphemeralSessionsMiddleware; use OC\AppFramework\Middleware\MiddlewareDispatcher; +use OC\AppFramework\Middleware\NotModifiedMiddleware; use OC\AppFramework\Middleware\OCSMiddleware; +use OC\AppFramework\Middleware\PublicShare\PublicShareMiddleware; +use OC\AppFramework\Middleware\Security\BruteForceMiddleware; use OC\AppFramework\Middleware\Security\CORSMiddleware; +use OC\AppFramework\Middleware\Security\CSPMiddleware; +use OC\AppFramework\Middleware\Security\FeaturePolicyMiddleware; +use OC\AppFramework\Middleware\Security\PasswordConfirmationMiddleware; use OC\AppFramework\Middleware\Security\RateLimitingMiddleware; +use OC\AppFramework\Middleware\Security\ReloadExecutionMiddleware; +use OC\AppFramework\Middleware\Security\SameSiteCookieMiddleware; use OC\AppFramework\Middleware\Security\SecurityMiddleware; use OC\AppFramework\Middleware\SessionMiddleware; use OC\AppFramework\ScopedPsrLogger; @@ -172,25 +181,12 @@ class DIContainer extends SimpleContainer implements IAppContainer { $dispatcher = new MiddlewareDispatcher(); - $dispatcher->registerMiddleware( - $c->get(OC\AppFramework\Middleware\CompressionMiddleware::class) - ); - - $dispatcher->registerMiddleware($c->get(OC\AppFramework\Middleware\NotModifiedMiddleware::class)); - - $dispatcher->registerMiddleware( - $c->get(OC\AppFramework\Middleware\Security\ReloadExecutionMiddleware::class) - ); - - $dispatcher->registerMiddleware( - $c->get(OC\AppFramework\Middleware\Security\SameSiteCookieMiddleware::class) - ); - $dispatcher->registerMiddleware( - $c->get(CORSMiddleware::class) - ); - $dispatcher->registerMiddleware( - $c->get(OCSMiddleware::class) - ); + $dispatcher->registerMiddleware($c->get(CompressionMiddleware::class)); + $dispatcher->registerMiddleware($c->get(NotModifiedMiddleware::class)); + $dispatcher->registerMiddleware($c->get(ReloadExecutionMiddleware::class)); + $dispatcher->registerMiddleware($c->get(SameSiteCookieMiddleware::class)); + $dispatcher->registerMiddleware($c->get(CORSMiddleware::class)); + $dispatcher->registerMiddleware($c->get(OCSMiddleware::class)); $dispatcher->registerMiddleware($c->get(FlowV2EphemeralSessionsMiddleware::class)); @@ -211,28 +207,14 @@ class DIContainer extends SimpleContainer implements IAppContainer { $c->get(IRemoteAddress::class), ); $dispatcher->registerMiddleware($securityMiddleware); - $dispatcher->registerMiddleware( - $c->get(OC\AppFramework\Middleware\Security\CSPMiddleware::class) - ); - $dispatcher->registerMiddleware( - $c->get(OC\AppFramework\Middleware\Security\FeaturePolicyMiddleware::class) - ); - $dispatcher->registerMiddleware( - $c->get(OC\AppFramework\Middleware\Security\PasswordConfirmationMiddleware::class) - ); - $dispatcher->registerMiddleware( - $c->get(TwoFactorMiddleware::class) - ); - $dispatcher->registerMiddleware( - $c->get(OC\AppFramework\Middleware\Security\BruteForceMiddleware::class) - ); + $dispatcher->registerMiddleware($c->get(CSPMiddleware::class)); + $dispatcher->registerMiddleware($c->get(FeaturePolicyMiddleware::class)); + $dispatcher->registerMiddleware($c->get(PasswordConfirmationMiddleware::class)); + $dispatcher->registerMiddleware($c->get(TwoFactorMiddleware::class)); + $dispatcher->registerMiddleware($c->get(BruteForceMiddleware::class)); $dispatcher->registerMiddleware($c->get(RateLimitingMiddleware::class)); - $dispatcher->registerMiddleware( - $c->get(OC\AppFramework\Middleware\PublicShare\PublicShareMiddleware::class) - ); - $dispatcher->registerMiddleware( - $c->get(\OC\AppFramework\Middleware\AdditionalScriptsMiddleware::class) - ); + $dispatcher->registerMiddleware($c->get(PublicShareMiddleware::class)); + $dispatcher->registerMiddleware($c->get(AdditionalScriptsMiddleware::class)); $coordinator = $c->get(\OC\AppFramework\Bootstrap\Coordinator::class); $registrationContext = $coordinator->getRegistrationContext(); @@ -249,9 +231,7 @@ class DIContainer extends SimpleContainer implements IAppContainer { $dispatcher->registerMiddleware($c->get($middleWare)); } - $dispatcher->registerMiddleware( - $c->get(SessionMiddleware::class) - ); + $dispatcher->registerMiddleware($c->get(SessionMiddleware::class)); return $dispatcher; }); @@ -369,7 +349,10 @@ class DIContainer extends SimpleContainer implements IAppContainer { return parent::query($name); } elseif (str_starts_with($name, \OC\AppFramework\App::buildAppNamespace($this->appName) . '\\')) { return parent::query($name); - } elseif (str_starts_with($name, 'OC\\AppFramework\\Services\\')) { + } elseif ( + str_starts_with($name, 'OC\\AppFramework\\Services\\') + || str_starts_with($name, 'OC\\AppFramework\\Middleware\\') + ) { /* AppFramework services are scoped to the application */ return parent::query($name); }