X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FCatalyst%2FPlugin%2FConfigLoader.pm;h=8763331d33cabe73042f6e672aef602ff8ba2058;hb=7f0397f817a8f446fa55b0a3798b21754048ee7e;hp=5442a71d611939236194a0c9f21d973d9c2b2c15;hpb=2b402f346273ef43d95e12a1807dceab4ec67064;p=catagits%2FCatalyst-Plugin-ConfigLoader.git diff --git a/lib/Catalyst/Plugin/ConfigLoader.pm b/lib/Catalyst/Plugin/ConfigLoader.pm index 5442a71..8763331 100644 --- a/lib/Catalyst/Plugin/ConfigLoader.pm +++ b/lib/Catalyst/Plugin/ConfigLoader.pm @@ -2,10 +2,13 @@ package Catalyst::Plugin::ConfigLoader; use strict; use warnings; + use Config::Any; use NEXT; use Data::Visitor::Callback; -our $VERSION = '0.13'; +use Catalyst::Utils (); + +our $VERSION = '0.16'; =head1 NAME @@ -44,22 +47,53 @@ loaded, set the C section. =cut sub setup { - my $c = shift; - + my $c = shift; my @files = $c->find_files; - my $cfg = Config::Any->load_stems({stems => \@files, filter => \&_fix_syntax}); - - for my $ref (@$cfg) { - my ($file, $config) = each %$ref; - $c->config($config); - $c->log->debug( qq(Loaded Config "$file") ) - if $c->debug; + my $cfg = Config::Any->load_files( { + files => \@files, + filter => \&_fix_syntax, + use_ext => 1 + } ); + + # split the responses into normal and local cfg + my $local_suffix = $c->get_config_local_suffix; + my( @cfg, @localcfg ); + for( @$cfg ) { + if( ( keys %$_ )[ 0 ] =~ m{ $local_suffix \. }xms ) { + push @localcfg, $_; + } else { + push @cfg, $_; + } } + + # load all the normal cfgs, then the local cfgs last so they can override + # normal cfgs + $c->load_config( $_ ) for @cfg, @localcfg; $c->finalize_config; $c->NEXT::setup( @_ ); } +=head2 load_config + +This method handles loading the configuration data into the Catalyst +context object. It does not return a value. + +=cut + +sub load_config { + my $c = shift; + my $ref = shift; + + my( $file, $config ) = each %$ref; + + $c->config( $config ); + $c->log->debug( qq(Loaded Config "$file") ) + if $c->debug; + + return; +} + =head2 find_files This method determines the potential file paths to be used for config loading. @@ -70,17 +104,18 @@ L for loading. sub find_files { my $c = shift; - my ($path, $extension) = $c->get_config_path; - my $suffix = $c->get_config_local_suffix; + my( $path, $extension ) = $c->get_config_path; + my $suffix = $c->get_config_local_suffix; my @extensions = @{ Config::Any->extensions }; my @files; if ($extension) { next unless grep { $_ eq $extension } @extensions; - push @files, $path; + push @files, $path, "${path}_${suffix}"; } else { @files = map { ( "$path.$_", "${path}_${suffix}.$_" ) } @extensions; } + @files; } @@ -96,6 +131,8 @@ The order of preference is specified as: =item * C<$ENV{ MYAPP_CONFIG }> +=item * C<$ENV{ CATALYST_CONFIG }> + =item * C<$c-Econfig-E{ file }> =item * C<$c-Epath_to( $application_prefix )> @@ -111,7 +148,7 @@ sub get_config_path { my $c = shift; my $appname = ref $c || $c; my $prefix = Catalyst::Utils::appprefix( $appname ); - my $path = $ENV{ Catalyst::Utils::class2env( $appname ) . '_CONFIG' } + my $path = Catalyst::Utils::env_value( $c, 'CONFIG' ) || $c->config->{ file } || $c->path_to( $prefix ); @@ -132,12 +169,11 @@ this value is C, but it can be specified in the following order of prefer =over 4 -=item * C<$ENV{ CATALYST_CONFIG_LOCAL_SUFFIX }> - =item * C<$ENV{ MYAPP_CONFIG_LOCAL_SUFFIX }> -=item * C<$c-Econfig-E{ config_local_suffix }> +=item * C<$ENV{ CATALYST_CONFIG_LOCAL_SUFFIX }> +=item * C<$c-Econfig-E{ config_local_suffix }> =back @@ -146,8 +182,7 @@ this value is C, but it can be specified in the following order of prefer sub get_config_local_suffix { my $c = shift; my $appname = ref $c || $c; - my $suffix = $ENV{ CATALYST_CONFIG_LOCAL_SUFFIX } - || $ENV{ Catalyst::Utils::class2env( $appname ) . '_CONFIG_LOCAL_SUFFIX' } + my $suffix = Catalyst::Utils::env_value( $c, 'CONFIG_LOCAL_SUFFIX' ) || $c->config->{ config_local_suffix } || 'local'; @@ -195,11 +230,57 @@ sub finalize_config { my $v = Data::Visitor::Callback->new( plain_value => sub { return unless defined $_; - s{__HOME__}{ $c->path_to( '' ) }e; - s{__path_to\((.+)\)__}{ $c->path_to( split( '/', $1 ) ) }e; + s{__HOME__}{ $c->path_to( '' ) }eg; + s{__path_to\((.+?)\)__}{ $c->path_to( split( '/', $1 ) ) }eg; } ); $v->visit( $c->config ); } +=head1 AUTHOR + +Brian Cassidy Ebricas@cpan.orgE + +=head1 CONTRIBUTORS + +The following people have generously donated their time to the +development of this module: + +=over 4 + +=item * Joel Bernstein Erataxis@cpan.orgE - Rewrite to use L + +=item * David Kamholz Edkamholz@cpan.orgE - L integration + +=back + +Work to this module has been generously sponsored by: + +=over 4 + +=item * Portugal Telecom L - Work done by Joel Bernstein + +=back + +=head1 COPYRIGHT AND LICENSE + +Copyright 2007 by Brian Cassidy + +This library is free software; you can redistribute it and/or modify +it under the same terms as Perl itself. + +=head1 SEE ALSO + +=over 4 + +=item * L + +=item * L + +=item * L + +=back + +=cut + 1;