X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FCatalyst%2FIOC%2FContainer.pm;h=742a5eb8e89e8f8cee7354ccfbb4982cc22a500e;hb=a43734f6dc70998f00f2b4fc0d2dbe6fb6a0f69d;hp=d5a259fae19044c03ea09523e4fbe1ce8c9ad724;hpb=9f4997cf7f459ed5f6c6d80e1bc473001b2f5351;p=catagits%2FCatalyst-Runtime.git diff --git a/lib/Catalyst/IOC/Container.pm b/lib/Catalyst/IOC/Container.pm index d5a259f..742a5eb 100644 --- a/lib/Catalyst/IOC/Container.pm +++ b/lib/Catalyst/IOC/Container.pm @@ -39,9 +39,9 @@ has substitutions => ( ); has application_name => ( - is => 'ro', - isa => 'Str', - default => 'MyApp', + is => 'ro', + isa => 'Str', + required => 1, ); has sub_container_class => ( @@ -73,29 +73,27 @@ sub BUILD { local_files global_config local_config + class_config config_local_suffix config_path locate_components /; + my $config = $self->resolve( service => 'config' ); + $self->add_sub_container( $self->build_controller_subcontainer ); - # FIXME - the config should be merged at this point - my $config = $self->resolve( service => 'config' ); - my $default_view = $params->{default_view} || $config->{default_view}; - my $default_model = $params->{default_model} || $config->{default_model}; - $self->add_sub_container( $self->build_view_subcontainer( - default_component => $default_view, + default_component => $config->{default_view}, ) ); $self->add_sub_container( $self->build_model_subcontainer( - default_component => $default_model, + default_component => $config->{default_model}, ) ); } @@ -224,15 +222,17 @@ sub build_raw_config_service { my @global = @{$s->param('global_config')}; my @locals = @{$s->param('local_config')}; - my $config = {}; + my $config = $s->param('class_config'); + for my $cfg (@global, @locals) { for (keys %$cfg) { $config = Catalyst::Utils::merge_hashes( $config, $cfg->{$_} ); } } + return $config; }, - dependencies => [ depends_on('global_config'), depends_on('local_config') ], + dependencies => [ depends_on('global_config'), depends_on('local_config'), depends_on('class_config') ], ); } @@ -290,6 +290,26 @@ sub build_local_files_service { ); } +sub build_class_config_service { + my $self = shift; + + return Bread::Board::BlockInjection->new( + lifecycle => 'Singleton', + name => 'class_config', + block => sub { + my $s = shift; + my $app = $s->param('application_name'); + + # Container might be called outside Catalyst context + return {} unless Class::MOP::is_class_loaded($app); + + # config might not have been defined + return $app->config || {}; + }, + dependencies => [ depends_on('application_name') ], + ); +} + sub build_global_config_service { my $self = shift; @@ -580,7 +600,7 @@ sub get_components_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 = $self->resolve(service => $service); + my $comp = $sub_container->resolve(service => $service); my $compname = ref $comp || $comp; my $type = ref $comp ? 'instance' : 'class'; push @comps_names_types, [ $compname, $type ]; @@ -779,6 +799,11 @@ Reads config from global_files. Reads config from local_files. +=head2 build_class_config_service + +Reads config set from the application's class attribute config, +i.e. MyApp->config( name => 'MyApp', ... ) + =head2 build_config_path_service Splits the path to the config file, and returns on array ref containing @@ -818,23 +843,36 @@ to L. =head2 get_component_from_sub_container($sub_container, $name, $c, @args) -Looks for components in a given subcontainer (such as controller, model or view), and returns the searched component. If $name is undef, it returns the default component (such as default_view, if $sub_container is 'view'). If $name is a regexp, it returns an array of matching components. Otherwise, it looks for the component with name $name. +Looks for components in a given subcontainer (such as controller, model or +view), and returns the searched component. If $name is undef, it returns the +default component (such as default_view, if $sub_container is 'view'). If +$name is a regexp, it returns an array of matching components. Otherwise, it +looks for the component with name $name. =head2 get_components_names_types -Gets all components from all containers and returns them as an array of arrayrefs containing the component name and the component type (i.e., whether it's an instance or a class). +Gets all components from all containers and returns them as an array of +arrayrefs containing the component name and the component type (i.e., whether +it's an instance or a class). =head2 get_all_components -Fetches all the components, in each of the sub_containers model, view and controller, and returns a readonly hash. The keys are the class names, and the values are the blessed objects. This is what is returned by $c->components. +Fetches all the components, in each of the sub_containers model, view and +controller, and returns a readonly hash. The keys are the class names, and +the values are the blessed objects. This is what is returned by $c->components. =head2 add_component -Adds a component to the appropriate subcontainer. The subcontainer is guessed by the component name given. +Adds a component to the appropriate subcontainer. The subcontainer is guessed +by the component name given. =head2 find_component -Searches for components in all containers. If $component is the full class name, the subcontainer is guessed, and it gets the searched component in there. Otherwise, it looks for a component with that name in all subcontainers. If $component is a regexp, it calls the method below, find_component_regexp, and matches all components against that regexp. +Searches for components in all containers. If $component is the full class +name, the subcontainer is guessed, and it gets the searched component in there. +Otherwise, it looks for a component with that name in all subcontainers. If +$component is a regexp, it calls the method below, find_component_regexp, +and matches all components against that regexp. =head2 find_component_regexp