1 package Catalyst::IOC::Container;
5 use Data::Visitor::Callback;
6 use Catalyst::Utils ();
7 use Hash::Util qw/lock_hash/;
8 use MooseX::Types::LoadableClass qw/ LoadableClass /;
10 use Catalyst::IOC::BlockInjection;
11 use namespace::autoclean;
13 extends 'Bread::Board::Container';
15 has config_local_suffix => (
24 default => sub { +{} },
33 has substitutions => (
36 default => sub { +{} },
45 has sub_container_class => (
49 default => 'Catalyst::IOC::SubContainer',
51 new_sub_container => 'new',
56 my ( $self, $params ) = @_;
59 $self->${\"build_${_}_service"}
78 $self->add_sub_container(
79 $self->build_controller_subcontainer
82 # FIXME - the config should be merged at this point
83 my $config = $self->resolve( service => 'config' );
84 my $default_view = $params->{default_view} || $config->{default_view};
85 my $default_model = $params->{default_model} || $config->{default_model};
87 $self->add_sub_container(
88 $self->build_view_subcontainer(
89 default_component => $default_view,
93 $self->add_sub_container(
94 $self->build_model_subcontainer(
95 default_component => $default_model,
100 sub build_model_subcontainer {
103 return $self->new_sub_container( @_,
108 sub build_view_subcontainer {
111 return $self->new_sub_container( @_,
116 sub build_controller_subcontainer {
119 return $self->new_sub_container(
120 name => 'controller',
124 sub build_name_service {
127 return Bread::Board::Literal->new( name => 'name', value => $self->name );
130 sub build_driver_service {
133 return Bread::Board::Literal->new( name => 'driver', value => $self->driver );
136 sub build_file_service {
139 return Bread::Board::Literal->new( name => 'file', value => $self->file );
142 sub build_substitutions_service {
145 return Bread::Board::Literal->new( name => 'substitutions', value => $self->substitutions );
148 sub build_extensions_service {
151 return Bread::Board::BlockInjection->new(
152 name => 'extensions',
154 return \@{Config::Any->extensions};
159 sub build_prefix_service {
162 return Bread::Board::BlockInjection->new(
165 return Catalyst::Utils::appprefix( shift->param('name') );
167 dependencies => [ depends_on('name') ],
171 sub build_path_service {
174 return Bread::Board::BlockInjection->new(
179 return Catalyst::Utils::env_value( $s->param('name'), 'CONFIG' )
181 || $s->param('name')->path_to( $s->param('prefix') );
183 dependencies => [ depends_on('file'), depends_on('name'), depends_on('prefix') ],
187 sub build_config_service {
190 return Bread::Board::BlockInjection->new(
195 my $v = Data::Visitor::Callback->new(
197 return unless defined $_;
198 return $self->_config_substitutions( $s->param('name'), $s->param('substitutions'), $_ );
202 $v->visit( $s->param('raw_config') );
204 dependencies => [ depends_on('name'), depends_on('raw_config'), depends_on('substitutions') ],
208 sub build_raw_config_service {
211 return Bread::Board::BlockInjection->new(
212 name => 'raw_config',
216 my @global = @{$s->param('global_config')};
217 my @locals = @{$s->param('local_config')};
220 for my $cfg (@global, @locals) {
222 $config = Catalyst::Utils::merge_hashes( $config, $cfg->{$_} );
227 dependencies => [ depends_on('global_config'), depends_on('local_config') ],
231 sub build_global_files_service {
234 return Bread::Board::BlockInjection->new(
235 name => 'global_files',
239 my ( $path, $extension ) = @{$s->param('config_path')};
241 my @extensions = @{$s->param('extensions')};
245 die "Unable to handle files with the extension '${extension}'" unless grep { $_ eq $extension } @extensions;
248 @files = map { "$path.$_" } @extensions;
252 dependencies => [ depends_on('extensions'), depends_on('config_path') ],
256 sub build_local_files_service {
259 return Bread::Board::BlockInjection->new(
260 name => 'local_files',
264 my ( $path, $extension ) = @{$s->param('config_path')};
265 my $suffix = $s->param('config_local_suffix');
267 my @extensions = @{$s->param('extensions')};
271 die "Unable to handle files with the extension '${extension}'" unless grep { $_ eq $extension } @extensions;
272 $path =~ s{\.$extension}{_$suffix.$extension};
275 @files = map { "${path}_${suffix}.$_" } @extensions;
279 dependencies => [ depends_on('extensions'), depends_on('config_path'), depends_on('config_local_suffix') ],
283 sub build_global_config_service {
286 return Bread::Board::BlockInjection->new(
287 name => 'global_config',
291 return Config::Any->load_files({
292 files => $s->param('global_files'),
293 filter => \&_fix_syntax,
295 driver_args => $s->param('driver'),
298 dependencies => [ depends_on('global_files') ],
302 sub build_local_config_service {
305 return Bread::Board::BlockInjection->new(
306 name => 'local_config',
310 return Config::Any->load_files({
311 files => $s->param('local_files'),
312 filter => \&_fix_syntax,
314 driver_args => $s->param('driver'),
317 dependencies => [ depends_on('local_files') ],
321 sub build_config_path_service {
324 return Bread::Board::BlockInjection->new(
325 name => 'config_path',
329 my $path = $s->param('path');
330 my $prefix = $s->param('prefix');
332 my ( $extension ) = ( $path =~ m{\.(.{1,4})$} );
335 $path =~ s{[\/\\]$}{};
339 return [ $path, $extension ];
341 dependencies => [ depends_on('prefix'), depends_on('path') ],
345 sub build_config_local_suffix_service {
348 return Bread::Board::BlockInjection->new(
349 name => 'config_local_suffix',
352 my $suffix = Catalyst::Utils::env_value( $s->param('name'), 'CONFIG_LOCAL_SUFFIX' ) || $self->config_local_suffix;
356 dependencies => [ depends_on('name') ],
364 prefix => $_ eq 'Component' ? '' : $_ . '::',
365 values => delete $config->{ lc $_ } || delete $config->{ $_ }
367 grep { ref $config->{ lc $_ } || ref $config->{ $_ } }
368 qw( Component Model M View V Controller C Plugin )
371 foreach my $comp ( @components ) {
372 my $prefix = $comp->{ prefix };
373 foreach my $element ( keys %{ $comp->{ values } } ) {
374 $config->{ "$prefix$element" } = $comp->{ values }->{ $element };
379 sub _config_substitutions {
380 my ( $self, $name, $subs, $arg ) = @_;
382 $subs->{ HOME } ||= sub { shift->path_to( '' ); };
386 if (! defined($ENV{$v})) {
387 Catalyst::Exception->throw( message =>
388 "Missing environment variable: $v" );
394 $subs->{ path_to } ||= sub { shift->path_to( @_ ); };
395 $subs->{ literal } ||= sub { return $_[ 1 ]; };
396 my $subsre = join( '|', keys %$subs );
398 $arg =~ s{__($subsre)(?:\((.+?)\))?__}{ $subs->{ $1 }->( $name, $2 ? split( /,/, $2 ) : () ) }eg;
402 sub get_component_from_sub_container {
403 my ( $self, $sub_container_name, $name, $c, @args ) = @_;
405 my $sub_container = $self->get_sub_container( $sub_container_name );
408 my $default = $sub_container->default_component;
410 return $sub_container->get_component( $default, $c, @args )
411 if $default && $sub_container->has_service( $default );
413 # FIXME - should I be calling $c->log->warn here?
414 # this is never a controller, so this is safe
415 $c->log->warn( "Calling \$c->$sub_container_name() is not supported unless you specify one of:" );
416 $c->log->warn( "* \$c->config(default_$sub_container_name => 'the name of the default $sub_container_name to use')" );
417 $c->log->warn( "* \$c->stash->{current_$sub_container_name} # the name of the view to use for this request" );
418 $c->log->warn( "* \$c->stash->{current_${sub_container_name}_instance} # the instance of the $sub_container_name to use for this request" );
423 return $sub_container->get_component_regexp( $name, $c, @args )
426 return $sub_container->get_component( $name, $c, @args )
427 if $sub_container->has_service( $name );
430 "Attempted to use $sub_container_name '$name', " .
431 "but it does not exist"
438 my ( $self, $component, $c, @args ) = @_;
439 my ( $type, $name ) = _get_component_type_name($component);
442 return $self->get_component_from_sub_container(
443 $type, $name, $c, @args
446 my $query = ref $component
451 for my $subcontainer_name (qw/model view controller/) {
452 my $subcontainer = $self->get_sub_container( $subcontainer_name );
453 my @components = $subcontainer->get_service_list;
454 @result = grep { m{$component} } @components;
456 return map { $subcontainer->get_component( $_, $c, @args ) } @result
460 # FIXME - I guess I shouldn't be calling $c->components here
461 # one last search for things like $c->comp(qr/::M::/)
462 @result = $self->find_component_regexp(
463 $c->components, $component, $c, @args
464 ) if !@result and ref $component;
466 # it expects an empty list on failed searches
470 sub find_component_regexp {
471 my ( $self, $components, $component, @args ) = @_;
474 my @components = grep { m{$component} } keys %{ $components };
477 my ($type, $name) = _get_component_type_name($_);
479 push @result, $self->get_component_from_sub_container(
487 # FIXME sorry for the name again :)
488 sub get_components_types {
492 for my $sub_container_name (qw/model view controller/) {
493 my $sub_container = $self->get_sub_container($sub_container_name);
494 for my $service ( $sub_container->get_service_list ) {
495 my $comp = $self->resolve(service => $service);
496 my $compname = ref $comp || $comp;
497 my $type = ref $comp ? 'instance' : 'class';
498 push @comps_types, [ $compname, $type ];
505 sub get_all_components {
510 map { $_ => $self->get_sub_container($_) } qw(model view controller)
513 for my $container (keys %$containers) {
514 for my $component ($containers->{$container}->get_service_list) {
515 my $comp = $containers->{$container}->resolve(
516 service => $component
518 my $comp_name = ref $comp || $comp;
519 $components{$comp_name} = $comp;
523 return lock_hash %components;
527 my ( $self, $component, $class ) = @_;
528 my ( $type, $name ) = _get_component_type_name($component);
532 $self->get_sub_container($type)->add_service(
533 Catalyst::IOC::BlockInjection->new(
535 block => sub { $self->setup_component( $component, $class ) },
540 # FIXME: should this sub exist?
541 # should it be moved to Catalyst::Utils,
542 # or replaced by something already existing there?
543 sub _get_component_type_name {
544 my ( $component ) = @_;
546 my @parts = split /::/, $component;
548 while (my $type = shift @parts) {
549 return ('controller', join '::', @parts)
550 if $type =~ /^(c|controller)$/i;
552 return ('model', join '::', @parts)
553 if $type =~ /^(m|model)$/i;
555 return ('view', join '::', @parts)
556 if $type =~ /^(v|view)$/i;
559 return (undef, $component);
562 # FIXME ugly and temporary
563 # Just moved it here the way it was, so we can work on it here in the container
564 sub setup_component {
565 my ( $self, $component, $class ) = @_;
567 unless ( $component->can( 'COMPONENT' ) ) {
571 # FIXME I know this isn't the "Dependency Injection" way of doing things,
573 my $suffix = Catalyst::Utils::class2classsuffix( $component );
574 my $config = $self->resolve(service => 'config')->{ $suffix } || {};
576 # Stash catalyst_component_name in the config here, so that custom COMPONENT
577 # methods also pass it. local to avoid pointlessly shitting in config
578 # for the debug screen, as $component is already the key name.
579 local $config->{catalyst_component_name} = $component;
581 my $instance = eval { $component->COMPONENT( $class, $config ); };
583 if ( my $error = $@ ) {
585 Catalyst::Exception->throw(
586 message => qq/Couldn't instantiate component "$component", "$error"/
589 elsif (!blessed $instance) {
590 my $metaclass = Moose::Util::find_meta($component);
591 my $method_meta = $metaclass->find_method_by_name('COMPONENT');
592 my $component_method_from = $method_meta->associated_metaclass->name;
593 my $value = defined($instance) ? $instance : 'undef';
594 Catalyst::Exception->throw(
596 qq/Couldn't instantiate component "$component", COMPONENT() method (from $component_method_from) didn't return an object-like value (value was $value)./
612 Catalyst::Container - IOC for Catalyst components
622 =head2 build_model_subcontainer
624 Container that stores all models.
626 =head2 build_view_subcontainer
628 Container that stores all views.
630 =head2 build_controller_subcontainer
632 Container that stores all controllers.
636 =head2 build_name_service
638 Name of the application.
640 =head2 build_driver_service
642 Config options passed directly to the driver being used.
644 =head2 build_file_service
648 =head2 build_substitutions_service
650 Executes all the substitutions in config. See L</_config_substitutions> method.
652 =head2 build_extensions_service
654 =head2 build_prefix_service
656 =head2 build_path_service
658 =head2 build_config_service
660 =head2 build_raw_config_service
662 =head2 build_global_files_service
664 =head2 build_local_files_service
666 =head2 build_global_config_service
668 =head2 build_local_config_service
670 =head2 build_config_path_service
672 =head2 build_config_local_suffix_service
674 Determines the suffix of files used to override the main config. By default
675 this value is C<local>, which will load C<myapp_local.conf>. The suffix can
676 be specified in the following order of preference:
680 =item * C<$ENV{ MYAPP_CONFIG_LOCAL_SUFFIX }>
682 =item * C<$ENV{ CATALYST_CONFIG_LOCAL_SUFFIX }>
686 The first one of these values found replaces the default of C<local> in the
687 name of the local config file to be loaded.
689 For example, if C< $ENV{ MYAPP_CONFIG_LOCAL_SUFFIX }> is set to C<testing>,
690 ConfigLoader will try and load C<myapp_testing.conf> instead of
693 =head2 get_component_from_sub_container($sub_container, $name, $c, @args)
695 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.
697 =head2 get_components_types
699 =head2 get_all_components
701 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.
705 Adds a component to the appropriate subcontainer. The subcontainer is guessed by the component name given.
707 =head2 find_component
709 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.
711 =head2 find_component_regexp
713 Finds components that match a given regexp. Used internally, by find_component.
715 =head2 setup_component
719 =head2 _config_substitutions
721 This method substitutes macros found with calls to a function. There are a
722 number of default macros:
726 =item * C<__HOME__> - replaced with C<$c-E<gt>path_to('')>
728 =item * C<__ENV(foo)__> - replaced with the value of C<$ENV{foo}>
730 =item * C<__path_to(foo/bar)__> - replaced with C<$c-E<gt>path_to('foo/bar')>
732 =item * C<__literal(__FOO__)__> - leaves __FOO__ alone (allows you to use
733 C<__DATA__> as a config value, for example)
737 The parameter list is split on comma (C<,>). You can override this method to
738 do your own string munging, or you can define your own macros in
739 C<MyApp-E<gt>config-E<gt>{ 'Plugin::ConfigLoader' }-E<gt>{ substitutions }>.
742 MyApp->config->{ 'Plugin::ConfigLoader' }->{ substitutions } = {
743 baz => sub { my $c = shift; qux( @_ ); }
746 The above will respond to C<__baz(x,y)__> in config strings.
750 Catalyst Contributors, see Catalyst.pm
754 This library is free software. You can redistribute it and/or modify it under
755 the same terms as Perl itself.