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