4 use Bread::Board qw/depends_on/;
5 use Catalyst::IOC::ConstructorInjection;
8 use Sub::Exporter -setup => {
17 groups => { default => [qw/
30 ${"${caller}::customise_container"} = sub {
31 local ${"${caller}::current_container"} = shift;
36 sub model (&) { _subcontainer( shift, caller, 'model' ) }
37 sub view (&) { _subcontainer( shift, caller, 'view' ) }
38 sub controller (&) { _subcontainer( shift, caller, 'controller' ) }
40 sub _subcontainer (&$$) {
41 my ( $code, $caller, $subcontainer ) = @_;
42 local ${"${caller}::current_container"} =
43 ${"${caller}::current_container"}->get_sub_container($subcontainer);
48 my ($name, %args) = @_;
50 $args{dependencies} ||= {};
51 $args{dependencies}{application_name} = depends_on( '/application_name' );
53 my $lifecycle = $args{lifecycle};
54 my %catalyst_lifecycles = map { $_ => 1 } qw/ COMPONENTSingleton Request /;
55 $args{lifecycle} = $lifecycle
56 ? $catalyst_lifecycles{$lifecycle} ? "+Catalyst::IOC::LifeCycle::$lifecycle" : $lifecycle
60 # FIXME - check $args{type} here!
62 my $component_name = join '::', (
63 ${"${caller}::current_container"}->resolve(service => '/application_name'),
64 ucfirst(${"${caller}::current_container"}->name),
68 my $service = Catalyst::IOC::ConstructorInjection->new(
71 catalyst_component_name => $component_name,
73 ${"${caller}::current_container"}->add_service($service);
84 Catalyst::IOC - IOC for Catalyst, based on Bread::Board
88 package MyApp::Container;
91 extends 'Catalyst::IOC::Container';
98 # model Bar needs model Foo to be built before
99 # and Bar's constructor gets Foo as a parameter
100 component Bar => ( dependencies => [
101 depends_on('/model/Foo'),
104 # Baz is rebuilt once per HTTP request
105 component Baz => ( lifecycle => 'Request' );
107 # built only once per application life time
108 component Quux => ( lifecycle => 'Singleton' );
110 # built once per app life time and uses an external model,
111 # outside the default directory
112 # no need for wrappers or Catalyst::Model::Adaptor
114 lifecycle => 'Singleton',
115 class => 'My::External::Class',
119 component HTML => ();
122 component Root => ();
128 Catalyst::IOC provides "sugar" methods to extend the behavior of the default
135 Sets up the root container to be customised.
139 Sets up the model container to be customised.
143 Sets up the view container to be customised.
147 Sets up the controller container to be customised.
151 Adds a component to the subcontainer. Works like L<Bread::Board::service>.
155 Catalyst Contributors, see Catalyst.pm
163 This library is free software. You can redistribute it and/or modify it under
164 the same terms as Perl itself.