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 (); |
2bb0da6d |
7 | use MooseX::Types::LoadableClass qw/ LoadableClass /; |
a6c13ff4 |
8 | use Catalyst::IOC::BlockInjection; |
8b749525 |
9 | use namespace::autoclean; |
b4a6fa62 |
10 | |
11 | extends 'Bread::Board::Container'; |
12 | |
13 | has config_local_suffix => ( |
442ab13e |
14 | is => 'ro', |
b4a6fa62 |
15 | isa => 'Str', |
16 | default => 'local', |
17 | ); |
18 | |
19 | has driver => ( |
442ab13e |
20 | is => 'ro', |
b4a6fa62 |
21 | isa => 'HashRef', |
22 | default => sub { +{} }, |
23 | ); |
24 | |
25 | has file => ( |
442ab13e |
26 | is => 'ro', |
b4a6fa62 |
27 | isa => 'Str', |
28 | default => '', |
29 | ); |
30 | |
31 | has substitutions => ( |
442ab13e |
32 | is => 'ro', |
b4a6fa62 |
33 | isa => 'HashRef', |
34 | default => sub { +{} }, |
35 | ); |
36 | |
37 | has name => ( |
442ab13e |
38 | is => 'ro', |
b4a6fa62 |
39 | isa => 'Str', |
40 | default => 'TestApp', |
41 | ); |
42 | |
2bb0da6d |
43 | has sub_container_class => ( |
44 | isa => LoadableClass, |
45 | is => 'ro', |
46 | coerce => 1, |
a6c13ff4 |
47 | default => 'Catalyst::IOC::SubContainer', |
8b749525 |
48 | handles => { |
49 | new_sub_container => 'new', |
50 | } |
2bb0da6d |
51 | ); |
52 | |
b4a6fa62 |
53 | sub BUILD { |
a2c0d071 |
54 | my ( $self, $params ) = @_; |
b4a6fa62 |
55 | |
292277c1 |
56 | $self->add_service( |
57 | $self->${\"build_${_}_service"} |
58 | ) for qw/ |
7451d1ea |
59 | substitutions |
60 | file |
61 | driver |
62 | name |
63 | prefix |
64 | extensions |
65 | path |
66 | config |
67 | raw_config |
68 | global_files |
69 | local_files |
70 | global_config |
71 | local_config |
72 | config_local_suffix |
73 | config_path |
74 | /; |
f04816ce |
75 | |
292277c1 |
76 | $self->add_sub_container( |
a2c0d071 |
77 | $self->build_controller_subcontainer |
78 | ); |
79 | |
2921bab3 |
80 | my $config = $self->resolve( service => 'config' ); |
81 | my $default_view = $params->{default_view} || $config->{default_view}; |
82 | my $default_model = $params->{default_model} || $config->{default_model}; |
83 | |
a2c0d071 |
84 | $self->add_sub_container( |
85 | $self->build_view_subcontainer( |
2921bab3 |
86 | default_component => $default_view, |
a2c0d071 |
87 | ) |
88 | ); |
89 | |
90 | $self->add_sub_container( |
91 | $self->build_model_subcontainer( |
2921bab3 |
92 | default_component => $default_model, |
a2c0d071 |
93 | ) |
94 | ); |
f04816ce |
95 | } |
96 | |
97 | sub build_model_subcontainer { |
98 | my $self = shift; |
99 | |
a2c0d071 |
100 | return $self->new_sub_container( @_, |
5a53ef3d |
101 | name => 'model', |
b06ded69 |
102 | ); |
f04816ce |
103 | } |
104 | |
105 | sub build_view_subcontainer { |
106 | my $self = shift; |
107 | |
a2c0d071 |
108 | return $self->new_sub_container( @_, |
5a53ef3d |
109 | name => 'view', |
b06ded69 |
110 | ); |
f04816ce |
111 | } |
112 | |
113 | sub build_controller_subcontainer { |
114 | my $self = shift; |
115 | |
b06ded69 |
116 | return $self->new_sub_container( |
5a53ef3d |
117 | name => 'controller', |
b06ded69 |
118 | ); |
f04816ce |
119 | } |
120 | |
f04816ce |
121 | sub build_name_service { |
122 | my $self = shift; |
292277c1 |
123 | |
124 | return Bread::Board::Literal->new( name => 'name', value => $self->name ); |
f04816ce |
125 | } |
126 | |
127 | sub build_driver_service { |
128 | my $self = shift; |
292277c1 |
129 | |
130 | return Bread::Board::Literal->new( name => 'driver', value => $self->driver ); |
f04816ce |
131 | } |
132 | |
133 | sub build_file_service { |
134 | my $self = shift; |
292277c1 |
135 | |
136 | return Bread::Board::Literal->new( name => 'file', value => $self->file ); |
f04816ce |
137 | } |
138 | |
139 | sub build_substitutions_service { |
140 | my $self = shift; |
292277c1 |
141 | |
142 | return Bread::Board::Literal->new( name => 'substitutions', value => $self->substitutions ); |
f04816ce |
143 | } |
144 | |
145 | sub build_extensions_service { |
146 | my $self = shift; |
292277c1 |
147 | |
148 | return Bread::Board::BlockInjection->new( |
149 | name => 'extensions', |
150 | block => sub { |
151 | return \@{Config::Any->extensions}; |
152 | }, |
f04816ce |
153 | ); |
154 | } |
b4a6fa62 |
155 | |
f04816ce |
156 | sub build_prefix_service { |
157 | my $self = shift; |
292277c1 |
158 | |
159 | return Bread::Board::BlockInjection->new( |
160 | name => 'prefix', |
161 | block => sub { |
162 | return Catalyst::Utils::appprefix( shift->param('name') ); |
163 | }, |
164 | dependencies => [ depends_on('name') ], |
f04816ce |
165 | ); |
166 | } |
b4a6fa62 |
167 | |
f04816ce |
168 | sub build_path_service { |
169 | my $self = shift; |
292277c1 |
170 | |
171 | return Bread::Board::BlockInjection->new( |
172 | name => 'path', |
173 | block => sub { |
174 | my $s = shift; |
175 | |
176 | return Catalyst::Utils::env_value( $s->param('name'), 'CONFIG' ) |
177 | || $s->param('file') |
178 | || $s->param('name')->path_to( $s->param('prefix') ); |
179 | }, |
180 | dependencies => [ depends_on('file'), depends_on('name'), depends_on('prefix') ], |
f04816ce |
181 | ); |
182 | } |
b4a6fa62 |
183 | |
f04816ce |
184 | sub build_config_service { |
185 | my $self = shift; |
292277c1 |
186 | |
187 | return Bread::Board::BlockInjection->new( |
188 | name => 'config', |
189 | block => sub { |
190 | my $s = shift; |
191 | |
192 | my $v = Data::Visitor::Callback->new( |
193 | plain_value => sub { |
194 | return unless defined $_; |
195 | return $self->_config_substitutions( $s->param('name'), $s->param('substitutions'), $_ ); |
196 | } |
197 | |
198 | ); |
199 | $v->visit( $s->param('raw_config') ); |
200 | }, |
201 | dependencies => [ depends_on('name'), depends_on('raw_config'), depends_on('substitutions') ], |
f04816ce |
202 | ); |
203 | } |
b4a6fa62 |
204 | |
f04816ce |
205 | sub build_raw_config_service { |
206 | my $self = shift; |
292277c1 |
207 | |
208 | return Bread::Board::BlockInjection->new( |
209 | name => 'raw_config', |
210 | block => sub { |
211 | my $s = shift; |
212 | |
213 | my @global = @{$s->param('global_config')}; |
214 | my @locals = @{$s->param('local_config')}; |
215 | |
216 | my $config = {}; |
217 | for my $cfg (@global, @locals) { |
218 | for (keys %$cfg) { |
219 | $config = Catalyst::Utils::merge_hashes( $config, $cfg->{$_} ); |
b4a6fa62 |
220 | } |
292277c1 |
221 | } |
222 | return $config; |
223 | }, |
224 | dependencies => [ depends_on('global_config'), depends_on('local_config') ], |
f04816ce |
225 | ); |
226 | } |
b4a6fa62 |
227 | |
f04816ce |
228 | sub build_global_files_service { |
229 | my $self = shift; |
b4a6fa62 |
230 | |
292277c1 |
231 | return Bread::Board::BlockInjection->new( |
232 | name => 'global_files', |
233 | block => sub { |
234 | my $s = shift; |
b4a6fa62 |
235 | |
292277c1 |
236 | my ( $path, $extension ) = @{$s->param('config_path')}; |
b4a6fa62 |
237 | |
292277c1 |
238 | my @extensions = @{$s->param('extensions')}; |
239 | |
240 | my @files; |
241 | if ( $extension ) { |
242 | die "Unable to handle files with the extension '${extension}'" unless grep { $_ eq $extension } @extensions; |
243 | push @files, $path; |
244 | } else { |
245 | @files = map { "$path.$_" } @extensions; |
246 | } |
247 | return \@files; |
248 | }, |
249 | dependencies => [ depends_on('extensions'), depends_on('config_path') ], |
f04816ce |
250 | ); |
251 | } |
b4a6fa62 |
252 | |
f04816ce |
253 | sub build_local_files_service { |
254 | my $self = shift; |
292277c1 |
255 | |
256 | return Bread::Board::BlockInjection->new( |
257 | name => 'local_files', |
258 | block => sub { |
259 | my $s = shift; |
260 | |
261 | my ( $path, $extension ) = @{$s->param('config_path')}; |
262 | my $suffix = $s->param('config_local_suffix'); |
263 | |
264 | my @extensions = @{$s->param('extensions')}; |
265 | |
266 | my @files; |
267 | if ( $extension ) { |
268 | die "Unable to handle files with the extension '${extension}'" unless grep { $_ eq $extension } @extensions; |
269 | $path =~ s{\.$extension}{_$suffix.$extension}; |
270 | push @files, $path; |
271 | } else { |
272 | @files = map { "${path}_${suffix}.$_" } @extensions; |
273 | } |
274 | return \@files; |
275 | }, |
276 | dependencies => [ depends_on('extensions'), depends_on('config_path'), depends_on('config_local_suffix') ], |
f04816ce |
277 | ); |
278 | } |
b4a6fa62 |
279 | |
f04816ce |
280 | sub build_global_config_service { |
281 | my $self = shift; |
292277c1 |
282 | |
283 | return Bread::Board::BlockInjection->new( |
284 | name => 'global_config', |
285 | block => sub { |
286 | my $s = shift; |
287 | |
288 | return Config::Any->load_files({ |
289 | files => $s->param('global_files'), |
290 | filter => \&_fix_syntax, |
291 | use_ext => 1, |
292 | driver_args => $s->param('driver'), |
293 | }); |
294 | }, |
295 | dependencies => [ depends_on('global_files') ], |
f04816ce |
296 | ); |
297 | } |
b4a6fa62 |
298 | |
f04816ce |
299 | sub build_local_config_service { |
300 | my $self = shift; |
292277c1 |
301 | |
302 | return Bread::Board::BlockInjection->new( |
303 | name => 'local_config', |
304 | block => sub { |
305 | my $s = shift; |
306 | |
307 | return Config::Any->load_files({ |
308 | files => $s->param('local_files'), |
309 | filter => \&_fix_syntax, |
310 | use_ext => 1, |
311 | driver_args => $s->param('driver'), |
312 | }); |
313 | }, |
314 | dependencies => [ depends_on('local_files') ], |
f04816ce |
315 | ); |
316 | } |
b4a6fa62 |
317 | |
f04816ce |
318 | sub build_config_path_service { |
319 | my $self = shift; |
b4a6fa62 |
320 | |
292277c1 |
321 | return Bread::Board::BlockInjection->new( |
322 | name => 'config_path', |
323 | block => sub { |
324 | my $s = shift; |
b4a6fa62 |
325 | |
292277c1 |
326 | my $path = $s->param('path'); |
327 | my $prefix = $s->param('prefix'); |
b4a6fa62 |
328 | |
292277c1 |
329 | my ( $extension ) = ( $path =~ m{\.(.{1,4})$} ); |
330 | |
331 | if ( -d $path ) { |
332 | $path =~ s{[\/\\]$}{}; |
333 | $path .= "/$prefix"; |
334 | } |
b4a6fa62 |
335 | |
292277c1 |
336 | return [ $path, $extension ]; |
337 | }, |
338 | dependencies => [ depends_on('prefix'), depends_on('path') ], |
f04816ce |
339 | ); |
340 | } |
b4a6fa62 |
341 | |
f04816ce |
342 | sub build_config_local_suffix_service { |
343 | my $self = shift; |
292277c1 |
344 | |
345 | return Bread::Board::BlockInjection->new( |
346 | name => 'config_local_suffix', |
347 | block => sub { |
348 | my $s = shift; |
349 | my $suffix = Catalyst::Utils::env_value( $s->param('name'), 'CONFIG_LOCAL_SUFFIX' ) || $self->config_local_suffix; |
350 | |
351 | return $suffix; |
352 | }, |
353 | dependencies => [ depends_on('name') ], |
f04816ce |
354 | ); |
b4a6fa62 |
355 | } |
356 | |
357 | sub _fix_syntax { |
358 | my $config = shift; |
359 | my @components = ( |
360 | map +{ |
361 | prefix => $_ eq 'Component' ? '' : $_ . '::', |
362 | values => delete $config->{ lc $_ } || delete $config->{ $_ } |
363 | }, |
364 | grep { ref $config->{ lc $_ } || ref $config->{ $_ } } |
365 | qw( Component Model M View V Controller C Plugin ) |
366 | ); |
367 | |
368 | foreach my $comp ( @components ) { |
369 | my $prefix = $comp->{ prefix }; |
370 | foreach my $element ( keys %{ $comp->{ values } } ) { |
371 | $config->{ "$prefix$element" } = $comp->{ values }->{ $element }; |
372 | } |
373 | } |
374 | } |
375 | |
376 | sub _config_substitutions { |
6682389c |
377 | my ( $self, $name, $subs, $arg ) = @_; |
b4a6fa62 |
378 | |
379 | $subs->{ HOME } ||= sub { shift->path_to( '' ); }; |
380 | $subs->{ ENV } ||= |
381 | sub { |
382 | my ( $c, $v ) = @_; |
383 | if (! defined($ENV{$v})) { |
384 | Catalyst::Exception->throw( message => |
385 | "Missing environment variable: $v" ); |
386 | return ""; |
387 | } else { |
388 | return $ENV{ $v }; |
389 | } |
390 | }; |
391 | $subs->{ path_to } ||= sub { shift->path_to( @_ ); }; |
392 | $subs->{ literal } ||= sub { return $_[ 1 ]; }; |
393 | my $subsre = join( '|', keys %$subs ); |
394 | |
6682389c |
395 | $arg =~ s{__($subsre)(?:\((.+?)\))?__}{ $subs->{ $1 }->( $name, $2 ? split( /,/, $2 ) : () ) }eg; |
396 | return $arg; |
b4a6fa62 |
397 | } |
398 | |
a17e0ff8 |
399 | sub get_component_from_sub_container { |
400 | my ( $self, $sub_container_name, $name, $c, @args ) = @_; |
401 | |
402 | my $sub_container = $self->get_sub_container( $sub_container_name ); |
403 | |
0e747f0c |
404 | if (!$name) { |
a2c0d071 |
405 | my $default = $sub_container->default_component; |
0e747f0c |
406 | |
407 | return $sub_container->get_component( $default, $c, @args ) |
408 | if $default && $sub_container->has_service( $default ); |
409 | |
410 | # this is never a controller, so this is safe |
411 | $c->log->warn( "Calling \$c->$sub_container_name() is not supported unless you specify one of:" ); |
412 | $c->log->warn( "* \$c->config(default_$sub_container_name => 'the name of the default $sub_container_name to use')" ); |
413 | $c->log->warn( "* \$c->stash->{current_$sub_container_name} # the name of the view to use for this request" ); |
414 | $c->log->warn( "* \$c->stash->{current_${sub_container_name}_instance} # the instance of the $sub_container_name to use for this request" ); |
a2c0d071 |
415 | |
416 | return; |
0e747f0c |
417 | } |
418 | |
a17e0ff8 |
419 | return $sub_container->get_component_regexp( $name, $c, @args ) |
420 | if ref $name; |
421 | |
422 | return $sub_container->get_component( $name, $c, @args ) |
423 | if $sub_container->has_service( $name ); |
424 | |
425 | $c->log->warn( |
426 | "Attempted to use $sub_container_name '$name', " . |
427 | "but it does not exist" |
428 | ); |
429 | |
430 | return; |
431 | } |
432 | |
c4aedec7 |
433 | sub find_component { |
434 | my ( $self, $component, @args ) = @_; |
435 | my @result; |
436 | |
437 | my $query = ref $component |
438 | ? $component |
439 | : qr{^$component$} |
440 | ; |
441 | |
442 | for my $subcontainer_name (qw/model view controller/) { |
443 | my $subcontainer = $self->get_sub_container($subcontainer_name); |
444 | my @components = $subcontainer->get_service_list; |
445 | @result = grep { m{$component} } @components; |
446 | |
447 | return map { $subcontainer->get_component( $_, @args ) } @result |
448 | if @result; |
449 | } |
450 | |
451 | # it expects an empty list on failed searches |
452 | return @result; |
453 | } |
454 | |
4e2b302e |
455 | sub find_component_regexp { |
456 | my ( $self, $components, $component, @args ) = @_; |
457 | my @result; |
458 | |
459 | my @components = grep { m{$component} } keys %{ $components }; |
460 | |
461 | for (@components) { |
462 | # FIXME this is naughty enough being called inside Catalyst.pm |
463 | # find some alternative for this sub and remember to delete here |
464 | my ($type, $name) = Catalyst::_get_component_type_name($_); |
465 | |
466 | push @result, $self->get_component_from_sub_container( |
467 | $type, $name, @args |
468 | ) if $type; |
469 | } |
470 | |
471 | return @result; |
472 | } |
473 | |
c4aedec7 |
474 | |
d057ddb9 |
475 | 1; |
476 | |
477 | __END__ |
478 | |
479 | =pod |
480 | |
481 | =head1 NAME |
482 | |
483 | Catalyst::Container - IOC for Catalyst components |
484 | |
485 | =head1 METHODS |
486 | |
487 | =head2 build_model_subcontainer |
488 | |
489 | =head2 build_view_subcontainer |
490 | |
491 | =head2 build_controller_subcontainer |
492 | |
493 | =head2 build_name_service |
494 | |
495 | =head2 build_driver_service |
496 | |
497 | =head2 build_file_service |
498 | |
499 | =head2 build_substitutions_service |
500 | |
501 | =head2 build_extensions_service |
502 | |
503 | =head2 build_prefix_service |
504 | |
505 | =head2 build_path_service |
506 | |
507 | =head2 build_config_service |
508 | |
509 | =head2 build_raw_config_service |
510 | |
511 | =head2 build_global_files_service |
512 | |
513 | =head2 build_local_files_service |
514 | |
515 | =head2 build_global_config_service |
516 | |
517 | =head2 build_local_config_service |
518 | |
519 | =head2 build_config_path_service |
520 | |
521 | =head2 build_config_local_suffix_service |
522 | |
8dc2fca3 |
523 | =head2 get_component_from_sub_container |
524 | |
c4aedec7 |
525 | =head2 find_component |
526 | |
4e2b302e |
527 | =head2 find_component_regexp |
528 | |
d057ddb9 |
529 | =head2 _fix_syntax |
530 | |
531 | =head2 _config_substitutions |
532 | |
bf3c8088 |
533 | =head1 AUTHORS |
534 | |
e8ed391e |
535 | Catalyst Contributors, see Catalyst.pm |
bf3c8088 |
536 | |
e8ed391e |
537 | =head1 COPYRIGHT |
bf3c8088 |
538 | |
539 | This library is free software. You can redistribute it and/or modify it under |
540 | the same terms as Perl itself. |
541 | |
542 | =cut |