Update my part of the TODO
[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;
ff0e9735 11use Catalyst::IOC::BlockInjection;
b7da37bd 12use Catalyst::IOC::ConstructorInjection;
2f32de9a 13use Module::Pluggable::Object ();
8b749525 14use namespace::autoclean;
b4a6fa62 15
16extends 'Bread::Board::Container';
17
18has config_local_suffix => (
442ab13e 19 is => 'ro',
b4a6fa62 20 isa => 'Str',
21 default => 'local',
22);
23
24has driver => (
442ab13e 25 is => 'ro',
b4a6fa62 26 isa => 'HashRef',
27 default => sub { +{} },
28);
29
30has file => (
442ab13e 31 is => 'ro',
b4a6fa62 32 isa => 'Str',
33 default => '',
34);
35
36has substitutions => (
442ab13e 37 is => 'ro',
b4a6fa62 38 isa => 'HashRef',
39 default => sub { +{} },
40);
41
6c743f02 42has application_name => (
55f417ad 43 is => 'ro',
44 isa => 'Str',
45 required => 1,
b4a6fa62 46);
47
2bb0da6d 48has sub_container_class => (
49 isa => LoadableClass,
50 is => 'ro',
51 coerce => 1,
a6c13ff4 52 default => 'Catalyst::IOC::SubContainer',
8b749525 53 handles => {
54 new_sub_container => 'new',
55 }
2bb0da6d 56);
57
b4a6fa62 58sub BUILD {
a2c0d071 59 my ( $self, $params ) = @_;
b4a6fa62 60
292277c1 61 $self->add_service(
62 $self->${\"build_${_}_service"}
63 ) for qw/
7451d1ea 64 substitutions
65 file
66 driver
6c743f02 67 application_name
7451d1ea 68 prefix
69 extensions
70 path
71 config
72 raw_config
73 global_files
74 local_files
75 global_config
76 local_config
0fcf9a51 77 class_config
7451d1ea 78 config_local_suffix
79 config_path
3e4f5406 80 locate_components
7451d1ea 81 /;
f04816ce 82
0fcf9a51 83 my $config = $self->resolve( service => 'config' );
84
292277c1 85 $self->add_sub_container(
ff0e9735 86 $self->build_component_subcontainer
87 );
88
89 $self->add_sub_container(
a2c0d071 90 $self->build_controller_subcontainer
91 );
92
93 $self->add_sub_container(
94 $self->build_view_subcontainer(
0fcf9a51 95 default_component => $config->{default_view},
a2c0d071 96 )
97 );
98
99 $self->add_sub_container(
100 $self->build_model_subcontainer(
0fcf9a51 101 default_component => $config->{default_model},
a2c0d071 102 )
103 );
f04816ce 104}
105
106sub build_model_subcontainer {
107 my $self = shift;
108
a2c0d071 109 return $self->new_sub_container( @_,
5a53ef3d 110 name => 'model',
b06ded69 111 );
f04816ce 112}
113
114sub build_view_subcontainer {
115 my $self = shift;
116
a2c0d071 117 return $self->new_sub_container( @_,
5a53ef3d 118 name => 'view',
b06ded69 119 );
f04816ce 120}
121
122sub build_controller_subcontainer {
123 my $self = shift;
124
b06ded69 125 return $self->new_sub_container(
5a53ef3d 126 name => 'controller',
b06ded69 127 );
f04816ce 128}
129
ff0e9735 130sub build_component_subcontainer {
131 my $self = shift;
132
133 return Bread::Board::Container->new(
134 name => 'component',
135 );
136}
137
5b47e4a9 138sub build_application_name_service {
f04816ce 139 my $self = shift;
292277c1 140
6c743f02 141 return Bread::Board::Literal->new( name => 'application_name', value => $self->application_name );
f04816ce 142}
143
144sub build_driver_service {
145 my $self = shift;
292277c1 146
147 return Bread::Board::Literal->new( name => 'driver', value => $self->driver );
f04816ce 148}
149
150sub build_file_service {
151 my $self = shift;
292277c1 152
153 return Bread::Board::Literal->new( name => 'file', value => $self->file );
f04816ce 154}
155
156sub build_substitutions_service {
157 my $self = shift;
292277c1 158
159 return Bread::Board::Literal->new( name => 'substitutions', value => $self->substitutions );
f04816ce 160}
161
162sub build_extensions_service {
163 my $self = shift;
292277c1 164
165 return Bread::Board::BlockInjection->new(
7a267bb5 166 lifecycle => 'Singleton',
292277c1 167 name => 'extensions',
168 block => sub {
169 return \@{Config::Any->extensions};
170 },
f04816ce 171 );
172}
b4a6fa62 173
f04816ce 174sub build_prefix_service {
175 my $self = shift;
292277c1 176
177 return Bread::Board::BlockInjection->new(
7a267bb5 178 lifecycle => 'Singleton',
292277c1 179 name => 'prefix',
180 block => sub {
dca40344 181 return Catalyst::Utils::appprefix( shift->param('application_name') );
292277c1 182 },
dca40344 183 dependencies => [ depends_on('application_name') ],
f04816ce 184 );
185}
b4a6fa62 186
f04816ce 187sub build_path_service {
188 my $self = shift;
292277c1 189
190 return Bread::Board::BlockInjection->new(
7a267bb5 191 lifecycle => 'Singleton',
292277c1 192 name => 'path',
193 block => sub {
194 my $s = shift;
195
dca40344 196 return Catalyst::Utils::env_value( $s->param('application_name'), 'CONFIG' )
292277c1 197 || $s->param('file')
6c743f02 198 || $s->param('application_name')->path_to( $s->param('prefix') );
292277c1 199 },
6c743f02 200 dependencies => [ depends_on('file'), depends_on('application_name'), depends_on('prefix') ],
f04816ce 201 );
202}
b4a6fa62 203
f04816ce 204sub build_config_service {
205 my $self = shift;
292277c1 206
207 return Bread::Board::BlockInjection->new(
7a267bb5 208 lifecycle => 'Singleton',
292277c1 209 name => 'config',
210 block => sub {
211 my $s = shift;
212
213 my $v = Data::Visitor::Callback->new(
214 plain_value => sub {
215 return unless defined $_;
6c743f02 216 return $self->_config_substitutions( $s->param('application_name'), $s->param('substitutions'), $_ );
292277c1 217 }
218
219 );
220 $v->visit( $s->param('raw_config') );
221 },
6c743f02 222 dependencies => [ depends_on('application_name'), depends_on('raw_config'), depends_on('substitutions') ],
f04816ce 223 );
224}
b4a6fa62 225
f04816ce 226sub build_raw_config_service {
227 my $self = shift;
292277c1 228
229 return Bread::Board::BlockInjection->new(
7a267bb5 230 lifecycle => 'Singleton',
292277c1 231 name => 'raw_config',
232 block => sub {
233 my $s = shift;
234
235 my @global = @{$s->param('global_config')};
236 my @locals = @{$s->param('local_config')};
237
0fcf9a51 238 my $config = $s->param('class_config');
239
292277c1 240 for my $cfg (@global, @locals) {
241 for (keys %$cfg) {
242 $config = Catalyst::Utils::merge_hashes( $config, $cfg->{$_} );
b4a6fa62 243 }
292277c1 244 }
0fcf9a51 245
292277c1 246 return $config;
247 },
0fcf9a51 248 dependencies => [ depends_on('global_config'), depends_on('local_config'), depends_on('class_config') ],
f04816ce 249 );
250}
b4a6fa62 251
f04816ce 252sub build_global_files_service {
253 my $self = shift;
b4a6fa62 254
292277c1 255 return Bread::Board::BlockInjection->new(
7a267bb5 256 lifecycle => 'Singleton',
292277c1 257 name => 'global_files',
258 block => sub {
259 my $s = shift;
b4a6fa62 260
292277c1 261 my ( $path, $extension ) = @{$s->param('config_path')};
b4a6fa62 262
292277c1 263 my @extensions = @{$s->param('extensions')};
264
265 my @files;
266 if ( $extension ) {
267 die "Unable to handle files with the extension '${extension}'" unless grep { $_ eq $extension } @extensions;
268 push @files, $path;
269 } else {
270 @files = map { "$path.$_" } @extensions;
271 }
272 return \@files;
273 },
274 dependencies => [ depends_on('extensions'), depends_on('config_path') ],
f04816ce 275 );
276}
b4a6fa62 277
f04816ce 278sub build_local_files_service {
279 my $self = shift;
292277c1 280
281 return Bread::Board::BlockInjection->new(
7a267bb5 282 lifecycle => 'Singleton',
292277c1 283 name => 'local_files',
284 block => sub {
285 my $s = shift;
286
287 my ( $path, $extension ) = @{$s->param('config_path')};
288 my $suffix = $s->param('config_local_suffix');
289
290 my @extensions = @{$s->param('extensions')};
291
292 my @files;
293 if ( $extension ) {
294 die "Unable to handle files with the extension '${extension}'" unless grep { $_ eq $extension } @extensions;
295 $path =~ s{\.$extension}{_$suffix.$extension};
296 push @files, $path;
297 } else {
298 @files = map { "${path}_${suffix}.$_" } @extensions;
299 }
300 return \@files;
301 },
302 dependencies => [ depends_on('extensions'), depends_on('config_path'), depends_on('config_local_suffix') ],
f04816ce 303 );
304}
b4a6fa62 305
0fcf9a51 306sub build_class_config_service {
307 my $self = shift;
308
309 return Bread::Board::BlockInjection->new(
310 lifecycle => 'Singleton',
311 name => 'class_config',
312 block => sub {
313 my $s = shift;
314 my $app = $s->param('application_name');
315
316 # Container might be called outside Catalyst context
317 return {} unless Class::MOP::is_class_loaded($app);
318
319 # config might not have been defined
320 return $app->config || {};
321 },
322 dependencies => [ depends_on('application_name') ],
323 );
324}
325
f04816ce 326sub build_global_config_service {
327 my $self = shift;
292277c1 328
329 return Bread::Board::BlockInjection->new(
7a267bb5 330 lifecycle => 'Singleton',
292277c1 331 name => 'global_config',
332 block => sub {
333 my $s = shift;
334
335 return Config::Any->load_files({
336 files => $s->param('global_files'),
337 filter => \&_fix_syntax,
338 use_ext => 1,
339 driver_args => $s->param('driver'),
340 });
341 },
342 dependencies => [ depends_on('global_files') ],
f04816ce 343 );
344}
b4a6fa62 345
f04816ce 346sub build_local_config_service {
347 my $self = shift;
292277c1 348
349 return Bread::Board::BlockInjection->new(
7a267bb5 350 lifecycle => 'Singleton',
292277c1 351 name => 'local_config',
352 block => sub {
353 my $s = shift;
354
355 return Config::Any->load_files({
356 files => $s->param('local_files'),
357 filter => \&_fix_syntax,
358 use_ext => 1,
359 driver_args => $s->param('driver'),
360 });
361 },
362 dependencies => [ depends_on('local_files') ],
f04816ce 363 );
364}
b4a6fa62 365
f04816ce 366sub build_config_path_service {
367 my $self = shift;
b4a6fa62 368
292277c1 369 return Bread::Board::BlockInjection->new(
7a267bb5 370 lifecycle => 'Singleton',
292277c1 371 name => 'config_path',
372 block => sub {
373 my $s = shift;
b4a6fa62 374
292277c1 375 my $path = $s->param('path');
376 my $prefix = $s->param('prefix');
b4a6fa62 377
292277c1 378 my ( $extension ) = ( $path =~ m{\.(.{1,4})$} );
379
380 if ( -d $path ) {
381 $path =~ s{[\/\\]$}{};
382 $path .= "/$prefix";
383 }
b4a6fa62 384
292277c1 385 return [ $path, $extension ];
386 },
387 dependencies => [ depends_on('prefix'), depends_on('path') ],
f04816ce 388 );
389}
b4a6fa62 390
f04816ce 391sub build_config_local_suffix_service {
392 my $self = shift;
292277c1 393
394 return Bread::Board::BlockInjection->new(
7a267bb5 395 lifecycle => 'Singleton',
292277c1 396 name => 'config_local_suffix',
397 block => sub {
398 my $s = shift;
6c743f02 399 my $suffix = Catalyst::Utils::env_value( $s->param('application_name'), 'CONFIG_LOCAL_SUFFIX' ) || $self->config_local_suffix;
292277c1 400
401 return $suffix;
402 },
6c743f02 403 dependencies => [ depends_on('application_name') ],
f04816ce 404 );
b4a6fa62 405}
406
3e4f5406 407sub build_locate_components_service {
408 my $self = shift;
409
410 return Bread::Board::BlockInjection->new(
411 lifecycle => 'Singleton',
412 name => 'locate_components',
413 block => sub {
414 my $s = shift;
415 my $class = $s->param('application_name');
416 my $config = $s->param('config')->{ setup_components };
417
418 Catalyst::Exception->throw(
419 qq{You are using search_extra config option. That option is\n} .
420 qq{deprecated, please refer to the documentation for\n} .
421 qq{other ways of achieving the same results.\n}
422 ) if delete $config->{ search_extra };
423
424 my @paths = qw( ::Controller ::C ::Model ::M ::View ::V );
425
426 my $locator = Module::Pluggable::Object->new(
427 search_path => [ map { s/^(?=::)/$class/; $_; } @paths ],
428 %$config
429 );
430
b47ed80b 431 return [ $locator->plugins ];
3e4f5406 432 },
433 dependencies => [ depends_on('application_name'), depends_on('config') ],
434 );
435}
436
88d81c1b 437sub setup_components {
fa6607ec 438 my $self = shift;
88d81c1b 439 my $class = $self->resolve( service => 'application_name' );
440 my @comps = @{ $self->resolve( service => 'locate_components' ) };
441 my %comps = map { $_ => 1 } @comps;
442 my $deprecatedcatalyst_component_names = 0;
fa6607ec 443
88d81c1b 444 for my $component ( @comps ) {
fa6607ec 445
88d81c1b 446 # We pass ignore_loaded here so that overlay files for (e.g.)
447 # Model::DBI::Schema sub-classes are loaded - if it's in @comps
448 # we know M::P::O found a file on disk so this is safe
fa6607ec 449
88d81c1b 450 Catalyst::Utils::ensure_class_loaded( $component, { ignore_loaded => 1 } );
451 }
fa6607ec 452
88d81c1b 453 for my $component (@comps) {
6e2a1222 454 $self->add_component( $component );
88d81c1b 455 # FIXME - $instance->expand_modules() is broken
456 my @expanded_components = $self->expand_component_module( $component );
457
458 if (
459 !$deprecatedcatalyst_component_names &&
460 ($deprecatedcatalyst_component_names = $component =~ m/::[CMV]::/) ||
461 ($deprecatedcatalyst_component_names = grep { /::[CMV]::/ } @expanded_components)
462 ) {
463 # FIXME - should I be calling warn here?
6e2a1222 464 # Maybe it's time to remove it, or become fatal
88d81c1b 465 $class->log->warn(qq{Your application is using the deprecated ::[MVC]:: type naming scheme.\n}.
466 qq{Please switch your class names to ::Model::, ::View:: and ::Controller: as appropriate.\n}
467 );
468 }
fa6607ec 469
88d81c1b 470 for my $component (@expanded_components) {
6e2a1222 471 $self->add_component( $component )
88d81c1b 472 unless $comps{$component};
473 }
474 }
fa6607ec 475
88d81c1b 476 $self->get_sub_container('model')->make_single_default;
477 $self->get_sub_container('view')->make_single_default;
fa6607ec 478}
479
b4a6fa62 480sub _fix_syntax {
481 my $config = shift;
482 my @components = (
483 map +{
484 prefix => $_ eq 'Component' ? '' : $_ . '::',
485 values => delete $config->{ lc $_ } || delete $config->{ $_ }
486 },
487 grep { ref $config->{ lc $_ } || ref $config->{ $_ } }
488 qw( Component Model M View V Controller C Plugin )
489 );
490
491 foreach my $comp ( @components ) {
492 my $prefix = $comp->{ prefix };
493 foreach my $element ( keys %{ $comp->{ values } } ) {
494 $config->{ "$prefix$element" } = $comp->{ values }->{ $element };
495 }
496 }
497}
498
499sub _config_substitutions {
6682389c 500 my ( $self, $name, $subs, $arg ) = @_;
b4a6fa62 501
502 $subs->{ HOME } ||= sub { shift->path_to( '' ); };
503 $subs->{ ENV } ||=
504 sub {
505 my ( $c, $v ) = @_;
506 if (! defined($ENV{$v})) {
507 Catalyst::Exception->throw( message =>
508 "Missing environment variable: $v" );
509 return "";
510 } else {
511 return $ENV{ $v };
512 }
513 };
514 $subs->{ path_to } ||= sub { shift->path_to( @_ ); };
515 $subs->{ literal } ||= sub { return $_[ 1 ]; };
516 my $subsre = join( '|', keys %$subs );
517
6682389c 518 $arg =~ s{__($subsre)(?:\((.+?)\))?__}{ $subs->{ $1 }->( $name, $2 ? split( /,/, $2 ) : () ) }eg;
519 return $arg;
b4a6fa62 520}
521
a17e0ff8 522sub get_component_from_sub_container {
523 my ( $self, $sub_container_name, $name, $c, @args ) = @_;
524
525 my $sub_container = $self->get_sub_container( $sub_container_name );
526
0e747f0c 527 if (!$name) {
a2c0d071 528 my $default = $sub_container->default_component;
0e747f0c 529
530 return $sub_container->get_component( $default, $c, @args )
531 if $default && $sub_container->has_service( $default );
532
a0146296 533 # FIXME - should I be calling $c->log->warn here?
0e747f0c 534 # this is never a controller, so this is safe
535 $c->log->warn( "Calling \$c->$sub_container_name() is not supported unless you specify one of:" );
536 $c->log->warn( "* \$c->config(default_$sub_container_name => 'the name of the default $sub_container_name to use')" );
537 $c->log->warn( "* \$c->stash->{current_$sub_container_name} # the name of the view to use for this request" );
538 $c->log->warn( "* \$c->stash->{current_${sub_container_name}_instance} # the instance of the $sub_container_name to use for this request" );
a2c0d071 539
540 return;
0e747f0c 541 }
542
a17e0ff8 543 return $sub_container->get_component_regexp( $name, $c, @args )
544 if ref $name;
545
546 return $sub_container->get_component( $name, $c, @args )
547 if $sub_container->has_service( $name );
548
549 $c->log->warn(
550 "Attempted to use $sub_container_name '$name', " .
551 "but it does not exist"
552 );
553
554 return;
555}
556
c4aedec7 557sub find_component {
ec17c391 558 my ( $self, $component, @args ) = @_;
f147e6c2 559 my ( $type, $name ) = _get_component_type_name($component);
c4aedec7 560 my @result;
561
d0f954b4 562 return $self->get_component_from_sub_container(
ec17c391 563 $type, $name, @args
d0f954b4 564 ) if $type;
565
c4aedec7 566 my $query = ref $component
567 ? $component
568 : qr{^$component$}
569 ;
570
571 for my $subcontainer_name (qw/model view controller/) {
a0146296 572 my $subcontainer = $self->get_sub_container( $subcontainer_name );
c4aedec7 573 my @components = $subcontainer->get_service_list;
574 @result = grep { m{$component} } @components;
575
ec17c391 576 return map { $subcontainer->get_component( $_, @args ) } @result
c4aedec7 577 if @result;
578 }
579
d0f954b4 580 # one last search for things like $c->comp(qr/::M::/)
581 @result = $self->find_component_regexp(
ec17c391 582 $component, @args
d0f954b4 583 ) if !@result and ref $component;
584
c4aedec7 585 # it expects an empty list on failed searches
586 return @result;
587}
588
4e2b302e 589sub find_component_regexp {
ec17c391 590 my ( $self, $component, @args ) = @_;
4e2b302e 591 my @result;
592
ec17c391 593 my @components = grep { m{$component} } keys %{ $self->get_all_components };
4e2b302e 594
595 for (@components) {
f147e6c2 596 my ($type, $name) = _get_component_type_name($_);
4e2b302e 597
598 push @result, $self->get_component_from_sub_container(
599 $type, $name, @args
600 ) if $type;
601 }
602
603 return @result;
604}
605
9f4997cf 606# FIXME - t0m, how do you feel about this name?
607# also, do you think I should draw it here, or just return the data structure?
608sub get_components_names_types {
be80b0a5 609 my ( $self ) = @_;
9f4997cf 610 my @comps_names_types;
be80b0a5 611
612 for my $sub_container_name (qw/model view controller/) {
613 my $sub_container = $self->get_sub_container($sub_container_name);
614 for my $service ( $sub_container->get_service_list ) {
1b5451ef 615 my $comp = $sub_container->resolve(service => $service);
be80b0a5 616 my $compname = ref $comp || $comp;
617 my $type = ref $comp ? 'instance' : 'class';
9f4997cf 618 push @comps_names_types, [ $compname, $type ];
be80b0a5 619 }
620 }
621
9f4997cf 622 return @comps_names_types;
be80b0a5 623}
c4aedec7 624
b4410fc3 625sub get_all_components {
626 my $self = shift;
627 my %components;
628
ec17c391 629 my $container = $self->get_sub_container('component');
b4410fc3 630
ec17c391 631 for my $component ($container->get_service_list) {
632 my $comp = $container->resolve(
633 service => $component
634 );
635 my $comp_name = ref $comp || $comp;
636 $components{$comp_name} = $comp;
b4410fc3 637 }
638
639 return lock_hash %components;
640}
641
f147e6c2 642sub add_component {
6e2a1222 643 my ( $self, $component ) = @_;
f147e6c2 644 my ( $type, $name ) = _get_component_type_name($component);
645
646 return unless $type;
647
ff0e9735 648 my $component_service_name = "${type}_${name}";
649
b844dcad 650 # The 'component' sub-container will create the object, and store it's
651 # instance, which, by default, will live throughout the application.
652 # The model/view/controller sub-containers only reference the instance
653 # held in the aforementioned sub-container, and execute the ACCEPT_CONTEXT
654 # sub every time they are called, when it exists.
655 my $instance_container = $self->get_sub_container('component');
656 my $accept_context_container = $self->get_sub_container($type);
657
658 $instance_container->add_service(
b7da37bd 659 Catalyst::IOC::ConstructorInjection->new(
ff0e9735 660 name => $component_service_name,
b7da37bd 661 class => $component,
ff0e9735 662 lifecycle => 'Singleton',
bf142143 663 dependencies => [
664 depends_on( '/application_name' ),
665 depends_on( '/config' ),
666 ],
ff0e9735 667 )
b844dcad 668 ) unless $instance_container->has_service( $component_service_name );
669 # ^ custom containers might have added the service already.
670 # we don't want to override that.
ff0e9735 671
b844dcad 672 $accept_context_container->add_service(
ff0e9735 673 Catalyst::IOC::BlockInjection->new(
674 name => $name,
675 dependencies => [
676 depends_on( "/component/$component_service_name" ),
677 ],
b844dcad 678 block => sub { shift->param($component_service_name) },
f147e6c2 679 )
b844dcad 680 ) unless $accept_context_container->has_service( $name );
681 # ^ same as above
f147e6c2 682}
683
684# FIXME: should this sub exist?
685# should it be moved to Catalyst::Utils,
686# or replaced by something already existing there?
687sub _get_component_type_name {
688 my ( $component ) = @_;
689
690 my @parts = split /::/, $component;
691
692 while (my $type = shift @parts) {
693 return ('controller', join '::', @parts)
694 if $type =~ /^(c|controller)$/i;
695
696 return ('model', join '::', @parts)
697 if $type =~ /^(m|model)$/i;
698
699 return ('view', join '::', @parts)
700 if $type =~ /^(v|view)$/i;
701 }
702
703 return (undef, $component);
704}
705
d3742403 706sub expand_component_module {
707 my ( $class, $module ) = @_;
708 return Devel::InnerPackage::list_packages( $module );
709}
0dff29e2 710
d057ddb9 7111;
712
713__END__
714
715=pod
716
717=head1 NAME
718
719Catalyst::Container - IOC for Catalyst components
720
2c2ed473 721=head1 SYNOPSIS
722
723=head1 DESCRIPTION
724
d057ddb9 725=head1 METHODS
726
bbb306a9 727=head1 Building Containers
a0146296 728
309caf39 729=head2 build_component_subcontainer
730
731Container that stores all components, i.e. all models, views and controllers
732together. Each service is an instance of the actual component, and by default
733it lives while the application is running. Retrieving components from this
734subcontainer will instantiate the component, if it hasn't been instantiated
735already, but will not execute ACCEPT_CONTEXT.
736
d057ddb9 737=head2 build_model_subcontainer
738
309caf39 739Container that stores references for all models that are inside the components
740subcontainer. Retrieving a model triggers ACCEPT_CONTEXT, if it exists.
a0146296 741
d057ddb9 742=head2 build_view_subcontainer
743
309caf39 744Same as L<build_model_subcontainer>, but for views.
a0146296 745
d057ddb9 746=head2 build_controller_subcontainer
747
309caf39 748Same as L<build_model_subcontainer>, but for controllers. The difference is
749that there is no ACCEPT_CONTEXT for controllers.
a0146296 750
bbb306a9 751=head1 Building Services
a0146296 752
a4541222 753=head2 build_application_name_service
d057ddb9 754
a4541222 755Name of the application (such as MyApp).
a0146296 756
d057ddb9 757=head2 build_driver_service
758
a0146296 759Config options passed directly to the driver being used.
760
d057ddb9 761=head2 build_file_service
762
a0146296 763?
764
d057ddb9 765=head2 build_substitutions_service
766
bbb306a9 767This method substitutes macros found with calls to a function. There are a
768number of default macros:
769
770=over
771
772=item * C<__HOME__> - replaced with C<$c-E<gt>path_to('')>
773
774=item * C<__ENV(foo)__> - replaced with the value of C<$ENV{foo}>
775
776=item * C<__path_to(foo/bar)__> - replaced with C<$c-E<gt>path_to('foo/bar')>
777
778=item * C<__literal(__FOO__)__> - leaves __FOO__ alone (allows you to use
779C<__DATA__> as a config value, for example)
780
781=back
782
783The parameter list is split on comma (C<,>). You can override this method to
784do your own string munging, or you can define your own macros in
5faa454d 785C<< <MyApp->config( 'Plugin::ConfigLoader' => { substitutions => { ... } } ) >>.
bbb306a9 786Example:
787
5faa454d 788 MyApp->config( 'Plugin::ConfigLoader' => {
789 substitutions => {
790 baz => sub { my $c = shift; qux( @_ ); },
791 },
792 });
bbb306a9 793
794The above will respond to C<__baz(x,y)__> in config strings.
a0146296 795
d057ddb9 796=head2 build_extensions_service
797
bbb306a9 798Config::Any's available config file extensions (e.g. xml, json, pl, etc).
799
d057ddb9 800=head2 build_prefix_service
801
bbb306a9 802The prefix, based on the application name, that will be used to lookup the
803config files (which will be in the format $prefix.$extension). If the app is
804MyApp::Foo, the prefix will be myapp_foo.
805
d057ddb9 806=head2 build_path_service
807
bbb306a9 808The path to the config file (or environment variable, if defined).
809
d057ddb9 810=head2 build_config_service
811
bbb306a9 812The resulting configuration for the application, after it has successfully
813been loaded, and all substitutions have been made.
814
d057ddb9 815=head2 build_raw_config_service
816
bbb306a9 817The merge of local_config and global_config hashes, before substitutions.
818
d057ddb9 819=head2 build_global_files_service
820
bbb306a9 821Gets all files for config that don't have the local_suffix, such as myapp.conf.
822
d057ddb9 823=head2 build_local_files_service
824
bbb306a9 825Gets all files for config that have the local_suffix, such as myapp_local.conf.
826
d057ddb9 827=head2 build_global_config_service
828
bbb306a9 829Reads config from global_files.
830
d057ddb9 831=head2 build_local_config_service
832
bbb306a9 833Reads config from local_files.
834
800a31b0 835=head2 build_class_config_service
836
837Reads config set from the application's class attribute config,
838i.e. MyApp->config( name => 'MyApp', ... )
839
d057ddb9 840=head2 build_config_path_service
841
bbb306a9 842Splits the path to the config file, and returns on array ref containing
843the path to the config file minus the extension in the first position,
844and the extension in the second.
845
d057ddb9 846=head2 build_config_local_suffix_service
847
a0146296 848Determines the suffix of files used to override the main config. By default
849this value is C<local>, which will load C<myapp_local.conf>. The suffix can
850be specified in the following order of preference:
851
852=over
853
854=item * C<$ENV{ MYAPP_CONFIG_LOCAL_SUFFIX }>
855
856=item * C<$ENV{ CATALYST_CONFIG_LOCAL_SUFFIX }>
857
858=back
859
860The first one of these values found replaces the default of C<local> in the
861name of the local config file to be loaded.
862
863For example, if C< $ENV{ MYAPP_CONFIG_LOCAL_SUFFIX }> is set to C<testing>,
864ConfigLoader will try and load C<myapp_testing.conf> instead of
865C<myapp_local.conf>.
866
bbb306a9 867=head2 build_locate_components_service
868
869This method is meant to provide a list of component modules that should be
870setup for the application. By default, it will use L<Module::Pluggable>.
871
872Specify a C<setup_components> config option to pass additional options directly
873to L<Module::Pluggable>.
874
875=head1 Other methods
876
a0146296 877=head2 get_component_from_sub_container($sub_container, $name, $c, @args)
878
164a0b2b 879Looks for components in a given subcontainer (such as controller, model or
880view), and returns the searched component. If $name is undef, it returns the
881default component (such as default_view, if $sub_container is 'view'). If
882$name is a regexp, it returns an array of matching components. Otherwise, it
883looks for the component with name $name.
8dc2fca3 884
9f4997cf 885=head2 get_components_names_types
886
164a0b2b 887Gets all components from all containers and returns them as an array of
888arrayrefs containing the component name and the component type (i.e., whether
889it's an instance or a class).
409db9cb 890
b4410fc3 891=head2 get_all_components
892
164a0b2b 893Fetches all the components, in each of the sub_containers model, view and
894controller, and returns a readonly hash. The keys are the class names, and
895the values are the blessed objects. This is what is returned by $c->components.
a0146296 896
f147e6c2 897=head2 add_component
898
164a0b2b 899Adds a component to the appropriate subcontainer. The subcontainer is guessed
900by the component name given.
a0146296 901
c4aedec7 902=head2 find_component
903
164a0b2b 904Searches for components in all containers. If $component is the full class
905name, the subcontainer is guessed, and it gets the searched component in there.
906Otherwise, it looks for a component with that name in all subcontainers. If
907$component is a regexp, it calls the method below, find_component_regexp,
908and matches all components against that regexp.
a0146296 909
4e2b302e 910=head2 find_component_regexp
911
a0146296 912Finds components that match a given regexp. Used internally, by find_component.
913
6e2a1222 914=head2 expand_component_module
d3742403 915
916Components found by C<locate_components> will be passed to this method, which
917is expected to return a list of component (package) names to be set up.
918
ebf7f0a5 919=head2 setup_components
fa6607ec 920
bf3c8088 921=head1 AUTHORS
922
e8ed391e 923Catalyst Contributors, see Catalyst.pm
bf3c8088 924
e8ed391e 925=head1 COPYRIGHT
bf3c8088 926
927This library is free software. You can redistribute it and/or modify it under
928the same terms as Perl itself.
929
930=cut