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_name_service {
108 return Bread::Board::Literal->new( name => 'name', value => $self->name );
111 sub build_driver_service {
114 return Bread::Board::Literal->new( name => 'driver', value => $self->driver );
117 sub build_file_service {
120 return Bread::Board::Literal->new( name => 'file', value => $self->file );
123 sub build_substitutions_service {
126 return Bread::Board::Literal->new( name => 'substitutions', value => $self->substitutions );
129 sub build_extensions_service {
132 return Bread::Board::BlockInjection->new(
133 name => 'extensions',
135 return \@{Config::Any->extensions};
140 sub build_prefix_service {
143 return Bread::Board::BlockInjection->new(
146 return Catalyst::Utils::appprefix( shift->param('name') );
148 dependencies => [ depends_on('name') ],
152 sub build_path_service {
155 return Bread::Board::BlockInjection->new(
160 return Catalyst::Utils::env_value( $s->param('name'), 'CONFIG' )
162 || $s->param('name')->path_to( $s->param('prefix') );
164 dependencies => [ depends_on('file'), depends_on('name'), depends_on('prefix') ],
168 sub build_config_service {
171 return Bread::Board::BlockInjection->new(
176 my $v = Data::Visitor::Callback->new(
178 return unless defined $_;
179 return $self->_config_substitutions( $s->param('name'), $s->param('substitutions'), $_ );
183 $v->visit( $s->param('raw_config') );
185 dependencies => [ depends_on('name'), depends_on('raw_config'), depends_on('substitutions') ],
189 sub build_raw_config_service {
192 return Bread::Board::BlockInjection->new(
193 name => 'raw_config',
197 my @global = @{$s->param('global_config')};
198 my @locals = @{$s->param('local_config')};
201 for my $cfg (@global, @locals) {
203 $config = Catalyst::Utils::merge_hashes( $config, $cfg->{$_} );
208 dependencies => [ depends_on('global_config'), depends_on('local_config') ],
212 sub build_global_files_service {
215 return Bread::Board::BlockInjection->new(
216 name => 'global_files',
220 my ( $path, $extension ) = @{$s->param('config_path')};
222 my @extensions = @{$s->param('extensions')};
226 die "Unable to handle files with the extension '${extension}'" unless grep { $_ eq $extension } @extensions;
229 @files = map { "$path.$_" } @extensions;
233 dependencies => [ depends_on('extensions'), depends_on('config_path') ],
237 sub build_local_files_service {
240 return Bread::Board::BlockInjection->new(
241 name => 'local_files',
245 my ( $path, $extension ) = @{$s->param('config_path')};
246 my $suffix = $s->param('config_local_suffix');
248 my @extensions = @{$s->param('extensions')};
252 die "Unable to handle files with the extension '${extension}'" unless grep { $_ eq $extension } @extensions;
253 $path =~ s{\.$extension}{_$suffix.$extension};
256 @files = map { "${path}_${suffix}.$_" } @extensions;
260 dependencies => [ depends_on('extensions'), depends_on('config_path'), depends_on('config_local_suffix') ],
264 sub build_global_config_service {
267 return Bread::Board::BlockInjection->new(
268 name => 'global_config',
272 return Config::Any->load_files({
273 files => $s->param('global_files'),
274 filter => \&_fix_syntax,
276 driver_args => $s->param('driver'),
279 dependencies => [ depends_on('global_files') ],
283 sub build_local_config_service {
286 return Bread::Board::BlockInjection->new(
287 name => 'local_config',
291 return Config::Any->load_files({
292 files => $s->param('local_files'),
293 filter => \&_fix_syntax,
295 driver_args => $s->param('driver'),
298 dependencies => [ depends_on('local_files') ],
302 sub build_config_path_service {
305 return Bread::Board::BlockInjection->new(
306 name => 'config_path',
310 my $path = $s->param('path');
311 my $prefix = $s->param('prefix');
313 my ( $extension ) = ( $path =~ m{\.(.{1,4})$} );
316 $path =~ s{[\/\\]$}{};
320 return [ $path, $extension ];
322 dependencies => [ depends_on('prefix'), depends_on('path') ],
326 sub build_config_local_suffix_service {
329 return Bread::Board::BlockInjection->new(
330 name => 'config_local_suffix',
333 my $suffix = Catalyst::Utils::env_value( $s->param('name'), 'CONFIG_LOCAL_SUFFIX' ) || $self->config_local_suffix;
337 dependencies => [ depends_on('name') ],
345 prefix => $_ eq 'Component' ? '' : $_ . '::',
346 values => delete $config->{ lc $_ } || delete $config->{ $_ }
348 grep { ref $config->{ lc $_ } || ref $config->{ $_ } }
349 qw( Component Model M View V Controller C Plugin )
352 foreach my $comp ( @components ) {
353 my $prefix = $comp->{ prefix };
354 foreach my $element ( keys %{ $comp->{ values } } ) {
355 $config->{ "$prefix$element" } = $comp->{ values }->{ $element };
360 sub _config_substitutions {
361 my ( $self, $name, $subs, $arg ) = @_;
363 $subs->{ HOME } ||= sub { shift->path_to( '' ); };
367 if (! defined($ENV{$v})) {
368 Catalyst::Exception->throw( message =>
369 "Missing environment variable: $v" );
375 $subs->{ path_to } ||= sub { shift->path_to( @_ ); };
376 $subs->{ literal } ||= sub { return $_[ 1 ]; };
377 my $subsre = join( '|', keys %$subs );
379 $arg =~ s{__($subsre)(?:\((.+?)\))?__}{ $subs->{ $1 }->( $name, $2 ? split( /,/, $2 ) : () ) }eg;
383 sub get_component_from_sub_container {
384 my ( $self, $sub_container_name, $name, $c, @args ) = @_;
386 my $sub_container = $self->get_sub_container( $sub_container_name );
388 return $sub_container->get_component_regexp( $name, $c, @args )
391 return $sub_container->get_component( $name, $c, @args )
392 if $sub_container->has_service( $name );
395 "Attempted to use $sub_container_name '$name', " .
396 "but it does not exist"
410 Catalyst::Container - IOC for Catalyst components
414 =head2 build_model_subcontainer
416 =head2 build_view_subcontainer
418 =head2 build_controller_subcontainer
420 =head2 build_name_service
422 =head2 build_driver_service
424 =head2 build_file_service
426 =head2 build_substitutions_service
428 =head2 build_extensions_service
430 =head2 build_prefix_service
432 =head2 build_path_service
434 =head2 build_config_service
436 =head2 build_raw_config_service
438 =head2 build_global_files_service
440 =head2 build_local_files_service
442 =head2 build_global_config_service
444 =head2 build_local_config_service
446 =head2 build_config_path_service
448 =head2 build_config_local_suffix_service
452 =head2 _config_substitutions
456 Catalyst Contributors, see Catalyst.pm
460 This library is free software. You can redistribute it and/or modify it under
461 the same terms as Perl itself.