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