1 package Catalyst::Plugin::ConfigLoader;
8 use Data::Visitor::Callback;
9 use Catalyst::Utils ();
11 our $VERSION = '0.22';
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 }>.
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. myapp_local.yaml)
56 which will override any duplicate settings.
62 This method is automatically called by Catalyst's setup routine. It will
63 attempt to use each plugin and, once a file has been successfully
64 loaded, set the C<config()> section.
70 my @files = $c->find_files;
71 my $cfg = Config::Any->load_files(
73 filter => \&_fix_syntax,
75 driver_args => $c->config->{ 'Plugin::ConfigLoader' }->{ driver }
79 # map the array of hashrefs to a simple hash
80 my %configs = map { %$_ } @$cfg;
82 # split the responses into normal and local cfg
83 my $local_suffix = $c->get_config_local_suffix;
84 my ( @main, @locals );
85 for ( sort keys %configs ) {
86 if ( m{$local_suffix\.}ms ) {
94 # load all the normal cfgs, then the local cfgs last so they can override
96 $c->load_config( { $_ => $configs{ $_ } } ) for @main, @locals;
99 $c->next::method( @_ );
104 This method handles loading the configuration data into the Catalyst
105 context object. It does not return a value.
113 my ( $file, $config ) = %$ref;
115 $c->config( $config );
116 $c->log->debug( qq(Loaded Config "$file") )
124 This method determines the potential file paths to be used for config loading.
125 It returns an array of paths (up to the filename less the extension) to pass to
126 L<Config::Any|Config::Any> for loading.
132 my ( $path, $extension ) = $c->get_config_path;
133 my $suffix = $c->get_config_local_suffix;
134 my @extensions = @{ Config::Any->extensions };
138 die "Unable to handle files with the extension '${extension}'"
139 unless grep { $_ eq $extension } @extensions;
140 ( my $local = $path ) =~ s{\.$extension}{_$suffix.$extension};
141 push @files, $path, $local;
144 @files = map { ( "$path.$_", "${path}_${suffix}.$_" ) } @extensions;
149 =head2 get_config_path
151 This method determines the path, filename prefix and file extension to be used
152 for config loading. It returns the path (up to the filename less the
153 extension) to check and the specific extension to use (if it was specified).
155 The order of preference is specified as:
159 =item * C<$ENV{ MYAPP_CONFIG }>
161 =item * C<$ENV{ CATALYST_CONFIG }>
163 =item * C<$c-E<gt>config-E<gt>{ 'Plugin::ConfigLoader' }-E<gt>{ file }>
165 =item * C<$c-E<gt>path_to( $application_prefix )>
169 If either of the first two user-specified options are directories, the
170 application prefix will be added on to the end of the path.
174 sub get_config_path {
178 my $appname = ref $c || $c;
179 my $prefix = Catalyst::Utils::appprefix( $appname );
180 my $path = Catalyst::Utils::env_value( $c, 'CONFIG' )
181 || $c->config->{ 'Plugin::ConfigLoader' }->{ file }
182 || $c->path_to( $prefix );
184 my ( $extension ) = ( $path =~ m{\.(.{1,4})$} );
187 $path =~ s{[\/\\]$}{};
191 return ( $path, $extension );
194 =head2 get_config_local_suffix
196 Determines the suffix of files used to override the main config. By default
197 this value is C<local>, but it can be specified in the following order of preference:
201 =item * C<$ENV{ MYAPP_CONFIG_LOCAL_SUFFIX }>
203 =item * C<$ENV{ CATALYST_CONFIG_LOCAL_SUFFIX }>
205 =item * C<$c-E<gt>config-E<gt>{ 'Plugin::ConfigLoader' }-E<gt>{ config_local_suffix }>
211 sub get_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 }
226 prefix => $_ eq 'Component' ? '' : $_ . '::',
227 values => delete $config->{ lc $_ } || delete $config->{ $_ }
229 grep { ref $config->{ lc $_ } || ref $config->{ $_ } }
230 qw( Component Model M View V Controller C Plugin )
233 foreach my $comp ( @components ) {
234 my $prefix = $comp->{ prefix };
235 foreach my $element ( keys %{ $comp->{ values } } ) {
236 $config->{ "$prefix$element" } = $comp->{ values }->{ $element };
241 =head2 finalize_config
243 This method is called after the config file is loaded. It can be
244 used to implement tuning of config values that can only be done
245 at runtime. If you need to do this to properly configure any
246 plugins, it's important to load ConfigLoader before them.
247 ConfigLoader provides a default finalize_config method which
248 walks through the loaded config hash and calls the C<config_substitutions>
253 sub finalize_config {
255 my $v = Data::Visitor::Callback->new(
257 return unless defined $_;
258 $c->config_substitutions( $_ );
261 $v->visit( $c->config );
264 =head2 config_substitutions( $value )
266 This method substitutes macros found with calls to a function. There are three
271 =item * C<__HOME__> - replaced with C<$c-E<gt>path_to('')>
273 =item * C<__path_to(foo/bar)__> - replaced with C<$c-E<gt>path_to('foo/bar')>
275 =item * C<__literal(__FOO__)__> - leaves __FOO__ alone (allows you to use
276 C<__DATA__> as a config value, for example)
280 The parameter list is split on comma (C<,>). You can override this method to
281 do your own string munging, or you can define your own macros in
282 C<MyApp-E<gt>config-E<gt>{ 'Plugin::ConfigLoader' }-E<gt>{ substitutions }>.
285 MyApp->config->{ 'Plugin::ConfigLoader' }->{ substitutions } = {
286 baz => sub { my $c = shift; qux( @_ ); }
289 The above will respond to C<__baz(x,y)__> in config strings.
293 sub config_substitutions {
295 my $subs = $c->config->{ 'Plugin::ConfigLoader' }->{ substitutions }
297 $subs->{ HOME } ||= sub { shift->path_to( '' ); };
298 $subs->{ path_to } ||= sub { shift->path_to( @_ ); };
299 $subs->{ literal } ||= sub { return $_[ 1 ]; };
300 my $subsre = join( '|', keys %$subs );
303 s{__($subsre)(?:\((.+?)\))?__}{ $subs->{ $1 }->( $c, $2 ? split( /,/, $2 ) : () ) }eg;
309 Brian Cassidy E<lt>bricas@cpan.orgE<gt>
313 The following people have generously donated their time to the
314 development of this module:
318 =item * Joel Bernstein E<lt>rataxis@cpan.orgE<gt> - Rewrite to use L<Config::Any>
320 =item * David Kamholz E<lt>dkamholz@cpan.orgE<gt> - L<Data::Visitor> integration
324 Work to this module has been generously sponsored by:
328 =item * Portugal Telecom L<http://www.sapo.pt/> - Work done by Joel Bernstein
332 =head1 COPYRIGHT AND LICENSE
334 Copyright 2008 by Brian Cassidy
336 This library is free software; you can redistribute it and/or modify
337 it under the same terms as Perl itself.
345 =item * L<Catalyst::Plugin::ConfigLoader::Manual>
347 =item * L<Config::Any>