attributes can all be readonly
[catagits/Catalyst-Runtime.git] / lib / Catalyst / IOC / Container.pm
1 package Catalyst::IOC::Container;
2 use Bread::Board;
3 use Moose;
4 use Config::Any;
5 use Data::Visitor::Callback;
6 use Catalyst::Utils ();
7 use MooseX::Types::LoadableClass qw/ LoadableClass /;
8 use Catalyst::IOC::BlockInjection;
9 use namespace::autoclean;
10
11 extends 'Bread::Board::Container';
12
13 has config_local_suffix => (
14     is      => 'ro',
15     isa     => 'Str',
16     default => 'local',
17 );
18
19 has driver => (
20     is      => 'ro',
21     isa     => 'HashRef',
22     default => sub { +{} },
23 );
24
25 has file => (
26     is      => 'ro',
27     isa     => 'Str',
28     default => '',
29 );
30
31 has substitutions => (
32     is      => 'ro',
33     isa     => 'HashRef',
34     default => sub { +{} },
35 );
36
37 has name => (
38     is      => 'ro',
39     isa     => 'Str',
40     default => 'TestApp',
41 );
42
43 has sub_container_class => (
44     isa     => LoadableClass,
45     is      => 'ro',
46     coerce  => 1,
47     default => 'Catalyst::IOC::SubContainer',
48     handles => {
49         new_sub_container => 'new',
50     }
51 );
52
53 sub BUILD {
54     my $self = shift;
55
56     $self->add_service(
57         $self->${\"build_${_}_service"}
58     ) for qw/
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     /;
75
76     $self->add_sub_container(
77         $self->${ \"build_${_}_subcontainer" }
78     ) for qw/ model view controller /;
79 }
80
81 sub build_model_subcontainer {
82     my $self = shift;
83
84     return $self->new_sub_container( name => 'model' );
85 }
86
87 sub build_view_subcontainer {
88     my $self = shift;
89
90     return $self->new_sub_container( name => 'view' );
91 }
92
93 sub build_controller_subcontainer {
94     my $self = shift;
95
96     return $self->new_sub_container( name => 'controller' );
97 }
98
99 sub build_name_service {
100     my $self = shift;
101
102     return Bread::Board::Literal->new( name => 'name', value => $self->name );
103 }
104
105 sub build_driver_service {
106     my $self = shift;
107
108     return Bread::Board::Literal->new( name => 'driver', value => $self->driver );
109 }
110
111 sub build_file_service {
112     my $self = shift;
113
114     return Bread::Board::Literal->new( name => 'file', value => $self->file );
115 }
116
117 sub build_substitutions_service {
118     my $self = shift;
119
120     return Bread::Board::Literal->new( name => 'substitutions', value => $self->substitutions );
121 }
122
123 sub build_extensions_service {
124     my $self = shift;
125
126     return Bread::Board::BlockInjection->new(
127         name => 'extensions',
128         block => sub {
129             return \@{Config::Any->extensions};
130         },
131     );
132 }
133
134 sub build_prefix_service {
135     my $self = shift;
136
137     return Bread::Board::BlockInjection->new(
138         name => 'prefix',
139         block => sub {
140             return Catalyst::Utils::appprefix( shift->param('name') );
141         },
142         dependencies => [ depends_on('name') ],
143     );
144 }
145
146 sub build_path_service {
147     my $self = shift;
148
149     return Bread::Board::BlockInjection->new(
150         name => 'path',
151         block => sub {
152             my $s = shift;
153
154             return Catalyst::Utils::env_value( $s->param('name'), 'CONFIG' )
155             || $s->param('file')
156             || $s->param('name')->path_to( $s->param('prefix') );
157         },
158         dependencies => [ depends_on('file'), depends_on('name'), depends_on('prefix') ],
159     );
160 }
161
162 sub build_config_service {
163     my $self = shift;
164
165     return Bread::Board::BlockInjection->new(
166         name => 'config',
167         block => sub {
168             my $s = shift;
169
170             my $v = Data::Visitor::Callback->new(
171                 plain_value => sub {
172                     return unless defined $_;
173                     return $self->_config_substitutions( $s->param('name'), $s->param('substitutions'), $_ );
174                 }
175
176             );
177             $v->visit( $s->param('raw_config') );
178         },
179         dependencies => [ depends_on('name'), depends_on('raw_config'), depends_on('substitutions') ],
180     );
181 }
182
183 sub build_raw_config_service {
184     my $self = shift;
185
186     return Bread::Board::BlockInjection->new(
187         name => 'raw_config',
188         block => sub {
189             my $s = shift;
190
191             my @global = @{$s->param('global_config')};
192             my @locals = @{$s->param('local_config')};
193
194             my $config = {};
195             for my $cfg (@global, @locals) {
196                 for (keys %$cfg) {
197                     $config = Catalyst::Utils::merge_hashes( $config, $cfg->{$_} );
198                 }
199             }
200             return $config;
201         },
202         dependencies => [ depends_on('global_config'), depends_on('local_config') ],
203     );
204 }
205
206 sub build_global_files_service {
207     my $self = shift;
208
209     return Bread::Board::BlockInjection->new(
210         name => 'global_files',
211         block => sub {
212             my $s = shift;
213
214             my ( $path, $extension ) = @{$s->param('config_path')};
215
216             my @extensions = @{$s->param('extensions')};
217
218             my @files;
219             if ( $extension ) {
220                 die "Unable to handle files with the extension '${extension}'" unless grep { $_ eq $extension } @extensions;
221                 push @files, $path;
222             } else {
223                 @files = map { "$path.$_" } @extensions;
224             }
225             return \@files;
226         },
227         dependencies => [ depends_on('extensions'), depends_on('config_path') ],
228     );
229 }
230
231 sub build_local_files_service {
232     my $self = shift;
233
234     return Bread::Board::BlockInjection->new(
235         name => 'local_files',
236         block => sub {
237             my $s = shift;
238
239             my ( $path, $extension ) = @{$s->param('config_path')};
240             my $suffix = $s->param('config_local_suffix');
241
242             my @extensions = @{$s->param('extensions')};
243
244             my @files;
245             if ( $extension ) {
246                 die "Unable to handle files with the extension '${extension}'" unless grep { $_ eq $extension } @extensions;
247                 $path =~ s{\.$extension}{_$suffix.$extension};
248                 push @files, $path;
249             } else {
250                 @files = map { "${path}_${suffix}.$_" } @extensions;
251             }
252             return \@files;
253         },
254         dependencies => [ depends_on('extensions'), depends_on('config_path'), depends_on('config_local_suffix') ],
255     );
256 }
257
258 sub build_global_config_service {
259     my $self = shift;
260
261     return Bread::Board::BlockInjection->new(
262         name => 'global_config',
263         block => sub {
264             my $s = shift;
265
266             return Config::Any->load_files({
267                 files       => $s->param('global_files'),
268                 filter      => \&_fix_syntax,
269                 use_ext     => 1,
270                 driver_args => $s->param('driver'),
271             });
272         },
273         dependencies => [ depends_on('global_files') ],
274     );
275 }
276
277 sub build_local_config_service {
278     my $self = shift;
279
280     return Bread::Board::BlockInjection->new(
281         name => 'local_config',
282         block => sub {
283             my $s = shift;
284
285             return Config::Any->load_files({
286                 files       => $s->param('local_files'),
287                 filter      => \&_fix_syntax,
288                 use_ext     => 1,
289                 driver_args => $s->param('driver'),
290             });
291         },
292         dependencies => [ depends_on('local_files') ],
293     );
294 }
295
296 sub build_config_path_service {
297     my $self = shift;
298
299     return Bread::Board::BlockInjection->new(
300         name => 'config_path',
301         block => sub {
302             my $s = shift;
303
304             my $path = $s->param('path');
305             my $prefix = $s->param('prefix');
306
307             my ( $extension ) = ( $path =~ m{\.(.{1,4})$} );
308
309             if ( -d $path ) {
310                 $path =~ s{[\/\\]$}{};
311                 $path .= "/$prefix";
312             }
313
314             return [ $path, $extension ];
315         },
316         dependencies => [ depends_on('prefix'), depends_on('path') ],
317     );
318 }
319
320 sub build_config_local_suffix_service {
321     my $self = shift;
322
323     return Bread::Board::BlockInjection->new(
324         name => 'config_local_suffix',
325         block => sub {
326             my $s = shift;
327             my $suffix = Catalyst::Utils::env_value( $s->param('name'), 'CONFIG_LOCAL_SUFFIX' ) || $self->config_local_suffix;
328
329             return $suffix;
330         },
331         dependencies => [ depends_on('name') ],
332     );
333 }
334
335 sub _fix_syntax {
336     my $config     = shift;
337     my @components = (
338         map +{
339             prefix => $_ eq 'Component' ? '' : $_ . '::',
340             values => delete $config->{ lc $_ } || delete $config->{ $_ }
341         },
342         grep { ref $config->{ lc $_ } || ref $config->{ $_ } }
343             qw( Component Model M View V Controller C Plugin )
344     );
345
346     foreach my $comp ( @components ) {
347         my $prefix = $comp->{ prefix };
348         foreach my $element ( keys %{ $comp->{ values } } ) {
349             $config->{ "$prefix$element" } = $comp->{ values }->{ $element };
350         }
351     }
352 }
353
354 sub _config_substitutions {
355     my ( $self, $name, $subs, $arg ) = @_;
356
357     $subs->{ HOME } ||= sub { shift->path_to( '' ); };
358     $subs->{ ENV } ||=
359         sub {
360             my ( $c, $v ) = @_;
361             if (! defined($ENV{$v})) {
362                 Catalyst::Exception->throw( message =>
363                     "Missing environment variable: $v" );
364                 return "";
365             } else {
366                 return $ENV{ $v };
367             }
368         };
369     $subs->{ path_to } ||= sub { shift->path_to( @_ ); };
370     $subs->{ literal } ||= sub { return $_[ 1 ]; };
371     my $subsre = join( '|', keys %$subs );
372
373     $arg =~ s{__($subsre)(?:\((.+?)\))?__}{ $subs->{ $1 }->( $name, $2 ? split( /,/, $2 ) : () ) }eg;
374     return $arg;
375 }
376
377 1;
378
379 __END__
380
381 =pod
382
383 =head1 NAME
384
385 Catalyst::Container - IOC for Catalyst components
386
387 =head1 METHODS
388
389 =head2 build_model_subcontainer
390
391 =head2 build_view_subcontainer
392
393 =head2 build_controller_subcontainer
394
395 =head2 build_name_service
396
397 =head2 build_driver_service
398
399 =head2 build_file_service
400
401 =head2 build_substitutions_service
402
403 =head2 build_extensions_service
404
405 =head2 build_prefix_service
406
407 =head2 build_path_service
408
409 =head2 build_config_service
410
411 =head2 build_raw_config_service
412
413 =head2 build_global_files_service
414
415 =head2 build_local_files_service
416
417 =head2 build_global_config_service
418
419 =head2 build_local_config_service
420
421 =head2 build_config_path_service
422
423 =head2 build_config_local_suffix_service
424
425 =head2 _fix_syntax
426
427 =head2 _config_substitutions
428
429 =head1 AUTHORS
430
431 Catalyst Contributors, see Catalyst.pm
432
433 =head1 COPYRIGHT
434
435 This library is free software. You can redistribute it and/or modify it under
436 the same terms as Perl itself.
437
438 =cut