1 package Catalyst::IOC::Container;
5 use Data::Visitor::Callback;
6 use Catalyst::Utils ();
7 use MooseX::Types::LoadableClass qw/ LoadableClass /;
8 use Catalyst::IOC::BlockInjection;
9 use namespace::autoclean;
11 extends 'Bread::Board::Container';
13 has config_local_suffix => (
22 default => sub { +{} },
31 has substitutions => (
34 default => sub { +{} },
43 has sub_container_class => (
47 default => 'Catalyst::IOC::SubContainer',
49 new_sub_container => 'new',
57 $self->${\"build_${_}_service"}
78 $self->add_sub_container(
79 $self->${ \"build_${_}_subcontainer" }
80 ) for qw/ model view controller /;
83 sub build_model_subcontainer {
86 return $self->new_sub_container(
91 sub build_view_subcontainer {
94 return $self->new_sub_container(
99 sub build_controller_subcontainer {
102 return $self->new_sub_container(
103 name => 'controller',
107 sub build_default_model_service {
108 Bread::Board::BlockInjection->new(
109 name => 'default_model',
111 shift->param('config')->{default_model};
113 dependencies => [ depends_on('config') ],
117 sub build_default_view_service {
118 Bread::Board::BlockInjection->new(
119 name => 'default_view',
121 shift->param('config')->{default_view};
123 dependencies => [ depends_on('config') ],
127 sub build_name_service {
130 return Bread::Board::Literal->new( name => 'name', value => $self->name );
133 sub build_driver_service {
136 return Bread::Board::Literal->new( name => 'driver', value => $self->driver );
139 sub build_file_service {
142 return Bread::Board::Literal->new( name => 'file', value => $self->file );
145 sub build_substitutions_service {
148 return Bread::Board::Literal->new( name => 'substitutions', value => $self->substitutions );
151 sub build_extensions_service {
154 return Bread::Board::BlockInjection->new(
155 name => 'extensions',
157 return \@{Config::Any->extensions};
162 sub build_prefix_service {
165 return Bread::Board::BlockInjection->new(
168 return Catalyst::Utils::appprefix( shift->param('name') );
170 dependencies => [ depends_on('name') ],
174 sub build_path_service {
177 return Bread::Board::BlockInjection->new(
182 return Catalyst::Utils::env_value( $s->param('name'), 'CONFIG' )
184 || $s->param('name')->path_to( $s->param('prefix') );
186 dependencies => [ depends_on('file'), depends_on('name'), depends_on('prefix') ],
190 sub build_config_service {
193 return Bread::Board::BlockInjection->new(
198 my $v = Data::Visitor::Callback->new(
200 return unless defined $_;
201 return $self->_config_substitutions( $s->param('name'), $s->param('substitutions'), $_ );
205 $v->visit( $s->param('raw_config') );
207 dependencies => [ depends_on('name'), depends_on('raw_config'), depends_on('substitutions') ],
211 sub build_raw_config_service {
214 return Bread::Board::BlockInjection->new(
215 name => 'raw_config',
219 my @global = @{$s->param('global_config')};
220 my @locals = @{$s->param('local_config')};
223 for my $cfg (@global, @locals) {
225 $config = Catalyst::Utils::merge_hashes( $config, $cfg->{$_} );
230 dependencies => [ depends_on('global_config'), depends_on('local_config') ],
234 sub build_global_files_service {
237 return Bread::Board::BlockInjection->new(
238 name => 'global_files',
242 my ( $path, $extension ) = @{$s->param('config_path')};
244 my @extensions = @{$s->param('extensions')};
248 die "Unable to handle files with the extension '${extension}'" unless grep { $_ eq $extension } @extensions;
251 @files = map { "$path.$_" } @extensions;
255 dependencies => [ depends_on('extensions'), depends_on('config_path') ],
259 sub build_local_files_service {
262 return Bread::Board::BlockInjection->new(
263 name => 'local_files',
267 my ( $path, $extension ) = @{$s->param('config_path')};
268 my $suffix = $s->param('config_local_suffix');
270 my @extensions = @{$s->param('extensions')};
274 die "Unable to handle files with the extension '${extension}'" unless grep { $_ eq $extension } @extensions;
275 $path =~ s{\.$extension}{_$suffix.$extension};
278 @files = map { "${path}_${suffix}.$_" } @extensions;
282 dependencies => [ depends_on('extensions'), depends_on('config_path'), depends_on('config_local_suffix') ],
286 sub build_global_config_service {
289 return Bread::Board::BlockInjection->new(
290 name => 'global_config',
294 return Config::Any->load_files({
295 files => $s->param('global_files'),
296 filter => \&_fix_syntax,
298 driver_args => $s->param('driver'),
301 dependencies => [ depends_on('global_files') ],
305 sub build_local_config_service {
308 return Bread::Board::BlockInjection->new(
309 name => 'local_config',
313 return Config::Any->load_files({
314 files => $s->param('local_files'),
315 filter => \&_fix_syntax,
317 driver_args => $s->param('driver'),
320 dependencies => [ depends_on('local_files') ],
324 sub build_config_path_service {
327 return Bread::Board::BlockInjection->new(
328 name => 'config_path',
332 my $path = $s->param('path');
333 my $prefix = $s->param('prefix');
335 my ( $extension ) = ( $path =~ m{\.(.{1,4})$} );
338 $path =~ s{[\/\\]$}{};
342 return [ $path, $extension ];
344 dependencies => [ depends_on('prefix'), depends_on('path') ],
348 sub build_config_local_suffix_service {
351 return Bread::Board::BlockInjection->new(
352 name => 'config_local_suffix',
355 my $suffix = Catalyst::Utils::env_value( $s->param('name'), 'CONFIG_LOCAL_SUFFIX' ) || $self->config_local_suffix;
359 dependencies => [ depends_on('name') ],
367 prefix => $_ eq 'Component' ? '' : $_ . '::',
368 values => delete $config->{ lc $_ } || delete $config->{ $_ }
370 grep { ref $config->{ lc $_ } || ref $config->{ $_ } }
371 qw( Component Model M View V Controller C Plugin )
374 foreach my $comp ( @components ) {
375 my $prefix = $comp->{ prefix };
376 foreach my $element ( keys %{ $comp->{ values } } ) {
377 $config->{ "$prefix$element" } = $comp->{ values }->{ $element };
382 sub _config_substitutions {
383 my ( $self, $name, $subs, $arg ) = @_;
385 $subs->{ HOME } ||= sub { shift->path_to( '' ); };
389 if (! defined($ENV{$v})) {
390 Catalyst::Exception->throw( message =>
391 "Missing environment variable: $v" );
397 $subs->{ path_to } ||= sub { shift->path_to( @_ ); };
398 $subs->{ literal } ||= sub { return $_[ 1 ]; };
399 my $subsre = join( '|', keys %$subs );
401 $arg =~ s{__($subsre)(?:\((.+?)\))?__}{ $subs->{ $1 }->( $name, $2 ? split( /,/, $2 ) : () ) }eg;
405 sub get_component_from_sub_container {
406 my ( $self, $sub_container_name, $name, $c, @args ) = @_;
408 my $sub_container = $self->get_sub_container( $sub_container_name );
411 my $default_name = 'default_' . $sub_container_name;
412 my $default = $self->resolve( service => $default_name )
413 if $self->has_service($default_name);
415 return $sub_container->get_component( $default, $c, @args )
416 if $default && $sub_container->has_service( $default );
418 # this is never a controller, so this is safe
419 $c->log->warn( "Calling \$c->$sub_container_name() is not supported unless you specify one of:" );
420 $c->log->warn( "* \$c->config(default_$sub_container_name => 'the name of the default $sub_container_name to use')" );
421 $c->log->warn( "* \$c->stash->{current_$sub_container_name} # the name of the view to use for this request" );
422 $c->log->warn( "* \$c->stash->{current_${sub_container_name}_instance} # the instance of the $sub_container_name to use for this request" );
425 return $sub_container->get_component_regexp( $name, $c, @args )
428 return $sub_container->get_component( $name, $c, @args )
429 if $sub_container->has_service( $name );
432 "Attempted to use $sub_container_name '$name', " .
433 "but it does not exist"
447 Catalyst::Container - IOC for Catalyst components
451 =head2 build_model_subcontainer
453 =head2 build_view_subcontainer
455 =head2 build_controller_subcontainer
457 =head2 build_default_model_service
459 =head2 build_default_view_service
461 =head2 build_name_service
463 =head2 build_driver_service
465 =head2 build_file_service
467 =head2 build_substitutions_service
469 =head2 build_extensions_service
471 =head2 build_prefix_service
473 =head2 build_path_service
475 =head2 build_config_service
477 =head2 build_raw_config_service
479 =head2 build_global_files_service
481 =head2 build_local_files_service
483 =head2 build_global_config_service
485 =head2 build_local_config_service
487 =head2 build_config_path_service
489 =head2 build_config_local_suffix_service
493 =head2 _config_substitutions
497 Catalyst Contributors, see Catalyst.pm
501 This library is free software. You can redistribute it and/or modify it under
502 the same terms as Perl itself.