diff options
author | Christoph Wurst <christoph@winzerhof-wurst.at> | 2020-06-29 15:52:47 +0200 |
---|---|---|
committer | Christoph Wurst <christoph@winzerhof-wurst.at> | 2020-07-03 14:37:46 +0200 |
commit | 4a3ea04baaa67f2cbf23c3d9f776373adad375a9 (patch) | |
tree | 148892dbdefd211681d03041bdfc9a35f7e86a33 /lib/public | |
parent | 0825530a1b1cbb98195ec15cbbd46d121bc65373 (diff) | |
download | nextcloud-server-4a3ea04baaa67f2cbf23c3d9f776373adad375a9.tar.gz nextcloud-server-4a3ea04baaa67f2cbf23c3d9f776373adad375a9.zip |
Callable parameter injection
This is like what we have to DI and classes, but for callables.
The motivating factor is to get rid of *service locators* in the `boot`
method of apps as a new pattern is about to emerge where we have lots of
`query` calls on the app or server container in order to fetch some
services.
With this little helper it's possible to call another (public) method
and magically have everything injected.
Signed-off-by: Christoph Wurst <christoph@winzerhof-wurst.at>
Diffstat (limited to 'lib/public')
-rw-r--r-- | lib/public/AppFramework/Bootstrap/IBootContext.php | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/lib/public/AppFramework/Bootstrap/IBootContext.php b/lib/public/AppFramework/Bootstrap/IBootContext.php index 6ceec5d2fcb..47b26528c66 100644 --- a/lib/public/AppFramework/Bootstrap/IBootContext.php +++ b/lib/public/AppFramework/Bootstrap/IBootContext.php @@ -26,7 +26,9 @@ declare(strict_types=1); namespace OCP\AppFramework\Bootstrap; use OCP\AppFramework\IAppContainer; +use OCP\AppFramework\QueryException; use OCP\IServerContainer; +use Throwable; /** * @since 20.0.0 @@ -52,4 +54,24 @@ interface IBootContext { * @since 20.0.0 */ public function getServerContainer(): IServerContainer; + + /** + * Invoke the given callable and inject all parameters based on their types + * and names + * + * Note: when used with methods, make sure they are public or use \Closure::fromCallable + * to wrap the private method call, e.g. + * * `$context->injectFn([$obj, 'publicMethod'])` + * * `$context->injectFn([$this, 'publicMethod'])` + * * `$context->injectFn(\Closure::fromCallable([$this, 'privateMethod']))` + * + * Note: the app container will be queried + * + * @param callable $fn + * @throws QueryException if at least one of the parameter can't be resolved + * @throws Throwable any error the function invocation might cause + * @return mixed|null the return value of the invoked function, if any + * @since 20.0.0 + */ + public function injectFn(callable $fn); } |