name -> application_name in POD
[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
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
6c743f02 129 return Bread::Board::Literal->new( name => 'application_name', value => $self->application_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')
6c743f02 186 || $s->param('application_name')->path_to( $s->param('prefix') );
292277c1 187 },
6c743f02 188 dependencies => [ depends_on('file'), depends_on('application_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 $_;
6c743f02 204 return $self->_config_substitutions( $s->param('application_name'), $s->param('substitutions'), $_ );
292277c1 205 }
206
207 );
208 $v->visit( $s->param('raw_config') );
209 },
6c743f02 210 dependencies => [ depends_on('application_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;
6c743f02 365 my $suffix = Catalyst::Utils::env_value( $s->param('application_name'), 'CONFIG_LOCAL_SUFFIX' ) || $self->config_local_suffix;
292277c1 366
367 return $suffix;
368 },
6c743f02 369 dependencies => [ depends_on('application_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;
0811a16f 653 my $deprecatedcatalyst_component_names = 0;
a1c3fa90 654
655 for my $component ( @comps ) {
656
657 # We pass ignore_loaded here so that overlay files for (e.g.)
658 # Model::DBI::Schema sub-classes are loaded - if it's in @comps
659 # we know M::P::O found a file on disk so this is safe
660
661 Catalyst::Utils::ensure_class_loaded( $component, { ignore_loaded => 1 } );
662 }
663
664 for my $component (@comps) {
665 $self->add_component( $component, $class );
666 # FIXME - $instance->expand_modules() is broken
667 my @expanded_components = $self->expand_component_module( $component );
a1c3fa90 668
0811a16f 669 if (
670 !$deprecatedcatalyst_component_names &&
671 ($deprecatedcatalyst_component_names = $component =~ m/::[CMV]::/) ||
672 ($deprecatedcatalyst_component_names = grep { /::[CMV]::/ } @expanded_components)
673 ) {
a1c3fa90 674 # FIXME - should I be calling warn here?
675 $class->log->warn(qq{Your application is using the deprecated ::[MVC]:: type naming scheme.\n}.
676 qq{Please switch your class names to ::Model::, ::View:: and ::Controller: as appropriate.\n}
0811a16f 677 );
678 }
a1c3fa90 679
0811a16f 680 for my $component (@expanded_components) {
681 $self->add_component( $component, $class )
682 unless $comps{$component};
a1c3fa90 683 }
684 }
685
686 $self->get_sub_container('model')->make_single_default;
687 $self->get_sub_container('view')->make_single_default;
688}
689
d057ddb9 6901;
691
692__END__
693
694=pod
695
696=head1 NAME
697
698Catalyst::Container - IOC for Catalyst components
699
2c2ed473 700=head1 SYNOPSIS
701
702=head1 DESCRIPTION
703
d057ddb9 704=head1 METHODS
705
a0146296 706=head1 Containers
707
d057ddb9 708=head2 build_model_subcontainer
709
a0146296 710Container that stores all models.
711
d057ddb9 712=head2 build_view_subcontainer
713
a0146296 714Container that stores all views.
715
d057ddb9 716=head2 build_controller_subcontainer
717
a0146296 718Container that stores all controllers.
719
720=head1 Services
721
a4541222 722=head2 build_application_name_service
d057ddb9 723
a4541222 724Name of the application (such as MyApp).
a0146296 725
d057ddb9 726=head2 build_driver_service
727
a0146296 728Config options passed directly to the driver being used.
729
d057ddb9 730=head2 build_file_service
731
a0146296 732?
733
d057ddb9 734=head2 build_substitutions_service
735
a0146296 736Executes all the substitutions in config. See L</_config_substitutions> method.
737
d057ddb9 738=head2 build_extensions_service
739
740=head2 build_prefix_service
741
742=head2 build_path_service
743
744=head2 build_config_service
745
746=head2 build_raw_config_service
747
748=head2 build_global_files_service
749
750=head2 build_local_files_service
751
752=head2 build_global_config_service
753
754=head2 build_local_config_service
755
756=head2 build_config_path_service
757
758=head2 build_config_local_suffix_service
759
a0146296 760Determines the suffix of files used to override the main config. By default
761this value is C<local>, which will load C<myapp_local.conf>. The suffix can
762be specified in the following order of preference:
763
764=over
765
766=item * C<$ENV{ MYAPP_CONFIG_LOCAL_SUFFIX }>
767
768=item * C<$ENV{ CATALYST_CONFIG_LOCAL_SUFFIX }>
769
770=back
771
772The first one of these values found replaces the default of C<local> in the
773name of the local config file to be loaded.
774
775For example, if C< $ENV{ MYAPP_CONFIG_LOCAL_SUFFIX }> is set to C<testing>,
776ConfigLoader will try and load C<myapp_testing.conf> instead of
777C<myapp_local.conf>.
778
779=head2 get_component_from_sub_container($sub_container, $name, $c, @args)
780
781Looks 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 782
409db9cb 783=head2 get_components_types
784
b4410fc3 785=head2 get_all_components
786
a0146296 787Fetches 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.
788
f147e6c2 789=head2 add_component
790
a0146296 791Adds a component to the appropriate subcontainer. The subcontainer is guessed by the component name given.
792
c4aedec7 793=head2 find_component
794
a0146296 795Searches 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.
796
4e2b302e 797=head2 find_component_regexp
798
a0146296 799Finds components that match a given regexp. Used internally, by find_component.
800
0dff29e2 801=head2 setup_component
802
d057ddb9 803=head2 _fix_syntax
804
805=head2 _config_substitutions
806
a0146296 807This method substitutes macros found with calls to a function. There are a
808number of default macros:
809
810=over
811
812=item * C<__HOME__> - replaced with C<$c-E<gt>path_to('')>
813
814=item * C<__ENV(foo)__> - replaced with the value of C<$ENV{foo}>
815
816=item * C<__path_to(foo/bar)__> - replaced with C<$c-E<gt>path_to('foo/bar')>
817
818=item * C<__literal(__FOO__)__> - leaves __FOO__ alone (allows you to use
819C<__DATA__> as a config value, for example)
820
821=back
822
823The parameter list is split on comma (C<,>). You can override this method to
824do your own string munging, or you can define your own macros in
825C<MyApp-E<gt>config-E<gt>{ 'Plugin::ConfigLoader' }-E<gt>{ substitutions }>.
826Example:
827
828 MyApp->config->{ 'Plugin::ConfigLoader' }->{ substitutions } = {
829 baz => sub { my $c = shift; qux( @_ ); }
830 }
831
832The above will respond to C<__baz(x,y)__> in config strings.
833
d3742403 834=head2 $c->expand_component_module( $component, $setup_component_config )
835
836Components found by C<locate_components> will be passed to this method, which
837is expected to return a list of component (package) names to be set up.
838
2f32de9a 839=head2 locate_components( $setup_component_config )
840
841This method is meant to provide a list of component modules that should be
842setup for the application. By default, it will use L<Module::Pluggable>.
843
844Specify a C<setup_components> config option to pass additional options directly
845to L<Module::Pluggable>.
d3742403 846
bf3c8088 847=head1 AUTHORS
848
e8ed391e 849Catalyst Contributors, see Catalyst.pm
bf3c8088 850
e8ed391e 851=head1 COPYRIGHT
bf3c8088 852
853This library is free software. You can redistribute it and/or modify it under
854the same terms as Perl itself.
855
856=cut