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