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