Commit | Line | Data |
2336ae0b |
1 | package Catalyst::IOC; |
fb7aaa61 |
2 | use strict; |
3 | use warnings; |
69e61491 |
4 | use Bread::Board qw/depends_on/; |
71d3df94 |
5 | use Catalyst::IOC::ConstructorInjection; |
2336ae0b |
6 | |
71d3df94 |
7 | use 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 |
26 | sub 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 |
37 | sub model (&) { &_subcontainer } |
38 | sub view (&) { &_subcontainer } |
39 | sub controller (&) { &_subcontainer } |
4bb29053 |
40 | |
a07a543a |
41 | sub _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 |
53 | sub 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} ||= {}; |
96c9f5d4 |
64 | $args{dependencies}{catalyst_application} = depends_on( '/catalyst_application' ); |
71d3df94 |
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 '::', ( |
96c9f5d4 |
75 | $current_container->resolve(service => '/catalyst_application'), |
f76c40f0 |
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 |
89 | 1; |
51f62e09 |
90 | |
91 | __END__ |
92 | |
93 | =pod |
94 | |
95 | =head1 NAME |
96 | |
97 | Catalyst::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 |
141 | Catalyst::IOC provides "sugar" methods to extend the behavior of the default |
142 | Catalyst container. |
143 | |
51f62e09 |
144 | =head1 METHODS |
145 | |
4bb29053 |
146 | =head2 container |
147 | |
9226f304 |
148 | Sets up the root container to be customised. |
149 | |
4bb29053 |
150 | =head2 model |
151 | |
9226f304 |
152 | Sets up the model container to be customised. |
153 | |
4bb29053 |
154 | =head2 view |
155 | |
9226f304 |
156 | Sets up the view container to be customised. |
157 | |
4bb29053 |
158 | =head2 controller |
159 | |
9226f304 |
160 | Sets up the controller container to be customised. |
161 | |
4bb29053 |
162 | =head2 component |
163 | |
202784d3 |
164 | Adds a component to the sub-container. Works like L<Bread::Board::service>. |
9226f304 |
165 | |
51f62e09 |
166 | =head1 AUTHORS |
167 | |
168 | Catalyst Contributors, see Catalyst.pm |
169 | |
4e4e003e |
170 | =head1 SEE ALSO |
171 | |
172 | L<Bread::Board> |
173 | |
51f62e09 |
174 | =head1 COPYRIGHT |
175 | |
176 | This library is free software. You can redistribute it and/or modify it under |
177 | the same terms as Perl itself. |
178 | |
179 | =cut |