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(
110 shift->param('config')->{default_model};
112 dependencies => [ depends_on('config') ],
116 sub build_default_view_service {
117 Bread::Board::BlockInjection->new(
118 name => 'default_view',
120 shift->param('config')->{default_view};
122 dependencies => [ depends_on('config') ],
126 sub build_name_service {
129 return Bread::Board::Literal->new( name => 'name', value => $self->name );
132 sub build_driver_service {
135 return Bread::Board::Literal->new( name => 'driver', value => $self->driver );
138 sub build_file_service {
141 return Bread::Board::Literal->new( name => 'file', value => $self->file );
144 sub build_substitutions_service {
147 return Bread::Board::Literal->new( name => 'substitutions', value => $self->substitutions );
150 sub build_extensions_service {
153 return Bread::Board::BlockInjection->new(
154 name => 'extensions',
156 return \@{Config::Any->extensions};
161 sub build_prefix_service {
164 return Bread::Board::BlockInjection->new(
167 return Catalyst::Utils::appprefix( shift->param('name') );
169 dependencies => [ depends_on('name') ],
173 sub build_path_service {
176 return Bread::Board::BlockInjection->new(
181 return Catalyst::Utils::env_value( $s->param('name'), 'CONFIG' )
183 || $s->param('name')->path_to( $s->param('prefix') );
185 dependencies => [ depends_on('file'), depends_on('name'), depends_on('prefix') ],
189 sub build_config_service {
192 return Bread::Board::BlockInjection->new(
197 my $v = Data::Visitor::Callback->new(
199 return unless defined $_;
200 return $self->_config_substitutions( $s->param('name'), $s->param('substitutions'), $_ );
204 $v->visit( $s->param('raw_config') );
206 dependencies => [ depends_on('name'), depends_on('raw_config'), depends_on('substitutions') ],
210 sub build_raw_config_service {
213 return Bread::Board::BlockInjection->new(
214 name => 'raw_config',
218 my @global = @{$s->param('global_config')};
219 my @locals = @{$s->param('local_config')};
222 for my $cfg (@global, @locals) {
224 $config = Catalyst::Utils::merge_hashes( $config, $cfg->{$_} );
229 dependencies => [ depends_on('global_config'), depends_on('local_config') ],
233 sub build_global_files_service {
236 return Bread::Board::BlockInjection->new(
237 name => 'global_files',
241 my ( $path, $extension ) = @{$s->param('config_path')};
243 my @extensions = @{$s->param('extensions')};
247 die "Unable to handle files with the extension '${extension}'" unless grep { $_ eq $extension } @extensions;
250 @files = map { "$path.$_" } @extensions;
254 dependencies => [ depends_on('extensions'), depends_on('config_path') ],
258 sub build_local_files_service {
261 return Bread::Board::BlockInjection->new(
262 name => 'local_files',
266 my ( $path, $extension ) = @{$s->param('config_path')};
267 my $suffix = $s->param('config_local_suffix');
269 my @extensions = @{$s->param('extensions')};
273 die "Unable to handle files with the extension '${extension}'" unless grep { $_ eq $extension } @extensions;
274 $path =~ s{\.$extension}{_$suffix.$extension};
277 @files = map { "${path}_${suffix}.$_" } @extensions;
281 dependencies => [ depends_on('extensions'), depends_on('config_path'), depends_on('config_local_suffix') ],
285 sub build_global_config_service {
288 return Bread::Board::BlockInjection->new(
289 name => 'global_config',
293 return Config::Any->load_files({
294 files => $s->param('global_files'),
295 filter => \&_fix_syntax,
297 driver_args => $s->param('driver'),
300 dependencies => [ depends_on('global_files') ],
304 sub build_local_config_service {
307 return Bread::Board::BlockInjection->new(
308 name => 'local_config',
312 return Config::Any->load_files({
313 files => $s->param('local_files'),
314 filter => \&_fix_syntax,
316 driver_args => $s->param('driver'),
319 dependencies => [ depends_on('local_files') ],
323 sub build_config_path_service {
326 return Bread::Board::BlockInjection->new(
327 name => 'config_path',
331 my $path = $s->param('path');
332 my $prefix = $s->param('prefix');
334 my ( $extension ) = ( $path =~ m{\.(.{1,4})$} );
337 $path =~ s{[\/\\]$}{};
341 return [ $path, $extension ];
343 dependencies => [ depends_on('prefix'), depends_on('path') ],
347 sub build_config_local_suffix_service {
350 return Bread::Board::BlockInjection->new(
351 name => 'config_local_suffix',
354 my $suffix = Catalyst::Utils::env_value( $s->param('name'), 'CONFIG_LOCAL_SUFFIX' ) || $self->config_local_suffix;
358 dependencies => [ depends_on('name') ],
366 prefix => $_ eq 'Component' ? '' : $_ . '::',
367 values => delete $config->{ lc $_ } || delete $config->{ $_ }
369 grep { ref $config->{ lc $_ } || ref $config->{ $_ } }
370 qw( Component Model M View V Controller C Plugin )
373 foreach my $comp ( @components ) {
374 my $prefix = $comp->{ prefix };
375 foreach my $element ( keys %{ $comp->{ values } } ) {
376 $config->{ "$prefix$element" } = $comp->{ values }->{ $element };
381 sub _config_substitutions {
382 my ( $self, $name, $subs, $arg ) = @_;
384 $subs->{ HOME } ||= sub { shift->path_to( '' ); };
388 if (! defined($ENV{$v})) {
389 Catalyst::Exception->throw( message =>
390 "Missing environment variable: $v" );
396 $subs->{ path_to } ||= sub { shift->path_to( @_ ); };
397 $subs->{ literal } ||= sub { return $_[ 1 ]; };
398 my $subsre = join( '|', keys %$subs );
400 $arg =~ s{__($subsre)(?:\((.+?)\))?__}{ $subs->{ $1 }->( $name, $2 ? split( /,/, $2 ) : () ) }eg;
404 sub get_component_from_sub_container {
405 my ( $self, $sub_container_name, $name, $c, @args ) = @_;
407 my $sub_container = $self->get_sub_container( $sub_container_name );
410 my $default_name = 'default_' . $sub_container_name;
411 my $default = $self->resolve( service => $default_name )
412 if $self->has_service($default_name);
414 return $sub_container->get_component( $default, $c, @args )
415 if $default && $sub_container->has_service( $default );
417 # this is never a controller, so this is safe
418 $c->log->warn( "Calling \$c->$sub_container_name() is not supported unless you specify one of:" );
419 $c->log->warn( "* \$c->config(default_$sub_container_name => 'the name of the default $sub_container_name to use')" );
420 $c->log->warn( "* \$c->stash->{current_$sub_container_name} # the name of the view to use for this request" );
421 $c->log->warn( "* \$c->stash->{current_${sub_container_name}_instance} # the instance of the $sub_container_name to use for this request" );
424 return $sub_container->get_component_regexp( $name, $c, @args )
427 return $sub_container->get_component( $name, $c, @args )
428 if $sub_container->has_service( $name );
431 "Attempted to use $sub_container_name '$name', " .
432 "but it does not exist"
446 Catalyst::Container - IOC for Catalyst components
450 =head2 build_model_subcontainer
452 =head2 build_view_subcontainer
454 =head2 build_controller_subcontainer
456 =head2 build_default_model_service
458 =head2 build_default_view_service
460 =head2 build_name_service
462 =head2 build_driver_service
464 =head2 build_file_service
466 =head2 build_substitutions_service
468 =head2 build_extensions_service
470 =head2 build_prefix_service
472 =head2 build_path_service
474 =head2 build_config_service
476 =head2 build_raw_config_service
478 =head2 build_global_files_service
480 =head2 build_local_files_service
482 =head2 build_global_config_service
484 =head2 build_local_config_service
486 =head2 build_config_path_service
488 =head2 build_config_local_suffix_service
492 =head2 _config_substitutions
496 Catalyst Contributors, see Catalyst.pm
500 This library is free software. You can redistribute it and/or modify it under
501 the same terms as Perl itself.