TODO item done, whitespace fix
[catagits/Catalyst-Runtime.git] / lib / Catalyst / IOC.pm
CommitLineData
2336ae0b 1package Catalyst::IOC;
fb7aaa61 2use strict;
3use warnings;
69e61491 4use Bread::Board qw/depends_on/;
71d3df94 5use Catalyst::IOC::ConstructorInjection;
2336ae0b 6
71d3df94 7use Sub::Exporter -setup => {
8 exports => [qw/
69e61491 9 depends_on
71d3df94 10 component
7cd05fd2 11 model
9226f304 12 view
13 controller
7cd05fd2 14 container
71d3df94 15 /],
16 groups => { default => [qw/
69e61491 17 depends_on
71d3df94 18 component
7cd05fd2 19 model
9226f304 20 view
21 controller
7cd05fd2 22 container
71d3df94 23 /]},
24};
fddc99a9 25
7cd05fd2 26sub container (&) {
27 my $code = shift;
85ebb635 28 my $caller = caller;
f76c40f0 29
30 no strict 'refs';
85ebb635 31 ${"${caller}::customise_container"} = sub {
85ebb635 32 local ${"${caller}::current_container"} = shift;
7cd05fd2 33 $code->();
34 };
35}
36
a07a543a 37sub model (&) { &_subcontainer }
38sub view (&) { &_subcontainer }
39sub controller (&) { &_subcontainer }
4bb29053 40
a07a543a 41sub _subcontainer {
42 my $code = shift;
43
44 my ( $caller, $f, $l, $subcontainer ) = caller(1);
45 $subcontainer =~ s/^Catalyst::IOC:://;
f76c40f0 46
47 no strict 'refs';
4bb29053 48 local ${"${caller}::current_container"} =
49 ${"${caller}::current_container"}->get_sub_container($subcontainer);
7cd05fd2 50 $code->();
51}
71d3df94 52
4bb29053 53sub component ($;%) {
71d3df94 54 my ($name, %args) = @_;
f76c40f0 55 my $current_container;
56
57 {
58 no strict 'refs';
59 my $caller = caller;
60 $current_container = ${"${caller}::current_container"};
61 }
62
71d3df94 63 $args{dependencies} ||= {};
64 $args{dependencies}{application_name} = depends_on( '/application_name' );
65
f76c40f0 66 my $lifecycle = $args{lifecycle} || 'Singleton';
6f009cc6 67 $args{lifecycle} = grep( m/^$lifecycle$/, qw/COMPONENTSingleton Request/ )
f76c40f0 68 ? "+Catalyst::IOC::LifeCycle::$lifecycle"
69 : $lifecycle
129508fb 70 ;
71
71d3df94 72 # FIXME - check $args{type} here!
73
cdd4485f 74 my $component_name = join '::', (
f76c40f0 75 $current_container->resolve(service => '/application_name'),
76 ucfirst($current_container->name),
cdd4485f 77 $name
78 );
79
f76c40f0 80 $current_container->add_service(
81 Catalyst::IOC::ConstructorInjection->new(
82 %args,
83 name => $name,
84 catalyst_component_name => $component_name,
85 )
7cd05fd2 86 );
71d3df94 87}
2336ae0b 88
fb7aaa61 891;
51f62e09 90
91__END__
92
93=pod
94
95=head1 NAME
96
97Catalyst::IOC - IOC for Catalyst, based on Bread::Board
98
99=head1 SYNOPSIS
100
4e4e003e 101 package MyApp::Container;
9226f304 102 use Moose;
4e4e003e 103 use Catalyst::IOC;
9226f304 104 extends 'Catalyst::IOC::Container';
105
106 container {
107 model {
108 # default component
109 component Foo => ();
110
111 # model Bar needs model Foo to be built before
112 # and Bar's constructor gets Foo as a parameter
113 component Bar => ( dependencies => [
114 depends_on('/model/Foo'),
115 ]);
116
117 # Baz is rebuilt once per HTTP request
118 component Baz => ( lifecycle => 'Request' );
119
120 # built only once per application life time
121 component Quux => ( lifecycle => 'Singleton' );
122
123 # built once per app life time and uses an external model,
124 # outside the default directory
125 # no need for wrappers or Catalyst::Model::Adaptor
126 component Fnar => (
127 lifecycle => 'Singleton',
128 class => 'My::External::Class',
129 );
130 };
131 view {
132 component HTML => ();
133 };
134 controller {
135 component Root => ();
136 };
4e4e003e 137 }
138
51f62e09 139=head1 DESCRIPTION
140
9226f304 141Catalyst::IOC provides "sugar" methods to extend the behavior of the default
142Catalyst container.
143
51f62e09 144=head1 METHODS
145
4bb29053 146=head2 container
147
9226f304 148Sets up the root container to be customised.
149
4bb29053 150=head2 model
151
9226f304 152Sets up the model container to be customised.
153
4bb29053 154=head2 view
155
9226f304 156Sets up the view container to be customised.
157
4bb29053 158=head2 controller
159
9226f304 160Sets up the controller container to be customised.
161
4bb29053 162=head2 component
163
9226f304 164Adds a component to the subcontainer. Works like L<Bread::Board::service>.
165
51f62e09 166=head1 AUTHORS
167
168Catalyst Contributors, see Catalyst.pm
169
4e4e003e 170=head1 SEE ALSO
171
172L<Bread::Board>
173
51f62e09 174=head1 COPYRIGHT
175
176This library is free software. You can redistribute it and/or modify it under
177the same terms as Perl itself.
178
179=cut