created disable_regex_fallback attr
[catagits/Catalyst-Runtime.git] / lib / Catalyst / IOC / SubContainer.pm
CommitLineData
a6c13ff4 1package Catalyst::IOC::SubContainer;
59aacfa7 2use Bread::Board;
3use Moose;
a6c13ff4 4use Catalyst::IOC::BlockInjection;
b06ded69 5use Catalyst::Utils;
59aacfa7 6
7extends 'Bread::Board::Container';
8
b06ded69 9has disable_regex_fallback => (
10 is => 'ro',
11 isa => 'Bool',
12 default => 1,
13);
14
59aacfa7 15sub get_component {
16 my ( $self, $name, $args ) = @_;
4f38bf4b 17
18 return $self->resolve(
19 service => $name,
20 parameters => { accept_context_args => $args },
21 );
59aacfa7 22}
23
1f90ca59 24sub get_component_regexp {
25 my ( $self, $c, $name, $args ) = @_;
26
27 return
b06ded69 28 if $self->disable_regex_fallback && !ref $name;
1f90ca59 29
b06ded69 30 my $query = ref $name ? $name : qr{$name}i;
31 my $prefix = Catalyst::Utils::class2classprefix($query) // '';
32 $query =~ s/^${prefix}:://i;
1f90ca59 33
1f90ca59 34 my @result = map {
ff974a7c 35 $self->get_component( $_, $args )
36 } grep { m/$query/ } $self->get_service_list;
1f90ca59 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
59aacfa7 481;
4f38bf4b 49
50__END__
51
52=pod
53
54=head1 NAME
55
a6c13ff4 56Catalyst::IOC::SubContainer - Container for models, controllers and views
4f38bf4b 57
58=head1 METHODS
59
60=head2 get_component
61
62=head2 get_component_regexp
63
64=head1 AUTHORS
65
66Catalyst Contributors, see Catalyst.pm
67
68=head1 COPYRIGHT
69
70This library is free software. You can redistribute it and/or modify it under
71the same terms as Perl itself.
72
73=cut