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