fixme and warning
[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
287df7b2 642 # big problem when setting up the dispatcher - this method is called
643 # as $container->get_all_components('MyApp'). What to do with Request
644 # life cycles?
3552850b 645 foreach my $type (qw/model view controller /) {
b32d8169 646 my $container = $self->get_sub_container($type);
3552850b 647
648 for my $component ($container->get_service_list) {
9084f394 649 my $comp_service = $container->get_service($component);
c887dd0d 650
94e35f00 651 warn "getting $component in $class, type $type";
652
9084f394 653 $components{$comp_service->catalyst_component_name} = $comp_service->get(ctx => $class);
3552850b 654 }
b4410fc3 655 }
656
657 return lock_hash %components;
658}
659
f147e6c2 660sub add_component {
6e2a1222 661 my ( $self, $component ) = @_;
f147e6c2 662 my ( $type, $name ) = _get_component_type_name($component);
663
664 return unless $type;
665
b844dcad 666 # The 'component' sub-container will create the object, and store it's
667 # instance, which, by default, will live throughout the application.
668 # The model/view/controller sub-containers only reference the instance
669 # held in the aforementioned sub-container, and execute the ACCEPT_CONTEXT
670 # sub every time they are called, when it exists.
671 my $instance_container = $self->get_sub_container('component');
672 my $accept_context_container = $self->get_sub_container($type);
673
25af999b 674 # Custom containers might have added the service already
675 # We don't want to override that
676 return if $accept_context_container->has_service( $name );
677
678 my $component_service_name = "${type}_${name}";
679
b844dcad 680 $instance_container->add_service(
b7da37bd 681 Catalyst::IOC::ConstructorInjection->new(
ff0e9735 682 name => $component_service_name,
28ec44a3 683 catalyst_component_name => $component,
b7da37bd 684 class => $component,
ff0e9735 685 lifecycle => 'Singleton',
bf142143 686 dependencies => [
687 depends_on( '/application_name' ),
bf142143 688 ],
ff0e9735 689 )
25af999b 690 );
ff0e9735 691
b844dcad 692 $accept_context_container->add_service(
ff0e9735 693 Catalyst::IOC::BlockInjection->new(
694 name => $name,
9084f394 695 catalyst_component_name => $component,
ff0e9735 696 dependencies => [
697 depends_on( "/component/$component_service_name" ),
698 ],
b844dcad 699 block => sub { shift->param($component_service_name) },
f147e6c2 700 )
25af999b 701 );
f147e6c2 702}
703
704# FIXME: should this sub exist?
705# should it be moved to Catalyst::Utils,
706# or replaced by something already existing there?
707sub _get_component_type_name {
708 my ( $component ) = @_;
ae0690a5 709 my $result;
f147e6c2 710
ae0690a5 711 while ( !$result and (my $index = index $component, '::') > 0 ) {
712 my $type = lc substr $component, 0, $index;
713 $component = substr $component, $index + 2;
714 $result = first { $type eq $_ or $type eq substr($_, 0, 1) }
715 qw{ model view controller };
f147e6c2 716 }
717
ae0690a5 718 return ($result, $component);
f147e6c2 719}
720
d3742403 721sub expand_component_module {
722 my ( $class, $module ) = @_;
723 return Devel::InnerPackage::list_packages( $module );
724}
0dff29e2 725
d057ddb9 7261;
727
728__END__
729
730=pod
731
732=head1 NAME
733
734Catalyst::Container - IOC for Catalyst components
735
2c2ed473 736=head1 SYNOPSIS
737
738=head1 DESCRIPTION
739
d057ddb9 740=head1 METHODS
741
bbb306a9 742=head1 Building Containers
a0146296 743
309caf39 744=head2 build_component_subcontainer
745
746Container that stores all components, i.e. all models, views and controllers
747together. Each service is an instance of the actual component, and by default
748it lives while the application is running. Retrieving components from this
749subcontainer will instantiate the component, if it hasn't been instantiated
750already, but will not execute ACCEPT_CONTEXT.
751
d057ddb9 752=head2 build_model_subcontainer
753
309caf39 754Container that stores references for all models that are inside the components
755subcontainer. Retrieving a model triggers ACCEPT_CONTEXT, if it exists.
a0146296 756
d057ddb9 757=head2 build_view_subcontainer
758
309caf39 759Same as L<build_model_subcontainer>, but for views.
a0146296 760
d057ddb9 761=head2 build_controller_subcontainer
762
59221ae6 763Same as L<build_model_subcontainer>, but for controllers.
a0146296 764
bbb306a9 765=head1 Building Services
a0146296 766
a4541222 767=head2 build_application_name_service
d057ddb9 768
a4541222 769Name of the application (such as MyApp).
a0146296 770
d057ddb9 771=head2 build_driver_service
772
a0146296 773Config options passed directly to the driver being used.
774
d057ddb9 775=head2 build_file_service
776
a0146296 777?
778
d057ddb9 779=head2 build_substitutions_service
780
bbb306a9 781This method substitutes macros found with calls to a function. There are a
782number of default macros:
783
784=over
785
786=item * C<__HOME__> - replaced with C<$c-E<gt>path_to('')>
787
788=item * C<__ENV(foo)__> - replaced with the value of C<$ENV{foo}>
789
790=item * C<__path_to(foo/bar)__> - replaced with C<$c-E<gt>path_to('foo/bar')>
791
792=item * C<__literal(__FOO__)__> - leaves __FOO__ alone (allows you to use
793C<__DATA__> as a config value, for example)
794
795=back
796
797The parameter list is split on comma (C<,>). You can override this method to
798do your own string munging, or you can define your own macros in
5faa454d 799C<< <MyApp->config( 'Plugin::ConfigLoader' => { substitutions => { ... } } ) >>.
bbb306a9 800Example:
801
5faa454d 802 MyApp->config( 'Plugin::ConfigLoader' => {
803 substitutions => {
804 baz => sub { my $c = shift; qux( @_ ); },
805 },
806 });
bbb306a9 807
808The above will respond to C<__baz(x,y)__> in config strings.
a0146296 809
d057ddb9 810=head2 build_extensions_service
811
bbb306a9 812Config::Any's available config file extensions (e.g. xml, json, pl, etc).
813
d057ddb9 814=head2 build_prefix_service
815
bbb306a9 816The prefix, based on the application name, that will be used to lookup the
817config files (which will be in the format $prefix.$extension). If the app is
818MyApp::Foo, the prefix will be myapp_foo.
819
d057ddb9 820=head2 build_path_service
821
bbb306a9 822The path to the config file (or environment variable, if defined).
823
d057ddb9 824=head2 build_config_service
825
bbb306a9 826The resulting configuration for the application, after it has successfully
827been loaded, and all substitutions have been made.
828
d057ddb9 829=head2 build_raw_config_service
830
bbb306a9 831The merge of local_config and global_config hashes, before substitutions.
832
d057ddb9 833=head2 build_global_files_service
834
bbb306a9 835Gets all files for config that don't have the local_suffix, such as myapp.conf.
836
d057ddb9 837=head2 build_local_files_service
838
bbb306a9 839Gets all files for config that have the local_suffix, such as myapp_local.conf.
840
d057ddb9 841=head2 build_global_config_service
842
bbb306a9 843Reads config from global_files.
844
d057ddb9 845=head2 build_local_config_service
846
bbb306a9 847Reads config from local_files.
848
800a31b0 849=head2 build_class_config_service
850
851Reads config set from the application's class attribute config,
852i.e. MyApp->config( name => 'MyApp', ... )
853
d057ddb9 854=head2 build_config_path_service
855
bbb306a9 856Splits the path to the config file, and returns on array ref containing
857the path to the config file minus the extension in the first position,
858and the extension in the second.
859
d057ddb9 860=head2 build_config_local_suffix_service
861
a0146296 862Determines the suffix of files used to override the main config. By default
863this value is C<local>, which will load C<myapp_local.conf>. The suffix can
864be specified in the following order of preference:
865
866=over
867
868=item * C<$ENV{ MYAPP_CONFIG_LOCAL_SUFFIX }>
869
870=item * C<$ENV{ CATALYST_CONFIG_LOCAL_SUFFIX }>
871
872=back
873
874The first one of these values found replaces the default of C<local> in the
875name of the local config file to be loaded.
876
877For example, if C< $ENV{ MYAPP_CONFIG_LOCAL_SUFFIX }> is set to C<testing>,
878ConfigLoader will try and load C<myapp_testing.conf> instead of
879C<myapp_local.conf>.
880
bbb306a9 881=head2 build_locate_components_service
882
883This method is meant to provide a list of component modules that should be
884setup for the application. By default, it will use L<Module::Pluggable>.
885
886Specify a C<setup_components> config option to pass additional options directly
887to L<Module::Pluggable>.
888
889=head1 Other methods
890
a0146296 891=head2 get_component_from_sub_container($sub_container, $name, $c, @args)
892
164a0b2b 893Looks for components in a given subcontainer (such as controller, model or
894view), and returns the searched component. If $name is undef, it returns the
895default component (such as default_view, if $sub_container is 'view'). If
896$name is a regexp, it returns an array of matching components. Otherwise, it
897looks for the component with name $name.
8dc2fca3 898
b4410fc3 899=head2 get_all_components
900
164a0b2b 901Fetches all the components, in each of the sub_containers model, view and
902controller, and returns a readonly hash. The keys are the class names, and
903the values are the blessed objects. This is what is returned by $c->components.
a0146296 904
f147e6c2 905=head2 add_component
906
164a0b2b 907Adds a component to the appropriate subcontainer. The subcontainer is guessed
908by the component name given.
a0146296 909
c4aedec7 910=head2 find_component
911
164a0b2b 912Searches for components in all containers. If $component is the full class
913name, the subcontainer is guessed, and it gets the searched component in there.
914Otherwise, it looks for a component with that name in all subcontainers. If
ae9bf2af 915$component is a regexp it calls _find_component_regexp and matches all
916components against that regexp.
a0146296 917
6e2a1222 918=head2 expand_component_module
d3742403 919
920Components found by C<locate_components> will be passed to this method, which
921is expected to return a list of component (package) names to be set up.
922
ebf7f0a5 923=head2 setup_components
fa6607ec 924
bf3c8088 925=head1 AUTHORS
926
e8ed391e 927Catalyst Contributors, see Catalyst.pm
bf3c8088 928
e8ed391e 929=head1 COPYRIGHT
bf3c8088 930
931This library is free software. You can redistribute it and/or modify it under
932the same terms as Perl itself.
933
934=cut