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