1 package Catalyst::Container;
5 use Data::Visitor::Callback;
6 use Catalyst::Utils ();
7 use MooseX::Types::LoadableClass qw/ LoadableClass /;
8 use Catalyst::BlockInjection;
10 extends 'Bread::Board::Container';
12 has config_local_suffix => (
21 default => sub { +{} },
30 has substitutions => (
33 default => sub { +{} },
42 has sub_container_class => (
46 default => 'Bread::Board::Container',
52 $self->build_root_container;
54 $self->build_model_subcontainer;
55 $self->build_view_subcontainer;
56 $self->build_controller_subcontainer;
59 sub build_model_subcontainer {
62 $self->add_sub_container(
63 $self->sub_container_class->new( name => 'model' )
67 sub build_view_subcontainer {
70 $self->add_sub_container(
71 $self->sub_container_class->new( name => 'view' )
75 sub build_controller_subcontainer {
78 $self->add_sub_container(
79 $self->sub_container_class->new( name => 'controller' )
83 sub build_root_container {
86 $self->build_substitutions_service();
87 $self->build_file_service();
88 $self->build_driver_service();
89 $self->build_name_service();
90 $self->build_prefix_service();
91 $self->build_extensions_service();
92 $self->build_path_service();
93 $self->build_config_service();
94 $self->build_raw_config_service();
95 $self->build_global_files_service();
96 $self->build_local_files_service();
97 $self->build_global_config_service();
98 $self->build_local_config_service();
99 $self->build_config_local_suffix_service();
100 $self->build_config_path_service();
103 sub build_name_service {
106 Bread::Board::Literal->new( name => 'name', value => $self->name )
110 sub build_driver_service {
113 Bread::Board::Literal->new( name => 'driver', value => $self->driver )
117 sub build_file_service {
120 Bread::Board::Literal->new( name => 'file', value => $self->file )
124 sub build_substitutions_service {
127 Bread::Board::Literal->new( name => 'substitutions', value => $self->substitutions )
131 sub build_extensions_service {
134 Bread::Board::BlockInjection->new(
135 name => 'extensions',
137 return \@{Config::Any->extensions};
143 sub build_prefix_service {
146 Bread::Board::BlockInjection->new(
149 return Catalyst::Utils::appprefix( shift->param('name') );
151 dependencies => [ depends_on('name') ],
156 sub build_path_service {
159 Bread::Board::BlockInjection->new(
164 return Catalyst::Utils::env_value( $s->param('name'), 'CONFIG' )
166 || $s->param('name')->path_to( $s->param('prefix') );
168 dependencies => [ depends_on('file'), depends_on('name'), depends_on('prefix') ],
173 sub build_config_service {
176 Bread::Board::BlockInjection->new(
181 my $v = Data::Visitor::Callback->new(
183 return unless defined $_;
184 return $self->_config_substitutions( $s->param('name'), $s->param('substitutions'), $_ );
188 $v->visit( $s->param('raw_config') );
190 dependencies => [ depends_on('name'), depends_on('raw_config'), depends_on('substitutions') ],
195 sub build_raw_config_service {
198 Bread::Board::BlockInjection->new(
199 name => 'raw_config',
203 my @global = @{$s->param('global_config')};
204 my @locals = @{$s->param('local_config')};
207 for my $cfg (@global, @locals) {
209 $config = Catalyst::Utils::merge_hashes( $config, $cfg->{$_} );
214 dependencies => [ depends_on('global_config'), depends_on('local_config') ],
219 sub build_global_files_service {
222 Bread::Board::BlockInjection->new(
223 name => 'global_files',
227 my ( $path, $extension ) = @{$s->param('config_path')};
229 my @extensions = @{$s->param('extensions')};
233 die "Unable to handle files with the extension '${extension}'" unless grep { $_ eq $extension } @extensions;
236 @files = map { "$path.$_" } @extensions;
240 dependencies => [ depends_on('extensions'), depends_on('config_path') ],
245 sub build_local_files_service {
248 Bread::Board::BlockInjection->new(
249 name => 'local_files',
253 my ( $path, $extension ) = @{$s->param('config_path')};
254 my $suffix = $s->param('config_local_suffix');
256 my @extensions = @{$s->param('extensions')};
260 die "Unable to handle files with the extension '${extension}'" unless grep { $_ eq $extension } @extensions;
261 $path =~ s{\.$extension}{_$suffix.$extension};
264 @files = map { "${path}_${suffix}.$_" } @extensions;
268 dependencies => [ depends_on('extensions'), depends_on('config_path'), depends_on('config_local_suffix') ],
273 sub build_global_config_service {
276 Bread::Board::BlockInjection->new(
277 name => 'global_config',
281 return Config::Any->load_files({
282 files => $s->param('global_files'),
283 filter => \&_fix_syntax,
285 driver_args => $s->param('driver'),
288 dependencies => [ depends_on('global_files') ],
293 sub build_local_config_service {
296 Bread::Board::BlockInjection->new(
297 name => 'local_config',
301 return Config::Any->load_files({
302 files => $s->param('local_files'),
303 filter => \&_fix_syntax,
305 driver_args => $s->param('driver'),
308 dependencies => [ depends_on('local_files') ],
313 sub build_config_path_service {
316 Bread::Board::BlockInjection->new(
317 name => 'config_path',
321 my $path = $s->param('path');
322 my $prefix = $s->param('prefix');
324 my ( $extension ) = ( $path =~ m{\.(.{1,4})$} );
327 $path =~ s{[\/\\]$}{};
331 return [ $path, $extension ];
333 dependencies => [ depends_on('prefix'), depends_on('path') ],
338 sub build_config_local_suffix_service {
341 Bread::Board::BlockInjection->new(
342 name => 'config_local_suffix',
345 my $suffix = Catalyst::Utils::env_value( $s->param('name'), 'CONFIG_LOCAL_SUFFIX' ) || $self->config_local_suffix;
349 dependencies => [ depends_on('name') ],
358 prefix => $_ eq 'Component' ? '' : $_ . '::',
359 values => delete $config->{ lc $_ } || delete $config->{ $_ }
361 grep { ref $config->{ lc $_ } || ref $config->{ $_ } }
362 qw( Component Model M View V Controller C Plugin )
365 foreach my $comp ( @components ) {
366 my $prefix = $comp->{ prefix };
367 foreach my $element ( keys %{ $comp->{ values } } ) {
368 $config->{ "$prefix$element" } = $comp->{ values }->{ $element };
373 sub _config_substitutions {
374 my ( $self, $name, $subs, $arg ) = @_;
376 $subs->{ HOME } ||= sub { shift->path_to( '' ); };
380 if (! defined($ENV{$v})) {
381 Catalyst::Exception->throw( message =>
382 "Missing environment variable: $v" );
388 $subs->{ path_to } ||= sub { shift->path_to( @_ ); };
389 $subs->{ literal } ||= sub { return $_[ 1 ]; };
390 my $subsre = join( '|', keys %$subs );
392 $arg =~ s{__($subsre)(?:\((.+?)\))?__}{ $subs->{ $1 }->( $name, $2 ? split( /,/, $2 ) : () ) }eg;
397 my ( $self, $type, $name, $args ) = @_;
398 return $self->get_sub_container($type)->resolve( service => $name, parameters => { context => $args } );