more granular top-level hash merging
[catagits/Catalyst-Plugin-ConfigLoader.git] / lib / Catalyst / Plugin / ConfigLoader.pm
CommitLineData
b2d85594 1package Catalyst::Plugin::ConfigLoader;\r
2\r
3use strict;\r
4use warnings;\r
5\r
6use NEXT;\r
7use Module::Pluggable::Fast\r
8 name => '_config_loaders',\r
9 search => [ __PACKAGE__ ],\r
10 require => 1;\r
9742697f 11use Data::Visitor::Callback;\r
b2d85594 12\r
87dd9ae0 13our $VERSION = '0.08';\r
b2d85594 14\r
15=head1 NAME\r
16\r
17Catalyst::Plugin::ConfigLoader - Load config files of various types\r
18\r
19=head1 SYNOPSIS\r
20\r
c7413665 21 package MyApp;\r
22 \r
d2afa071 23 # ConfigLoader should be first in your list so\r
24 # other plugins can get the config information\r
25 use Catalyst qw( ConfigLoader ... );\r
135fd39b 26 \r
b2d85594 27 # by default myapp.* will be loaded\r
28 # you can specify a file if you'd like\r
87dd9ae0 29 __PACKAGE__->config( file => 'config.yaml' ); \r
b2d85594 30\r
31=head1 DESCRIPTION\r
32\r
04ea923f 33This module will attempt to load find and load a configuration\r
b2d85594 34file of various types. Currently it supports YAML, JSON, XML,\r
35INI and Perl formats.\r
36\r
04ea923f 37To support the distinction between development and production environments,\r
38this module will also attemp to load a local config (e.g. myapp_local.yaml)\r
39which will override any duplicate settings.\r
40\r
b2d85594 41=head1 METHODS\r
42\r
43=head2 setup( )\r
44\r
45This method is automatically called by Catalyst's setup routine. It will\r
d6277728 46attempt to use each plugin and, once a file has been successfully\r
04ea923f 47loaded, set the C<config()> section. \r
b2d85594 48\r
49=cut\r
50\r
51sub setup {\r
87dd9ae0 52 my $c = shift;\r
53 my( $path, $extension ) = $c->get_config_path;\r
b2d85594 54 \r
55 for my $loader ( $c->_config_loaders ) {\r
c7413665 56 my @files;\r
57 my @extensions = $loader->extensions;\r
58 if( $extension ) {\r
59 next unless grep { $_ eq $extension } @extensions;\r
60 push @files, $path;\r
61 }\r
62 else {\r
04ea923f 63 @files = map { ( "$path.$_", "${path}_local.$_" ) } @extensions;\r
c7413665 64 }\r
65\r
66 for( @files ) {\r
67 next unless -f $_;\r
68 my $config = $loader->load( $_ );\r
87dd9ae0 69\r
94621b26 70 $c->log->debug( "Loaded Config $_" ) if $c->debug;\r
7b57af98 71 \r
72 next if !$config;\r
73\r
74 _fix_syntax( $config );\r
75 \r
76 # merge hashes 1 level down\r
77 for my $key ( keys %$config ) {\r
78 if( exists $c->config->{ $key } ) {\r
79 my $isa_ref = ref $config->{ $key };\r
80\r
81 next if !$isa_ref or $isa_ref ne 'HASH';\r
82\r
83 my %temp = ( %{ $c->config->{ $key } }, %{ $config->{ $key } } );\r
84 $config->{ $key } = \%temp;\r
85 }\r
86 }\r
87 \r
88 $c->config( $config );\r
b2d85594 89 }\r
90 }\r
91\r
9742697f 92 $c->finalize_config;\r
93\r
b2d85594 94 $c->NEXT::setup( @_ );\r
95}\r
96\r
9742697f 97=head2 finalize_config\r
98\r
99This method is called after the config file is loaded. It can be\r
100used to implement tuning of config values that can only be done\r
101at runtime. If you need to do this to properly configure any\r
102plugins, it's important to load ConfigLoader before them.\r
103ConfigLoader provides a default finalize_config method which\r
104walks through the loaded config hash and replaces any strings\r
04ea923f 105beginning containing C<__HOME__> with the full path to\r
106app's home directory (i.e. C<$c-E<gt>path_to('')> ).\r
107You can also use C<__path_to('foo/bar')__> which translates to\r
108C<$c-E<gt>path_to('foo', 'bar')> \r
9742697f 109\r
110=cut\r
111\r
112sub finalize_config {\r
113 my $c = shift;\r
114 my $v = Data::Visitor::Callback->new(\r
da18b925 115 plain_value => sub {\r
116 return unless defined $_;\r
117 s[__HOME__][ $c->path_to( '' ) ]e;\r
118 s[__path_to\((.+)\)__][ $c->path_to( split( '/', $1 ) ) ]e;\r
119 }\r
9742697f 120 );\r
dc4daea5 121 $v->visit( $c->config );\r
9742697f 122}\r
123\r
87dd9ae0 124=head2 get_config_path\r
125\r
126This method determines the path, filename prefix and file extension to be used\r
127for config loading. It returns the path (up to the filename less the\r
128extension) to check and the specific extension to use (if it was specified).\r
129\r
130The order of preference is specified as:\r
131\r
132=over 4\r
133\r
134=item * C<$ENV{ MYAPP_CONFIG }>\r
135\r
136=item * C<$c->config->{ file }>\r
137\r
138=item * C<$c->path_to( $application_prefix )>\r
139\r
140=back\r
141\r
142If either of the first two user-specified options are directories, the\r
143application prefix will be added on to the end of the path.\r
144\r
145=cut\r
146\r
147sub get_config_path {\r
148 my $c = shift;\r
149 my $appname = ref $c || $c;\r
150 my $prefix = Catalyst::Utils::appprefix( $appname );\r
151 my $path = $ENV{ Catalyst::Utils::class2env( $appname ) . '_CONFIG' }\r
152 || $c->config->{ file }\r
153 || $c->path_to( $prefix );\r
154\r
155 my( $extension ) = ( $path =~ /\.(.{1,4})$/ );\r
156 \r
157 if( -d $path ) {\r
158 $path =~ s/[\/\\]$//;\r
159 $path .= "/$prefix";\r
160 }\r
161 \r
162 return( $path, $extension );\r
163}\r
164\r
1502c009 165sub _fix_syntax {\r
166 my $config = shift;\r
167 my @components = (\r
168 map +{\r
169 prefix => $_ eq 'Component' ? '' : $_ . '::',\r
170 values => delete $config->{ lc $_ } || delete $config->{ $_ }\r
171 }, qw( Component Model View Controller )\r
172 );\r
173\r
135fd39b 174 foreach my $comp ( @components ) {\r
1502c009 175 my $prefix = $comp->{ prefix };\r
176 foreach my $element ( keys %{ $comp->{ values } } ) {\r
177 $config->{ "$prefix$element" } = $comp->{ values }->{ $element };\r
178 }\r
179 }\r
180}\r
181\r
b2d85594 182=head1 AUTHOR\r
183\r
184=over 4 \r
185\r
186=item * Brian Cassidy E<lt>bricas@cpan.orgE<gt>\r
187\r
188=back\r
189\r
a8288af5 190=head1 CONTRIBUTORS\r
191\r
192The following people have generously donated their time to the\r
193development of this module:\r
194\r
195=over 4\r
196\r
197=item * David Kamholz E<lt>dkamholz@cpan.orgE<gt>\r
198\r
199=back\r
200\r
b2d85594 201=head1 COPYRIGHT AND LICENSE\r
202\r
203Copyright 2006 by Brian Cassidy\r
204\r
205This library is free software; you can redistribute it and/or modify\r
206it under the same terms as Perl itself. \r
207\r
208=head1 SEE ALSO\r
209\r
210=over 4 \r
211\r
212=item * L<Catalyst>\r
213\r
214=back\r
215\r
216=cut\r
217\r
a07967b8 2181;\r