ditto
[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;
59aacfa7 5
6extends 'Bread::Board::Container';
7
a2c0d071 8has default_component => (
eea0c6e2 9 isa => 'Str|Undef',
10 is => 'ro',
a2c0d071 11 required => 0,
5be6c1d8 12 writer => '_set_default_component',
a2c0d071 13);
14
59aacfa7 15sub get_component {
77e27f6e 16 my ( $self, $name, @args ) = @_;
4f38bf4b 17
18 return $self->resolve(
19 service => $name,
77e27f6e 20 parameters => { accept_context_args => \@args },
4f38bf4b 21 );
59aacfa7 22}
23
1f90ca59 24sub get_component_regexp {
5a53ef3d 25 my ( $self, $query, $c, @args ) = @_;
1f90ca59 26
1f90ca59 27 my @result = map {
77e27f6e 28 $self->get_component( $_, $c, @args )
ff974a7c 29 } grep { m/$query/ } $self->get_service_list;
1f90ca59 30
1f90ca59 31 return @result;
32}
33
eea0c6e2 34# FIXME - is this sub ok?
35# is the name ok too?
b79f858d 36sub make_single_default {
37 my ( $self ) = @_;
38
39 my @complist = $self->get_service_list;
40
5be6c1d8 41 $self->_set_default_component( shift @complist )
b79f858d 42 if !$self->default_component && scalar @complist == 1;
43}
44
59aacfa7 451;
4f38bf4b 46
47__END__
48
49=pod
50
51=head1 NAME
52
a6c13ff4 53Catalyst::IOC::SubContainer - Container for models, controllers and views
4f38bf4b 54
55=head1 METHODS
56
57=head2 get_component
58
eea0c6e2 59Gets the service of the container for the searched component. Also executes the ACCEPT_CONTEXT sub in the component, if it exists.
60
4f38bf4b 61=head2 get_component_regexp
62
eea0c6e2 63Gets all components from container that match a given regexp.
64
b79f858d 65=head2 make_single_default
66
eea0c6e2 67If the container has only one component, and no default has been defined, this method makes that one existing service the default.
68
4f38bf4b 69=head1 AUTHORS
70
71Catalyst Contributors, see Catalyst.pm
72
73=head1 COPYRIGHT
74
75This library is free software. You can redistribute it and/or modify it under
76the same terms as Perl itself.
77
78=cut