1 package Catalyst::Plugin::ConfigLoader;
8 use Data::Visitor::Callback;
10 our $VERSION = '0.14';
14 Catalyst::Plugin::ConfigLoader - Load config files of various types
20 # ConfigLoader should be first in your list so
21 # other plugins can get the config information
22 use Catalyst qw( ConfigLoader ... );
24 # by default myapp.* will be loaded
25 # you can specify a file if you'd like
26 __PACKAGE__->config( file => 'config.yaml' );
30 This module will attempt to load find and load a configuration
31 file of various types. Currently it supports YAML, JSON, XML,
34 To support the distinction between development and production environments,
35 this module will also attemp to load a local config (e.g. myapp_local.yaml)
36 which will override any duplicate settings.
42 This method is automatically called by Catalyst's setup routine. It will
43 attempt to use each plugin and, once a file has been successfully
44 loaded, set the C<config()> section.
50 my @files = $c->find_files;
51 my $cfg = Config::Any->load_files( {
53 filter => \&_fix_syntax,
57 # split the responses into normal and local cfg
58 my $local_suffix = $c->get_config_local_suffix;
59 my( @cfg, @localcfg );
61 if( ( keys %$_ )[ 0 ] =~ m{ $local_suffix \. }xms ) {
68 # load all the normal cfgs, then the local cfgs last so they can override
70 $c->load_config( $_ ) for @cfg, @localcfg;
73 $c->NEXT::setup( @_ );
78 This method handles loading the configuration data into the Catalyst
79 context object. It does not return a value.
87 my( $file, $config ) = each %$ref;
89 $c->config( $config );
90 $c->log->debug( qq(Loaded Config "$file") )
98 This method determines the potential file paths to be used for config loading.
99 It returns an array of paths (up to the filename less the extension) to pass to
100 L<Config::Any|Config::Any> for loading.
106 my( $path, $extension ) = $c->get_config_path;
107 my $suffix = $c->get_config_local_suffix;
108 my @extensions = @{ Config::Any->extensions };
112 next unless grep { $_ eq $extension } @extensions;
113 push @files, $path, "${path}_${suffix}";
115 @files = map { ( "$path.$_", "${path}_${suffix}.$_" ) } @extensions;
121 =head2 get_config_path
123 This method determines the path, filename prefix and file extension to be used
124 for config loading. It returns the path (up to the filename less the
125 extension) to check and the specific extension to use (if it was specified).
127 The order of preference is specified as:
131 =item * C<$ENV{ MYAPP_CONFIG }>
133 =item * C<$c-E<gt>config-E<gt>{ file }>
135 =item * C<$c-E<gt>path_to( $application_prefix )>
139 If either of the first two user-specified options are directories, the
140 application prefix will be added on to the end of the path.
144 sub get_config_path {
146 my $appname = ref $c || $c;
147 my $prefix = Catalyst::Utils::appprefix( $appname );
148 my $path = $ENV{ Catalyst::Utils::class2env( $appname ) . '_CONFIG' }
149 || $c->config->{ file }
150 || $c->path_to( $prefix );
152 my( $extension ) = ( $path =~ m{\.(.{1,4})$} );
155 $path =~ s{[\/\\]$}{};
159 return( $path, $extension );
162 =head2 get_config_local_suffix
164 Determines the suffix of files used to override the main config. By default
165 this value is C<local>, but it can be specified in the following order of preference:
169 =item * C<$ENV{ CATALYST_CONFIG_LOCAL_SUFFIX }>
171 =item * C<$ENV{ MYAPP_CONFIG_LOCAL_SUFFIX }>
173 =item * C<$c-E<gt>config-E<gt>{ config_local_suffix }>
179 sub get_config_local_suffix {
181 my $appname = ref $c || $c;
182 my $suffix = $ENV{ CATALYST_CONFIG_LOCAL_SUFFIX }
183 || $ENV{ Catalyst::Utils::class2env( $appname ) . '_CONFIG_LOCAL_SUFFIX' }
184 || $c->config->{ config_local_suffix }
194 prefix => $_ eq 'Component' ? '' : $_ . '::',
195 values => delete $config->{ lc $_ } || delete $config->{ $_ }
198 ref $config->{ lc $_ } || ref $config->{ $_ }
200 qw( Component Model M View V Controller C )
203 foreach my $comp ( @components ) {
204 my $prefix = $comp->{ prefix };
205 foreach my $element ( keys %{ $comp->{ values } } ) {
206 $config->{ "$prefix$element" } = $comp->{ values }->{ $element };
211 =head2 finalize_config
213 This method is called after the config file is loaded. It can be
214 used to implement tuning of config values that can only be done
215 at runtime. If you need to do this to properly configure any
216 plugins, it's important to load ConfigLoader before them.
217 ConfigLoader provides a default finalize_config method which
218 walks through the loaded config hash and replaces any strings
219 beginning containing C<__HOME__> with the full path to
220 app's home directory (i.e. C<$c-E<gt>path_to('')> ).
221 You can also use C<__path_to(foo/bar)__> which translates to
222 C<$c-E<gt>path_to('foo', 'bar')>
226 sub finalize_config {
228 my $v = Data::Visitor::Callback->new(
230 return unless defined $_;
231 s{__HOME__}{ $c->path_to( '' ) }e;
232 s{__path_to\((.+)\)__}{ $c->path_to( split( '/', $1 ) ) }e;
235 $v->visit( $c->config );
242 =item * Brian Cassidy E<lt>bricas@cpan.orgE<gt>
248 The following people have generously donated their time to the
249 development of this module:
253 =item * Joel Bernstein E<lt>rataxis@cpan.orgE<gt> - Rewrite to use L<Config::Any>
255 =item * David Kamholz E<lt>dkamholz@cpan.orgE<gt> - L<Data::Visitor> integration
259 Work to this module has been generously sponsored by:
263 =item * Portugal Telecom L<http://www.sapo.pt/> - Work done by Joel Bernstein
267 =head1 COPYRIGHT AND LICENSE
269 Copyright 2007 by Brian Cassidy
271 This library is free software; you can redistribute it and/or modify
272 it under the same terms as Perl itself.
280 =item * L<Catalyst::Plugin::ConfigLoader::Manual>
282 =item * L<Config::Any>