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( name => 'model' );
87 sub build_view_subcontainer {
90 return $self->new_sub_container( name => 'view' );
93 sub build_controller_subcontainer {
96 return $self->new_sub_container( name => 'controller' );
99 sub build_name_service {
102 return Bread::Board::Literal->new( name => 'name', value => $self->name );
105 sub build_driver_service {
108 return Bread::Board::Literal->new( name => 'driver', value => $self->driver );
111 sub build_file_service {
114 return Bread::Board::Literal->new( name => 'file', value => $self->file );
117 sub build_substitutions_service {
120 return Bread::Board::Literal->new( name => 'substitutions', value => $self->substitutions );
123 sub build_extensions_service {
126 return Bread::Board::BlockInjection->new(
127 name => 'extensions',
129 return \@{Config::Any->extensions};
134 sub build_prefix_service {
137 return Bread::Board::BlockInjection->new(
140 return Catalyst::Utils::appprefix( shift->param('name') );
142 dependencies => [ depends_on('name') ],
146 sub build_path_service {
149 return Bread::Board::BlockInjection->new(
154 return Catalyst::Utils::env_value( $s->param('name'), 'CONFIG' )
156 || $s->param('name')->path_to( $s->param('prefix') );
158 dependencies => [ depends_on('file'), depends_on('name'), depends_on('prefix') ],
162 sub build_config_service {
165 return Bread::Board::BlockInjection->new(
170 my $v = Data::Visitor::Callback->new(
172 return unless defined $_;
173 return $self->_config_substitutions( $s->param('name'), $s->param('substitutions'), $_ );
177 $v->visit( $s->param('raw_config') );
179 dependencies => [ depends_on('name'), depends_on('raw_config'), depends_on('substitutions') ],
183 sub build_raw_config_service {
186 return Bread::Board::BlockInjection->new(
187 name => 'raw_config',
191 my @global = @{$s->param('global_config')};
192 my @locals = @{$s->param('local_config')};
195 for my $cfg (@global, @locals) {
197 $config = Catalyst::Utils::merge_hashes( $config, $cfg->{$_} );
202 dependencies => [ depends_on('global_config'), depends_on('local_config') ],
206 sub build_global_files_service {
209 return Bread::Board::BlockInjection->new(
210 name => 'global_files',
214 my ( $path, $extension ) = @{$s->param('config_path')};
216 my @extensions = @{$s->param('extensions')};
220 die "Unable to handle files with the extension '${extension}'" unless grep { $_ eq $extension } @extensions;
223 @files = map { "$path.$_" } @extensions;
227 dependencies => [ depends_on('extensions'), depends_on('config_path') ],
231 sub build_local_files_service {
234 return Bread::Board::BlockInjection->new(
235 name => 'local_files',
239 my ( $path, $extension ) = @{$s->param('config_path')};
240 my $suffix = $s->param('config_local_suffix');
242 my @extensions = @{$s->param('extensions')};
246 die "Unable to handle files with the extension '${extension}'" unless grep { $_ eq $extension } @extensions;
247 $path =~ s{\.$extension}{_$suffix.$extension};
250 @files = map { "${path}_${suffix}.$_" } @extensions;
254 dependencies => [ depends_on('extensions'), depends_on('config_path'), depends_on('config_local_suffix') ],
258 sub build_global_config_service {
261 return Bread::Board::BlockInjection->new(
262 name => 'global_config',
266 return Config::Any->load_files({
267 files => $s->param('global_files'),
268 filter => \&_fix_syntax,
270 driver_args => $s->param('driver'),
273 dependencies => [ depends_on('global_files') ],
277 sub build_local_config_service {
280 return Bread::Board::BlockInjection->new(
281 name => 'local_config',
285 return Config::Any->load_files({
286 files => $s->param('local_files'),
287 filter => \&_fix_syntax,
289 driver_args => $s->param('driver'),
292 dependencies => [ depends_on('local_files') ],
296 sub build_config_path_service {
299 return Bread::Board::BlockInjection->new(
300 name => 'config_path',
304 my $path = $s->param('path');
305 my $prefix = $s->param('prefix');
307 my ( $extension ) = ( $path =~ m{\.(.{1,4})$} );
310 $path =~ s{[\/\\]$}{};
314 return [ $path, $extension ];
316 dependencies => [ depends_on('prefix'), depends_on('path') ],
320 sub build_config_local_suffix_service {
323 return Bread::Board::BlockInjection->new(
324 name => 'config_local_suffix',
327 my $suffix = Catalyst::Utils::env_value( $s->param('name'), 'CONFIG_LOCAL_SUFFIX' ) || $self->config_local_suffix;
331 dependencies => [ depends_on('name') ],
339 prefix => $_ eq 'Component' ? '' : $_ . '::',
340 values => delete $config->{ lc $_ } || delete $config->{ $_ }
342 grep { ref $config->{ lc $_ } || ref $config->{ $_ } }
343 qw( Component Model M View V Controller C Plugin )
346 foreach my $comp ( @components ) {
347 my $prefix = $comp->{ prefix };
348 foreach my $element ( keys %{ $comp->{ values } } ) {
349 $config->{ "$prefix$element" } = $comp->{ values }->{ $element };
354 sub _config_substitutions {
355 my ( $self, $name, $subs, $arg ) = @_;
357 $subs->{ HOME } ||= sub { shift->path_to( '' ); };
361 if (! defined($ENV{$v})) {
362 Catalyst::Exception->throw( message =>
363 "Missing environment variable: $v" );
369 $subs->{ path_to } ||= sub { shift->path_to( @_ ); };
370 $subs->{ literal } ||= sub { return $_[ 1 ]; };
371 my $subsre = join( '|', keys %$subs );
373 $arg =~ s{__($subsre)(?:\((.+?)\))?__}{ $subs->{ $1 }->( $name, $2 ? split( /,/, $2 ) : () ) }eg;
385 Catalyst::Container - IOC for Catalyst components
389 =head2 build_model_subcontainer
391 =head2 build_view_subcontainer
393 =head2 build_controller_subcontainer
395 =head2 build_name_service
397 =head2 build_driver_service
399 =head2 build_file_service
401 =head2 build_substitutions_service
403 =head2 build_extensions_service
405 =head2 build_prefix_service
407 =head2 build_path_service
409 =head2 build_config_service
411 =head2 build_raw_config_service
413 =head2 build_global_files_service
415 =head2 build_local_files_service
417 =head2 build_global_config_service
419 =head2 build_local_config_service
421 =head2 build_config_path_service
423 =head2 build_config_local_suffix_service
427 =head2 _config_substitutions
431 Catalyst Contributors, see Catalyst.pm
435 This library is free software. You can redistribute it and/or modify it under
436 the same terms as Perl itself.