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',
54 my ( $self, $params ) = @_;
57 $self->${\"build_${_}_service"}
76 $self->add_sub_container(
77 $self->build_controller_subcontainer
80 $self->add_sub_container(
81 $self->build_view_subcontainer(
82 default_component => $params->{default_view},
86 $self->add_sub_container(
87 $self->build_model_subcontainer(
88 default_component => $params->{default_model},
93 sub build_model_subcontainer {
96 return $self->new_sub_container( @_,
101 sub build_view_subcontainer {
104 return $self->new_sub_container( @_,
109 sub build_controller_subcontainer {
112 return $self->new_sub_container(
113 name => 'controller',
117 sub build_name_service {
120 return Bread::Board::Literal->new( name => 'name', value => $self->name );
123 sub build_driver_service {
126 return Bread::Board::Literal->new( name => 'driver', value => $self->driver );
129 sub build_file_service {
132 return Bread::Board::Literal->new( name => 'file', value => $self->file );
135 sub build_substitutions_service {
138 return Bread::Board::Literal->new( name => 'substitutions', value => $self->substitutions );
141 sub build_extensions_service {
144 return Bread::Board::BlockInjection->new(
145 name => 'extensions',
147 return \@{Config::Any->extensions};
152 sub build_prefix_service {
155 return Bread::Board::BlockInjection->new(
158 return Catalyst::Utils::appprefix( shift->param('name') );
160 dependencies => [ depends_on('name') ],
164 sub build_path_service {
167 return Bread::Board::BlockInjection->new(
172 return Catalyst::Utils::env_value( $s->param('name'), 'CONFIG' )
174 || $s->param('name')->path_to( $s->param('prefix') );
176 dependencies => [ depends_on('file'), depends_on('name'), depends_on('prefix') ],
180 sub build_config_service {
183 return Bread::Board::BlockInjection->new(
188 my $v = Data::Visitor::Callback->new(
190 return unless defined $_;
191 return $self->_config_substitutions( $s->param('name'), $s->param('substitutions'), $_ );
195 $v->visit( $s->param('raw_config') );
197 dependencies => [ depends_on('name'), depends_on('raw_config'), depends_on('substitutions') ],
201 sub build_raw_config_service {
204 return Bread::Board::BlockInjection->new(
205 name => 'raw_config',
209 my @global = @{$s->param('global_config')};
210 my @locals = @{$s->param('local_config')};
213 for my $cfg (@global, @locals) {
215 $config = Catalyst::Utils::merge_hashes( $config, $cfg->{$_} );
220 dependencies => [ depends_on('global_config'), depends_on('local_config') ],
224 sub build_global_files_service {
227 return Bread::Board::BlockInjection->new(
228 name => 'global_files',
232 my ( $path, $extension ) = @{$s->param('config_path')};
234 my @extensions = @{$s->param('extensions')};
238 die "Unable to handle files with the extension '${extension}'" unless grep { $_ eq $extension } @extensions;
241 @files = map { "$path.$_" } @extensions;
245 dependencies => [ depends_on('extensions'), depends_on('config_path') ],
249 sub build_local_files_service {
252 return Bread::Board::BlockInjection->new(
253 name => 'local_files',
257 my ( $path, $extension ) = @{$s->param('config_path')};
258 my $suffix = $s->param('config_local_suffix');
260 my @extensions = @{$s->param('extensions')};
264 die "Unable to handle files with the extension '${extension}'" unless grep { $_ eq $extension } @extensions;
265 $path =~ s{\.$extension}{_$suffix.$extension};
268 @files = map { "${path}_${suffix}.$_" } @extensions;
272 dependencies => [ depends_on('extensions'), depends_on('config_path'), depends_on('config_local_suffix') ],
276 sub build_global_config_service {
279 return Bread::Board::BlockInjection->new(
280 name => 'global_config',
284 return Config::Any->load_files({
285 files => $s->param('global_files'),
286 filter => \&_fix_syntax,
288 driver_args => $s->param('driver'),
291 dependencies => [ depends_on('global_files') ],
295 sub build_local_config_service {
298 return Bread::Board::BlockInjection->new(
299 name => 'local_config',
303 return Config::Any->load_files({
304 files => $s->param('local_files'),
305 filter => \&_fix_syntax,
307 driver_args => $s->param('driver'),
310 dependencies => [ depends_on('local_files') ],
314 sub build_config_path_service {
317 return Bread::Board::BlockInjection->new(
318 name => 'config_path',
322 my $path = $s->param('path');
323 my $prefix = $s->param('prefix');
325 my ( $extension ) = ( $path =~ m{\.(.{1,4})$} );
328 $path =~ s{[\/\\]$}{};
332 return [ $path, $extension ];
334 dependencies => [ depends_on('prefix'), depends_on('path') ],
338 sub build_config_local_suffix_service {
341 return Bread::Board::BlockInjection->new(
342 name => 'config_local_suffix',
345 my $suffix = Catalyst::Utils::env_value( $s->param('name'), 'CONFIG_LOCAL_SUFFIX' ) || $self->config_local_suffix;
349 dependencies => [ depends_on('name') ],
357 prefix => $_ eq 'Component' ? '' : $_ . '::',
358 values => delete $config->{ lc $_ } || delete $config->{ $_ }
360 grep { ref $config->{ lc $_ } || ref $config->{ $_ } }
361 qw( Component Model M View V Controller C Plugin )
364 foreach my $comp ( @components ) {
365 my $prefix = $comp->{ prefix };
366 foreach my $element ( keys %{ $comp->{ values } } ) {
367 $config->{ "$prefix$element" } = $comp->{ values }->{ $element };
372 sub _config_substitutions {
373 my ( $self, $name, $subs, $arg ) = @_;
375 $subs->{ HOME } ||= sub { shift->path_to( '' ); };
379 if (! defined($ENV{$v})) {
380 Catalyst::Exception->throw( message =>
381 "Missing environment variable: $v" );
387 $subs->{ path_to } ||= sub { shift->path_to( @_ ); };
388 $subs->{ literal } ||= sub { return $_[ 1 ]; };
389 my $subsre = join( '|', keys %$subs );
391 $arg =~ s{__($subsre)(?:\((.+?)\))?__}{ $subs->{ $1 }->( $name, $2 ? split( /,/, $2 ) : () ) }eg;
395 sub get_component_from_sub_container {
396 my ( $self, $sub_container_name, $name, $c, @args ) = @_;
398 my $sub_container = $self->get_sub_container( $sub_container_name );
401 my $default = $sub_container->default_component;
403 return $sub_container->get_component( $default, $c, @args )
404 if $default && $sub_container->has_service( $default );
406 # this is never a controller, so this is safe
407 $c->log->warn( "Calling \$c->$sub_container_name() is not supported unless you specify one of:" );
408 $c->log->warn( "* \$c->config(default_$sub_container_name => 'the name of the default $sub_container_name to use')" );
409 $c->log->warn( "* \$c->stash->{current_$sub_container_name} # the name of the view to use for this request" );
410 $c->log->warn( "* \$c->stash->{current_${sub_container_name}_instance} # the instance of the $sub_container_name to use for this request" );
415 return $sub_container->get_component_regexp( $name, $c, @args )
418 return $sub_container->get_component( $name, $c, @args )
419 if $sub_container->has_service( $name );
422 "Attempted to use $sub_container_name '$name', " .
423 "but it does not exist"
437 Catalyst::Container - IOC for Catalyst components
441 =head2 build_model_subcontainer
443 =head2 build_view_subcontainer
445 =head2 build_controller_subcontainer
447 =head2 build_default_model_service
449 =head2 build_default_view_service
451 =head2 build_name_service
453 =head2 build_driver_service
455 =head2 build_file_service
457 =head2 build_substitutions_service
459 =head2 build_extensions_service
461 =head2 build_prefix_service
463 =head2 build_path_service
465 =head2 build_config_service
467 =head2 build_raw_config_service
469 =head2 build_global_files_service
471 =head2 build_local_files_service
473 =head2 build_global_config_service
475 =head2 build_local_config_service
477 =head2 build_config_path_service
479 =head2 build_config_local_suffix_service
483 =head2 _config_substitutions
487 Catalyst Contributors, see Catalyst.pm
491 This library is free software. You can redistribute it and/or modify it under
492 the same terms as Perl itself.