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