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 disable_regex_fallback => (
19 has config_local_suffix => (
28 default => sub { +{} },
37 has substitutions => (
40 default => sub { +{} },
49 has sub_container_class => (
53 default => 'Catalyst::IOC::SubContainer',
55 new_sub_container => 'new',
63 $self->${\"build_${_}_service"}
82 $self->add_sub_container(
83 $self->${ \"build_${_}_subcontainer" }
84 ) for qw/ model view controller /;
87 sub build_model_subcontainer {
90 return $self->new_sub_container(
92 disable_regex_fallback => $self->disable_regex_fallback,
96 sub build_view_subcontainer {
99 return $self->new_sub_container(
101 disable_regex_fallback => $self->disable_regex_fallback,
105 sub build_controller_subcontainer {
108 return $self->new_sub_container(
109 name => 'controller',
110 disable_regex_fallback => $self->disable_regex_fallback,
114 sub build_name_service {
117 return Bread::Board::Literal->new( name => 'name', value => $self->name );
120 sub build_driver_service {
123 return Bread::Board::Literal->new( name => 'driver', value => $self->driver );
126 sub build_file_service {
129 return Bread::Board::Literal->new( name => 'file', value => $self->file );
132 sub build_substitutions_service {
135 return Bread::Board::Literal->new( name => 'substitutions', value => $self->substitutions );
138 sub build_extensions_service {
141 return Bread::Board::BlockInjection->new(
142 name => 'extensions',
144 return \@{Config::Any->extensions};
149 sub build_prefix_service {
152 return Bread::Board::BlockInjection->new(
155 return Catalyst::Utils::appprefix( shift->param('name') );
157 dependencies => [ depends_on('name') ],
161 sub build_path_service {
164 return Bread::Board::BlockInjection->new(
169 return Catalyst::Utils::env_value( $s->param('name'), 'CONFIG' )
171 || $s->param('name')->path_to( $s->param('prefix') );
173 dependencies => [ depends_on('file'), depends_on('name'), depends_on('prefix') ],
177 sub build_config_service {
180 return Bread::Board::BlockInjection->new(
185 my $v = Data::Visitor::Callback->new(
187 return unless defined $_;
188 return $self->_config_substitutions( $s->param('name'), $s->param('substitutions'), $_ );
192 $v->visit( $s->param('raw_config') );
194 dependencies => [ depends_on('name'), depends_on('raw_config'), depends_on('substitutions') ],
198 sub build_raw_config_service {
201 return Bread::Board::BlockInjection->new(
202 name => 'raw_config',
206 my @global = @{$s->param('global_config')};
207 my @locals = @{$s->param('local_config')};
210 for my $cfg (@global, @locals) {
212 $config = Catalyst::Utils::merge_hashes( $config, $cfg->{$_} );
217 dependencies => [ depends_on('global_config'), depends_on('local_config') ],
221 sub build_global_files_service {
224 return Bread::Board::BlockInjection->new(
225 name => 'global_files',
229 my ( $path, $extension ) = @{$s->param('config_path')};
231 my @extensions = @{$s->param('extensions')};
235 die "Unable to handle files with the extension '${extension}'" unless grep { $_ eq $extension } @extensions;
238 @files = map { "$path.$_" } @extensions;
242 dependencies => [ depends_on('extensions'), depends_on('config_path') ],
246 sub build_local_files_service {
249 return Bread::Board::BlockInjection->new(
250 name => 'local_files',
254 my ( $path, $extension ) = @{$s->param('config_path')};
255 my $suffix = $s->param('config_local_suffix');
257 my @extensions = @{$s->param('extensions')};
261 die "Unable to handle files with the extension '${extension}'" unless grep { $_ eq $extension } @extensions;
262 $path =~ s{\.$extension}{_$suffix.$extension};
265 @files = map { "${path}_${suffix}.$_" } @extensions;
269 dependencies => [ depends_on('extensions'), depends_on('config_path'), depends_on('config_local_suffix') ],
273 sub build_global_config_service {
276 return Bread::Board::BlockInjection->new(
277 name => 'global_config',
281 return Config::Any->load_files({
282 files => $s->param('global_files'),
283 filter => \&_fix_syntax,
285 driver_args => $s->param('driver'),
288 dependencies => [ depends_on('global_files') ],
292 sub build_local_config_service {
295 return Bread::Board::BlockInjection->new(
296 name => 'local_config',
300 return Config::Any->load_files({
301 files => $s->param('local_files'),
302 filter => \&_fix_syntax,
304 driver_args => $s->param('driver'),
307 dependencies => [ depends_on('local_files') ],
311 sub build_config_path_service {
314 return Bread::Board::BlockInjection->new(
315 name => 'config_path',
319 my $path = $s->param('path');
320 my $prefix = $s->param('prefix');
322 my ( $extension ) = ( $path =~ m{\.(.{1,4})$} );
325 $path =~ s{[\/\\]$}{};
329 return [ $path, $extension ];
331 dependencies => [ depends_on('prefix'), depends_on('path') ],
335 sub build_config_local_suffix_service {
338 return Bread::Board::BlockInjection->new(
339 name => 'config_local_suffix',
342 my $suffix = Catalyst::Utils::env_value( $s->param('name'), 'CONFIG_LOCAL_SUFFIX' ) || $self->config_local_suffix;
346 dependencies => [ depends_on('name') ],
354 prefix => $_ eq 'Component' ? '' : $_ . '::',
355 values => delete $config->{ lc $_ } || delete $config->{ $_ }
357 grep { ref $config->{ lc $_ } || ref $config->{ $_ } }
358 qw( Component Model M View V Controller C Plugin )
361 foreach my $comp ( @components ) {
362 my $prefix = $comp->{ prefix };
363 foreach my $element ( keys %{ $comp->{ values } } ) {
364 $config->{ "$prefix$element" } = $comp->{ values }->{ $element };
369 sub _config_substitutions {
370 my ( $self, $name, $subs, $arg ) = @_;
372 $subs->{ HOME } ||= sub { shift->path_to( '' ); };
376 if (! defined($ENV{$v})) {
377 Catalyst::Exception->throw( message =>
378 "Missing environment variable: $v" );
384 $subs->{ path_to } ||= sub { shift->path_to( @_ ); };
385 $subs->{ literal } ||= sub { return $_[ 1 ]; };
386 my $subsre = join( '|', keys %$subs );
388 $arg =~ s{__($subsre)(?:\((.+?)\))?__}{ $subs->{ $1 }->( $name, $2 ? split( /,/, $2 ) : () ) }eg;
400 Catalyst::Container - IOC for Catalyst components
404 =head2 build_model_subcontainer
406 =head2 build_view_subcontainer
408 =head2 build_controller_subcontainer
410 =head2 build_name_service
412 =head2 build_driver_service
414 =head2 build_file_service
416 =head2 build_substitutions_service
418 =head2 build_extensions_service
420 =head2 build_prefix_service
422 =head2 build_path_service
424 =head2 build_config_service
426 =head2 build_raw_config_service
428 =head2 build_global_files_service
430 =head2 build_local_files_service
432 =head2 build_global_config_service
434 =head2 build_local_config_service
436 =head2 build_config_path_service
438 =head2 build_config_local_suffix_service
442 =head2 _config_substitutions
446 Catalyst Contributors, see Catalyst.pm
450 This library is free software. You can redistribute it and/or modify it under
451 the same terms as Perl itself.