Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow IBootstrap::boot to have services injected directly #26723

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions apps/files/lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
use OCP\Share\IManager as IShareManager;
use OCP\Util;
use Psr\Container\ContainerInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;

class Application extends App implements IBootstrap {
public const APP_ID = 'files';
Expand Down Expand Up @@ -121,12 +122,16 @@ public function register(IRegistrationContext $context): void {
$context->registerNotifierService(Notifier::class);
}

public function boot(IBootContext $context): void {
$context->injectFn(Closure::fromCallable([$this, 'registerCollaboration']));
$context->injectFn([Listener::class, 'register']);
$context->injectFn(Closure::fromCallable([$this, 'registerSearchProvider']));
public function boot(IBootContext $context,
IProviderManager $providerManager,
EventDispatcherInterface $dispatcher,
ISearch $search,
IL10N $l10n): void {
$this->registerCollaboration($providerManager);
Listener::register($dispatcher);
$this->registerSearchProvider($search);
$this->registerTemplates();
$context->injectFn(Closure::fromCallable([$this, 'registerNavigation']));
$this->registerNavigation($l10n);
$this->registerHooks();
}

Expand Down
6 changes: 5 additions & 1 deletion lib/private/AppFramework/Bootstrap/Coordinator.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
use OC\Support\CrashReport\Registry;
use OC_App;
use OCP\AppFramework\App;
use OCP\AppFramework\Bootstrap\IBootContext;
use OCP\AppFramework\Bootstrap\IBootstrap;
use OCP\AppFramework\QueryException;
use OCP\Dashboard\IManager;
Expand Down Expand Up @@ -173,7 +174,10 @@ public function bootApp(string $appId): void {
if ($application instanceof IBootstrap) {
/** @var BootContext $context */
$context = new BootContext($application->getContainer());
$application->boot($context);
$injector = new FunctionInjector($application->getContainer(), [
IBootContext::class => $context,
]);
$injector->injectFn([$application, 'boot']);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reported that Psalm chokes on this at vimeo/psalm#5667

}
} catch (QueryException $e) {
$this->logger->logException($e, [
Expand Down
13 changes: 12 additions & 1 deletion lib/private/AppFramework/Bootstrap/FunctionInjector.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,26 @@ class FunctionInjector {
/** @var ContainerInterface */
private $container;

public function __construct(ContainerInterface $container) {
/**
* @var object[]
* @psalm-var array<class-string, object>
*/
private $overrides;

public function __construct(ContainerInterface $container, array $overrides = []) {
$this->container = $container;
$this->overrides = $overrides;
}

public function injectFn(callable $fn) {
$reflected = new ReflectionFunction(Closure::fromCallable($fn));
return $fn(...array_map(function (ReflectionParameter $param) {
// First we try by type (more likely these days)
if (($type = $param->getType()) !== null) {
if (isset($this->overrides[$type->getName()])) {
return $this->overrides[$type->getName()];
}

try {
return $this->container->get($type->getName());
} catch (QueryException $ex) {
Expand Down
15 changes: 1 addition & 14 deletions lib/public/AppFramework/Bootstrap/IBootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

/**
* @since 20.0.0
* @method void boot(IBootContext $context, ...$params) Boot the application
*/
interface IBootstrap {

Expand All @@ -37,18 +38,4 @@ interface IBootstrap {
* @since 20.0.0
*/
public function register(IRegistrationContext $context): void;

/**
* Boot the application
*
* At this stage you can assume that all services are registered and the DI
* container(s) are ready to be queried.
*
* This is also the state where an optional `appinfo/app.php` was loaded.
*
* @param IBootContext $context
*
* @since 20.0.0
*/
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be fine to just put this in the dev manual, right? At least that is fine for me.

public function boot(IBootContext $context): void;
}
13 changes: 13 additions & 0 deletions tests/lib/AppFramework/Bootstrap/FunctionInjectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,17 @@ public function testInjectFnByName(): void {
// Nothing to assert. No errors means everything is fine.
$this->addToAssertionCount(1);
}

public function testInjectWithOverride(): void {
$obj = new class() implements Foo {};

$injector = new FunctionInjector($this->container, [
Foo::class => $obj,
]);
$injector->injectFn(static function (Foo $f): void {
});

// Nothing to assert. No errors means everything is fine.
$this->addToAssertionCount(1);
}
}