removing noise (comments and warnings)
[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 use Sub::Exporter -setup => {
9     exports => [qw/
10         depends_on
11         component
12         model
13         container
14     /],
15     groups  => { default => [qw/
16         depends_on
17         component
18         model
19         container
20     /]},
21 };
22
23 sub container (&) {
24     my $code = shift;
25     my $caller = caller;
26     ${"${caller}::customise_container"} = sub {
27         local ${"${caller}::current_container"} = shift;
28         $code->();
29     };
30 }
31
32 sub model (&) {
33     my $code = shift;
34     my $caller = caller;
35     local ${"${caller}::current_container"} = ${"${caller}::current_container"}->get_sub_container('model');
36     $code->();
37 }
38
39 sub component {
40     my ($name, %args) = @_;
41     my $caller = caller;
42     $args{dependencies} ||= {};
43     $args{dependencies}{application_name} = depends_on( '/application_name' );
44
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
52     # FIXME - check $args{type} here!
53
54     my $component_name = join '::', (
55         ${"${caller}::current_container"}->resolve(service => '/application_name'),
56         ucfirst(${"${caller}::current_container"}->name),
57         $name
58     );
59
60     my $service = Catalyst::IOC::ConstructorInjection->new(
61         %args,
62         name => $name,
63         catalyst_component_name => $component_name,
64     );
65     ${"${caller}::current_container"}->add_service($service);
66 }
67
68 1;
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
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
115 =head1 DESCRIPTION
116
117 =head1 METHODS
118
119 =head1 AUTHORS
120
121 Catalyst Contributors, see Catalyst.pm
122
123 =head1 SEE ALSO
124
125 L<Bread::Board>
126
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