1 package Catalyst::Plugin::ConfigLoader;
8 use Data::Visitor::Callback;
9 use Catalyst::Utils ();
11 our $VERSION = '0.19';
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' } );
31 This module will attempt to load find and load a configuration
32 file of various types. Currently it supports YAML, JSON, XML,
33 INI and Perl formats. Special configuration for a particular driver format can
34 be stored in C<MyApp-E<gt>config-E<gt>{ 'Plugin::ConfigLoader' }-E<gt>{ driver }>.
36 To support the distinction between development and production environments,
37 this module will also attemp to load a local config (e.g. myapp_local.yaml)
38 which will override any duplicate settings.
44 This method is automatically called by Catalyst's setup routine. It will
45 attempt to use each plugin and, once a file has been successfully
46 loaded, set the C<config()> section.
52 my @files = $c->find_files;
53 my $cfg = Config::Any->load_files(
55 filter => \&_fix_syntax,
57 driver_args => $c->config->{ 'Plugin::ConfigLoader' }->{ driver }
62 # split the responses into normal and local cfg
63 my $local_suffix = $c->get_config_local_suffix;
64 my ( @cfg, @localcfg );
66 if ( ( keys %$_ )[ 0 ] =~ m{ $local_suffix \. }xms ) {
74 # load all the normal cfgs, then the local cfgs last so they can override
76 $c->load_config( $_ ) for @cfg, @localcfg;
79 $c->NEXT::setup( @_ );
84 This method handles loading the configuration data into the Catalyst
85 context object. It does not return a value.
93 my ( $file, $config ) = %$ref;
95 $c->config( $config );
96 $c->log->debug( qq(Loaded Config "$file") )
104 This method determines the potential file paths to be used for config loading.
105 It returns an array of paths (up to the filename less the extension) to pass to
106 L<Config::Any|Config::Any> for loading.
112 my ( $path, $extension ) = $c->get_config_path;
113 my $suffix = $c->get_config_local_suffix;
114 my @extensions = @{ Config::Any->extensions };
118 next unless grep { $_ eq $extension } @extensions;
119 ( my $local = $path ) =~ s{\.$extension}{_$suffix.$extension};
120 push @files, $path, $local;
123 @files = map { ( "$path.$_", "${path}_${suffix}.$_" ) } @extensions;
129 =head2 get_config_path
131 This method determines the path, filename prefix and file extension to be used
132 for config loading. It returns the path (up to the filename less the
133 extension) to check and the specific extension to use (if it was specified).
135 The order of preference is specified as:
139 =item * C<$ENV{ MYAPP_CONFIG }>
141 =item * C<$ENV{ CATALYST_CONFIG }>
143 =item * C<$c-E<gt>config-E<gt>{ 'Plugin::ConfigLoader' }-E<gt>{ file }>
145 =item * C<$c-E<gt>path_to( $application_prefix )>
149 If either of the first two user-specified options are directories, the
150 application prefix will be added on to the end of the path.
152 DEPRECATION NOTICE: C<$c-E<gt>config-E<gt>{ file }> is deprecated
153 and will be removed in the next release.
157 sub get_config_path {
161 if ( exists $c->config->{ file } ) {
163 q("file" config parameter has been deprecated in favor of "$c->config->{ 'Plugin::ConfigLoader' }->{ file }")
167 my $appname = ref $c || $c;
168 my $prefix = Catalyst::Utils::appprefix( $appname );
169 my $path = Catalyst::Utils::env_value( $c, 'CONFIG' )
170 || $c->config->{ 'Plugin::ConfigLoader' }->{ file }
171 || $c->config->{ file } # to be removed next release
172 || $c->path_to( $prefix );
174 my ( $extension ) = ( $path =~ m{\.(.{1,4})$} );
177 $path =~ s{[\/\\]$}{};
181 return ( $path, $extension );
184 =head2 get_config_local_suffix
186 Determines the suffix of files used to override the main config. By default
187 this value is C<local>, but it can be specified in the following order of preference:
191 =item * C<$ENV{ MYAPP_CONFIG_LOCAL_SUFFIX }>
193 =item * C<$ENV{ CATALYST_CONFIG_LOCAL_SUFFIX }>
195 =item * C<$c-E<gt>config-E<gt>{ 'Plugin::ConfigLoader' }-E<gt>{ config_local_suffix }>
199 DEPRECATION NOTICE: C<$c-E<gt>config-E<gt>{ config_local_suffix }> is deprecated
200 and will be removed in the next release.
204 sub get_config_local_suffix {
208 if ( exists $c->config->{ config_local_suffix } ) {
210 q("config_local_suffix" config parameter has been deprecated in favor of "$c->config->{ 'Plugin::ConfigLoader' }->{ config_local_suffix }")
214 my $appname = ref $c || $c;
215 my $suffix = Catalyst::Utils::env_value( $c, 'CONFIG_LOCAL_SUFFIX' )
216 || $c->config->{ 'Plugin::ConfigLoader' }->{ config_local_suffix }
218 ->{ config_local_suffix } # to be remove in the next release
228 prefix => $_ eq 'Component' ? '' : $_ . '::',
229 values => delete $config->{ lc $_ } || delete $config->{ $_ }
231 grep { ref $config->{ lc $_ } || ref $config->{ $_ } }
232 qw( Component Model M View V Controller C )
235 foreach my $comp ( @components ) {
236 my $prefix = $comp->{ prefix };
237 foreach my $element ( keys %{ $comp->{ values } } ) {
238 $config->{ "$prefix$element" } = $comp->{ values }->{ $element };
243 =head2 finalize_config
245 This method is called after the config file is loaded. It can be
246 used to implement tuning of config values that can only be done
247 at runtime. If you need to do this to properly configure any
248 plugins, it's important to load ConfigLoader before them.
249 ConfigLoader provides a default finalize_config method which
250 walks through the loaded config hash and calls the C<config_substitutions>
255 sub finalize_config {
257 my $v = Data::Visitor::Callback->new(
259 return unless defined $_;
260 $c->config_substitutions( $_ );
263 $v->visit( $c->config );
266 =head2 config_substitutions( $value )
268 This method substitutes macros found with calls to a function. There are three
273 =item * C<__HOME__> - replaced with C<$c-E<gt>path_to('')>
275 =item * C<__path_to(foo/bar)__> - replaced with C<$c-E<gt>path_to('foo/bar')>
277 =item * C<__literal(__FOO__)__> - leaves __FOO__ alone (allows you to use
278 C<__DATA__> as a config value, for example)
282 The parameter list is split on comma (C<,>). You can override this method to
283 do your own string munging, or you can define your own macros in
284 C<MyApp-E<gt>config-E<gt>{ 'Plugin::ConfigLoader' }-E<gt>{ substitutions }>.
287 MyApp->config->{ 'Plugin::ConfigLoader' }->{ substitutions } = {
288 baz => sub { my $c = shift; qux( @_ ); }
291 The above will respond to C<__baz(x,y)__> in config strings.
295 sub config_substitutions {
297 my $subs = $c->config->{ 'Plugin::ConfigLoader' }->{ substitutions }
299 $subs->{ HOME } ||= sub { shift->path_to( '' ); };
300 $subs->{ path_to } ||= sub { shift->path_to( @_ ); };
301 $subs->{ literal } ||= sub { return $_[ 1 ]; };
302 my $subsre = join( '|', keys %$subs );
305 s{__($subsre)(?:\((.+?)\))?__}{ $subs->{ $1 }->( $c, $2 ? split( /,/, $2 ) : () ) }eg;
311 Brian Cassidy E<lt>bricas@cpan.orgE<gt>
315 The following people have generously donated their time to the
316 development of this module:
320 =item * Joel Bernstein E<lt>rataxis@cpan.orgE<gt> - Rewrite to use L<Config::Any>
322 =item * David Kamholz E<lt>dkamholz@cpan.orgE<gt> - L<Data::Visitor> integration
326 Work to this module has been generously sponsored by:
330 =item * Portugal Telecom L<http://www.sapo.pt/> - Work done by Joel Bernstein
334 =head1 COPYRIGHT AND LICENSE
336 Copyright 2007 by Brian Cassidy
338 This library is free software; you can redistribute it and/or modify
339 it under the same terms as Perl itself.
347 =item * L<Catalyst::Plugin::ConfigLoader::Manual>
349 =item * L<Config::Any>