1 package Catalyst::Plugin::ConfigLoader;
8 use Data::Visitor::Callback;
9 use Catalyst::Utils ();
11 our $VERSION = '0.20';
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-E<gt>config-E<gt>{ 'Plugin::ConfigLoader' }-E<gt>{ driver }>.
45 To support the distinction between development and production environments,
46 this module will also attemp to load a local config (e.g. myapp_local.yaml)
47 which will override any duplicate settings.
53 This method is automatically called by Catalyst's setup routine. It will
54 attempt to use each plugin and, once a file has been successfully
55 loaded, set the C<config()> section.
61 my @files = $c->find_files;
62 my $cfg = Config::Any->load_files(
64 filter => \&_fix_syntax,
66 driver_args => $c->config->{ 'Plugin::ConfigLoader' }->{ driver }
70 # map the array of hashrefs to a simple hash
71 my %configs = map { %$_ } @$cfg;
73 # split the responses into normal and local cfg
74 my $local_suffix = $c->get_config_local_suffix;
75 my ( @main, @locals );
76 for ( sort keys %configs ) {
77 if ( m{$local_suffix\.}ms ) {
85 # load all the normal cfgs, then the local cfgs last so they can override
87 $c->load_config( { $_ => $configs{ $_ } } ) for @main, @locals;
90 $c->NEXT::setup( @_ );
95 This method handles loading the configuration data into the Catalyst
96 context object. It does not return a value.
104 my ( $file, $config ) = %$ref;
106 $c->config( $config );
107 $c->log->debug( qq(Loaded Config "$file") )
115 This method determines the potential file paths to be used for config loading.
116 It returns an array of paths (up to the filename less the extension) to pass to
117 L<Config::Any|Config::Any> for loading.
123 my ( $path, $extension ) = $c->get_config_path;
124 my $suffix = $c->get_config_local_suffix;
125 my @extensions = @{ Config::Any->extensions };
129 die "Unable to handle files with the extension '${extension}'"
130 unless grep { $_ eq $extension } @extensions;
131 ( my $local = $path ) =~ s{\.$extension}{_$suffix.$extension};
132 push @files, $path, $local;
135 @files = map { ( "$path.$_", "${path}_${suffix}.$_" ) } @extensions;
140 =head2 get_config_path
142 This method determines the path, filename prefix and file extension to be used
143 for config loading. It returns the path (up to the filename less the
144 extension) to check and the specific extension to use (if it was specified).
146 The order of preference is specified as:
150 =item * C<$ENV{ MYAPP_CONFIG }>
152 =item * C<$ENV{ CATALYST_CONFIG }>
154 =item * C<$c-E<gt>config-E<gt>{ 'Plugin::ConfigLoader' }-E<gt>{ file }>
156 =item * C<$c-E<gt>path_to( $application_prefix )>
160 If either of the first two user-specified options are directories, the
161 application prefix will be added on to the end of the path.
165 sub get_config_path {
169 my $appname = ref $c || $c;
170 my $prefix = Catalyst::Utils::appprefix( $appname );
171 my $path = Catalyst::Utils::env_value( $c, 'CONFIG' )
172 || $c->config->{ 'Plugin::ConfigLoader' }->{ file }
173 || $c->path_to( $prefix );
175 my ( $extension ) = ( $path =~ m{\.(.{1,4})$} );
178 $path =~ s{[\/\\]$}{};
182 return ( $path, $extension );
185 =head2 get_config_local_suffix
187 Determines the suffix of files used to override the main config. By default
188 this value is C<local>, but it can be specified in the following order of preference:
192 =item * C<$ENV{ MYAPP_CONFIG_LOCAL_SUFFIX }>
194 =item * C<$ENV{ CATALYST_CONFIG_LOCAL_SUFFIX }>
196 =item * C<$c-E<gt>config-E<gt>{ 'Plugin::ConfigLoader' }-E<gt>{ config_local_suffix }>
202 sub get_config_local_suffix {
205 my $appname = ref $c || $c;
206 my $suffix = Catalyst::Utils::env_value( $c, 'CONFIG_LOCAL_SUFFIX' )
207 || $c->config->{ 'Plugin::ConfigLoader' }->{ config_local_suffix }
217 prefix => $_ eq 'Component' ? '' : $_ . '::',
218 values => delete $config->{ lc $_ } || delete $config->{ $_ }
220 grep { ref $config->{ lc $_ } || ref $config->{ $_ } }
221 qw( Component Model M View V Controller C )
224 foreach my $comp ( @components ) {
225 my $prefix = $comp->{ prefix };
226 foreach my $element ( keys %{ $comp->{ values } } ) {
227 $config->{ "$prefix$element" } = $comp->{ values }->{ $element };
232 =head2 finalize_config
234 This method is called after the config file is loaded. It can be
235 used to implement tuning of config values that can only be done
236 at runtime. If you need to do this to properly configure any
237 plugins, it's important to load ConfigLoader before them.
238 ConfigLoader provides a default finalize_config method which
239 walks through the loaded config hash and calls the C<config_substitutions>
244 sub finalize_config {
246 my $v = Data::Visitor::Callback->new(
248 return unless defined $_;
249 $c->config_substitutions( $_ );
252 $v->visit( $c->config );
255 =head2 config_substitutions( $value )
257 This method substitutes macros found with calls to a function. There are three
262 =item * C<__HOME__> - replaced with C<$c-E<gt>path_to('')>
264 =item * C<__path_to(foo/bar)__> - replaced with C<$c-E<gt>path_to('foo/bar')>
266 =item * C<__literal(__FOO__)__> - leaves __FOO__ alone (allows you to use
267 C<__DATA__> as a config value, for example)
271 The parameter list is split on comma (C<,>). You can override this method to
272 do your own string munging, or you can define your own macros in
273 C<MyApp-E<gt>config-E<gt>{ 'Plugin::ConfigLoader' }-E<gt>{ substitutions }>.
276 MyApp->config->{ 'Plugin::ConfigLoader' }->{ substitutions } = {
277 baz => sub { my $c = shift; qux( @_ ); }
280 The above will respond to C<__baz(x,y)__> in config strings.
284 sub config_substitutions {
286 my $subs = $c->config->{ 'Plugin::ConfigLoader' }->{ substitutions }
288 $subs->{ HOME } ||= sub { shift->path_to( '' ); };
289 $subs->{ path_to } ||= sub { shift->path_to( @_ ); };
290 $subs->{ literal } ||= sub { return $_[ 1 ]; };
291 my $subsre = join( '|', keys %$subs );
294 s{__($subsre)(?:\((.+?)\))?__}{ $subs->{ $1 }->( $c, $2 ? split( /,/, $2 ) : () ) }eg;
300 Brian Cassidy E<lt>bricas@cpan.orgE<gt>
304 The following people have generously donated their time to the
305 development of this module:
309 =item * Joel Bernstein E<lt>rataxis@cpan.orgE<gt> - Rewrite to use L<Config::Any>
311 =item * David Kamholz E<lt>dkamholz@cpan.orgE<gt> - L<Data::Visitor> integration
315 Work to this module has been generously sponsored by:
319 =item * Portugal Telecom L<http://www.sapo.pt/> - Work done by Joel Bernstein
323 =head1 COPYRIGHT AND LICENSE
325 Copyright 2008 by Brian Cassidy
327 This library is free software; you can redistribute it and/or modify
328 it under the same terms as Perl itself.
336 =item * L<Catalyst::Plugin::ConfigLoader::Manual>
338 =item * L<Config::Any>