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