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