singleton lifecycle tests
[catagits/Catalyst-Runtime.git] / lib / Catalyst / IOC.pm
CommitLineData
2336ae0b 1package Catalyst::IOC;
fb7aaa61 2use strict;
3use warnings;
69e61491 4use Bread::Board qw/depends_on/;
71d3df94 5use Catalyst::IOC::ConstructorInjection;
85ebb635 6no strict 'refs';
2336ae0b 7
7cd05fd2 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..
71d3df94 12use Sub::Exporter -setup => {
13 exports => [qw/
69e61491 14 depends_on
71d3df94 15 component
7cd05fd2 16 model
17 container
71d3df94 18 /],
19 groups => { default => [qw/
69e61491 20 depends_on
71d3df94 21 component
7cd05fd2 22 model
23 container
71d3df94 24 /]},
25};
7e3dbfea 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?
71d3df94 41#use Moose::Exporter;
42#Moose::Exporter->setup_import_methods(
43# also => ['Bread::Board'],
44#);
7cd05fd2 45sub container (&) {
46 my $code = shift;
85ebb635 47 my $caller = caller;
48 ${"${caller}::customise_container"} = sub {
7cd05fd2 49 warn("In customise container");
85ebb635 50 local ${"${caller}::current_container"} = shift;
7cd05fd2 51 $code->();
52 };
53}
54
55sub model (&) {
56 my $code = shift;
85ebb635 57 my $caller = caller;
58 local ${"${caller}::current_container"} = ${"${caller}::current_container"}->get_sub_container('model');
7cd05fd2 59 $code->();
60}
71d3df94 61
62sub component {
63 my ($name, %args) = @_;
85ebb635 64 my $caller = caller;
71d3df94 65 $args{dependencies} ||= {};
66 $args{dependencies}{application_name} = depends_on( '/application_name' );
67
68 # FIXME - check $args{type} here!
69
cdd4485f 70 my $component_name = join '::', (
85ebb635 71 ${"${caller}::current_container"}->resolve(service => '/application_name'),
72 ucfirst(${"${caller}::current_container"}->name),
cdd4485f 73 $name
74 );
75
7cd05fd2 76 my $service = Catalyst::IOC::ConstructorInjection->new(
71d3df94 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)
cdd4485f 82 catalyst_component_name => $component_name,
7cd05fd2 83 );
85ebb635 84 ${"${caller}::current_container"}->add_service($service);
71d3df94 85}
2336ae0b 86
fb7aaa61 871;
51f62e09 88
4e4e003e 89# FIXME - should the code example below be on this file or Catalyst::IOC::Container?
90
51f62e09 91__END__
92
93=pod
94
95=head1 NAME
96
97Catalyst::IOC - IOC for Catalyst, based on Bread::Board
98
99=head1 SYNOPSIS
100
4e4e003e 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
51f62e09 136=head1 DESCRIPTION
137
138=head1 METHODS
139
140=head1 AUTHORS
141
142Catalyst Contributors, see Catalyst.pm
143
4e4e003e 144=head1 SEE ALSO
145
146L<Bread::Board>
147
51f62e09 148=head1 COPYRIGHT
149
150This library is free software. You can redistribute it and/or modify it under
151the same terms as Perl itself.
152
153=cut