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 | |
7cd05fd2 |
7 | # FIXME - All of these imports need to get the importing package |
8 | # as the customise_container and current_container variables |
9 | # NEED to be in the containers package so there can be multiple |
10 | # containers.. |
71d3df94 |
11 | use Sub::Exporter -setup => { |
12 | exports => [qw/ |
69e61491 |
13 | depends_on |
71d3df94 |
14 | component |
7cd05fd2 |
15 | model |
16 | container |
71d3df94 |
17 | /], |
18 | groups => { default => [qw/ |
69e61491 |
19 | depends_on |
71d3df94 |
20 | component |
7cd05fd2 |
21 | model |
22 | container |
71d3df94 |
23 | /]}, |
24 | }; |
7e3dbfea |
25 | #use Sub::Exporter -setup => [ |
26 | # qw( |
27 | # Bread::Board::as |
28 | # Bread::Board::container |
29 | # Bread::Board::depends_on |
30 | # Bread::Board::service |
31 | # Bread::Board::alias |
32 | # Bread::Board::wire_names |
33 | # Bread::Board::include |
34 | # Bread::Board::typemap |
35 | # Bread::Board::infer |
36 | # ) |
37 | #]; |
38 | # I'm probably doing it wrong. |
39 | # Anyway, I'll just use Moose::Exporter. Do I really have to use Sub::Exporter? |
71d3df94 |
40 | #use Moose::Exporter; |
41 | #Moose::Exporter->setup_import_methods( |
42 | # also => ['Bread::Board'], |
43 | #); |
7cd05fd2 |
44 | our $customise_container; |
45 | our $current_container; |
46 | |
47 | sub container (&) { |
48 | my $code = shift; |
49 | $customise_container = sub { |
50 | warn("In customise container"); |
51 | local $current_container = shift(); |
52 | $code->(); |
53 | }; |
54 | } |
55 | |
56 | sub model (&) { |
57 | my $code = shift; |
58 | local $current_container = $current_container->get_sub_container('model'); |
59 | $code->(); |
60 | } |
71d3df94 |
61 | |
62 | sub component { |
63 | my ($name, %args) = @_; |
64 | $args{dependencies} ||= {}; |
65 | $args{dependencies}{application_name} = depends_on( '/application_name' ); |
66 | |
67 | # FIXME - check $args{type} here! |
68 | |
7cd05fd2 |
69 | my $service = Catalyst::IOC::ConstructorInjection->new( |
71d3df94 |
70 | %args, |
71 | name => $name, |
72 | lifecycle => 'Singleton', |
73 | # XX FIXME - needs to become possible to intuit catalyst_component_name |
74 | # from dependencies (like config) |
75 | catalyst_component_name => 'TestAppCustomContainer::Model::Bar', |
7cd05fd2 |
76 | ); |
77 | $current_container->add_service($service); |
71d3df94 |
78 | } |
2336ae0b |
79 | |
fb7aaa61 |
80 | 1; |
51f62e09 |
81 | |
4e4e003e |
82 | # FIXME - should the code example below be on this file or Catalyst::IOC::Container? |
83 | |
51f62e09 |
84 | __END__ |
85 | |
86 | =pod |
87 | |
88 | =head1 NAME |
89 | |
90 | Catalyst::IOC - IOC for Catalyst, based on Bread::Board |
91 | |
92 | =head1 SYNOPSIS |
93 | |
4e4e003e |
94 | package MyApp::Container; |
95 | use Catalyst::IOC; |
96 | |
97 | sub BUILD { |
98 | my $self = shift; |
99 | |
100 | container $self => as { |
101 | container model => as { |
102 | |
103 | # default component |
104 | component Foo => (); |
105 | |
106 | # model Bar needs model Foo to be built before |
107 | # and Bar's constructor gets Foo as a parameter |
108 | component Bar => ( dependencies => [ |
109 | depends_on('/model/Foo'), |
110 | ]); |
111 | |
112 | # Baz is rebuilt once per HTTP request |
113 | component Baz => ( lifecycle => 'Request' ); |
114 | |
115 | # built only once per application life time |
116 | component Quux => ( lifecycle => 'Singleton' ); |
117 | |
118 | # built once per app life time and uses an external model, |
119 | # outside the default directory |
120 | # no need for wrappers or Catalyst::Model::Adaptor |
121 | component Fnar => ( |
122 | lifecycle => 'Singleton', |
123 | class => 'My::External::Class', |
124 | ); |
125 | }; |
126 | } |
127 | } |
128 | |
51f62e09 |
129 | =head1 DESCRIPTION |
130 | |
131 | =head1 METHODS |
132 | |
133 | =head1 AUTHORS |
134 | |
135 | Catalyst Contributors, see Catalyst.pm |
136 | |
4e4e003e |
137 | =head1 SEE ALSO |
138 | |
139 | L<Bread::Board> |
140 | |
51f62e09 |
141 | =head1 COPYRIGHT |
142 | |
143 | This library is free software. You can redistribute it and/or modify it under |
144 | the same terms as Perl itself. |
145 | |
146 | =cut |