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