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