diff options
author | Christoph Wurst <christoph@winzerhof-wurst.at> | 2023-01-23 17:11:39 +0100 |
---|---|---|
committer | Christoph Wurst <christoph@winzerhof-wurst.at> | 2023-01-26 11:54:28 +0100 |
commit | 8d9af3e26214f07e833a6b8d8b60329fc71916a7 (patch) | |
tree | c41dc1835261e45f9f5751b0d07d1f45d8f4d7ba /lib/private | |
parent | 3eb73d0a3d081194e676b7e7ba220a118e01e18d (diff) | |
download | nextcloud-server-8d9af3e26214f07e833a6b8d8b60329fc71916a7.tar.gz nextcloud-server-8d9af3e26214f07e833a6b8d8b60329fc71916a7.zip |
feat(app-framework): Add support for global middlewares
This allows apps to register middlewares that always register, not just
for the app's own requests
Signed-off-by: Christoph Wurst <christoph@winzerhof-wurst.at>
Diffstat (limited to 'lib/private')
3 files changed, 54 insertions, 7 deletions
diff --git a/lib/private/AppFramework/Bootstrap/MiddlewareRegistration.php b/lib/private/AppFramework/Bootstrap/MiddlewareRegistration.php new file mode 100644 index 00000000000..da226b311e0 --- /dev/null +++ b/lib/private/AppFramework/Bootstrap/MiddlewareRegistration.php @@ -0,0 +1,45 @@ +<?php + +declare(strict_types=1); + +/* + * @copyright 2023 Christoph Wurst <christoph@winzerhof-wurst.at> + * + * @author 2023 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; + +use OCP\AppFramework\Middleware; + +/** + * @psalm-immutable + * @template-extends ServiceRegistration<Middleware> + */ +class MiddlewareRegistration extends ServiceRegistration { + private bool $global; + + public function __construct(string $appId, string $service, bool $global) { + parent::__construct($appId, $service); + $this->global = $global; + } + + public function isGlobal(): bool { + return $this->global; + } +} diff --git a/lib/private/AppFramework/Bootstrap/RegistrationContext.php b/lib/private/AppFramework/Bootstrap/RegistrationContext.php index ac3f42ff2af..236aa106d02 100644 --- a/lib/private/AppFramework/Bootstrap/RegistrationContext.php +++ b/lib/private/AppFramework/Bootstrap/RegistrationContext.php @@ -94,7 +94,7 @@ class RegistrationContext { /** @var EventListenerRegistration[] */ private $eventListeners = []; - /** @var ServiceRegistration<Middleware>[] */ + /** @var MiddlewareRegistration[] */ private $middlewares = []; /** @var ServiceRegistration<IProvider>[] */ @@ -205,10 +205,11 @@ class RegistrationContext { ); } - public function registerMiddleware(string $class): void { + public function registerMiddleware(string $class, bool $global = false): void { $this->context->registerMiddleware( $this->appId, - $class + $class, + $global, ); } @@ -368,8 +369,8 @@ class RegistrationContext { /** * @psalm-param class-string<Middleware> $class */ - public function registerMiddleware(string $appId, string $class): void { - $this->middlewares[] = new ServiceRegistration($appId, $class); + public function registerMiddleware(string $appId, string $class, bool $global): void { + $this->middlewares[] = new MiddlewareRegistration($appId, $class, $global); } public function registerSearchProvider(string $appId, string $class) { @@ -617,7 +618,7 @@ class RegistrationContext { } /** - * @return ServiceRegistration<Middleware>[] + * @return MiddlewareRegistration[] */ public function getMiddlewareRegistrations(): array { return $this->middlewares; diff --git a/lib/private/AppFramework/DependencyInjection/DIContainer.php b/lib/private/AppFramework/DependencyInjection/DIContainer.php index 6e7f054a80b..9b202f07fbf 100644 --- a/lib/private/AppFramework/DependencyInjection/DIContainer.php +++ b/lib/private/AppFramework/DependencyInjection/DIContainer.php @@ -321,7 +321,8 @@ class DIContainer extends SimpleContainer implements IAppContainer { if ($registrationContext !== null) { $appId = $this->getAppName(); foreach ($registrationContext->getMiddlewareRegistrations() as $middlewareRegistration) { - if ($middlewareRegistration->getAppId() === $appId) { + if ($middlewareRegistration->getAppId() === $appId + || $middlewareRegistration->isGlobal()) { $dispatcher->registerMiddleware($c->get($middlewareRegistration->getService())); } } |