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 my $config = $self->resolve( service => 'config' );
83 my $default_view = $params->{default_view} || $config->{default_view};
84 my $default_model = $params->{default_model} || $config->{default_model};
86 $self->add_sub_container(
87 $self->build_view_subcontainer(
88 default_component => $default_view,
92 $self->add_sub_container(
93 $self->build_model_subcontainer(
94 default_component => $default_model,
99 sub build_model_subcontainer {
102 return $self->new_sub_container( @_,
107 sub build_view_subcontainer {
110 return $self->new_sub_container( @_,
115 sub build_controller_subcontainer {
118 return $self->new_sub_container(
119 name => 'controller',
123 sub build_name_service {
126 return Bread::Board::Literal->new( name => 'name', value => $self->name );
129 sub build_driver_service {
132 return Bread::Board::Literal->new( name => 'driver', value => $self->driver );
135 sub build_file_service {
138 return Bread::Board::Literal->new( name => 'file', value => $self->file );
141 sub build_substitutions_service {
144 return Bread::Board::Literal->new( name => 'substitutions', value => $self->substitutions );
147 sub build_extensions_service {
150 return Bread::Board::BlockInjection->new(
151 name => 'extensions',
153 return \@{Config::Any->extensions};
158 sub build_prefix_service {
161 return Bread::Board::BlockInjection->new(
164 return Catalyst::Utils::appprefix( shift->param('name') );
166 dependencies => [ depends_on('name') ],
170 sub build_path_service {
173 return Bread::Board::BlockInjection->new(
178 return Catalyst::Utils::env_value( $s->param('name'), 'CONFIG' )
180 || $s->param('name')->path_to( $s->param('prefix') );
182 dependencies => [ depends_on('file'), depends_on('name'), depends_on('prefix') ],
186 sub build_config_service {
189 return Bread::Board::BlockInjection->new(
194 my $v = Data::Visitor::Callback->new(
196 return unless defined $_;
197 return $self->_config_substitutions( $s->param('name'), $s->param('substitutions'), $_ );
201 $v->visit( $s->param('raw_config') );
203 dependencies => [ depends_on('name'), depends_on('raw_config'), depends_on('substitutions') ],
207 sub build_raw_config_service {
210 return Bread::Board::BlockInjection->new(
211 name => 'raw_config',
215 my @global = @{$s->param('global_config')};
216 my @locals = @{$s->param('local_config')};
219 for my $cfg (@global, @locals) {
221 $config = Catalyst::Utils::merge_hashes( $config, $cfg->{$_} );
226 dependencies => [ depends_on('global_config'), depends_on('local_config') ],
230 sub build_global_files_service {
233 return Bread::Board::BlockInjection->new(
234 name => 'global_files',
238 my ( $path, $extension ) = @{$s->param('config_path')};
240 my @extensions = @{$s->param('extensions')};
244 die "Unable to handle files with the extension '${extension}'" unless grep { $_ eq $extension } @extensions;
247 @files = map { "$path.$_" } @extensions;
251 dependencies => [ depends_on('extensions'), depends_on('config_path') ],
255 sub build_local_files_service {
258 return Bread::Board::BlockInjection->new(
259 name => 'local_files',
263 my ( $path, $extension ) = @{$s->param('config_path')};
264 my $suffix = $s->param('config_local_suffix');
266 my @extensions = @{$s->param('extensions')};
270 die "Unable to handle files with the extension '${extension}'" unless grep { $_ eq $extension } @extensions;
271 $path =~ s{\.$extension}{_$suffix.$extension};
274 @files = map { "${path}_${suffix}.$_" } @extensions;
278 dependencies => [ depends_on('extensions'), depends_on('config_path'), depends_on('config_local_suffix') ],
282 sub build_global_config_service {
285 return Bread::Board::BlockInjection->new(
286 name => 'global_config',
290 return Config::Any->load_files({
291 files => $s->param('global_files'),
292 filter => \&_fix_syntax,
294 driver_args => $s->param('driver'),
297 dependencies => [ depends_on('global_files') ],
301 sub build_local_config_service {
304 return Bread::Board::BlockInjection->new(
305 name => 'local_config',
309 return Config::Any->load_files({
310 files => $s->param('local_files'),
311 filter => \&_fix_syntax,
313 driver_args => $s->param('driver'),
316 dependencies => [ depends_on('local_files') ],
320 sub build_config_path_service {
323 return Bread::Board::BlockInjection->new(
324 name => 'config_path',
328 my $path = $s->param('path');
329 my $prefix = $s->param('prefix');
331 my ( $extension ) = ( $path =~ m{\.(.{1,4})$} );
334 $path =~ s{[\/\\]$}{};
338 return [ $path, $extension ];
340 dependencies => [ depends_on('prefix'), depends_on('path') ],
344 sub build_config_local_suffix_service {
347 return Bread::Board::BlockInjection->new(
348 name => 'config_local_suffix',
351 my $suffix = Catalyst::Utils::env_value( $s->param('name'), 'CONFIG_LOCAL_SUFFIX' ) || $self->config_local_suffix;
355 dependencies => [ depends_on('name') ],
363 prefix => $_ eq 'Component' ? '' : $_ . '::',
364 values => delete $config->{ lc $_ } || delete $config->{ $_ }
366 grep { ref $config->{ lc $_ } || ref $config->{ $_ } }
367 qw( Component Model M View V Controller C Plugin )
370 foreach my $comp ( @components ) {
371 my $prefix = $comp->{ prefix };
372 foreach my $element ( keys %{ $comp->{ values } } ) {
373 $config->{ "$prefix$element" } = $comp->{ values }->{ $element };
378 sub _config_substitutions {
379 my ( $self, $name, $subs, $arg ) = @_;
381 $subs->{ HOME } ||= sub { shift->path_to( '' ); };
385 if (! defined($ENV{$v})) {
386 Catalyst::Exception->throw( message =>
387 "Missing environment variable: $v" );
393 $subs->{ path_to } ||= sub { shift->path_to( @_ ); };
394 $subs->{ literal } ||= sub { return $_[ 1 ]; };
395 my $subsre = join( '|', keys %$subs );
397 $arg =~ s{__($subsre)(?:\((.+?)\))?__}{ $subs->{ $1 }->( $name, $2 ? split( /,/, $2 ) : () ) }eg;
401 sub get_component_from_sub_container {
402 my ( $self, $sub_container_name, $name, $c, @args ) = @_;
404 my $sub_container = $self->get_sub_container( $sub_container_name );
407 my $default = $sub_container->default_component;
409 return $sub_container->get_component( $default, $c, @args )
410 if $default && $sub_container->has_service( $default );
412 # this is never a controller, so this is safe
413 $c->log->warn( "Calling \$c->$sub_container_name() is not supported unless you specify one of:" );
414 $c->log->warn( "* \$c->config(default_$sub_container_name => 'the name of the default $sub_container_name to use')" );
415 $c->log->warn( "* \$c->stash->{current_$sub_container_name} # the name of the view to use for this request" );
416 $c->log->warn( "* \$c->stash->{current_${sub_container_name}_instance} # the instance of the $sub_container_name to use for this request" );
421 return $sub_container->get_component_regexp( $name, $c, @args )
424 return $sub_container->get_component( $name, $c, @args )
425 if $sub_container->has_service( $name );
428 "Attempted to use $sub_container_name '$name', " .
429 "but it does not exist"
436 my ( $self, $component, $c, @args ) = @_;
437 my ( $type, $name ) = _get_component_type_name($component);
440 return $self->get_component_from_sub_container(
441 $type, $name, $c, @args
444 my $query = ref $component
449 for my $subcontainer_name (qw/model view controller/) {
450 my $subcontainer = $self->get_sub_container($subcontainer_name);
451 my @components = $subcontainer->get_service_list;
452 @result = grep { m{$component} } @components;
454 return map { $subcontainer->get_component( $_, $c, @args ) } @result
458 # one last search for things like $c->comp(qr/::M::/)
459 @result = $self->find_component_regexp(
460 $c->components, $component, $c, @args
461 ) if !@result and ref $component;
463 # it expects an empty list on failed searches
467 sub find_component_regexp {
468 my ( $self, $components, $component, @args ) = @_;
471 my @components = grep { m{$component} } keys %{ $components };
474 my ($type, $name) = _get_component_type_name($_);
476 push @result, $self->get_component_from_sub_container(
484 # FIXME sorry for the name again :)
485 sub get_components_types {
489 for my $sub_container_name (qw/model view controller/) {
490 my $sub_container = $self->get_sub_container($sub_container_name);
491 for my $service ( $sub_container->get_service_list ) {
492 my $comp = $self->resolve(service => $service);
493 my $compname = ref $comp || $comp;
494 my $type = ref $comp ? 'instance' : 'class';
495 push @comps_types, [ $compname, $type ];
502 sub get_all_components {
507 map { $_ => $self->get_sub_container($_) } qw(model view controller)
510 for my $container (keys %$containers) {
511 for my $component ($containers->{$container}->get_service_list) {
512 my $comp = $containers->{$container}->resolve(
513 service => $component
515 my $comp_name = ref $comp || $comp;
516 $components{$comp_name} = $comp;
520 return lock_hash %components;
524 my ( $self, $component, $class ) = @_;
525 my ( $type, $name ) = _get_component_type_name($component);
529 my $instance = setup_component( $component, $class );
531 $self->get_sub_container($type)->add_service(
532 Catalyst::IOC::BlockInjection->new(
534 block => sub { $instance },
541 # FIXME: should this sub exist?
542 # should it be moved to Catalyst::Utils,
543 # or replaced by something already existing there?
544 sub _get_component_type_name {
545 my ( $component ) = @_;
547 my @parts = split /::/, $component;
549 while (my $type = shift @parts) {
550 return ('controller', join '::', @parts)
551 if $type =~ /^(c|controller)$/i;
553 return ('model', join '::', @parts)
554 if $type =~ /^(m|model)$/i;
556 return ('view', join '::', @parts)
557 if $type =~ /^(v|view)$/i;
560 return (undef, $component);
563 # FIXME ugly and temporary
564 # Just moved it here the way it was, so we can work on it here in the container
565 sub setup_component {
566 my ( $component, $class ) = @_;
568 unless ( $component->can( 'COMPONENT' ) ) {
572 # FIXME I know this isn't the "Dependency Injection" way of doing things,
574 my $suffix = Catalyst::Utils::class2classsuffix( $component );
575 my $config = $class->config->{ $suffix } || {};
577 # Stash catalyst_component_name in the config here, so that custom COMPONENT
578 # methods also pass it. local to avoid pointlessly shitting in config
579 # for the debug screen, as $component is already the key name.
580 local $config->{catalyst_component_name} = $component;
582 my $instance = eval { $component->COMPONENT( $class, $config ); };
584 if ( my $error = $@ ) {
586 Catalyst::Exception->throw(
587 message => qq/Couldn't instantiate component "$component", "$error"/
590 elsif (!blessed $instance) {
591 my $metaclass = Moose::Util::find_meta($component);
592 my $method_meta = $metaclass->find_method_by_name('COMPONENT');
593 my $component_method_from = $method_meta->associated_metaclass->name;
594 my $value = defined($instance) ? $instance : 'undef';
595 Catalyst::Exception->throw(
597 qq/Couldn't instantiate component "$component", COMPONENT() method (from $component_method_from) didn't return an object-like value (value was $value)./
613 Catalyst::Container - IOC for Catalyst components
617 =head2 build_model_subcontainer
619 =head2 build_view_subcontainer
621 =head2 build_controller_subcontainer
623 =head2 build_name_service
625 =head2 build_driver_service
627 =head2 build_file_service
629 =head2 build_substitutions_service
631 =head2 build_extensions_service
633 =head2 build_prefix_service
635 =head2 build_path_service
637 =head2 build_config_service
639 =head2 build_raw_config_service
641 =head2 build_global_files_service
643 =head2 build_local_files_service
645 =head2 build_global_config_service
647 =head2 build_local_config_service
649 =head2 build_config_path_service
651 =head2 build_config_local_suffix_service
653 =head2 get_component_from_sub_container
655 =head2 get_components_types
657 =head2 get_all_components
661 =head2 find_component
663 =head2 find_component_regexp
665 =head2 setup_component
669 =head2 _config_substitutions
673 Catalyst Contributors, see Catalyst.pm
677 This library is free software. You can redistribute it and/or modify it under
678 the same terms as Perl itself.