1 package Catalyst::Plugin::ConfigLoader;
8 use Data::Visitor::Callback;
9 use Catalyst::Utils ();
11 our $VERSION = '0.16';
15 Catalyst::Plugin::ConfigLoader - Load config files of various types
21 # ConfigLoader should be first in your list so
22 # other plugins can get the config information
23 use Catalyst qw( ConfigLoader ... );
25 # by default myapp.* will be loaded
26 # you can specify a file if you'd like
27 __PACKAGE__->config( file => 'config.yaml' );
31 This module will attempt to load find and load a configuration
32 file of various types. Currently it supports YAML, JSON, XML,
35 To support the distinction between development and production environments,
36 this module will also attemp to load a local config (e.g. myapp_local.yaml)
37 which will override any duplicate settings.
43 This method is automatically called by Catalyst's setup routine. It will
44 attempt to use each plugin and, once a file has been successfully
45 loaded, set the C<config()> section.
51 my @files = $c->find_files;
52 my $cfg = Config::Any->load_files( {
54 filter => \&_fix_syntax,
58 # split the responses into normal and local cfg
59 my $local_suffix = $c->get_config_local_suffix;
60 my( @cfg, @localcfg );
62 if( ( keys %$_ )[ 0 ] =~ m{ $local_suffix \. }xms ) {
69 # load all the normal cfgs, then the local cfgs last so they can override
71 $c->load_config( $_ ) for @cfg, @localcfg;
74 $c->NEXT::setup( @_ );
79 This method handles loading the configuration data into the Catalyst
80 context object. It does not return a value.
88 my( $file, $config ) = each %$ref;
90 $c->config( $config );
91 $c->log->debug( qq(Loaded Config "$file") )
99 This method determines the potential file paths to be used for config loading.
100 It returns an array of paths (up to the filename less the extension) to pass to
101 L<Config::Any|Config::Any> for loading.
107 my( $path, $extension ) = $c->get_config_path;
108 my $suffix = $c->get_config_local_suffix;
109 my @extensions = @{ Config::Any->extensions };
113 next unless grep { $_ eq $extension } @extensions;
114 push @files, $path, "${path}_${suffix}";
116 @files = map { ( "$path.$_", "${path}_${suffix}.$_" ) } @extensions;
122 =head2 get_config_path
124 This method determines the path, filename prefix and file extension to be used
125 for config loading. It returns the path (up to the filename less the
126 extension) to check and the specific extension to use (if it was specified).
128 The order of preference is specified as:
132 =item * C<$ENV{ MYAPP_CONFIG }>
134 =item * C<$ENV{ CATALYST_CONFIG }>
136 =item * C<$c-E<gt>config-E<gt>{ 'Plugin::ConfigLoader' }-E>gt>{ file }>
138 =item * C<$c-E<gt>path_to( $application_prefix )>
142 If either of the first two user-specified options are directories, the
143 application prefix will be added on to the end of the path.
145 DEPRECATION NOTICE: C<$c-E<gt>config-E<gt>{ file }> is deprecated
146 and will be removed in the next release.
150 sub get_config_path {
152 my $appname = ref $c || $c;
153 my $prefix = Catalyst::Utils::appprefix( $appname );
154 my $path = Catalyst::Utils::env_value( $c, 'CONFIG' )
155 || $c->config->{ 'Plugin::ConfigLoader' }->{ file }
156 || $c->config->{ file } # to be removed next release
157 || $c->path_to( $prefix );
159 my( $extension ) = ( $path =~ m{\.(.{1,4})$} );
162 $path =~ s{[\/\\]$}{};
166 return( $path, $extension );
169 =head2 get_config_local_suffix
171 Determines the suffix of files used to override the main config. By default
172 this value is C<local>, but it can be specified in the following order of preference:
176 =item * C<$ENV{ MYAPP_CONFIG_LOCAL_SUFFIX }>
178 =item * C<$ENV{ CATALYST_CONFIG_LOCAL_SUFFIX }>
180 =item * C<$c-E<gt>config-E<gt>{ 'Plugin::ConfigLoader' }-E<gt>{ config_local_suffix }>
184 DEPRECATION NOTICE: C<$c-E<gt>config-E<gt>{ config_local_suffix }> is deprecated
185 and will be removed in the next release.
189 sub get_config_local_suffix {
191 my $appname = ref $c || $c;
192 my $suffix = Catalyst::Utils::env_value( $c, 'CONFIG_LOCAL_SUFFIX' )
193 || $c->config->{ 'Plugin::ConfigLoader' }->{ config_local_suffix }
194 || $c->config->{ config_local_suffix } # to be remove in the next release
204 prefix => $_ eq 'Component' ? '' : $_ . '::',
205 values => delete $config->{ lc $_ } || delete $config->{ $_ }
208 ref $config->{ lc $_ } || ref $config->{ $_ }
210 qw( Component Model M View V Controller C )
213 foreach my $comp ( @components ) {
214 my $prefix = $comp->{ prefix };
215 foreach my $element ( keys %{ $comp->{ values } } ) {
216 $config->{ "$prefix$element" } = $comp->{ values }->{ $element };
221 =head2 finalize_config
223 This method is called after the config file is loaded. It can be
224 used to implement tuning of config values that can only be done
225 at runtime. If you need to do this to properly configure any
226 plugins, it's important to load ConfigLoader before them.
227 ConfigLoader provides a default finalize_config method which
228 walks through the loaded config hash and calls the C<config_substitutions>
233 sub finalize_config {
235 my $v = Data::Visitor::Callback->new(
237 return unless defined $_;
238 $c->config_substitutions( $_ );
241 $v->visit( $c->config );
244 =head2 config_substitutions( $value )
246 This method substitutes macros found with calls to a function. There are three
251 =item * C<__HOME__> - replaced with C<$c-E<gt>path_to('')>
253 =item * C<__path_to(foo/bar)__> - replaced with C<$c-E<gt>path_to('foo/bar')>
255 =item * C<__literal(__FOO__)__> - leaves __FOO__ alone (allows you to use
256 C<__DATA__> as a config value, for example)
260 The parameter list is split on comma (C<,>). You can override this method to
261 do your own string munging, or you can define your own macros in
262 C<MyApp-E<gt>config-E<gt>{ 'Plugin::ConfigLoader' }-E<gt>{ substitutions }>.
265 MyApp->config->{ 'Plugin::ConfigLoader' }->{ substitutions } = {
266 baz => sub { my $c = shift; qux( @_ ); }
269 The above will respond to C<__baz(x,y)__> in config strings.
273 sub config_substitutions {
275 my $subs = $c->config->{ 'Plugin::ConfigLoader' }->{ substitutions } || {};
276 $subs->{ HOME } ||= sub { shift->path_to( '' ); };
277 $subs->{ path_to } ||= sub { shift->path_to( @_ ); };
278 $subs->{ literal } ||= sub { return $_[ 1 ]; };
279 my $subsre = join( '|', keys %$subs );
282 s{__($subsre)(?:\((.+?)\))?__}{ $subs->{ $1 }->( $c, $2 ? split( /,/, $2 ) : () ) }eg;
289 Brian Cassidy E<lt>bricas@cpan.orgE<gt>
293 The following people have generously donated their time to the
294 development of this module:
298 =item * Joel Bernstein E<lt>rataxis@cpan.orgE<gt> - Rewrite to use L<Config::Any>
300 =item * David Kamholz E<lt>dkamholz@cpan.orgE<gt> - L<Data::Visitor> integration
304 Work to this module has been generously sponsored by:
308 =item * Portugal Telecom L<http://www.sapo.pt/> - Work done by Joel Bernstein
312 =head1 COPYRIGHT AND LICENSE
314 Copyright 2007 by Brian Cassidy
316 This library is free software; you can redistribute it and/or modify
317 it under the same terms as Perl itself.
325 =item * L<Catalyst::Plugin::ConfigLoader::Manual>
327 =item * L<Config::Any>