4 use Bread::Board qw/depends_on/;
5 use Catalyst::IOC::ConstructorInjection;
7 use Sub::Exporter -setup => {
16 groups => { default => [qw/
31 ${"${caller}::customise_container"} = sub {
32 local ${"${caller}::current_container"} = shift;
37 sub model (&) { _subcontainer( shift, (caller)[0], 'model' ) }
38 sub view (&) { _subcontainer( shift, (caller)[0], 'view' ) }
39 sub controller (&) { _subcontainer( shift, (caller)[0], 'controller' ) }
41 sub _subcontainer (&$$) {
42 my ( $code, $caller, $subcontainer ) = @_;
45 local ${"${caller}::current_container"} =
46 ${"${caller}::current_container"}->get_sub_container($subcontainer);
51 my ($name, %args) = @_;
52 my $current_container;
57 $current_container = ${"${caller}::current_container"};
60 $args{dependencies} ||= {};
61 $args{dependencies}{application_name} = depends_on( '/application_name' );
63 my $lifecycle = $args{lifecycle} || 'Singleton';
64 $args{lifecycle} = grep( m/^$lifecycle$/, qw/COMPONENTSingleton Request/)
65 ? "+Catalyst::IOC::LifeCycle::$lifecycle"
69 # FIXME - check $args{type} here!
71 my $component_name = join '::', (
72 $current_container->resolve(service => '/application_name'),
73 ucfirst($current_container->name),
77 $current_container->add_service(
78 Catalyst::IOC::ConstructorInjection->new(
81 catalyst_component_name => $component_name,
94 Catalyst::IOC - IOC for Catalyst, based on Bread::Board
98 package MyApp::Container;
101 extends 'Catalyst::IOC::Container';
108 # model Bar needs model Foo to be built before
109 # and Bar's constructor gets Foo as a parameter
110 component Bar => ( dependencies => [
111 depends_on('/model/Foo'),
114 # Baz is rebuilt once per HTTP request
115 component Baz => ( lifecycle => 'Request' );
117 # built only once per application life time
118 component Quux => ( lifecycle => 'Singleton' );
120 # built once per app life time and uses an external model,
121 # outside the default directory
122 # no need for wrappers or Catalyst::Model::Adaptor
124 lifecycle => 'Singleton',
125 class => 'My::External::Class',
129 component HTML => ();
132 component Root => ();
138 Catalyst::IOC provides "sugar" methods to extend the behavior of the default
145 Sets up the root container to be customised.
149 Sets up the model container to be customised.
153 Sets up the view container to be customised.
157 Sets up the controller container to be customised.
161 Adds a component to the subcontainer. Works like L<Bread::Board::service>.
165 Catalyst Contributors, see Catalyst.pm
173 This library is free software. You can redistribute it and/or modify it under
174 the same terms as Perl itself.