Esempio n. 1
0
    $c->configure('b', function ($b) {
        $b += 1;
    });
    // no change
    eq($c->get('a'), 3, 'can apply multiple configuration functions');
    eq($c->get('b'), 4, 'can infer component name from param name');
    expect(NotFoundException::class, 'should throw on attempt to configure undefined component', function () use($c) {
        $c->configure('nope', function () {
        });
    });
    $c = new Container();
    $c->register(Foo::class);
    $c->register('zap', Bar::class);
    $ok = false;
    $c->configure(Foo::class, function (Foo $foo, Bar $bar) use(&$ok) {
        $ok = true;
    }, ['bar' => $c->ref('zap')]);
    $got_foo = false;
    $c->configure(function (Foo $few) use(&$got_foo) {
        $got_foo = true;
    });
    $c->get(Foo::class);
    ok($ok, 'can use parameter list/map in calls to configure()');
    ok($got_foo, 'can infer component name from argument type-hint');
});
test('named components take precedence over type-hints', function () {
    $container = new Container();
    $container->register(FileCache::class, ["/tmp/foo"]);
    $container->register("cache", FileCache::class, ["/tmp/bar"]);
    $by_type = null;
    $container->call(function (FileCache $cache) use(&$by_type) {