changing get_component and get_component_args
[catagits/Catalyst-Runtime.git] / lib / Catalyst / IOC / SubContainer.pm
1 package Catalyst::IOC::SubContainer;
2 use Bread::Board;
3 use Moose;
4 use Catalyst::IOC::BlockInjection;
5
6 extends 'Bread::Board::Container';
7
8 has disable_regex_fallback => (
9     is      => 'ro',
10     isa     => 'Bool',
11     default => 1,
12 );
13
14 sub get_component {
15     my ( $self, $name, @args ) = @_;
16
17     return $self->resolve(
18         service    => $name,
19         parameters => { accept_context_args => \@args },
20     );
21 }
22
23 sub get_component_regexp {
24     my ( $self, $name, $c, @args ) = @_;
25
26     return
27         if $self->disable_regex_fallback && !ref $name;
28
29     my $query   = ref $name ? $name : qr{$name}i;
30     my $appname = $self->parent->name;
31     $query      =~ s/^${appname}:://i;
32     $query      =~ s/[MVC]|(Model|View|Controller):://i;
33
34     my @result = map {
35         $self->get_component( $_, $c, @args )
36     } grep { m/$query/ } $self->get_service_list;
37
38     if (!ref $name && $result[0]) {
39         $c->log->warn( Carp::shortmess(qq(Found results for "${name}" using regexp fallback)) );
40         $c->log->warn( 'Relying on the regexp fallback behavior for component resolution' );
41         $c->log->warn( 'is unreliable and unsafe. You have been warned' );
42         return $result[0];
43     }
44
45     return @result;
46 }
47
48 1;
49
50 __END__
51
52 =pod
53
54 =head1 NAME
55
56 Catalyst::IOC::SubContainer - Container for models, controllers and views
57
58 =head1 METHODS
59
60 =head2 get_component
61
62 =head2 get_component_regexp
63
64 =head1 AUTHORS
65
66 Catalyst Contributors, see Catalyst.pm
67
68 =head1 COPYRIGHT
69
70 This library is free software. You can redistribute it and/or modify it under
71 the same terms as Perl itself.
72
73 =cut