X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FCatalyst%2FIOC%2FContainer.pm;h=a1ca69a3359b6f3b51a32455e6dc406b3c123273;hb=8697f11669be14108294ead3f14bbf4054fa7210;hp=051f758518a1686b7b153676bb5825aad03d4962;hpb=642eba832e13709cafb0da9b6a1f00216fd45503;p=catagits%2FCatalyst-Runtime.git diff --git a/lib/Catalyst/IOC/Container.pm b/lib/Catalyst/IOC/Container.pm index 051f758..a1ca69a 100644 --- a/lib/Catalyst/IOC/Container.pm +++ b/lib/Catalyst/IOC/Container.pm @@ -472,9 +472,6 @@ sub setup_components { unless $comps{$component}; } } - - $self->get_sub_container('model')->make_single_default; - $self->get_sub_container('view')->make_single_default; } sub _fix_syntax { @@ -603,25 +600,6 @@ sub find_component_regexp { return @result; } -# FIXME - t0m, how do you feel about this name? -# also, do you think I should draw it here, or just return the data structure? -sub get_components_names_types { - my ( $self ) = @_; - my @comps_names_types; - - for my $sub_container_name (qw/model view controller/) { - my $sub_container = $self->get_sub_container($sub_container_name); - for my $service ( $sub_container->get_service_list ) { - my $comp = $sub_container->resolve(service => $service); - my $compname = ref $comp || $comp; - my $type = ref $comp ? 'instance' : 'class'; - push @comps_names_types, [ $compname, $type ]; - } - } - - return @comps_names_types; -} - sub get_all_components { my $self = shift; my %components; @@ -647,7 +625,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 +643,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? @@ -715,17 +704,26 @@ Catalyst::Container - IOC for Catalyst components =head1 Building Containers +=head2 build_component_subcontainer + +Container that stores all components, i.e. all models, views and controllers +together. Each service is an instance of the actual component, and by default +it lives while the application is running. Retrieving components from this +subcontainer will instantiate the component, if it hasn't been instantiated +already, but will not execute ACCEPT_CONTEXT. + =head2 build_model_subcontainer -Container that stores all models. +Container that stores references for all models that are inside the components +subcontainer. Retrieving a model triggers ACCEPT_CONTEXT, if it exists. =head2 build_view_subcontainer -Container that stores all views. +Same as L, but for views. =head2 build_controller_subcontainer -Container that stores all controllers. +Same as L, but for controllers. =head1 Building Services @@ -761,12 +759,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.