1 package Catalyst::Container;
5 use Data::Visitor::Callback;
6 use Catalyst::Utils ();
7 use MooseX::Types::LoadableClass qw/ LoadableClass /;
8 use Catalyst::BlockInjection;
9 use namespace::autoclean;
11 extends 'Bread::Board::Container';
13 # FIXME - Why do any of these attributes need to be rw?
14 has config_local_suffix => (
23 default => sub { +{} },
32 has substitutions => (
35 default => sub { +{} },
44 has sub_container_class => (
48 default => 'Catalyst::SubContainer',
50 new_sub_container => 'new',
54 # FIXME - Move all the Pod to the bottom!
58 Catalyst::Container - IOC for Catalyst components
68 $self->${\"build_${_}_service"}
87 $self->add_sub_container(
88 $self->${ \"build_${_}_subcontainer" }
89 ) for qw/ model view controller /;
92 =head2 build_model_subcontainer
96 sub build_model_subcontainer {
99 return $self->new_sub_container( name => 'model' );
102 =head2 build_view_subcontainer
106 sub build_view_subcontainer {
109 return $self->new_sub_container( name => 'view' );
112 =head2 build_controller_subcontainer
116 sub build_controller_subcontainer {
119 return $self->new_sub_container( name => 'controller' );
122 =head2 build_name_service
126 sub build_name_service {
129 return Bread::Board::Literal->new( name => 'name', value => $self->name );
132 =head2 build_driver_service
136 sub build_driver_service {
139 return Bread::Board::Literal->new( name => 'driver', value => $self->driver );
142 =head2 build_file_service
146 sub build_file_service {
149 return Bread::Board::Literal->new( name => 'file', value => $self->file );
152 =head2 build_substitutions_service
156 sub build_substitutions_service {
159 return Bread::Board::Literal->new( name => 'substitutions', value => $self->substitutions );
162 =head2 build_extensions_service
166 sub build_extensions_service {
169 return Bread::Board::BlockInjection->new(
170 name => 'extensions',
172 return \@{Config::Any->extensions};
177 =head2 build_prefix_service
181 sub build_prefix_service {
184 return Bread::Board::BlockInjection->new(
187 return Catalyst::Utils::appprefix( shift->param('name') );
189 dependencies => [ depends_on('name') ],
193 =head2 build_path_service
197 sub build_path_service {
200 return Bread::Board::BlockInjection->new(
205 return Catalyst::Utils::env_value( $s->param('name'), 'CONFIG' )
207 || $s->param('name')->path_to( $s->param('prefix') );
209 dependencies => [ depends_on('file'), depends_on('name'), depends_on('prefix') ],
213 =head2 build_config_service
217 sub build_config_service {
220 return Bread::Board::BlockInjection->new(
225 my $v = Data::Visitor::Callback->new(
227 return unless defined $_;
228 return $self->_config_substitutions( $s->param('name'), $s->param('substitutions'), $_ );
232 $v->visit( $s->param('raw_config') );
234 dependencies => [ depends_on('name'), depends_on('raw_config'), depends_on('substitutions') ],
238 =head2 build_raw_config_service
242 sub build_raw_config_service {
245 return Bread::Board::BlockInjection->new(
246 name => 'raw_config',
250 my @global = @{$s->param('global_config')};
251 my @locals = @{$s->param('local_config')};
254 for my $cfg (@global, @locals) {
256 $config = Catalyst::Utils::merge_hashes( $config, $cfg->{$_} );
261 dependencies => [ depends_on('global_config'), depends_on('local_config') ],
265 =head2 build_global_files_service
269 sub build_global_files_service {
272 return Bread::Board::BlockInjection->new(
273 name => 'global_files',
277 my ( $path, $extension ) = @{$s->param('config_path')};
279 my @extensions = @{$s->param('extensions')};
283 die "Unable to handle files with the extension '${extension}'" unless grep { $_ eq $extension } @extensions;
286 @files = map { "$path.$_" } @extensions;
290 dependencies => [ depends_on('extensions'), depends_on('config_path') ],
294 =head2 build_local_files_service
298 sub build_local_files_service {
301 return Bread::Board::BlockInjection->new(
302 name => 'local_files',
306 my ( $path, $extension ) = @{$s->param('config_path')};
307 my $suffix = $s->param('config_local_suffix');
309 my @extensions = @{$s->param('extensions')};
313 die "Unable to handle files with the extension '${extension}'" unless grep { $_ eq $extension } @extensions;
314 $path =~ s{\.$extension}{_$suffix.$extension};
317 @files = map { "${path}_${suffix}.$_" } @extensions;
321 dependencies => [ depends_on('extensions'), depends_on('config_path'), depends_on('config_local_suffix') ],
325 =head2 build_global_config_service
329 sub build_global_config_service {
332 return Bread::Board::BlockInjection->new(
333 name => 'global_config',
337 return Config::Any->load_files({
338 files => $s->param('global_files'),
339 filter => \&_fix_syntax,
341 driver_args => $s->param('driver'),
344 dependencies => [ depends_on('global_files') ],
348 =head2 build_local_config_service
352 sub build_local_config_service {
355 return Bread::Board::BlockInjection->new(
356 name => 'local_config',
360 return Config::Any->load_files({
361 files => $s->param('local_files'),
362 filter => \&_fix_syntax,
364 driver_args => $s->param('driver'),
367 dependencies => [ depends_on('local_files') ],
371 =head2 build_config_path_service
375 sub build_config_path_service {
378 return Bread::Board::BlockInjection->new(
379 name => 'config_path',
383 my $path = $s->param('path');
384 my $prefix = $s->param('prefix');
386 my ( $extension ) = ( $path =~ m{\.(.{1,4})$} );
389 $path =~ s{[\/\\]$}{};
393 return [ $path, $extension ];
395 dependencies => [ depends_on('prefix'), depends_on('path') ],
399 =head2 build_config_local_suffix_service
403 sub build_config_local_suffix_service {
406 return Bread::Board::BlockInjection->new(
407 name => 'config_local_suffix',
410 my $suffix = Catalyst::Utils::env_value( $s->param('name'), 'CONFIG_LOCAL_SUFFIX' ) || $self->config_local_suffix;
414 dependencies => [ depends_on('name') ],
426 prefix => $_ eq 'Component' ? '' : $_ . '::',
427 values => delete $config->{ lc $_ } || delete $config->{ $_ }
429 grep { ref $config->{ lc $_ } || ref $config->{ $_ } }
430 qw( Component Model M View V Controller C Plugin )
433 foreach my $comp ( @components ) {
434 my $prefix = $comp->{ prefix };
435 foreach my $element ( keys %{ $comp->{ values } } ) {
436 $config->{ "$prefix$element" } = $comp->{ values }->{ $element };
441 =head2 _config_substitutions
445 sub _config_substitutions {
446 my ( $self, $name, $subs, $arg ) = @_;
448 $subs->{ HOME } ||= sub { shift->path_to( '' ); };
452 if (! defined($ENV{$v})) {
453 Catalyst::Exception->throw( message =>
454 "Missing environment variable: $v" );
460 $subs->{ path_to } ||= sub { shift->path_to( @_ ); };
461 $subs->{ literal } ||= sub { return $_[ 1 ]; };
462 my $subsre = join( '|', keys %$subs );
464 $arg =~ s{__($subsre)(?:\((.+?)\))?__}{ $subs->{ $1 }->( $name, $2 ? split( /,/, $2 ) : () ) }eg;
470 Catalyst Contributors, see Catalyst.pm
474 This library is free software. You can redistribute it and/or modify it under
475 the same terms as Perl itself.