1 package Catalyst::Container;
5 use Data::Visitor::Callback;
6 use Catalyst::Utils ();
8 extends 'Bread::Board::Container';
10 has config_local_suffix => (
19 default => sub { +{} },
28 has substitutions => (
31 default => sub { +{} },
43 container $self => as {
44 service name => $self->name;
45 service driver => $self->driver;
46 service file => $self->file;
47 service substitutions => $self->substitutions;
49 service extensions => (
51 return \@{Config::Any->extensions};
57 return Catalyst::Utils::appprefix( shift->param('name') );
59 dependencies => [ depends_on('name') ],
66 return Catalyst::Utils::env_value( $s->param('name'), 'CONFIG' )
68 || $s->param('name')->path_to( $s->param('prefix') );
70 dependencies => [ depends_on('file'), depends_on('name'), depends_on('prefix') ],
77 my $v = Data::Visitor::Callback->new(
79 return unless defined $_;
80 return $self->_config_substitutions( $s->param('name'), $s->param('substitutions'), $_ );
84 $v->visit( $s->param('raw_config') );
86 dependencies => [ depends_on('name'), depends_on('raw_config'), depends_on('substitutions') ],
89 service raw_config => (
93 my @global = @{$s->param('global_config')};
94 my @locals = @{$s->param('local_config')};
97 for my $cfg (@global, @locals) {
99 $config = Catalyst::Utils::merge_hashes( $config, $cfg->{$_} );
104 dependencies => [ depends_on('global_config'), depends_on('local_config') ],
107 service global_files => (
111 my ( $path, $extension ) = @{$s->param('config_path')};
113 my @extensions = @{$s->param('extensions')};
117 die "Unable to handle files with the extension '${extension}'" unless grep { $_ eq $extension } @extensions;
120 @files = map { "$path.$_" } @extensions;
124 dependencies => [ depends_on('extensions'), depends_on('config_path') ],
127 service local_files => (
131 my ( $path, $extension ) = @{$s->param('config_path')};
132 my $suffix = $s->param('config_local_suffix');
134 my @extensions = @{$s->param('extensions')};
138 die "Unable to handle files with the extension '${extension}'" unless grep { $_ eq $extension } @extensions;
139 $path =~ s{\.$extension}{_$suffix.$extension};
142 @files = map { "${path}_${suffix}.$_" } @extensions;
146 dependencies => [ depends_on('extensions'), depends_on('config_path'), depends_on('config_local_suffix') ],
149 service global_config => (
153 return Config::Any->load_files({
154 files => $s->param('global_files'),
155 filter => \&_fix_syntax,
157 driver_args => $s->param('driver'),
160 dependencies => [ depends_on('global_files') ],
163 service local_config => (
167 return Config::Any->load_files({
168 files => $s->param('local_files'),
169 filter => \&_fix_syntax,
171 driver_args => $s->param('driver'),
174 dependencies => [ depends_on('local_files') ],
177 service config_path => (
181 my $path = $s->param('path');
182 my $prefix = $s->param('prefix');
184 my ( $extension ) = ( $path =~ m{\.(.{1,4})$} );
187 $path =~ s{[\/\\]$}{};
191 return [ $path, $extension ];
193 dependencies => [ depends_on('prefix'), depends_on('path') ],
196 service config_local_suffix => (
199 my $suffix = Catalyst::Utils::env_value( $s->param('name'), 'CONFIG_LOCAL_SUFFIX' ) || $self->config_local_suffix;
203 dependencies => [ depends_on('name') ],
213 prefix => $_ eq 'Component' ? '' : $_ . '::',
214 values => delete $config->{ lc $_ } || delete $config->{ $_ }
216 grep { ref $config->{ lc $_ } || ref $config->{ $_ } }
217 qw( Component Model M View V Controller C Plugin )
220 foreach my $comp ( @components ) {
221 my $prefix = $comp->{ prefix };
222 foreach my $element ( keys %{ $comp->{ values } } ) {
223 $config->{ "$prefix$element" } = $comp->{ values }->{ $element };
228 sub _config_substitutions {
229 my ( $self, $name, $subs, $arg ) = @_;
231 $subs->{ HOME } ||= sub { shift->path_to( '' ); };
235 if (! defined($ENV{$v})) {
236 Catalyst::Exception->throw( message =>
237 "Missing environment variable: $v" );
243 $subs->{ path_to } ||= sub { shift->path_to( @_ ); };
244 $subs->{ literal } ||= sub { return $_[ 1 ]; };
245 my $subsre = join( '|', keys %$subs );
247 $arg =~ s{__($subsre)(?:\((.+?)\))?__}{ $subs->{ $1 }->( $name, $2 ? split( /,/, $2 ) : () ) }eg;