8306db85514fed9c259c6f2a8a61dfbba2177f0b
[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 use Catalyst::Utils;
6
7 extends 'Bread::Board::Container';
8
9 has disable_regex_fallback => (
10     is      => 'ro',
11     isa     => 'Bool',
12     default => 1,
13 );
14
15 sub get_component {
16     my ( $self, $name, $args ) = @_;
17
18     return $self->resolve(
19         service    => $name,
20         parameters => { accept_context_args => $args },
21     );
22 }
23
24 sub get_component_regexp {
25     my ( $self, $c, $name, $args ) = @_;
26
27     return
28         if $self->disable_regex_fallback && !ref $name;
29
30     my $query  = ref $name ? $name : qr{$name}i;
31     my $prefix = Catalyst::Utils::class2classprefix($query) // '';
32     $query     =~ s/^${prefix}:://i;
33
34     my @result = map {
35         $self->get_component( $_, $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