4870bf097a4f0f9382b43a012a380cb8a7a2a5a6
[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     required => 0,
11     writer   => '_set_default_component',
12 );
13
14 sub get_component {
15     my ( $self, $name, @args ) = @_;
16
17     return $self->resolve(
18         service    => $name,
19         parameters => { accept_context_args => \@args },
20     );
21 }
22
23 sub get_component_regexp {
24     my ( $self, $query, $c, @args ) = @_;
25
26     my @result = map {
27         $self->get_component( $_, $c, @args )
28     } grep { m/$query/ } $self->get_service_list;
29
30     return @result;
31 }
32
33 # FIXME - is this sub ok?
34 # is the name ok too?
35 sub make_single_default {
36     my ( $self ) = @_;
37
38     my @complist = $self->get_service_list;
39
40     $self->_set_default_component( shift @complist )
41         if !$self->default_component && scalar @complist == 1;
42 }
43
44 1;
45
46 __END__
47
48 =pod
49
50 =head1 NAME
51
52 Catalyst::IOC::SubContainer - Container for models, controllers and views
53
54 =head1 SYNOPSIS
55
56 =head1 DESCRIPTION
57
58 =head1 METHODS
59
60 =head2 get_component
61
62 Gets the service of the container for the searched component. Also executes
63 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,
72 this method makes that one existing service the default.
73
74 =head1 AUTHORS
75
76 Catalyst Contributors, see Catalyst.pm
77
78 =head1 COPYRIGHT
79
80 This library is free software. You can redistribute it and/or modify it under
81 the same terms as Perl itself.
82
83 =cut