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_${_}_service"} for qw/
70 $self->build_model_subcontainer;
71 $self->build_view_subcontainer;
72 $self->build_controller_subcontainer;
75 sub build_model_subcontainer {
78 $self->add_sub_container(
79 $self->sub_container_class->new( name => 'model' )
83 sub build_view_subcontainer {
86 $self->add_sub_container(
87 $self->sub_container_class->new( name => 'view' )
91 sub build_controller_subcontainer {
94 $self->add_sub_container(
95 $self->sub_container_class->new( name => 'controller' )
99 sub build_name_service {
102 Bread::Board::Literal->new( name => 'name', value => $self->name )
106 sub build_driver_service {
109 Bread::Board::Literal->new( name => 'driver', value => $self->driver )
113 sub build_file_service {
116 Bread::Board::Literal->new( name => 'file', value => $self->file )
120 sub build_substitutions_service {
123 Bread::Board::Literal->new( name => 'substitutions', value => $self->substitutions )
127 sub build_extensions_service {
130 Bread::Board::BlockInjection->new(
131 name => 'extensions',
133 return \@{Config::Any->extensions};
139 sub build_prefix_service {
142 Bread::Board::BlockInjection->new(
145 return Catalyst::Utils::appprefix( shift->param('name') );
147 dependencies => [ depends_on('name') ],
152 sub build_path_service {
155 Bread::Board::BlockInjection->new(
160 return Catalyst::Utils::env_value( $s->param('name'), 'CONFIG' )
162 || $s->param('name')->path_to( $s->param('prefix') );
164 dependencies => [ depends_on('file'), depends_on('name'), depends_on('prefix') ],
169 sub build_config_service {
172 Bread::Board::BlockInjection->new(
177 my $v = Data::Visitor::Callback->new(
179 return unless defined $_;
180 return $self->_config_substitutions( $s->param('name'), $s->param('substitutions'), $_ );
184 $v->visit( $s->param('raw_config') );
186 dependencies => [ depends_on('name'), depends_on('raw_config'), depends_on('substitutions') ],
191 sub build_raw_config_service {
194 Bread::Board::BlockInjection->new(
195 name => 'raw_config',
199 my @global = @{$s->param('global_config')};
200 my @locals = @{$s->param('local_config')};
203 for my $cfg (@global, @locals) {
205 $config = Catalyst::Utils::merge_hashes( $config, $cfg->{$_} );
210 dependencies => [ depends_on('global_config'), depends_on('local_config') ],
215 sub build_global_files_service {
218 Bread::Board::BlockInjection->new(
219 name => 'global_files',
223 my ( $path, $extension ) = @{$s->param('config_path')};
225 my @extensions = @{$s->param('extensions')};
229 die "Unable to handle files with the extension '${extension}'" unless grep { $_ eq $extension } @extensions;
232 @files = map { "$path.$_" } @extensions;
236 dependencies => [ depends_on('extensions'), depends_on('config_path') ],
241 sub build_local_files_service {
244 Bread::Board::BlockInjection->new(
245 name => 'local_files',
249 my ( $path, $extension ) = @{$s->param('config_path')};
250 my $suffix = $s->param('config_local_suffix');
252 my @extensions = @{$s->param('extensions')};
256 die "Unable to handle files with the extension '${extension}'" unless grep { $_ eq $extension } @extensions;
257 $path =~ s{\.$extension}{_$suffix.$extension};
260 @files = map { "${path}_${suffix}.$_" } @extensions;
264 dependencies => [ depends_on('extensions'), depends_on('config_path'), depends_on('config_local_suffix') ],
269 sub build_global_config_service {
272 Bread::Board::BlockInjection->new(
273 name => 'global_config',
277 return Config::Any->load_files({
278 files => $s->param('global_files'),
279 filter => \&_fix_syntax,
281 driver_args => $s->param('driver'),
284 dependencies => [ depends_on('global_files') ],
289 sub build_local_config_service {
292 Bread::Board::BlockInjection->new(
293 name => 'local_config',
297 return Config::Any->load_files({
298 files => $s->param('local_files'),
299 filter => \&_fix_syntax,
301 driver_args => $s->param('driver'),
304 dependencies => [ depends_on('local_files') ],
309 sub build_config_path_service {
312 Bread::Board::BlockInjection->new(
313 name => 'config_path',
317 my $path = $s->param('path');
318 my $prefix = $s->param('prefix');
320 my ( $extension ) = ( $path =~ m{\.(.{1,4})$} );
323 $path =~ s{[\/\\]$}{};
327 return [ $path, $extension ];
329 dependencies => [ depends_on('prefix'), depends_on('path') ],
334 sub build_config_local_suffix_service {
337 Bread::Board::BlockInjection->new(
338 name => 'config_local_suffix',
341 my $suffix = Catalyst::Utils::env_value( $s->param('name'), 'CONFIG_LOCAL_SUFFIX' ) || $self->config_local_suffix;
345 dependencies => [ depends_on('name') ],
354 prefix => $_ eq 'Component' ? '' : $_ . '::',
355 values => delete $config->{ lc $_ } || delete $config->{ $_ }
357 grep { ref $config->{ lc $_ } || ref $config->{ $_ } }
358 qw( Component Model M View V Controller C Plugin )
361 foreach my $comp ( @components ) {
362 my $prefix = $comp->{ prefix };
363 foreach my $element ( keys %{ $comp->{ values } } ) {
364 $config->{ "$prefix$element" } = $comp->{ values }->{ $element };
369 sub _config_substitutions {
370 my ( $self, $name, $subs, $arg ) = @_;
372 $subs->{ HOME } ||= sub { shift->path_to( '' ); };
376 if (! defined($ENV{$v})) {
377 Catalyst::Exception->throw( message =>
378 "Missing environment variable: $v" );
384 $subs->{ path_to } ||= sub { shift->path_to( @_ ); };
385 $subs->{ literal } ||= sub { return $_[ 1 ]; };
386 my $subsre = join( '|', keys %$subs );
388 $arg =~ s{__($subsre)(?:\((.+?)\))?__}{ $subs->{ $1 }->( $name, $2 ? split( /,/, $2 ) : () ) }eg;
393 my ( $self, $type, $name, $args ) = @_;
394 return $self->get_sub_container($type)->resolve( service => $name, parameters => { context => $args } );