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