simplified a bit
[catagits/Catalyst-Runtime.git] / lib / Catalyst / SubContainer.pm
1 package Catalyst::SubContainer;
2 use Bread::Board;
3 use Moose;
4 use Catalyst::BlockInjection;
5
6 extends 'Bread::Board::Container';
7
8 sub get_component {
9     my ( $self, $name, $args ) = @_;
10     return $self->resolve( service => $name, parameters => { context => $args } );
11 }
12
13 sub get_component_regexp {
14     my ( $self, $c, $name, $args ) = @_;
15
16     return
17         if $c->config->{disable_component_resolution_regex_fallback} && !ref $name;
18
19     my $appclass = ref $c || $c;
20     my $prefix   = ucfirst $self->name;
21     my $p        = substr $prefix, 0, 1;
22
23     my $query = ref $name ? $name : qr{$name}i;
24     $query =~ s/^${appclass}::($p|$prefix):://i;
25
26     my @result = map {
27         $self->get_component( $_, $args )
28     } grep { m/$query/ } $self->get_service_list;
29
30     if (!ref $name && $result[0]) {
31         $c->log->warn( Carp::shortmess(qq(Found results for "${name}" using regexp fallback)) );
32         $c->log->warn( 'Relying on the regexp fallback behavior for component resolution' );
33         $c->log->warn( 'is unreliable and unsafe. You have been warned' );
34         return $result[0];
35     }
36
37     return @result;
38 }
39
40 1;