updated to version 0.08
Brian Cassidy [Tue, 23 May 2006 13:49:44 +0000 (13:49 +0000)]
lib/Catalyst/Plugin/ConfigLoader.pm
lib/Catalyst/Plugin/ConfigLoader/YAML.pm

index 374dc41..edc69d4 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,10 +66,26 @@ sub setup {
         for( @files ) {\r
             next unless -f $_;\r
             my $config = $loader->load( $_ );\r
+\r
+            $c->log->debug( "Loaded Config $_" ) if $c->debug;\r
             \r
+            next if !$config;\r
+\r
             _fix_syntax( $config );\r
             \r
-            $c->config( $config ) if $config;\r
+            # merge hashes 1 level down\r
+            for my $key ( keys %$config ) {\r
+                if( exists $c->config->{ $key } ) {\r
+                    my $isa_ref = ref $config->{ $key };\r
+\r
+                    next if !$isa_ref or $isa_ref ne 'HASH';\r
+\r
+                    my %temp = ( %{ $c->config->{ $key } }, %{ $config->{ $key } } );\r
+                    $config->{ $key } = \%temp;\r
+                }\r
+            }\r
+            \r
+            $c->config( $config );\r
         }\r
     }\r
 \r
@@ -107,6 +121,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
index 3e521ff..f1e6a86 100644 (file)
@@ -15,6 +15,9 @@ Loads YAML files. Example:
     name: TestApp\r
     Controller::Foo:\r
         foo: bar\r
+    Model::Baz:\r
+        qux: xyzzy\r
+    \r
 \r
 =head1 METHODS\r
 \r