moved setup_component practically unchanged to the container
[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 ();
b4410fc3 7use Hash::Util qw/lock_hash/;
2bb0da6d 8use MooseX::Types::LoadableClass qw/ LoadableClass /;
0dff29e2 9use Moose::Util;
a6c13ff4 10use Catalyst::IOC::BlockInjection;
8b749525 11use namespace::autoclean;
b4a6fa62 12
13extends 'Bread::Board::Container';
14
15has config_local_suffix => (
442ab13e 16 is => 'ro',
b4a6fa62 17 isa => 'Str',
18 default => 'local',
19);
20
21has driver => (
442ab13e 22 is => 'ro',
b4a6fa62 23 isa => 'HashRef',
24 default => sub { +{} },
25);
26
27has file => (
442ab13e 28 is => 'ro',
b4a6fa62 29 isa => 'Str',
30 default => '',
31);
32
33has substitutions => (
442ab13e 34 is => 'ro',
b4a6fa62 35 isa => 'HashRef',
36 default => sub { +{} },
37);
38
39has name => (
442ab13e 40 is => 'ro',
b4a6fa62 41 isa => 'Str',
42 default => 'TestApp',
43);
44
2bb0da6d 45has sub_container_class => (
46 isa => LoadableClass,
47 is => 'ro',
48 coerce => 1,
a6c13ff4 49 default => 'Catalyst::IOC::SubContainer',
8b749525 50 handles => {
51 new_sub_container => 'new',
52 }
2bb0da6d 53);
54
b4a6fa62 55sub BUILD {
a2c0d071 56 my ( $self, $params ) = @_;
b4a6fa62 57
292277c1 58 $self->add_service(
59 $self->${\"build_${_}_service"}
60 ) for qw/
7451d1ea 61 substitutions
62 file
63 driver
64 name
65 prefix
66 extensions
67 path
68 config
69 raw_config
70 global_files
71 local_files
72 global_config
73 local_config
74 config_local_suffix
75 config_path
76 /;
f04816ce 77
292277c1 78 $self->add_sub_container(
a2c0d071 79 $self->build_controller_subcontainer
80 );
81
2921bab3 82 my $config = $self->resolve( service => 'config' );
83 my $default_view = $params->{default_view} || $config->{default_view};
84 my $default_model = $params->{default_model} || $config->{default_model};
85
a2c0d071 86 $self->add_sub_container(
87 $self->build_view_subcontainer(
2921bab3 88 default_component => $default_view,
a2c0d071 89 )
90 );
91
92 $self->add_sub_container(
93 $self->build_model_subcontainer(
2921bab3 94 default_component => $default_model,
a2c0d071 95 )
96 );
f04816ce 97}
98
99sub build_model_subcontainer {
100 my $self = shift;
101
a2c0d071 102 return $self->new_sub_container( @_,
5a53ef3d 103 name => 'model',
b06ded69 104 );
f04816ce 105}
106
107sub build_view_subcontainer {
108 my $self = shift;
109
a2c0d071 110 return $self->new_sub_container( @_,
5a53ef3d 111 name => 'view',
b06ded69 112 );
f04816ce 113}
114
115sub build_controller_subcontainer {
116 my $self = shift;
117
b06ded69 118 return $self->new_sub_container(
5a53ef3d 119 name => 'controller',
b06ded69 120 );
f04816ce 121}
122
f04816ce 123sub build_name_service {
124 my $self = shift;
292277c1 125
126 return Bread::Board::Literal->new( name => 'name', value => $self->name );
f04816ce 127}
128
129sub build_driver_service {
130 my $self = shift;
292277c1 131
132 return Bread::Board::Literal->new( name => 'driver', value => $self->driver );
f04816ce 133}
134
135sub build_file_service {
136 my $self = shift;
292277c1 137
138 return Bread::Board::Literal->new( name => 'file', value => $self->file );
f04816ce 139}
140
141sub build_substitutions_service {
142 my $self = shift;
292277c1 143
144 return Bread::Board::Literal->new( name => 'substitutions', value => $self->substitutions );
f04816ce 145}
146
147sub build_extensions_service {
148 my $self = shift;
292277c1 149
150 return Bread::Board::BlockInjection->new(
151 name => 'extensions',
152 block => sub {
153 return \@{Config::Any->extensions};
154 },
f04816ce 155 );
156}
b4a6fa62 157
f04816ce 158sub build_prefix_service {
159 my $self = shift;
292277c1 160
161 return Bread::Board::BlockInjection->new(
162 name => 'prefix',
163 block => sub {
164 return Catalyst::Utils::appprefix( shift->param('name') );
165 },
166 dependencies => [ depends_on('name') ],
f04816ce 167 );
168}
b4a6fa62 169
f04816ce 170sub build_path_service {
171 my $self = shift;
292277c1 172
173 return Bread::Board::BlockInjection->new(
174 name => 'path',
175 block => sub {
176 my $s = shift;
177
178 return Catalyst::Utils::env_value( $s->param('name'), 'CONFIG' )
179 || $s->param('file')
180 || $s->param('name')->path_to( $s->param('prefix') );
181 },
182 dependencies => [ depends_on('file'), depends_on('name'), depends_on('prefix') ],
f04816ce 183 );
184}
b4a6fa62 185
f04816ce 186sub build_config_service {
187 my $self = shift;
292277c1 188
189 return Bread::Board::BlockInjection->new(
190 name => 'config',
191 block => sub {
192 my $s = shift;
193
194 my $v = Data::Visitor::Callback->new(
195 plain_value => sub {
196 return unless defined $_;
197 return $self->_config_substitutions( $s->param('name'), $s->param('substitutions'), $_ );
198 }
199
200 );
201 $v->visit( $s->param('raw_config') );
202 },
203 dependencies => [ depends_on('name'), depends_on('raw_config'), depends_on('substitutions') ],
f04816ce 204 );
205}
b4a6fa62 206
f04816ce 207sub build_raw_config_service {
208 my $self = shift;
292277c1 209
210 return Bread::Board::BlockInjection->new(
211 name => 'raw_config',
212 block => sub {
213 my $s = shift;
214
215 my @global = @{$s->param('global_config')};
216 my @locals = @{$s->param('local_config')};
217
218 my $config = {};
219 for my $cfg (@global, @locals) {
220 for (keys %$cfg) {
221 $config = Catalyst::Utils::merge_hashes( $config, $cfg->{$_} );
b4a6fa62 222 }
292277c1 223 }
224 return $config;
225 },
226 dependencies => [ depends_on('global_config'), depends_on('local_config') ],
f04816ce 227 );
228}
b4a6fa62 229
f04816ce 230sub build_global_files_service {
231 my $self = shift;
b4a6fa62 232
292277c1 233 return Bread::Board::BlockInjection->new(
234 name => 'global_files',
235 block => sub {
236 my $s = shift;
b4a6fa62 237
292277c1 238 my ( $path, $extension ) = @{$s->param('config_path')};
b4a6fa62 239
292277c1 240 my @extensions = @{$s->param('extensions')};
241
242 my @files;
243 if ( $extension ) {
244 die "Unable to handle files with the extension '${extension}'" unless grep { $_ eq $extension } @extensions;
245 push @files, $path;
246 } else {
247 @files = map { "$path.$_" } @extensions;
248 }
249 return \@files;
250 },
251 dependencies => [ depends_on('extensions'), depends_on('config_path') ],
f04816ce 252 );
253}
b4a6fa62 254
f04816ce 255sub build_local_files_service {
256 my $self = shift;
292277c1 257
258 return Bread::Board::BlockInjection->new(
259 name => 'local_files',
260 block => sub {
261 my $s = shift;
262
263 my ( $path, $extension ) = @{$s->param('config_path')};
264 my $suffix = $s->param('config_local_suffix');
265
266 my @extensions = @{$s->param('extensions')};
267
268 my @files;
269 if ( $extension ) {
270 die "Unable to handle files with the extension '${extension}'" unless grep { $_ eq $extension } @extensions;
271 $path =~ s{\.$extension}{_$suffix.$extension};
272 push @files, $path;
273 } else {
274 @files = map { "${path}_${suffix}.$_" } @extensions;
275 }
276 return \@files;
277 },
278 dependencies => [ depends_on('extensions'), depends_on('config_path'), depends_on('config_local_suffix') ],
f04816ce 279 );
280}
b4a6fa62 281
f04816ce 282sub build_global_config_service {
283 my $self = shift;
292277c1 284
285 return Bread::Board::BlockInjection->new(
286 name => 'global_config',
287 block => sub {
288 my $s = shift;
289
290 return Config::Any->load_files({
291 files => $s->param('global_files'),
292 filter => \&_fix_syntax,
293 use_ext => 1,
294 driver_args => $s->param('driver'),
295 });
296 },
297 dependencies => [ depends_on('global_files') ],
f04816ce 298 );
299}
b4a6fa62 300
f04816ce 301sub build_local_config_service {
302 my $self = shift;
292277c1 303
304 return Bread::Board::BlockInjection->new(
305 name => 'local_config',
306 block => sub {
307 my $s = shift;
308
309 return Config::Any->load_files({
310 files => $s->param('local_files'),
311 filter => \&_fix_syntax,
312 use_ext => 1,
313 driver_args => $s->param('driver'),
314 });
315 },
316 dependencies => [ depends_on('local_files') ],
f04816ce 317 );
318}
b4a6fa62 319
f04816ce 320sub build_config_path_service {
321 my $self = shift;
b4a6fa62 322
292277c1 323 return Bread::Board::BlockInjection->new(
324 name => 'config_path',
325 block => sub {
326 my $s = shift;
b4a6fa62 327
292277c1 328 my $path = $s->param('path');
329 my $prefix = $s->param('prefix');
b4a6fa62 330
292277c1 331 my ( $extension ) = ( $path =~ m{\.(.{1,4})$} );
332
333 if ( -d $path ) {
334 $path =~ s{[\/\\]$}{};
335 $path .= "/$prefix";
336 }
b4a6fa62 337
292277c1 338 return [ $path, $extension ];
339 },
340 dependencies => [ depends_on('prefix'), depends_on('path') ],
f04816ce 341 );
342}
b4a6fa62 343
f04816ce 344sub build_config_local_suffix_service {
345 my $self = shift;
292277c1 346
347 return Bread::Board::BlockInjection->new(
348 name => 'config_local_suffix',
349 block => sub {
350 my $s = shift;
351 my $suffix = Catalyst::Utils::env_value( $s->param('name'), 'CONFIG_LOCAL_SUFFIX' ) || $self->config_local_suffix;
352
353 return $suffix;
354 },
355 dependencies => [ depends_on('name') ],
f04816ce 356 );
b4a6fa62 357}
358
359sub _fix_syntax {
360 my $config = shift;
361 my @components = (
362 map +{
363 prefix => $_ eq 'Component' ? '' : $_ . '::',
364 values => delete $config->{ lc $_ } || delete $config->{ $_ }
365 },
366 grep { ref $config->{ lc $_ } || ref $config->{ $_ } }
367 qw( Component Model M View V Controller C Plugin )
368 );
369
370 foreach my $comp ( @components ) {
371 my $prefix = $comp->{ prefix };
372 foreach my $element ( keys %{ $comp->{ values } } ) {
373 $config->{ "$prefix$element" } = $comp->{ values }->{ $element };
374 }
375 }
376}
377
378sub _config_substitutions {
6682389c 379 my ( $self, $name, $subs, $arg ) = @_;
b4a6fa62 380
381 $subs->{ HOME } ||= sub { shift->path_to( '' ); };
382 $subs->{ ENV } ||=
383 sub {
384 my ( $c, $v ) = @_;
385 if (! defined($ENV{$v})) {
386 Catalyst::Exception->throw( message =>
387 "Missing environment variable: $v" );
388 return "";
389 } else {
390 return $ENV{ $v };
391 }
392 };
393 $subs->{ path_to } ||= sub { shift->path_to( @_ ); };
394 $subs->{ literal } ||= sub { return $_[ 1 ]; };
395 my $subsre = join( '|', keys %$subs );
396
6682389c 397 $arg =~ s{__($subsre)(?:\((.+?)\))?__}{ $subs->{ $1 }->( $name, $2 ? split( /,/, $2 ) : () ) }eg;
398 return $arg;
b4a6fa62 399}
400
a17e0ff8 401sub get_component_from_sub_container {
402 my ( $self, $sub_container_name, $name, $c, @args ) = @_;
403
404 my $sub_container = $self->get_sub_container( $sub_container_name );
405
0e747f0c 406 if (!$name) {
a2c0d071 407 my $default = $sub_container->default_component;
0e747f0c 408
409 return $sub_container->get_component( $default, $c, @args )
410 if $default && $sub_container->has_service( $default );
411
412 # this is never a controller, so this is safe
413 $c->log->warn( "Calling \$c->$sub_container_name() is not supported unless you specify one of:" );
414 $c->log->warn( "* \$c->config(default_$sub_container_name => 'the name of the default $sub_container_name to use')" );
415 $c->log->warn( "* \$c->stash->{current_$sub_container_name} # the name of the view to use for this request" );
416 $c->log->warn( "* \$c->stash->{current_${sub_container_name}_instance} # the instance of the $sub_container_name to use for this request" );
a2c0d071 417
418 return;
0e747f0c 419 }
420
a17e0ff8 421 return $sub_container->get_component_regexp( $name, $c, @args )
422 if ref $name;
423
424 return $sub_container->get_component( $name, $c, @args )
425 if $sub_container->has_service( $name );
426
427 $c->log->warn(
428 "Attempted to use $sub_container_name '$name', " .
429 "but it does not exist"
430 );
431
432 return;
433}
434
c4aedec7 435sub find_component {
d0f954b4 436 my ( $self, $component, $c, @args ) = @_;
f147e6c2 437 my ( $type, $name ) = _get_component_type_name($component);
c4aedec7 438 my @result;
439
d0f954b4 440 return $self->get_component_from_sub_container(
441 $type, $name, $c, @args
442 ) if $type;
443
c4aedec7 444 my $query = ref $component
445 ? $component
446 : qr{^$component$}
447 ;
448
449 for my $subcontainer_name (qw/model view controller/) {
450 my $subcontainer = $self->get_sub_container($subcontainer_name);
451 my @components = $subcontainer->get_service_list;
452 @result = grep { m{$component} } @components;
453
d0f954b4 454 return map { $subcontainer->get_component( $_, $c, @args ) } @result
c4aedec7 455 if @result;
456 }
457
d0f954b4 458 # one last search for things like $c->comp(qr/::M::/)
459 @result = $self->find_component_regexp(
460 $c->components, $component, $c, @args
461 ) if !@result and ref $component;
462
c4aedec7 463 # it expects an empty list on failed searches
464 return @result;
465}
466
4e2b302e 467sub find_component_regexp {
468 my ( $self, $components, $component, @args ) = @_;
469 my @result;
470
471 my @components = grep { m{$component} } keys %{ $components };
472
473 for (@components) {
f147e6c2 474 my ($type, $name) = _get_component_type_name($_);
4e2b302e 475
476 push @result, $self->get_component_from_sub_container(
477 $type, $name, @args
478 ) if $type;
479 }
480
481 return @result;
482}
483
409db9cb 484# FIXME sorry for the name again :)
be80b0a5 485sub get_components_types {
486 my ( $self ) = @_;
487 my @comps_types;
488
489 for my $sub_container_name (qw/model view controller/) {
490 my $sub_container = $self->get_sub_container($sub_container_name);
491 for my $service ( $sub_container->get_service_list ) {
492 my $comp = $self->resolve(service => $service);
493 my $compname = ref $comp || $comp;
494 my $type = ref $comp ? 'instance' : 'class';
495 push @comps_types, [ $compname, $type ];
496 }
497 }
498
499 return @comps_types;
500}
c4aedec7 501
b4410fc3 502sub get_all_components {
503 my $self = shift;
504 my %components;
505
506 my $containers = {
507 map { $_ => $self->get_sub_container($_) } qw(model view controller)
508 };
509
510 for my $container (keys %$containers) {
511 for my $component ($containers->{$container}->get_service_list) {
512 my $comp = $containers->{$container}->resolve(
513 service => $component
514 );
515 my $comp_name = ref $comp || $comp;
516 $components{$comp_name} = $comp;
517 }
518 }
519
520 return lock_hash %components;
521}
522
f147e6c2 523sub add_component {
0dff29e2 524 my ( $self, $component, $class ) = @_;
f147e6c2 525 my ( $type, $name ) = _get_component_type_name($component);
526
527 return unless $type;
528
0dff29e2 529 my $instance = setup_component( $component, $class );
530
f147e6c2 531 $self->get_sub_container($type)->add_service(
532 Catalyst::IOC::BlockInjection->new(
533 name => $name,
534 block => sub { $instance },
535 )
536 );
0dff29e2 537
538 return $instance;
f147e6c2 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 {
566 my ( $component, $class ) = @_;
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 );
575 my $config = $class->config->{ $suffix } || {};
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
604
d057ddb9 6051;
606
607__END__
608
609=pod
610
611=head1 NAME
612
613Catalyst::Container - IOC for Catalyst components
614
615=head1 METHODS
616
617=head2 build_model_subcontainer
618
619=head2 build_view_subcontainer
620
621=head2 build_controller_subcontainer
622
623=head2 build_name_service
624
625=head2 build_driver_service
626
627=head2 build_file_service
628
629=head2 build_substitutions_service
630
631=head2 build_extensions_service
632
633=head2 build_prefix_service
634
635=head2 build_path_service
636
637=head2 build_config_service
638
639=head2 build_raw_config_service
640
641=head2 build_global_files_service
642
643=head2 build_local_files_service
644
645=head2 build_global_config_service
646
647=head2 build_local_config_service
648
649=head2 build_config_path_service
650
651=head2 build_config_local_suffix_service
652
8dc2fca3 653=head2 get_component_from_sub_container
654
409db9cb 655=head2 get_components_types
656
b4410fc3 657=head2 get_all_components
658
f147e6c2 659=head2 add_component
660
c4aedec7 661=head2 find_component
662
4e2b302e 663=head2 find_component_regexp
664
0dff29e2 665=head2 setup_component
666
d057ddb9 667=head2 _fix_syntax
668
669=head2 _config_substitutions
670
bf3c8088 671=head1 AUTHORS
672
e8ed391e 673Catalyst Contributors, see Catalyst.pm
bf3c8088 674
e8ed391e 675=head1 COPYRIGHT
bf3c8088 676
677This library is free software. You can redistribute it and/or modify it under
678the same terms as Perl itself.
679
680=cut