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