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',
58 $self->${\"build_${_}_service"}
77 $self->add_sub_container(
78 $self->${ \"build_${_}_subcontainer" }
79 ) for qw/ model view controller /;
82 sub build_model_subcontainer {
85 return $self->new_sub_container( name => 'model' );
88 sub build_view_subcontainer {
91 return $self->new_sub_container( name => 'view' );
94 sub build_controller_subcontainer {
97 return $self->new_sub_container( name => 'controller' );
100 sub build_name_service {
103 return Bread::Board::Literal->new( name => 'name', value => $self->name );
106 sub build_driver_service {
109 return Bread::Board::Literal->new( name => 'driver', value => $self->driver );
112 sub build_file_service {
115 return Bread::Board::Literal->new( name => 'file', value => $self->file );
118 sub build_substitutions_service {
121 return Bread::Board::Literal->new( name => 'substitutions', value => $self->substitutions );
124 sub build_extensions_service {
127 return Bread::Board::BlockInjection->new(
128 name => 'extensions',
130 return \@{Config::Any->extensions};
135 sub build_prefix_service {
138 return Bread::Board::BlockInjection->new(
141 return Catalyst::Utils::appprefix( shift->param('name') );
143 dependencies => [ depends_on('name') ],
147 sub build_path_service {
150 return Bread::Board::BlockInjection->new(
155 return Catalyst::Utils::env_value( $s->param('name'), 'CONFIG' )
157 || $s->param('name')->path_to( $s->param('prefix') );
159 dependencies => [ depends_on('file'), depends_on('name'), depends_on('prefix') ],
163 sub build_config_service {
166 return Bread::Board::BlockInjection->new(
171 my $v = Data::Visitor::Callback->new(
173 return unless defined $_;
174 return $self->_config_substitutions( $s->param('name'), $s->param('substitutions'), $_ );
178 $v->visit( $s->param('raw_config') );
180 dependencies => [ depends_on('name'), depends_on('raw_config'), depends_on('substitutions') ],
184 sub build_raw_config_service {
187 return Bread::Board::BlockInjection->new(
188 name => 'raw_config',
192 my @global = @{$s->param('global_config')};
193 my @locals = @{$s->param('local_config')};
196 for my $cfg (@global, @locals) {
198 $config = Catalyst::Utils::merge_hashes( $config, $cfg->{$_} );
203 dependencies => [ depends_on('global_config'), depends_on('local_config') ],
207 sub build_global_files_service {
210 return Bread::Board::BlockInjection->new(
211 name => 'global_files',
215 my ( $path, $extension ) = @{$s->param('config_path')};
217 my @extensions = @{$s->param('extensions')};
221 die "Unable to handle files with the extension '${extension}'" unless grep { $_ eq $extension } @extensions;
224 @files = map { "$path.$_" } @extensions;
228 dependencies => [ depends_on('extensions'), depends_on('config_path') ],
232 sub build_local_files_service {
235 return Bread::Board::BlockInjection->new(
236 name => 'local_files',
240 my ( $path, $extension ) = @{$s->param('config_path')};
241 my $suffix = $s->param('config_local_suffix');
243 my @extensions = @{$s->param('extensions')};
247 die "Unable to handle files with the extension '${extension}'" unless grep { $_ eq $extension } @extensions;
248 $path =~ s{\.$extension}{_$suffix.$extension};
251 @files = map { "${path}_${suffix}.$_" } @extensions;
255 dependencies => [ depends_on('extensions'), depends_on('config_path'), depends_on('config_local_suffix') ],
259 sub build_global_config_service {
262 return Bread::Board::BlockInjection->new(
263 name => 'global_config',
267 return Config::Any->load_files({
268 files => $s->param('global_files'),
269 filter => \&_fix_syntax,
271 driver_args => $s->param('driver'),
274 dependencies => [ depends_on('global_files') ],
278 sub build_local_config_service {
281 return Bread::Board::BlockInjection->new(
282 name => 'local_config',
286 return Config::Any->load_files({
287 files => $s->param('local_files'),
288 filter => \&_fix_syntax,
290 driver_args => $s->param('driver'),
293 dependencies => [ depends_on('local_files') ],
297 sub build_config_path_service {
300 return Bread::Board::BlockInjection->new(
301 name => 'config_path',
305 my $path = $s->param('path');
306 my $prefix = $s->param('prefix');
308 my ( $extension ) = ( $path =~ m{\.(.{1,4})$} );
311 $path =~ s{[\/\\]$}{};
315 return [ $path, $extension ];
317 dependencies => [ depends_on('prefix'), depends_on('path') ],
321 sub build_config_local_suffix_service {
324 return Bread::Board::BlockInjection->new(
325 name => 'config_local_suffix',
328 my $suffix = Catalyst::Utils::env_value( $s->param('name'), 'CONFIG_LOCAL_SUFFIX' ) || $self->config_local_suffix;
332 dependencies => [ depends_on('name') ],
340 prefix => $_ eq 'Component' ? '' : $_ . '::',
341 values => delete $config->{ lc $_ } || delete $config->{ $_ }
343 grep { ref $config->{ lc $_ } || ref $config->{ $_ } }
344 qw( Component Model M View V Controller C Plugin )
347 foreach my $comp ( @components ) {
348 my $prefix = $comp->{ prefix };
349 foreach my $element ( keys %{ $comp->{ values } } ) {
350 $config->{ "$prefix$element" } = $comp->{ values }->{ $element };
355 sub _config_substitutions {
356 my ( $self, $name, $subs, $arg ) = @_;
358 $subs->{ HOME } ||= sub { shift->path_to( '' ); };
362 if (! defined($ENV{$v})) {
363 Catalyst::Exception->throw( message =>
364 "Missing environment variable: $v" );
370 $subs->{ path_to } ||= sub { shift->path_to( @_ ); };
371 $subs->{ literal } ||= sub { return $_[ 1 ]; };
372 my $subsre = join( '|', keys %$subs );
374 $arg =~ s{__($subsre)(?:\((.+?)\))?__}{ $subs->{ $1 }->( $name, $2 ? split( /,/, $2 ) : () ) }eg;
386 Catalyst::Container - IOC for Catalyst components
390 =head2 build_model_subcontainer
392 =head2 build_view_subcontainer
394 =head2 build_controller_subcontainer
396 =head2 build_name_service
398 =head2 build_driver_service
400 =head2 build_file_service
402 =head2 build_substitutions_service
404 =head2 build_extensions_service
406 =head2 build_prefix_service
408 =head2 build_path_service
410 =head2 build_config_service
412 =head2 build_raw_config_service
414 =head2 build_global_files_service
416 =head2 build_local_files_service
418 =head2 build_global_config_service
420 =head2 build_local_config_service
422 =head2 build_config_path_service
424 =head2 build_config_local_suffix_service
428 =head2 _config_substitutions
432 Catalyst Contributors, see Catalyst.pm
436 This library is free software. You can redistribute it and/or modify it under
437 the same terms as Perl itself.