From 4a3ea04baaa67f2cbf23c3d9f776373adad375a9 Mon Sep 17 00:00:00 2001 From: Christoph Wurst Date: Mon, 29 Jun 2020 15:52:47 +0200 Subject: 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 --- lib/public/AppFramework/Bootstrap/IBootContext.php | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) (limited to 'lib/public') 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); } -- cgit v1.2.3