my $self = shift;
$self->get_sub_container('component')->add_service(
- Catalyst::IOC::ConstructorInjection->new(
- name => 'model_Bar',
- lifecycle => 'Singleton',
- class => 'TestAppCustomContainer::Model::Bar',
- dependencies => [
- depends_on( '/application_name' ),
- depends_on( '/config' ),
- depends_on( '/model/Foo' ),
- ],
- )
- );
- $self->get_sub_container('model')->add_service(
- Catalyst::IOC::BlockInjection->new(
- name => 'Bar',
- lifecycle => 'Singleton',
- dependencies => [
- Bread::Board::Dependency->new(
- service_path => 'Foo',
+ # Catalyst::IOC::ConstructorInjection gives the constructor the wrong
+ # parameters
+ Bread::Board::ConstructorInjection->new(
+ name => 'model_Bar',
+ lifecycle => 'Singleton',
+ class => 'TestAppCustomContainer::Model::Bar',
+ constructor_name => 'new',
+ dependencies => {
+ application_name => depends_on( '/application_name' ),
+ config => depends_on( '/config' ),
+ foo => Bread::Board::Dependency->new(
+ service_name => 'foo',
+ service_path => '/model/Foo',
# FIXME - obviously this is a mistake
# what to do with ctx here?
accept_context_args => [ +{} ],
},
),
+ },
+ )
+ );
+ $self->get_sub_container('model')->add_service(
+ Catalyst::IOC::BlockInjection->new(
+ name => 'Bar',
+ lifecycle => 'Singleton',
+ dependencies => [
depends_on( '/component/model_Bar' ),
],
block => sub {
- my $s = shift;
-
- my $foo = $s->param('Foo');
- $foo->inc_bar_got_it;
-
- return $s->param('model_Bar');
+ shift->param('model_Bar');
},
)
);
+ # FIXME - this is to avoid the default service to be added
+ # if that happened, the app would die
$self->get_sub_container('component')->add_service(
- Catalyst::IOC::ConstructorInjection->new(
- name => 'model_Baz',
- class => 'TestAppCustomContainer::Model::Baz',
- lifecycle => 'Singleton',
-
- # while it doesn't fully work
- #lifecycle => '+Catalyst::IOC::LifeCycle::Request',
- dependencies => [
- depends_on( '/application_name' ),
- depends_on( '/config' ),
- depends_on( '/model/Foo' ),
- ],
- )
+ service model_Baz => 'TestAppCustomContainer::Model::Baz',
);
$self->get_sub_container('model')->add_service(
+ # FIXME - i think it should be a ConstructorInjection
+ # but only BlockInjection gets ctx parameter
Catalyst::IOC::BlockInjection->new(
name => 'Baz',
+ lifecycle => '+Catalyst::IOC::LifeCycle::Request',
dependencies => [
Bread::Board::Dependency->new(
+ service_name => 'foo',
service_path => 'Foo',
# FIXME - same as above
accept_context_args => [ +{} ],
},
),
- depends_on( '/component/model_Baz' ),
],
block => sub {
- my $s = shift;
-
- my $foo = $s->param('Foo');
- $foo->inc_baz_got_it;
-
- return $s->param('model_Baz');
+ TestAppCustomContainer::Model::Baz->new(foo => shift->param('foo'));
},
)
);
is(get('/container_isa'), $self->container_class, 'and container isa our container class');
{
- # Foo ACCEPT_CONTEXT called twice - total: 2
- # TestAppCustomContainer::Role::HoldsFoo COMPONENT
- # and in the block injection
+ # Foo ACCEPT_CONTEXT called - total: 1
ok(my ($res, $c) = ctx_request('/get_model_baz'), 'request');
ok($res->is_success, 'request 2xx');
is($res->content, 'TestAppCustomContainer::Model::Baz', 'content is expected');
- ok(my $baz = $c->container->get_sub_container('component')->resolve(service => 'model_Baz'), 'fetching Baz');
+ ok(my $baz = $c->container->get_sub_container('model')->resolve(service => 'Baz', parameters => { ctx => $c, accept_context_args => [$c] } ), 'fetching Baz');
isa_ok($baz, 'TestAppCustomContainer::Model::Baz');
is($baz->accept_context_called, 1, 'ACCEPT_CONTEXT called');
isa_ok($baz->foo, 'TestAppCustomContainer::Model::Foo', 'Baz got Foo ok');
isa_ok($foo, 'TestAppCustomContainer::Model::Foo');
is($foo->baz_got_it, 1, 'Baz accessed Foo once');
- # Foo ACCEPT_CONTEXT called - total: 3
+ # Foo ACCEPT_CONTEXT called - total: 2
ok(get('/get_model_baz'), 'another request');
- is($baz->accept_context_called, 2, 'ACCEPT_CONTEXT called again');
+ is($baz->accept_context_called, 1, 'ACCEPT_CONTEXT not called again (instance per context)');
is($foo->baz_got_it, 2, 'Baz accessed Foo again');
}
{
- # Foo ACCEPT_CONTEXT called twice - total: 5
+ # Foo ACCEPT_CONTEXT called - total: 3
ok(my ($res, $c) = ctx_request('/get_model_bar'), 'request');
ok($res->is_success, 'request 2xx');
is($res->content, 'TestAppCustomContainer::Model::Bar', 'content is expected');
isa_ok($foo, 'TestAppCustomContainer::Model::Foo');
is($foo->bar_got_it, 1, 'Bar accessed Foo once');
- # Foo ACCEPT_CONTEXT *not* called - total: 5
+ # Foo ACCEPT_CONTEXT *not* called - total: 3
ok(get('/get_model_bar'), 'another request');
is($bar->accept_context_called, 1, 'ACCEPT_CONTEXT not called again (lifecycle is Singleton)');
is($foo->bar_got_it, 1, 'Bar didn\'t access Foo again');
}
{
- # Foo ACCEPT_CONTEXT called - total: 6
+ # Foo ACCEPT_CONTEXT called - total: 4
ok(my ($res, $c) = ctx_request('/get_model_foo'), 'request');
ok($res->is_success, 'request 2xx');
is($res->content, 'TestAppCustomContainer::Model::Foo', 'content is expected');
ok(my $foo = $c->container->get_sub_container('component')->resolve(service => 'model_Foo'), 'fetching Foo');
isa_ok($foo, 'TestAppCustomContainer::Model::Foo');
- is($foo->accept_context_called, 6, 'ACCEPT_CONTEXT called');
+ is($foo->accept_context_called, 4, 'ACCEPT_CONTEXT called');
is($foo->bar_got_it, 1, 'Bar accessed Foo once');
is($foo->baz_got_it, 2, 'Baz accessed Foo twice');
}