try to make a summary of some sort
[catagits/Catalyst-Runtime.git] / lib / Catalyst / IOC / Container.pm
CommitLineData
a6c13ff4 1package Catalyst::IOC::Container;
b4a6fa62 2use Bread::Board;
3use Moose;
4use Config::Any;
5use Data::Visitor::Callback;
6use Catalyst::Utils ();
d3742403 7use Devel::InnerPackage ();
b4410fc3 8use Hash::Util qw/lock_hash/;
2bb0da6d 9use MooseX::Types::LoadableClass qw/ LoadableClass /;
0dff29e2 10use Moose::Util;
a6c13ff4 11use Catalyst::IOC::BlockInjection;
8b749525 12use namespace::autoclean;
b4a6fa62 13
14extends 'Bread::Board::Container';
15
16has config_local_suffix => (
442ab13e 17 is => 'ro',
b4a6fa62 18 isa => 'Str',
19 default => 'local',
20);
21
22has driver => (
442ab13e 23 is => 'ro',
b4a6fa62 24 isa => 'HashRef',
25 default => sub { +{} },
26);
27
28has file => (
442ab13e 29 is => 'ro',
b4a6fa62 30 isa => 'Str',
31 default => '',
32);
33
34has substitutions => (
442ab13e 35 is => 'ro',
b4a6fa62 36 isa => 'HashRef',
37 default => sub { +{} },
38);
39
40has name => (
442ab13e 41 is => 'ro',
b4a6fa62 42 isa => 'Str',
a0146296 43 default => 'MyApp',
b4a6fa62 44);
45
2bb0da6d 46has sub_container_class => (
47 isa => LoadableClass,
48 is => 'ro',
49 coerce => 1,
a6c13ff4 50 default => 'Catalyst::IOC::SubContainer',
8b749525 51 handles => {
52 new_sub_container => 'new',
53 }
2bb0da6d 54);
55
b4a6fa62 56sub BUILD {
a2c0d071 57 my ( $self, $params ) = @_;
b4a6fa62 58
292277c1 59 $self->add_service(
60 $self->${\"build_${_}_service"}
61 ) for qw/
7451d1ea 62 substitutions
63 file
64 driver
65 name
66 prefix
67 extensions
68 path
69 config
70 raw_config
71 global_files
72 local_files
73 global_config
74 local_config
75 config_local_suffix
76 config_path
77 /;
f04816ce 78
292277c1 79 $self->add_sub_container(
a2c0d071 80 $self->build_controller_subcontainer
81 );
82
a0146296 83 # FIXME - the config should be merged at this point
2921bab3 84 my $config = $self->resolve( service => 'config' );
85 my $default_view = $params->{default_view} || $config->{default_view};
86 my $default_model = $params->{default_model} || $config->{default_model};
87
a2c0d071 88 $self->add_sub_container(
89 $self->build_view_subcontainer(
2921bab3 90 default_component => $default_view,
a2c0d071 91 )
92 );
93
94 $self->add_sub_container(
95 $self->build_model_subcontainer(
2921bab3 96 default_component => $default_model,
a2c0d071 97 )
98 );
f04816ce 99}
100
101sub build_model_subcontainer {
102 my $self = shift;
103
a2c0d071 104 return $self->new_sub_container( @_,
5a53ef3d 105 name => 'model',
b06ded69 106 );
f04816ce 107}
108
109sub build_view_subcontainer {
110 my $self = shift;
111
a2c0d071 112 return $self->new_sub_container( @_,
5a53ef3d 113 name => 'view',
b06ded69 114 );
f04816ce 115}
116
117sub build_controller_subcontainer {
118 my $self = shift;
119
b06ded69 120 return $self->new_sub_container(
5a53ef3d 121 name => 'controller',
b06ded69 122 );
f04816ce 123}
124
f04816ce 125sub build_name_service {
126 my $self = shift;
292277c1 127
128 return Bread::Board::Literal->new( name => 'name', value => $self->name );
f04816ce 129}
130
131sub build_driver_service {
132 my $self = shift;
292277c1 133
134 return Bread::Board::Literal->new( name => 'driver', value => $self->driver );
f04816ce 135}
136
137sub build_file_service {
138 my $self = shift;
292277c1 139
140 return Bread::Board::Literal->new( name => 'file', value => $self->file );
f04816ce 141}
142
143sub build_substitutions_service {
144 my $self = shift;
292277c1 145
146 return Bread::Board::Literal->new( name => 'substitutions', value => $self->substitutions );
f04816ce 147}
148
149sub build_extensions_service {
150 my $self = shift;
292277c1 151
152 return Bread::Board::BlockInjection->new(
153 name => 'extensions',
154 block => sub {
155 return \@{Config::Any->extensions};
156 },
f04816ce 157 );
158}
b4a6fa62 159
f04816ce 160sub build_prefix_service {
161 my $self = shift;
292277c1 162
163 return Bread::Board::BlockInjection->new(
164 name => 'prefix',
165 block => sub {
166 return Catalyst::Utils::appprefix( shift->param('name') );
167 },
168 dependencies => [ depends_on('name') ],
f04816ce 169 );
170}
b4a6fa62 171
f04816ce 172sub build_path_service {
173 my $self = shift;
292277c1 174
175 return Bread::Board::BlockInjection->new(
176 name => 'path',
177 block => sub {
178 my $s = shift;
179
180 return Catalyst::Utils::env_value( $s->param('name'), 'CONFIG' )
181 || $s->param('file')
182 || $s->param('name')->path_to( $s->param('prefix') );
183 },
184 dependencies => [ depends_on('file'), depends_on('name'), depends_on('prefix') ],
f04816ce 185 );
186}
b4a6fa62 187
f04816ce 188sub build_config_service {
189 my $self = shift;
292277c1 190
191 return Bread::Board::BlockInjection->new(
192 name => 'config',
193 block => sub {
194 my $s = shift;
195
196 my $v = Data::Visitor::Callback->new(
197 plain_value => sub {
198 return unless defined $_;
199 return $self->_config_substitutions( $s->param('name'), $s->param('substitutions'), $_ );
200 }
201
202 );
203 $v->visit( $s->param('raw_config') );
204 },
205 dependencies => [ depends_on('name'), depends_on('raw_config'), depends_on('substitutions') ],
f04816ce 206 );
207}
b4a6fa62 208
f04816ce 209sub build_raw_config_service {
210 my $self = shift;
292277c1 211
212 return Bread::Board::BlockInjection->new(
213 name => 'raw_config',
214 block => sub {
215 my $s = shift;
216
217 my @global = @{$s->param('global_config')};
218 my @locals = @{$s->param('local_config')};
219
220 my $config = {};
221 for my $cfg (@global, @locals) {
222 for (keys %$cfg) {
223 $config = Catalyst::Utils::merge_hashes( $config, $cfg->{$_} );
b4a6fa62 224 }
292277c1 225 }
226 return $config;
227 },
228 dependencies => [ depends_on('global_config'), depends_on('local_config') ],
f04816ce 229 );
230}
b4a6fa62 231
f04816ce 232sub build_global_files_service {
233 my $self = shift;
b4a6fa62 234
292277c1 235 return Bread::Board::BlockInjection->new(
236 name => 'global_files',
237 block => sub {
238 my $s = shift;
b4a6fa62 239
292277c1 240 my ( $path, $extension ) = @{$s->param('config_path')};
b4a6fa62 241
292277c1 242 my @extensions = @{$s->param('extensions')};
243
244 my @files;
245 if ( $extension ) {
246 die "Unable to handle files with the extension '${extension}'" unless grep { $_ eq $extension } @extensions;
247 push @files, $path;
248 } else {
249 @files = map { "$path.$_" } @extensions;
250 }
251 return \@files;
252 },
253 dependencies => [ depends_on('extensions'), depends_on('config_path') ],
f04816ce 254 );
255}
b4a6fa62 256
f04816ce 257sub build_local_files_service {
258 my $self = shift;
292277c1 259
260 return Bread::Board::BlockInjection->new(
261 name => 'local_files',
262 block => sub {
263 my $s = shift;
264
265 my ( $path, $extension ) = @{$s->param('config_path')};
266 my $suffix = $s->param('config_local_suffix');
267
268 my @extensions = @{$s->param('extensions')};
269
270 my @files;
271 if ( $extension ) {
272 die "Unable to handle files with the extension '${extension}'" unless grep { $_ eq $extension } @extensions;
273 $path =~ s{\.$extension}{_$suffix.$extension};
274 push @files, $path;
275 } else {
276 @files = map { "${path}_${suffix}.$_" } @extensions;
277 }
278 return \@files;
279 },
280 dependencies => [ depends_on('extensions'), depends_on('config_path'), depends_on('config_local_suffix') ],
f04816ce 281 );
282}
b4a6fa62 283
f04816ce 284sub build_global_config_service {
285 my $self = shift;
292277c1 286
287 return Bread::Board::BlockInjection->new(
288 name => 'global_config',
289 block => sub {
290 my $s = shift;
291
292 return Config::Any->load_files({
293 files => $s->param('global_files'),
294 filter => \&_fix_syntax,
295 use_ext => 1,
296 driver_args => $s->param('driver'),
297 });
298 },
299 dependencies => [ depends_on('global_files') ],
f04816ce 300 );
301}
b4a6fa62 302
f04816ce 303sub build_local_config_service {
304 my $self = shift;
292277c1 305
306 return Bread::Board::BlockInjection->new(
307 name => 'local_config',
308 block => sub {
309 my $s = shift;
310
311 return Config::Any->load_files({
312 files => $s->param('local_files'),
313 filter => \&_fix_syntax,
314 use_ext => 1,
315 driver_args => $s->param('driver'),
316 });
317 },
318 dependencies => [ depends_on('local_files') ],
f04816ce 319 );
320}
b4a6fa62 321
f04816ce 322sub build_config_path_service {
323 my $self = shift;
b4a6fa62 324
292277c1 325 return Bread::Board::BlockInjection->new(
326 name => 'config_path',
327 block => sub {
328 my $s = shift;
b4a6fa62 329
292277c1 330 my $path = $s->param('path');
331 my $prefix = $s->param('prefix');
b4a6fa62 332
292277c1 333 my ( $extension ) = ( $path =~ m{\.(.{1,4})$} );
334
335 if ( -d $path ) {
336 $path =~ s{[\/\\]$}{};
337 $path .= "/$prefix";
338 }
b4a6fa62 339
292277c1 340 return [ $path, $extension ];
341 },
342 dependencies => [ depends_on('prefix'), depends_on('path') ],
f04816ce 343 );
344}
b4a6fa62 345
f04816ce 346sub build_config_local_suffix_service {
347 my $self = shift;
292277c1 348
349 return Bread::Board::BlockInjection->new(
350 name => 'config_local_suffix',
351 block => sub {
352 my $s = shift;
353 my $suffix = Catalyst::Utils::env_value( $s->param('name'), 'CONFIG_LOCAL_SUFFIX' ) || $self->config_local_suffix;
354
355 return $suffix;
356 },
357 dependencies => [ depends_on('name') ],
f04816ce 358 );
b4a6fa62 359}
360
361sub _fix_syntax {
362 my $config = shift;
363 my @components = (
364 map +{
365 prefix => $_ eq 'Component' ? '' : $_ . '::',
366 values => delete $config->{ lc $_ } || delete $config->{ $_ }
367 },
368 grep { ref $config->{ lc $_ } || ref $config->{ $_ } }
369 qw( Component Model M View V Controller C Plugin )
370 );
371
372 foreach my $comp ( @components ) {
373 my $prefix = $comp->{ prefix };
374 foreach my $element ( keys %{ $comp->{ values } } ) {
375 $config->{ "$prefix$element" } = $comp->{ values }->{ $element };
376 }
377 }
378}
379
380sub _config_substitutions {
6682389c 381 my ( $self, $name, $subs, $arg ) = @_;
b4a6fa62 382
383 $subs->{ HOME } ||= sub { shift->path_to( '' ); };
384 $subs->{ ENV } ||=
385 sub {
386 my ( $c, $v ) = @_;
387 if (! defined($ENV{$v})) {
388 Catalyst::Exception->throw( message =>
389 "Missing environment variable: $v" );
390 return "";
391 } else {
392 return $ENV{ $v };
393 }
394 };
395 $subs->{ path_to } ||= sub { shift->path_to( @_ ); };
396 $subs->{ literal } ||= sub { return $_[ 1 ]; };
397 my $subsre = join( '|', keys %$subs );
398
6682389c 399 $arg =~ s{__($subsre)(?:\((.+?)\))?__}{ $subs->{ $1 }->( $name, $2 ? split( /,/, $2 ) : () ) }eg;
400 return $arg;
b4a6fa62 401}
402
a17e0ff8 403sub get_component_from_sub_container {
404 my ( $self, $sub_container_name, $name, $c, @args ) = @_;
405
406 my $sub_container = $self->get_sub_container( $sub_container_name );
407
0e747f0c 408 if (!$name) {
a2c0d071 409 my $default = $sub_container->default_component;
0e747f0c 410
411 return $sub_container->get_component( $default, $c, @args )
412 if $default && $sub_container->has_service( $default );
413
a0146296 414 # FIXME - should I be calling $c->log->warn here?
0e747f0c 415 # this is never a controller, so this is safe
416 $c->log->warn( "Calling \$c->$sub_container_name() is not supported unless you specify one of:" );
417 $c->log->warn( "* \$c->config(default_$sub_container_name => 'the name of the default $sub_container_name to use')" );
418 $c->log->warn( "* \$c->stash->{current_$sub_container_name} # the name of the view to use for this request" );
419 $c->log->warn( "* \$c->stash->{current_${sub_container_name}_instance} # the instance of the $sub_container_name to use for this request" );
a2c0d071 420
421 return;
0e747f0c 422 }
423
a17e0ff8 424 return $sub_container->get_component_regexp( $name, $c, @args )
425 if ref $name;
426
427 return $sub_container->get_component( $name, $c, @args )
428 if $sub_container->has_service( $name );
429
430 $c->log->warn(
431 "Attempted to use $sub_container_name '$name', " .
432 "but it does not exist"
433 );
434
435 return;
436}
437
c4aedec7 438sub find_component {
d0f954b4 439 my ( $self, $component, $c, @args ) = @_;
f147e6c2 440 my ( $type, $name ) = _get_component_type_name($component);
c4aedec7 441 my @result;
442
d0f954b4 443 return $self->get_component_from_sub_container(
444 $type, $name, $c, @args
445 ) if $type;
446
c4aedec7 447 my $query = ref $component
448 ? $component
449 : qr{^$component$}
450 ;
451
452 for my $subcontainer_name (qw/model view controller/) {
a0146296 453 my $subcontainer = $self->get_sub_container( $subcontainer_name );
c4aedec7 454 my @components = $subcontainer->get_service_list;
455 @result = grep { m{$component} } @components;
456
d0f954b4 457 return map { $subcontainer->get_component( $_, $c, @args ) } @result
c4aedec7 458 if @result;
459 }
460
a0146296 461 # FIXME - I guess I shouldn't be calling $c->components here
d0f954b4 462 # one last search for things like $c->comp(qr/::M::/)
463 @result = $self->find_component_regexp(
464 $c->components, $component, $c, @args
465 ) if !@result and ref $component;
466
c4aedec7 467 # it expects an empty list on failed searches
468 return @result;
469}
470
4e2b302e 471sub find_component_regexp {
472 my ( $self, $components, $component, @args ) = @_;
473 my @result;
474
475 my @components = grep { m{$component} } keys %{ $components };
476
477 for (@components) {
f147e6c2 478 my ($type, $name) = _get_component_type_name($_);
4e2b302e 479
480 push @result, $self->get_component_from_sub_container(
481 $type, $name, @args
482 ) if $type;
483 }
484
485 return @result;
486}
487
409db9cb 488# FIXME sorry for the name again :)
be80b0a5 489sub get_components_types {
490 my ( $self ) = @_;
491 my @comps_types;
492
493 for my $sub_container_name (qw/model view controller/) {
494 my $sub_container = $self->get_sub_container($sub_container_name);
495 for my $service ( $sub_container->get_service_list ) {
496 my $comp = $self->resolve(service => $service);
497 my $compname = ref $comp || $comp;
498 my $type = ref $comp ? 'instance' : 'class';
499 push @comps_types, [ $compname, $type ];
500 }
501 }
502
503 return @comps_types;
504}
c4aedec7 505
b4410fc3 506sub get_all_components {
507 my $self = shift;
508 my %components;
509
510 my $containers = {
511 map { $_ => $self->get_sub_container($_) } qw(model view controller)
512 };
513
514 for my $container (keys %$containers) {
515 for my $component ($containers->{$container}->get_service_list) {
516 my $comp = $containers->{$container}->resolve(
517 service => $component
518 );
519 my $comp_name = ref $comp || $comp;
520 $components{$comp_name} = $comp;
521 }
522 }
523
524 return lock_hash %components;
525}
526
f147e6c2 527sub add_component {
0dff29e2 528 my ( $self, $component, $class ) = @_;
f147e6c2 529 my ( $type, $name ) = _get_component_type_name($component);
530
531 return unless $type;
532
533 $self->get_sub_container($type)->add_service(
534 Catalyst::IOC::BlockInjection->new(
535 name => $name,
10c4d3b0 536 block => sub { $self->setup_component( $component, $class ) },
f147e6c2 537 )
538 );
539}
540
541# FIXME: should this sub exist?
542# should it be moved to Catalyst::Utils,
543# or replaced by something already existing there?
544sub _get_component_type_name {
545 my ( $component ) = @_;
546
547 my @parts = split /::/, $component;
548
549 while (my $type = shift @parts) {
550 return ('controller', join '::', @parts)
551 if $type =~ /^(c|controller)$/i;
552
553 return ('model', join '::', @parts)
554 if $type =~ /^(m|model)$/i;
555
556 return ('view', join '::', @parts)
557 if $type =~ /^(v|view)$/i;
558 }
559
560 return (undef, $component);
561}
562
0dff29e2 563# FIXME ugly and temporary
564# Just moved it here the way it was, so we can work on it here in the container
565sub setup_component {
10c4d3b0 566 my ( $self, $component, $class ) = @_;
0dff29e2 567
568 unless ( $component->can( 'COMPONENT' ) ) {
569 return $component;
570 }
571
572 # FIXME I know this isn't the "Dependency Injection" way of doing things,
573 # its just temporary
574 my $suffix = Catalyst::Utils::class2classsuffix( $component );
10c4d3b0 575 my $config = $self->resolve(service => 'config')->{ $suffix } || {};
0dff29e2 576
577 # Stash catalyst_component_name in the config here, so that custom COMPONENT
578 # methods also pass it. local to avoid pointlessly shitting in config
579 # for the debug screen, as $component is already the key name.
580 local $config->{catalyst_component_name} = $component;
581
582 my $instance = eval { $component->COMPONENT( $class, $config ); };
583
584 if ( my $error = $@ ) {
585 chomp $error;
586 Catalyst::Exception->throw(
587 message => qq/Couldn't instantiate component "$component", "$error"/
588 );
589 }
590 elsif (!blessed $instance) {
591 my $metaclass = Moose::Util::find_meta($component);
592 my $method_meta = $metaclass->find_method_by_name('COMPONENT');
593 my $component_method_from = $method_meta->associated_metaclass->name;
594 my $value = defined($instance) ? $instance : 'undef';
595 Catalyst::Exception->throw(
596 message =>
597 qq/Couldn't instantiate component "$component", COMPONENT() method (from $component_method_from) didn't return an object-like value (value was $value)./
598 );
599 }
600
601 return $instance;
602}
603
d3742403 604sub expand_component_module {
605 my ( $class, $module ) = @_;
606 return Devel::InnerPackage::list_packages( $module );
607}
0dff29e2 608
d057ddb9 6091;
610
611__END__
612
613=pod
614
615=head1 NAME
616
617Catalyst::Container - IOC for Catalyst components
618
2c2ed473 619=head1 SYNOPSIS
620
621=head1 DESCRIPTION
622
d057ddb9 623=head1 METHODS
624
a0146296 625=head1 Containers
626
d057ddb9 627=head2 build_model_subcontainer
628
a0146296 629Container that stores all models.
630
d057ddb9 631=head2 build_view_subcontainer
632
a0146296 633Container that stores all views.
634
d057ddb9 635=head2 build_controller_subcontainer
636
a0146296 637Container that stores all controllers.
638
639=head1 Services
640
d057ddb9 641=head2 build_name_service
642
a0146296 643Name of the application.
644
d057ddb9 645=head2 build_driver_service
646
a0146296 647Config options passed directly to the driver being used.
648
d057ddb9 649=head2 build_file_service
650
a0146296 651?
652
d057ddb9 653=head2 build_substitutions_service
654
a0146296 655Executes all the substitutions in config. See L</_config_substitutions> method.
656
d057ddb9 657=head2 build_extensions_service
658
659=head2 build_prefix_service
660
661=head2 build_path_service
662
663=head2 build_config_service
664
665=head2 build_raw_config_service
666
667=head2 build_global_files_service
668
669=head2 build_local_files_service
670
671=head2 build_global_config_service
672
673=head2 build_local_config_service
674
675=head2 build_config_path_service
676
677=head2 build_config_local_suffix_service
678
a0146296 679Determines the suffix of files used to override the main config. By default
680this value is C<local>, which will load C<myapp_local.conf>. The suffix can
681be specified in the following order of preference:
682
683=over
684
685=item * C<$ENV{ MYAPP_CONFIG_LOCAL_SUFFIX }>
686
687=item * C<$ENV{ CATALYST_CONFIG_LOCAL_SUFFIX }>
688
689=back
690
691The first one of these values found replaces the default of C<local> in the
692name of the local config file to be loaded.
693
694For example, if C< $ENV{ MYAPP_CONFIG_LOCAL_SUFFIX }> is set to C<testing>,
695ConfigLoader will try and load C<myapp_testing.conf> instead of
696C<myapp_local.conf>.
697
698=head2 get_component_from_sub_container($sub_container, $name, $c, @args)
699
700Looks for components in a given subcontainer (such as controller, model or view), and returns the searched component. If $name is undef, it returns the default component (such as default_view, if $sub_container is 'view'). If $name is a regexp, it returns an array of matching components. Otherwise, it looks for the component with name $name.
8dc2fca3 701
409db9cb 702=head2 get_components_types
703
b4410fc3 704=head2 get_all_components
705
a0146296 706Fetches all the components, in each of the sub_containers model, view and controller, and returns a readonly hash. The keys are the class names, and the values are the blessed objects. This is what is returned by $c->components.
707
f147e6c2 708=head2 add_component
709
a0146296 710Adds a component to the appropriate subcontainer. The subcontainer is guessed by the component name given.
711
c4aedec7 712=head2 find_component
713
a0146296 714Searches for components in all containers. If $component is the full class name, the subcontainer is guessed, and it gets the searched component in there. Otherwise, it looks for a component with that name in all subcontainers. If $component is a regexp, it calls the method below, find_component_regexp, and matches all components against that regexp.
715
4e2b302e 716=head2 find_component_regexp
717
a0146296 718Finds components that match a given regexp. Used internally, by find_component.
719
0dff29e2 720=head2 setup_component
721
d057ddb9 722=head2 _fix_syntax
723
724=head2 _config_substitutions
725
a0146296 726This method substitutes macros found with calls to a function. There are a
727number of default macros:
728
729=over
730
731=item * C<__HOME__> - replaced with C<$c-E<gt>path_to('')>
732
733=item * C<__ENV(foo)__> - replaced with the value of C<$ENV{foo}>
734
735=item * C<__path_to(foo/bar)__> - replaced with C<$c-E<gt>path_to('foo/bar')>
736
737=item * C<__literal(__FOO__)__> - leaves __FOO__ alone (allows you to use
738C<__DATA__> as a config value, for example)
739
740=back
741
742The parameter list is split on comma (C<,>). You can override this method to
743do your own string munging, or you can define your own macros in
744C<MyApp-E<gt>config-E<gt>{ 'Plugin::ConfigLoader' }-E<gt>{ substitutions }>.
745Example:
746
747 MyApp->config->{ 'Plugin::ConfigLoader' }->{ substitutions } = {
748 baz => sub { my $c = shift; qux( @_ ); }
749 }
750
751The above will respond to C<__baz(x,y)__> in config strings.
752
d3742403 753=head2 $c->expand_component_module( $component, $setup_component_config )
754
755Components found by C<locate_components> will be passed to this method, which
756is expected to return a list of component (package) names to be set up.
757
758=cut
759
bf3c8088 760=head1 AUTHORS
761
e8ed391e 762Catalyst Contributors, see Catalyst.pm
bf3c8088 763
e8ed391e 764=head1 COPYRIGHT
bf3c8088 765
766This library is free software. You can redistribute it and/or modify it under
767the same terms as Perl itself.
768
769=cut