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; |
85ebb635 |
6 | no strict 'refs'; |
2336ae0b |
7 | |
71d3df94 |
8 | use Sub::Exporter -setup => { |
9 | exports => [qw/ |
69e61491 |
10 | depends_on |
71d3df94 |
11 | component |
7cd05fd2 |
12 | model |
13 | container |
71d3df94 |
14 | /], |
15 | groups => { default => [qw/ |
69e61491 |
16 | depends_on |
71d3df94 |
17 | component |
7cd05fd2 |
18 | model |
19 | container |
71d3df94 |
20 | /]}, |
21 | }; |
fddc99a9 |
22 | |
7cd05fd2 |
23 | sub container (&) { |
24 | my $code = shift; |
85ebb635 |
25 | my $caller = caller; |
26 | ${"${caller}::customise_container"} = sub { |
85ebb635 |
27 | local ${"${caller}::current_container"} = shift; |
7cd05fd2 |
28 | $code->(); |
29 | }; |
30 | } |
31 | |
32 | sub model (&) { |
33 | my $code = shift; |
85ebb635 |
34 | my $caller = caller; |
35 | local ${"${caller}::current_container"} = ${"${caller}::current_container"}->get_sub_container('model'); |
7cd05fd2 |
36 | $code->(); |
37 | } |
71d3df94 |
38 | |
39 | sub component { |
40 | my ($name, %args) = @_; |
85ebb635 |
41 | my $caller = caller; |
71d3df94 |
42 | $args{dependencies} ||= {}; |
43 | $args{dependencies}{application_name} = depends_on( '/application_name' ); |
44 | |
129508fb |
45 | my $lifecycle = $args{lifecycle}; |
46 | my %catalyst_lifecycles = map { $_ => 1 } qw/ COMPONENTSingleton Request /; |
47 | $args{lifecycle} = $lifecycle |
48 | ? $catalyst_lifecycles{$lifecycle} ? "+Catalyst::IOC::LifeCycle::$lifecycle" : $lifecycle |
49 | : 'Singleton' |
50 | ; |
51 | |
71d3df94 |
52 | # FIXME - check $args{type} here! |
53 | |
cdd4485f |
54 | my $component_name = join '::', ( |
85ebb635 |
55 | ${"${caller}::current_container"}->resolve(service => '/application_name'), |
56 | ucfirst(${"${caller}::current_container"}->name), |
cdd4485f |
57 | $name |
58 | ); |
59 | |
7cd05fd2 |
60 | my $service = Catalyst::IOC::ConstructorInjection->new( |
71d3df94 |
61 | %args, |
129508fb |
62 | name => $name, |
cdd4485f |
63 | catalyst_component_name => $component_name, |
7cd05fd2 |
64 | ); |
85ebb635 |
65 | ${"${caller}::current_container"}->add_service($service); |
71d3df94 |
66 | } |
2336ae0b |
67 | |
fb7aaa61 |
68 | 1; |
51f62e09 |
69 | |
70 | __END__ |
71 | |
72 | =pod |
73 | |
74 | =head1 NAME |
75 | |
76 | Catalyst::IOC - IOC for Catalyst, based on Bread::Board |
77 | |
78 | =head1 SYNOPSIS |
79 | |
4e4e003e |
80 | package MyApp::Container; |
81 | use Catalyst::IOC; |
82 | |
83 | sub BUILD { |
84 | my $self = shift; |
85 | |
86 | container $self => as { |
87 | container model => as { |
88 | |
89 | # default component |
90 | component Foo => (); |
91 | |
92 | # model Bar needs model Foo to be built before |
93 | # and Bar's constructor gets Foo as a parameter |
94 | component Bar => ( dependencies => [ |
95 | depends_on('/model/Foo'), |
96 | ]); |
97 | |
98 | # Baz is rebuilt once per HTTP request |
99 | component Baz => ( lifecycle => 'Request' ); |
100 | |
101 | # built only once per application life time |
102 | component Quux => ( lifecycle => 'Singleton' ); |
103 | |
104 | # built once per app life time and uses an external model, |
105 | # outside the default directory |
106 | # no need for wrappers or Catalyst::Model::Adaptor |
107 | component Fnar => ( |
108 | lifecycle => 'Singleton', |
109 | class => 'My::External::Class', |
110 | ); |
111 | }; |
112 | } |
113 | } |
114 | |
51f62e09 |
115 | =head1 DESCRIPTION |
116 | |
117 | =head1 METHODS |
118 | |
119 | =head1 AUTHORS |
120 | |
121 | Catalyst Contributors, see Catalyst.pm |
122 | |
4e4e003e |
123 | =head1 SEE ALSO |
124 | |
125 | L<Bread::Board> |
126 | |
51f62e09 |
127 | =head1 COPYRIGHT |
128 | |
129 | This library is free software. You can redistribute it and/or modify it under |
130 | the same terms as Perl itself. |
131 | |
132 | =cut |