added get_config_path and ability to specific a MYAPP_CONFIG ENV variable
Brian Cassidy [Mon, 15 May 2006 17:01:54 +0000 (17:01 +0000)]
Changes
lib/Catalyst/Plugin/ConfigLoader.pm

diff --git a/Changes b/Changes
index 4cd05ba..7d8737f 100644 (file)
--- a/Changes
+++ b/Changes
@@ -1,5 +1,9 @@
 Revision history for Perl extension Catalyst::Plugin::ConfigLoader.\r
 \r
+0.08  Mon May 15 2006\r
+    - added get_config_path() which extracts the path finding code\r
+    - added the ability to specify a MYAPP_CONFIG ENV variable\r
+\r
 0.07  Mon May 01 2006\r
     - added Config::General support\r
     - added nicer syntax for specifying models/views/controllers where\r
index 374dc41..8fe2df5 100644 (file)
@@ -10,7 +10,7 @@ use Module::Pluggable::Fast
     require => 1;\r
 use Data::Visitor::Callback;\r
 \r
-our $VERSION = '0.07';\r
+our $VERSION = '0.08';\r
 \r
 =head1 NAME\r
 \r
@@ -26,7 +26,7 @@ Catalyst::Plugin::ConfigLoader - Load config files of various types
     \r
     # by default myapp.* will be loaded\r
     # you can specify a file if you'd like\r
-    __PACKAGE__->config( file = > 'config.yaml' );    \r
+    __PACKAGE__->config( file => 'config.yaml' );    \r
 \r
 =head1 DESCRIPTION\r
 \r
@@ -49,10 +49,8 @@ loaded, set the C<config()> section.
 =cut\r
 \r
 sub setup {\r
-    my $c    = shift;\r
-    my $path = $c->config->{ file } || $c->path_to( Catalyst::Utils::appprefix( ref $c || $c ) );\r
-\r
-    my( $extension ) = ( $path =~ /\.(.{1,4})$/ );\r
+    my $c = shift;\r
+    my( $path, $extension ) = $c->get_config_path;\r
     \r
     for my $loader ( $c->_config_loaders ) {\r
         my @files;\r
@@ -68,9 +66,9 @@ sub setup {
         for( @files ) {\r
             next unless -f $_;\r
             my $config = $loader->load( $_ );\r
-            \r
-            _fix_syntax( $config );\r
-            \r
+\r
+            $c->debug( "Loaded Config $_" ) if $c->debug;\r
+           _fix_syntax( $config );\r
             $c->config( $config ) if $config;\r
         }\r
     }\r
@@ -107,6 +105,47 @@ sub finalize_config {
     $v->visit( $c->config );\r
 }\r
 \r
+=head2 get_config_path\r
+\r
+This method determines the path, filename prefix and file extension to be used\r
+for config loading. It returns the path (up to the filename less the\r
+extension) to check and the specific extension to use (if it was specified).\r
+\r
+The order of preference is specified as:\r
+\r
+=over 4\r
+\r
+=item * C<$ENV{ MYAPP_CONFIG }>\r
+\r
+=item * C<$c->config->{ file }>\r
+\r
+=item * C<$c->path_to( $application_prefix )>\r
+\r
+=back\r
+\r
+If either of the first two user-specified options are directories, the\r
+application prefix will be added on to the end of the path.\r
+\r
+=cut\r
+\r
+sub get_config_path {\r
+    my $c       = shift;\r
+    my $appname = ref $c || $c;\r
+    my $prefix  = Catalyst::Utils::appprefix( $appname );\r
+    my $path    = $ENV{ Catalyst::Utils::class2env( $appname ) . '_CONFIG' }\r
+        || $c->config->{ file }\r
+        || $c->path_to( $prefix );\r
+\r
+    my( $extension ) = ( $path =~ /\.(.{1,4})$/ );\r
+    \r
+    if( -d $path ) {\r
+        $path  =~ s/[\/\\]$//;\r
+        $path .= "/$prefix";\r
+    }\r
+    \r
+    return( $path, $extension );\r
+}\r
+\r
 sub _fix_syntax {\r
     my $config     = shift;\r
     my @components = (\r