1 package Catalyst::Plugin::ConfigLoader;
8 use Data::Visitor::Callback;
9 use Catalyst::Utils ();
11 our $VERSION = '0.35';
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( 'Plugin::ConfigLoader' => { file => 'config.yaml' } );
29 In the file, assuming it's in YAML format:
33 Accessible through the context object, or the class itself
35 $c->config->{foo} # bar
36 MyApp->config->{foo} # bar
40 This module will attempt to load find and load a configuration
41 file of various types. Currently it supports YAML, JSON, XML,
42 INI and Perl formats. Special configuration for a particular driver format can
43 be stored in C<< MyApp->config->{ 'Plugin::ConfigLoader' }->{ driver } >>.
44 For example, to pass arguments to L<Config::General>, use the following:
46 __PACKAGE__->config( 'Plugin::ConfigLoader' => {
48 'General' => { -LowerCaseNames => 1 }
52 See L<Config::Any>'s C<driver_args> parameter for more information.
54 To support the distinction between development and production environments,
55 this module will also attemp to load a local config (e.g. F<myapp_local.yaml>)
56 which will override any duplicate settings. See
57 L</get_config_local_suffix>
58 for details on how this is configured.
64 This method is automatically called by Catalyst's setup routine. It will
65 attempt to use each plugin and, once a file has been successfully
66 loaded, set the C<config()> section.
72 my @files = $c->find_files;
73 my $cfg = Config::Any->load_files(
75 filter => \&_fix_syntax,
77 driver_args => $c->config->{ 'Plugin::ConfigLoader' }->{ driver }
81 # map the array of hashrefs to a simple hash
82 my %configs = map { %$_ } @$cfg;
84 # split the responses into normal and local cfg
85 my $local_suffix = $c->get_config_local_suffix;
86 my ( @main, @locals );
87 for ( sort keys %configs ) {
88 if ( m{$local_suffix\.}ms ) {
96 # load all the normal cfgs, then the local cfgs last so they can override
98 $c->load_config( { $_ => $configs{ $_ } } ) for @main, @locals;
101 $c->next::method( @_ );
106 This method handles loading the configuration data into the Catalyst
107 context object. It does not return a value.
115 my ( $file, $config ) = %$ref;
117 $c->config( $config );
118 $c->log->debug( qq(Loaded Config "$file") )
126 This method determines the potential file paths to be used for config loading.
127 It returns an array of paths (up to the filename less the extension) to pass to
128 L<Config::Any> for loading.
134 my ( $path, $extension ) = $c->get_config_path;
135 my $suffix = $c->get_config_local_suffix;
136 my @extensions = @{ Config::Any->extensions };
140 die "Unable to handle files with the extension '${extension}'"
141 unless grep { $_ eq $extension } @extensions;
142 ( my $local = $path ) =~ s{\.$extension}{_$suffix.$extension};
143 push @files, $path, $local;
146 @files = map { ( "$path.$_", "${path}_${suffix}.$_" ) } @extensions;
151 =head2 get_config_path
153 This method determines the path, filename prefix and file extension to be used
154 for config loading. It returns the path (up to the filename less the
155 extension) to check and the specific extension to use (if it was specified).
157 The order of preference is specified as:
161 =item * C<$ENV{ MYAPP_CONFIG }>
163 =item * C<$ENV{ CATALYST_CONFIG }>
165 =item * C<< $c->config->{ 'Plugin::ConfigLoader' }->{ file } >>
167 =item * C<< $c->path_to( $application_prefix ) >>
171 If either of the first two user-specified options are directories, the
172 application prefix will be added on to the end of the path.
176 sub get_config_path {
180 my $appname = ref $c || $c;
181 my $prefix = Catalyst::Utils::appprefix( $appname );
182 my $path = Catalyst::Utils::env_value( $appname, 'CONFIG' )
183 || $c->config->{ 'Plugin::ConfigLoader' }->{ file }
184 || $c->path_to( $prefix );
186 ## don't look for extension if this is a dir
187 my ( $extension ) = !-d $path ? ( $path =~ m{\.([^\/\\.]{1,4})$} ) : () ;
190 $path =~ s{[\/\\]$}{};
194 return ( $path, $extension );
197 =head2 get_config_local_suffix
199 Determines the suffix of files used to override the main config. By default
200 this value is C<local>, which will load C<myapp_local.conf>. The suffix can
201 be specified in the following order of preference:
205 =item * C<$ENV{ MYAPP_CONFIG_LOCAL_SUFFIX }>
207 =item * C<$ENV{ CATALYST_CONFIG_LOCAL_SUFFIX }>
209 =item * C<< $c->config->{ 'Plugin::ConfigLoader' }->{ config_local_suffix } >>
213 The first one of these values found replaces the default of C<local> in the
214 name of the local config file to be loaded.
216 For example, if C< $ENV{ MYAPP_CONFIG_LOCAL_SUFFIX }> is set to C<testing>,
217 ConfigLoader will try and load F<myapp_testing.conf> instead of
222 sub get_config_local_suffix {
225 my $appname = ref $c || $c;
226 my $suffix = Catalyst::Utils::env_value( $appname, 'CONFIG_LOCAL_SUFFIX' )
227 || $c->config->{ 'Plugin::ConfigLoader' }->{ config_local_suffix }
237 prefix => $_ eq 'Component' ? '' : $_ . '::',
238 values => delete $config->{ lc $_ } || delete $config->{ $_ }
240 grep { ref $config->{ lc $_ } || ref $config->{ $_ } }
241 qw( Component Model M View V Controller C Plugin )
244 foreach my $comp ( @components ) {
245 my $prefix = $comp->{ prefix };
246 foreach my $element ( keys %{ $comp->{ values } } ) {
247 $config->{ "$prefix$element" } = $comp->{ values }->{ $element };
252 =head2 finalize_config
254 This method is called after the config file is loaded. It can be
255 used to implement tuning of config values that can only be done
256 at runtime. If you need to do this to properly configure any
257 plugins, it's important to load ConfigLoader before them.
258 ConfigLoader provides a default C<finalize_config> method which
259 walks through the loaded config hash and calls the
260 L<config_substitutions|/config_substitutions( $value )> method on any string.
264 sub finalize_config {
266 my $v = Data::Visitor::Callback->new(
268 return unless defined $_;
269 $c->config_substitutions( $_ );
272 $v->visit( $c->config );
275 =head2 config_substitutions( $value )
277 This method substitutes macros found with calls to a function. There are a
278 number of default macros:
282 =item * C<__HOME__> - replaced with C<$c-E<gt>path_to('')>
284 =item * C<__ENV(foo)__> - replaced with the value of C<$ENV{foo}>
286 =item * C<__path_to(foo/bar)__> - replaced with C<$c-E<gt>path_to('foo/bar')>
288 =item * C<__literal(__FOO__)__> - leaves __FOO__ alone (allows you to use
289 C<__DATA__> as a config value, for example)
293 The parameter list is split on comma (C<,>). You can override this method to
294 do your own string munging, or you can define your own macros in
295 C<< MyApp->config->{ 'Plugin::ConfigLoader' }->{ substitutions } >>.
298 MyApp->config->{ 'Plugin::ConfigLoader' }->{ substitutions } = {
299 baz => sub { my $c = shift; qux( @_ ); }
302 The above will respond to C<__baz(x,y)__> in config strings.
306 sub config_substitutions {
308 my $subs = $c->config->{ 'Plugin::ConfigLoader' }->{ substitutions }
310 $subs->{ HOME } ||= sub { shift->path_to( '' ); };
314 if (! defined($ENV{$v})) {
315 Catalyst::Exception->throw( message =>
316 "Missing environment variable: $v" );
322 $subs->{ path_to } ||= sub { shift->path_to( @_ ); };
323 $subs->{ literal } ||= sub { return $_[ 1 ]; };
324 my $subsre = join( '|', keys %$subs );
327 s{__($subsre)(?:\((.+?)\))?__}{ $subs->{ $1 }->( $c, $2 ? split( /,/, $2 ) : () ) }eg;
333 Brian Cassidy <bricas@cpan.org>
337 The following people have generously donated their time to the
338 development of this module:
342 =item * Joel Bernstein <rataxis@cpan.org> - Rewrite to use L<Config::Any>
344 =item * David Kamholz <dkamholz@cpan.org> - L<Data::Visitor> integration
346 =item * Stuart Watt - Addition of ENV macro.
350 Work to this module has been generously sponsored by:
354 =item * Portugal Telecom L<http://www.sapo.pt/> - Work done by Joel Bernstein
358 =head1 COPYRIGHT AND LICENSE
360 Copyright 2006-2010 by Brian Cassidy
362 This library is free software; you can redistribute it and/or modify
363 it under the same terms as Perl itself.
371 =item * L<Catalyst::Plugin::ConfigLoader::Manual>
373 =item * L<Config::Any>