diff options
author | Christoph Wurst <christoph@winzerhof-wurst.at> | 2022-12-15 10:37:27 +0100 |
---|---|---|
committer | Christoph Wurst <christoph@winzerhof-wurst.at> | 2023-01-18 14:00:38 +0100 |
commit | 20fcfb573951a594f71afaf97678d38d8b05c9f2 (patch) | |
tree | 9adb7ba2d021269dc0efd4d47d4076563a28c363 /lib/private/AppFramework | |
parent | 9e08e4999821a0cf7c6b08fd9ab05f8d057c8362 (diff) | |
download | nextcloud-server-20fcfb573951a594f71afaf97678d38d8b05c9f2.tar.gz nextcloud-server-20fcfb573951a594f71afaf97678d38d8b05c9f2.zip |
feat(app framework)!: Inject services into controller methods
Usually Nextcloud DI goes through constructor injection. This has the
implication that each instance of a class builds the full DI tree. That
is the injected services, their services, etc. Occasionally there is a
service that is only needed for one controller method. Then the DI tree
is build regardless if used or not.
If services are injected into the method, we only build the DI tree if
that method gets executed.
This is also how Laravel allows injection.
Signed-off-by: Christoph Wurst <christoph@winzerhof-wurst.at>
Diffstat (limited to 'lib/private/AppFramework')
-rw-r--r-- | lib/private/AppFramework/DependencyInjection/DIContainer.php | 3 | ||||
-rw-r--r-- | lib/private/AppFramework/Http/Dispatcher.php | 9 |
2 files changed, 10 insertions, 2 deletions
diff --git a/lib/private/AppFramework/DependencyInjection/DIContainer.php b/lib/private/AppFramework/DependencyInjection/DIContainer.php index 462f9b978fd..6e0e452bccd 100644 --- a/lib/private/AppFramework/DependencyInjection/DIContainer.php +++ b/lib/private/AppFramework/DependencyInjection/DIContainer.php @@ -191,7 +191,8 @@ class DIContainer extends SimpleContainer implements IAppContainer { $c->get(IConfig::class), $c->get(IDBConnection::class), $c->get(LoggerInterface::class), - $c->get(EventLogger::class) + $c->get(EventLogger::class), + $c, ); }); diff --git a/lib/private/AppFramework/Http/Dispatcher.php b/lib/private/AppFramework/Http/Dispatcher.php index aaaf2ba6560..cc288edc966 100644 --- a/lib/private/AppFramework/Http/Dispatcher.php +++ b/lib/private/AppFramework/Http/Dispatcher.php @@ -42,6 +42,7 @@ use OCP\AppFramework\Http\Response; use OCP\Diagnostics\IEventLogger; use OCP\IConfig; use OCP\IRequest; +use Psr\Container\ContainerInterface; use Psr\Log\LoggerInterface; /** @@ -73,6 +74,8 @@ class Dispatcher { /** @var IEventLogger */ private $eventLogger; + private ContainerInterface $appContainer; + /** * @param Http $protocol the http protocol with contains all status headers * @param MiddlewareDispatcher $middlewareDispatcher the dispatcher which @@ -92,7 +95,8 @@ class Dispatcher { IConfig $config, ConnectionAdapter $connection, LoggerInterface $logger, - IEventLogger $eventLogger) { + IEventLogger $eventLogger, + ContainerInterface $appContainer) { $this->protocol = $protocol; $this->middlewareDispatcher = $middlewareDispatcher; $this->reflector = $reflector; @@ -101,6 +105,7 @@ class Dispatcher { $this->connection = $connection; $this->logger = $logger; $this->eventLogger = $eventLogger; + $this->appContainer = $appContainer; } @@ -216,6 +221,8 @@ class Dispatcher { $value = false; } elseif ($value !== null && \in_array($type, $types, true)) { settype($value, $type); + } elseif ($value === null && $type !== null && $this->appContainer->has($type)) { + $value = $this->appContainer->get($type); } $arguments[] = $value; |