updating comment
[catagits/Catalyst-Runtime.git] / lib / Catalyst / IOC.pm
CommitLineData
2336ae0b 1package Catalyst::IOC;
fb7aaa61 2use strict;
3use warnings;
7e3dbfea 4use Bread::Board;
71d3df94 5use Catalyst::IOC::ConstructorInjection;
2336ae0b 6
7cd05fd2 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..
71d3df94 11use Sub::Exporter -setup => {
12 exports => [qw/
13 component
7cd05fd2 14 model
15 container
71d3df94 16 /],
17 groups => { default => [qw/
18 component
7cd05fd2 19 model
20 container
71d3df94 21 /]},
22};
7e3dbfea 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?
71d3df94 38#use Moose::Exporter;
39#Moose::Exporter->setup_import_methods(
40# also => ['Bread::Board'],
41#);
7cd05fd2 42our $customise_container;
43our $current_container;
44
45sub container (&) {
46 my $code = shift;
47 $customise_container = sub {
48 warn("In customise container");
49 local $current_container = shift();
50 $code->();
51 };
52}
53
54sub model (&) {
55 my $code = shift;
56 local $current_container = $current_container->get_sub_container('model');
57 $code->();
58}
71d3df94 59
60sub 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
7cd05fd2 67 my $service = Catalyst::IOC::ConstructorInjection->new(
71d3df94 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',
7cd05fd2 74 );
75 $current_container->add_service($service);
71d3df94 76}
2336ae0b 77
fb7aaa61 781;
51f62e09 79
4e4e003e 80# FIXME - should the code example below be on this file or Catalyst::IOC::Container?
81
51f62e09 82__END__
83
84=pod
85
86=head1 NAME
87
88Catalyst::IOC - IOC for Catalyst, based on Bread::Board
89
90=head1 SYNOPSIS
91
4e4e003e 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
51f62e09 127=head1 DESCRIPTION
128
129=head1 METHODS
130
131=head1 AUTHORS
132
133Catalyst Contributors, see Catalyst.pm
134
4e4e003e 135=head1 SEE ALSO
136
137L<Bread::Board>
138
51f62e09 139=head1 COPYRIGHT
140
141This library is free software. You can redistribute it and/or modify it under
142the same terms as Perl itself.
143
144=cut