moved setup_home to the container
[catagits/Catalyst-Runtime.git] / lib / Catalyst / IOC / SubContainer.pm
1 package Catalyst::IOC::SubContainer;
2 use Bread::Board;
3 use Moose;
4
5 extends 'Bread::Board::Container';
6
7 has default_component => (
8     isa      => 'Str|Undef',
9     is       => 'ro',
10     builder  => '_build_default_component',
11     lazy     => 1,
12 );
13
14 sub _build_default_component {
15     my ( $self ) = @_;
16
17     my @complist = $self->get_service_list;
18
19     scalar @complist == 1 ? $complist[0] : undef;
20 }
21
22
23 sub get_component {
24     my ( $self, $name, $ctx, @args ) = @_;
25
26     return $self->resolve(
27         service    => $name,
28         parameters => {
29             accept_context_args => [ $ctx, @args ],
30             ctx => $ctx,
31         },
32     );
33 }
34
35 sub get_component_regexp {
36     my ( $self, $query, $c, @args ) = @_;
37
38     my @result = map {
39         $self->get_component( $_, $c, @args )
40     } grep { m/$query/ } $self->get_service_list;
41
42     return @result;
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
64 the ACCEPT_CONTEXT sub in the component, if it exists.
65
66 =head2 get_component_regexp
67
68 Gets all components from container that match a given regexp.
69
70 =head2 make_single_default
71
72 If the container has only one component, and no default has been defined,
73 this method makes that one existing service the default.
74
75 =head1 AUTHORS
76
77 Catalyst Contributors, see Catalyst.pm
78
79 =head1 COPYRIGHT
80
81 This library is free software. You can redistribute it and/or modify it under
82 the same terms as Perl itself.
83
84 =cut