fix configloader to use Catalyst::Utils::env_value()
[catagits/Catalyst-Plugin-ConfigLoader.git] / lib / Catalyst / Plugin / ConfigLoader.pm
index 5442a71..8763331 100644 (file)
@@ -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<config()> 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<Config::Any|Config::Any> 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-E<gt>config-E<gt>{ file }>
 
 =item * C<$c-E<gt>path_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<local>, 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-E<gt>config-E<gt>{ config_local_suffix }>
+=item * C<$ENV{ CATALYST_CONFIG_LOCAL_SUFFIX }>
 
+=item * C<$c-E<gt>config-E<gt>{ config_local_suffix }>
 
 =back
 
@@ -146,8 +182,7 @@ this value is C<local>, 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 E<lt>bricas@cpan.orgE<gt>
+
+=head1 CONTRIBUTORS
+
+The following people have generously donated their time to the
+development of this module:
+
+=over 4
+
+=item * Joel Bernstein E<lt>rataxis@cpan.orgE<gt> - Rewrite to use L<Config::Any>
+
+=item * David Kamholz E<lt>dkamholz@cpan.orgE<gt> - L<Data::Visitor> integration
+
+=back
+
+Work to this module has been generously sponsored by: 
+
+=over 4
+
+=item * Portugal Telecom L<http://www.sapo.pt/> - 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<Catalyst>
+
+=item * L<Catalyst::Plugin::ConfigLoader::Manual>
+
+=item * L<Config::Any>
+
+=back
+
+=cut
+
 1;