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