1 package Catalyst::IOC::Container;
5 use Data::Visitor::Callback;
6 use Catalyst::Utils ();
7 use Hash::Util qw/lock_hash/;
8 use MooseX::Types::LoadableClass qw/ LoadableClass /;
9 use Catalyst::IOC::BlockInjection;
10 use namespace::autoclean;
12 extends 'Bread::Board::Container';
14 has config_local_suffix => (
23 default => sub { +{} },
32 has substitutions => (
35 default => sub { +{} },
44 has sub_container_class => (
48 default => 'Catalyst::IOC::SubContainer',
50 new_sub_container => 'new',
55 my ( $self, $params ) = @_;
58 $self->${\"build_${_}_service"}
77 $self->add_sub_container(
78 $self->build_controller_subcontainer
81 my $config = $self->resolve( service => 'config' );
82 my $default_view = $params->{default_view} || $config->{default_view};
83 my $default_model = $params->{default_model} || $config->{default_model};
85 $self->add_sub_container(
86 $self->build_view_subcontainer(
87 default_component => $default_view,
91 $self->add_sub_container(
92 $self->build_model_subcontainer(
93 default_component => $default_model,
98 sub build_model_subcontainer {
101 return $self->new_sub_container( @_,
106 sub build_view_subcontainer {
109 return $self->new_sub_container( @_,
114 sub build_controller_subcontainer {
117 return $self->new_sub_container(
118 name => 'controller',
122 sub build_name_service {
125 return Bread::Board::Literal->new( name => 'name', value => $self->name );
128 sub build_driver_service {
131 return Bread::Board::Literal->new( name => 'driver', value => $self->driver );
134 sub build_file_service {
137 return Bread::Board::Literal->new( name => 'file', value => $self->file );
140 sub build_substitutions_service {
143 return Bread::Board::Literal->new( name => 'substitutions', value => $self->substitutions );
146 sub build_extensions_service {
149 return Bread::Board::BlockInjection->new(
150 name => 'extensions',
152 return \@{Config::Any->extensions};
157 sub build_prefix_service {
160 return Bread::Board::BlockInjection->new(
163 return Catalyst::Utils::appprefix( shift->param('name') );
165 dependencies => [ depends_on('name') ],
169 sub build_path_service {
172 return Bread::Board::BlockInjection->new(
177 return Catalyst::Utils::env_value( $s->param('name'), 'CONFIG' )
179 || $s->param('name')->path_to( $s->param('prefix') );
181 dependencies => [ depends_on('file'), depends_on('name'), depends_on('prefix') ],
185 sub build_config_service {
188 return Bread::Board::BlockInjection->new(
193 my $v = Data::Visitor::Callback->new(
195 return unless defined $_;
196 return $self->_config_substitutions( $s->param('name'), $s->param('substitutions'), $_ );
200 $v->visit( $s->param('raw_config') );
202 dependencies => [ depends_on('name'), depends_on('raw_config'), depends_on('substitutions') ],
206 sub build_raw_config_service {
209 return Bread::Board::BlockInjection->new(
210 name => 'raw_config',
214 my @global = @{$s->param('global_config')};
215 my @locals = @{$s->param('local_config')};
218 for my $cfg (@global, @locals) {
220 $config = Catalyst::Utils::merge_hashes( $config, $cfg->{$_} );
225 dependencies => [ depends_on('global_config'), depends_on('local_config') ],
229 sub build_global_files_service {
232 return Bread::Board::BlockInjection->new(
233 name => 'global_files',
237 my ( $path, $extension ) = @{$s->param('config_path')};
239 my @extensions = @{$s->param('extensions')};
243 die "Unable to handle files with the extension '${extension}'" unless grep { $_ eq $extension } @extensions;
246 @files = map { "$path.$_" } @extensions;
250 dependencies => [ depends_on('extensions'), depends_on('config_path') ],
254 sub build_local_files_service {
257 return Bread::Board::BlockInjection->new(
258 name => 'local_files',
262 my ( $path, $extension ) = @{$s->param('config_path')};
263 my $suffix = $s->param('config_local_suffix');
265 my @extensions = @{$s->param('extensions')};
269 die "Unable to handle files with the extension '${extension}'" unless grep { $_ eq $extension } @extensions;
270 $path =~ s{\.$extension}{_$suffix.$extension};
273 @files = map { "${path}_${suffix}.$_" } @extensions;
277 dependencies => [ depends_on('extensions'), depends_on('config_path'), depends_on('config_local_suffix') ],
281 sub build_global_config_service {
284 return Bread::Board::BlockInjection->new(
285 name => 'global_config',
289 return Config::Any->load_files({
290 files => $s->param('global_files'),
291 filter => \&_fix_syntax,
293 driver_args => $s->param('driver'),
296 dependencies => [ depends_on('global_files') ],
300 sub build_local_config_service {
303 return Bread::Board::BlockInjection->new(
304 name => 'local_config',
308 return Config::Any->load_files({
309 files => $s->param('local_files'),
310 filter => \&_fix_syntax,
312 driver_args => $s->param('driver'),
315 dependencies => [ depends_on('local_files') ],
319 sub build_config_path_service {
322 return Bread::Board::BlockInjection->new(
323 name => 'config_path',
327 my $path = $s->param('path');
328 my $prefix = $s->param('prefix');
330 my ( $extension ) = ( $path =~ m{\.(.{1,4})$} );
333 $path =~ s{[\/\\]$}{};
337 return [ $path, $extension ];
339 dependencies => [ depends_on('prefix'), depends_on('path') ],
343 sub build_config_local_suffix_service {
346 return Bread::Board::BlockInjection->new(
347 name => 'config_local_suffix',
350 my $suffix = Catalyst::Utils::env_value( $s->param('name'), 'CONFIG_LOCAL_SUFFIX' ) || $self->config_local_suffix;
354 dependencies => [ depends_on('name') ],
362 prefix => $_ eq 'Component' ? '' : $_ . '::',
363 values => delete $config->{ lc $_ } || delete $config->{ $_ }
365 grep { ref $config->{ lc $_ } || ref $config->{ $_ } }
366 qw( Component Model M View V Controller C Plugin )
369 foreach my $comp ( @components ) {
370 my $prefix = $comp->{ prefix };
371 foreach my $element ( keys %{ $comp->{ values } } ) {
372 $config->{ "$prefix$element" } = $comp->{ values }->{ $element };
377 sub _config_substitutions {
378 my ( $self, $name, $subs, $arg ) = @_;
380 $subs->{ HOME } ||= sub { shift->path_to( '' ); };
384 if (! defined($ENV{$v})) {
385 Catalyst::Exception->throw( message =>
386 "Missing environment variable: $v" );
392 $subs->{ path_to } ||= sub { shift->path_to( @_ ); };
393 $subs->{ literal } ||= sub { return $_[ 1 ]; };
394 my $subsre = join( '|', keys %$subs );
396 $arg =~ s{__($subsre)(?:\((.+?)\))?__}{ $subs->{ $1 }->( $name, $2 ? split( /,/, $2 ) : () ) }eg;
400 sub get_component_from_sub_container {
401 my ( $self, $sub_container_name, $name, $c, @args ) = @_;
403 my $sub_container = $self->get_sub_container( $sub_container_name );
406 my $default = $sub_container->default_component;
408 return $sub_container->get_component( $default, $c, @args )
409 if $default && $sub_container->has_service( $default );
411 # this is never a controller, so this is safe
412 $c->log->warn( "Calling \$c->$sub_container_name() is not supported unless you specify one of:" );
413 $c->log->warn( "* \$c->config(default_$sub_container_name => 'the name of the default $sub_container_name to use')" );
414 $c->log->warn( "* \$c->stash->{current_$sub_container_name} # the name of the view to use for this request" );
415 $c->log->warn( "* \$c->stash->{current_${sub_container_name}_instance} # the instance of the $sub_container_name to use for this request" );
420 return $sub_container->get_component_regexp( $name, $c, @args )
423 return $sub_container->get_component( $name, $c, @args )
424 if $sub_container->has_service( $name );
427 "Attempted to use $sub_container_name '$name', " .
428 "but it does not exist"
435 my ( $self, $component, @args ) = @_;
438 my $query = ref $component
443 for my $subcontainer_name (qw/model view controller/) {
444 my $subcontainer = $self->get_sub_container($subcontainer_name);
445 my @components = $subcontainer->get_service_list;
446 @result = grep { m{$component} } @components;
448 return map { $subcontainer->get_component( $_, @args ) } @result
452 # it expects an empty list on failed searches
456 sub find_component_regexp {
457 my ( $self, $components, $component, @args ) = @_;
460 my @components = grep { m{$component} } keys %{ $components };
463 # FIXME this is naughty enough being called inside Catalyst.pm
464 # find some alternative for this sub and remember to delete here
465 my ($type, $name) = Catalyst::_get_component_type_name($_);
467 push @result, $self->get_component_from_sub_container(
475 # FIXME sorry for the name again :)
476 sub get_components_types {
480 for my $sub_container_name (qw/model view controller/) {
481 my $sub_container = $self->get_sub_container($sub_container_name);
482 for my $service ( $sub_container->get_service_list ) {
483 my $comp = $self->resolve(service => $service);
484 my $compname = ref $comp || $comp;
485 my $type = ref $comp ? 'instance' : 'class';
486 push @comps_types, [ $compname, $type ];
493 sub get_all_components {
498 map { $_ => $self->get_sub_container($_) } qw(model view controller)
501 for my $container (keys %$containers) {
502 for my $component ($containers->{$container}->get_service_list) {
503 my $comp = $containers->{$container}->resolve(
504 service => $component
506 my $comp_name = ref $comp || $comp;
507 $components{$comp_name} = $comp;
511 return lock_hash %components;
522 Catalyst::Container - IOC for Catalyst components
526 =head2 build_model_subcontainer
528 =head2 build_view_subcontainer
530 =head2 build_controller_subcontainer
532 =head2 build_name_service
534 =head2 build_driver_service
536 =head2 build_file_service
538 =head2 build_substitutions_service
540 =head2 build_extensions_service
542 =head2 build_prefix_service
544 =head2 build_path_service
546 =head2 build_config_service
548 =head2 build_raw_config_service
550 =head2 build_global_files_service
552 =head2 build_local_files_service
554 =head2 build_global_config_service
556 =head2 build_local_config_service
558 =head2 build_config_path_service
560 =head2 build_config_local_suffix_service
562 =head2 get_component_from_sub_container
564 =head2 get_components_types
566 =head2 get_all_components
568 =head2 find_component
570 =head2 find_component_regexp
574 =head2 _config_substitutions
578 Catalyst Contributors, see Catalyst.pm
582 This library is free software. You can redistribute it and/or modify it under
583 the same terms as Perl itself.