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