X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FCatalyst%2FIOC%2FContainer.pm;h=d3d67e298e4b7ad22b6e6de52a4cff785f8bc0ba;hb=5faa454d2d70f44f01c0cd5cab9861768d303225;hp=2569d1dfde078680fec40bedcfaca1cb481b330f;hpb=309caf3973a37bbe07a407ea8230ad4ee888d15b;p=catagits%2FCatalyst-Runtime.git diff --git a/lib/Catalyst/IOC/Container.pm b/lib/Catalyst/IOC/Container.pm index 2569d1d..d3d67e2 100644 --- a/lib/Catalyst/IOC/Container.pm +++ b/lib/Catalyst/IOC/Container.pm @@ -647,7 +647,15 @@ sub add_component { my $component_service_name = "${type}_${name}"; - $self->get_sub_container('component')->add_service( + # The 'component' sub-container will create the object, and store it's + # instance, which, by default, will live throughout the application. + # The model/view/controller sub-containers only reference the instance + # held in the aforementioned sub-container, and execute the ACCEPT_CONTEXT + # sub every time they are called, when it exists. + my $instance_container = $self->get_sub_container('component'); + my $accept_context_container = $self->get_sub_container($type); + + $instance_container->add_service( Catalyst::IOC::ConstructorInjection->new( name => $component_service_name, class => $component, @@ -657,17 +665,20 @@ sub add_component { depends_on( '/config' ), ], ) - ); + ) unless $instance_container->has_service( $component_service_name ); + # ^ custom containers might have added the service already. + # we don't want to override that. - $self->get_sub_container($type)->add_service( + $accept_context_container->add_service( Catalyst::IOC::BlockInjection->new( name => $name, dependencies => [ depends_on( "/component/$component_service_name" ), ], - block => sub { return shift->param($component_service_name) }, + block => sub { shift->param($component_service_name) }, ) - ); + ) unless $accept_context_container->has_service( $name ); + # ^ same as above } # FIXME: should this sub exist? @@ -771,12 +782,14 @@ C<__DATA__> as a config value, for example) The parameter list is split on comma (C<,>). You can override this method to do your own string munging, or you can define your own macros in -Cconfig-E{ 'Plugin::ConfigLoader' }-E{ substitutions }>. +C<< config( 'Plugin::ConfigLoader' => { substitutions => { ... } } ) >>. Example: - MyApp->config->{ 'Plugin::ConfigLoader' }->{ substitutions } = { - baz => sub { my $c = shift; qux( @_ ); } - } + MyApp->config( 'Plugin::ConfigLoader' => { + substitutions => { + baz => sub { my $c = shift; qux( @_ ); }, + }, + }); The above will respond to C<__baz(x,y)__> in config strings.