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"}
76 $self->add_sub_container(
77 $self->${ \"build_${_}_subcontainer" }
78 ) for qw/ model view controller /;
81 sub build_model_subcontainer {
84 return $self->new_sub_container(
89 sub build_view_subcontainer {
92 return $self->new_sub_container(
97 sub build_controller_subcontainer {
100 return $self->new_sub_container(
101 name => 'controller',
105 sub build_default_model {
106 Bread::Board::BlockInjection->new(
108 shift->param('config')->{default_model};
110 dependencies => [ depends_on('config') ],
114 sub build_default_view {
115 Bread::Board::BlockInjection->new(
116 name => 'default_view',
118 shift->param('config')->{default_view};
120 dependencies => [ depends_on('config') ],
124 sub build_name_service {
127 return Bread::Board::Literal->new( name => 'name', value => $self->name );
130 sub build_driver_service {
133 return Bread::Board::Literal->new( name => 'driver', value => $self->driver );
136 sub build_file_service {
139 return Bread::Board::Literal->new( name => 'file', value => $self->file );
142 sub build_substitutions_service {
145 return Bread::Board::Literal->new( name => 'substitutions', value => $self->substitutions );
148 sub build_extensions_service {
151 return Bread::Board::BlockInjection->new(
152 name => 'extensions',
154 return \@{Config::Any->extensions};
159 sub build_prefix_service {
162 return Bread::Board::BlockInjection->new(
165 return Catalyst::Utils::appprefix( shift->param('name') );
167 dependencies => [ depends_on('name') ],
171 sub build_path_service {
174 return Bread::Board::BlockInjection->new(
179 return Catalyst::Utils::env_value( $s->param('name'), 'CONFIG' )
181 || $s->param('name')->path_to( $s->param('prefix') );
183 dependencies => [ depends_on('file'), depends_on('name'), depends_on('prefix') ],
187 sub build_config_service {
190 return Bread::Board::BlockInjection->new(
195 my $v = Data::Visitor::Callback->new(
197 return unless defined $_;
198 return $self->_config_substitutions( $s->param('name'), $s->param('substitutions'), $_ );
202 $v->visit( $s->param('raw_config') );
204 dependencies => [ depends_on('name'), depends_on('raw_config'), depends_on('substitutions') ],
208 sub build_raw_config_service {
211 return Bread::Board::BlockInjection->new(
212 name => 'raw_config',
216 my @global = @{$s->param('global_config')};
217 my @locals = @{$s->param('local_config')};
220 for my $cfg (@global, @locals) {
222 $config = Catalyst::Utils::merge_hashes( $config, $cfg->{$_} );
227 dependencies => [ depends_on('global_config'), depends_on('local_config') ],
231 sub build_global_files_service {
234 return Bread::Board::BlockInjection->new(
235 name => 'global_files',
239 my ( $path, $extension ) = @{$s->param('config_path')};
241 my @extensions = @{$s->param('extensions')};
245 die "Unable to handle files with the extension '${extension}'" unless grep { $_ eq $extension } @extensions;
248 @files = map { "$path.$_" } @extensions;
252 dependencies => [ depends_on('extensions'), depends_on('config_path') ],
256 sub build_local_files_service {
259 return Bread::Board::BlockInjection->new(
260 name => 'local_files',
264 my ( $path, $extension ) = @{$s->param('config_path')};
265 my $suffix = $s->param('config_local_suffix');
267 my @extensions = @{$s->param('extensions')};
271 die "Unable to handle files with the extension '${extension}'" unless grep { $_ eq $extension } @extensions;
272 $path =~ s{\.$extension}{_$suffix.$extension};
275 @files = map { "${path}_${suffix}.$_" } @extensions;
279 dependencies => [ depends_on('extensions'), depends_on('config_path'), depends_on('config_local_suffix') ],
283 sub build_global_config_service {
286 return Bread::Board::BlockInjection->new(
287 name => 'global_config',
291 return Config::Any->load_files({
292 files => $s->param('global_files'),
293 filter => \&_fix_syntax,
295 driver_args => $s->param('driver'),
298 dependencies => [ depends_on('global_files') ],
302 sub build_local_config_service {
305 return Bread::Board::BlockInjection->new(
306 name => 'local_config',
310 return Config::Any->load_files({
311 files => $s->param('local_files'),
312 filter => \&_fix_syntax,
314 driver_args => $s->param('driver'),
317 dependencies => [ depends_on('local_files') ],
321 sub build_config_path_service {
324 return Bread::Board::BlockInjection->new(
325 name => 'config_path',
329 my $path = $s->param('path');
330 my $prefix = $s->param('prefix');
332 my ( $extension ) = ( $path =~ m{\.(.{1,4})$} );
335 $path =~ s{[\/\\]$}{};
339 return [ $path, $extension ];
341 dependencies => [ depends_on('prefix'), depends_on('path') ],
345 sub build_config_local_suffix_service {
348 return Bread::Board::BlockInjection->new(
349 name => 'config_local_suffix',
352 my $suffix = Catalyst::Utils::env_value( $s->param('name'), 'CONFIG_LOCAL_SUFFIX' ) || $self->config_local_suffix;
356 dependencies => [ depends_on('name') ],
364 prefix => $_ eq 'Component' ? '' : $_ . '::',
365 values => delete $config->{ lc $_ } || delete $config->{ $_ }
367 grep { ref $config->{ lc $_ } || ref $config->{ $_ } }
368 qw( Component Model M View V Controller C Plugin )
371 foreach my $comp ( @components ) {
372 my $prefix = $comp->{ prefix };
373 foreach my $element ( keys %{ $comp->{ values } } ) {
374 $config->{ "$prefix$element" } = $comp->{ values }->{ $element };
379 sub _config_substitutions {
380 my ( $self, $name, $subs, $arg ) = @_;
382 $subs->{ HOME } ||= sub { shift->path_to( '' ); };
386 if (! defined($ENV{$v})) {
387 Catalyst::Exception->throw( message =>
388 "Missing environment variable: $v" );
394 $subs->{ path_to } ||= sub { shift->path_to( @_ ); };
395 $subs->{ literal } ||= sub { return $_[ 1 ]; };
396 my $subsre = join( '|', keys %$subs );
398 $arg =~ s{__($subsre)(?:\((.+?)\))?__}{ $subs->{ $1 }->( $name, $2 ? split( /,/, $2 ) : () ) }eg;
402 sub get_component_from_sub_container {
403 my ( $self, $sub_container_name, $name, $c, @args ) = @_;
405 my $sub_container = $self->get_sub_container( $sub_container_name );
408 my $default_name = 'default_' . $sub_container_name;
409 my $default = $self->resolve( service => $default_name )
410 if $self->has_service($default_name);
412 return $sub_container->get_component( $default, $c, @args )
413 if $default && $sub_container->has_service( $default );
415 # this is never a controller, so this is safe
416 $c->log->warn( "Calling \$c->$sub_container_name() is not supported unless you specify one of:" );
417 $c->log->warn( "* \$c->config(default_$sub_container_name => 'the name of the default $sub_container_name to use')" );
418 $c->log->warn( "* \$c->stash->{current_$sub_container_name} # the name of the view to use for this request" );
419 $c->log->warn( "* \$c->stash->{current_${sub_container_name}_instance} # the instance of the $sub_container_name to use for this request" );
422 return $sub_container->get_component_regexp( $name, $c, @args )
425 return $sub_container->get_component( $name, $c, @args )
426 if $sub_container->has_service( $name );
429 "Attempted to use $sub_container_name '$name', " .
430 "but it does not exist"
444 Catalyst::Container - IOC for Catalyst components
448 =head2 build_model_subcontainer
450 =head2 build_view_subcontainer
452 =head2 build_controller_subcontainer
454 =head2 build_name_service
456 =head2 build_driver_service
458 =head2 build_file_service
460 =head2 build_substitutions_service
462 =head2 build_extensions_service
464 =head2 build_prefix_service
466 =head2 build_path_service
468 =head2 build_config_service
470 =head2 build_raw_config_service
472 =head2 build_global_files_service
474 =head2 build_local_files_service
476 =head2 build_global_config_service
478 =head2 build_local_config_service
480 =head2 build_config_path_service
482 =head2 build_config_local_suffix_service
486 =head2 _config_substitutions
490 Catalyst Contributors, see Catalyst.pm
494 This library is free software. You can redistribute it and/or modify it under
495 the same terms as Perl itself.