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