Compare commits

...

2 Commits

Author SHA1 Message Date
Christoph Wurst 06a4cf59b2
Inject file boot services directly
Signed-off-by: Christoph Wurst <christoph@winzerhof-wurst.at>
2021-04-23 16:07:04 +02:00
Christoph Wurst 4845834468
Allow IBootstrap::boot to have services injected directly
Signed-off-by: Christoph Wurst <christoph@winzerhof-wurst.at>
2021-04-23 16:01:47 +02:00
5 changed files with 41 additions and 21 deletions

View File

@ -64,6 +64,7 @@ use OCP\IUserSession;
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';
@ -121,12 +122,16 @@ class Application extends App implements IBootstrap {
$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();
}

View File

@ -32,6 +32,7 @@ namespace OC\AppFramework\Bootstrap;
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;
@ -173,7 +174,10 @@ class Coordinator {
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']);
}
} catch (QueryException $e) {
$this->logger->logException($e, [

View File

@ -38,8 +38,15 @@ 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) {
@ -47,6 +54,10 @@ class FunctionInjector {
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) {

View File

@ -28,6 +28,7 @@ namespace OCP\AppFramework\Bootstrap;
/**
* @since 20.0.0
* @method void boot(IBootContext $context, ...$params) Boot the application
*/
interface IBootstrap {
@ -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
*/
public function boot(IBootContext $context): void;
}

View File

@ -81,4 +81,17 @@ class FunctionInjectorTest extends TestCase {
// 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);
}
}