Commit | Line | Data |
f004a98a |
1 | package Catalyst::Plugin::ConfigLoader; |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
6 | use Config::Any; |
7 | use NEXT; |
8 | use Data::Visitor::Callback; |
7f0397f8 |
9 | use Catalyst::Utils (); |
f004a98a |
10 | |
7f0397f8 |
11 | our $VERSION = '0.16'; |
f004a98a |
12 | |
13 | =head1 NAME |
14 | |
15 | Catalyst::Plugin::ConfigLoader - Load config files of various types |
16 | |
17 | =head1 SYNOPSIS |
18 | |
19 | package MyApp; |
20 | |
21 | # ConfigLoader should be first in your list so |
22 | # other plugins can get the config information |
23 | use Catalyst qw( ConfigLoader ... ); |
24 | |
25 | # by default myapp.* will be loaded |
26 | # you can specify a file if you'd like |
27 | __PACKAGE__->config( file => 'config.yaml' ); |
28 | |
29 | =head1 DESCRIPTION |
30 | |
31 | This module will attempt to load find and load a configuration |
32 | file of various types. Currently it supports YAML, JSON, XML, |
33 | INI and Perl formats. |
34 | |
35 | To support the distinction between development and production environments, |
36 | this module will also attemp to load a local config (e.g. myapp_local.yaml) |
37 | which will override any duplicate settings. |
38 | |
39 | =head1 METHODS |
40 | |
41 | =head2 setup( ) |
42 | |
43 | This method is automatically called by Catalyst's setup routine. It will |
44 | attempt to use each plugin and, once a file has been successfully |
45 | loaded, set the C<config()> section. |
46 | |
47 | =cut |
48 | |
49 | sub setup { |
50 | my $c = shift; |
51 | my @files = $c->find_files; |
52 | my $cfg = Config::Any->load_files( { |
53 | files => \@files, |
54 | filter => \&_fix_syntax, |
55 | use_ext => 1 |
56 | } ); |
57 | |
58 | # split the responses into normal and local cfg |
59 | my $local_suffix = $c->get_config_local_suffix; |
60 | my( @cfg, @localcfg ); |
61 | for( @$cfg ) { |
62 | if( ( keys %$_ )[ 0 ] =~ m{ $local_suffix \. }xms ) { |
63 | push @localcfg, $_; |
64 | } else { |
65 | push @cfg, $_; |
66 | } |
67 | } |
68 | |
69 | # load all the normal cfgs, then the local cfgs last so they can override |
70 | # normal cfgs |
71 | $c->load_config( $_ ) for @cfg, @localcfg; |
72 | |
73 | $c->finalize_config; |
74 | $c->NEXT::setup( @_ ); |
75 | } |
76 | |
77 | =head2 load_config |
78 | |
79 | This method handles loading the configuration data into the Catalyst |
80 | context object. It does not return a value. |
81 | |
82 | =cut |
83 | |
84 | sub load_config { |
85 | my $c = shift; |
86 | my $ref = shift; |
87 | |
88 | my( $file, $config ) = each %$ref; |
89 | |
90 | $c->config( $config ); |
91 | $c->log->debug( qq(Loaded Config "$file") ) |
92 | if $c->debug; |
93 | |
94 | return; |
95 | } |
96 | |
97 | =head2 find_files |
98 | |
99 | This method determines the potential file paths to be used for config loading. |
100 | It returns an array of paths (up to the filename less the extension) to pass to |
101 | L<Config::Any|Config::Any> for loading. |
102 | |
103 | =cut |
104 | |
105 | sub find_files { |
106 | my $c = shift; |
107 | my( $path, $extension ) = $c->get_config_path; |
108 | my $suffix = $c->get_config_local_suffix; |
109 | my @extensions = @{ Config::Any->extensions }; |
110 | |
111 | my @files; |
112 | if ($extension) { |
113 | next unless grep { $_ eq $extension } @extensions; |
114 | push @files, $path, "${path}_${suffix}"; |
115 | } else { |
116 | @files = map { ( "$path.$_", "${path}_${suffix}.$_" ) } @extensions; |
117 | } |
118 | |
119 | @files; |
120 | } |
121 | |
122 | =head2 get_config_path |
123 | |
124 | This method determines the path, filename prefix and file extension to be used |
125 | for config loading. It returns the path (up to the filename less the |
126 | extension) to check and the specific extension to use (if it was specified). |
127 | |
128 | The order of preference is specified as: |
129 | |
130 | =over 4 |
131 | |
132 | =item * C<$ENV{ MYAPP_CONFIG }> |
133 | |
7f0397f8 |
134 | =item * C<$ENV{ CATALYST_CONFIG }> |
135 | |
f004a98a |
136 | =item * C<$c-E<gt>config-E<gt>{ file }> |
137 | |
138 | =item * C<$c-E<gt>path_to( $application_prefix )> |
139 | |
140 | =back |
141 | |
142 | If either of the first two user-specified options are directories, the |
143 | application prefix will be added on to the end of the path. |
144 | |
145 | =cut |
146 | |
147 | sub get_config_path { |
148 | my $c = shift; |
149 | my $appname = ref $c || $c; |
150 | my $prefix = Catalyst::Utils::appprefix( $appname ); |
7f0397f8 |
151 | my $path = Catalyst::Utils::env_value( $c, 'CONFIG' ) |
f004a98a |
152 | || $c->config->{ file } |
153 | || $c->path_to( $prefix ); |
154 | |
155 | my( $extension ) = ( $path =~ m{\.(.{1,4})$} ); |
156 | |
157 | if( -d $path ) { |
158 | $path =~ s{[\/\\]$}{}; |
159 | $path .= "/$prefix"; |
160 | } |
161 | |
162 | return( $path, $extension ); |
163 | } |
164 | |
165 | =head2 get_config_local_suffix |
166 | |
167 | Determines the suffix of files used to override the main config. By default |
168 | this value is C<local>, but it can be specified in the following order of preference: |
169 | |
170 | =over 4 |
171 | |
f004a98a |
172 | =item * C<$ENV{ MYAPP_CONFIG_LOCAL_SUFFIX }> |
173 | |
7f0397f8 |
174 | =item * C<$ENV{ CATALYST_CONFIG_LOCAL_SUFFIX }> |
175 | |
f004a98a |
176 | =item * C<$c-E<gt>config-E<gt>{ config_local_suffix }> |
177 | |
178 | =back |
179 | |
180 | =cut |
181 | |
182 | sub get_config_local_suffix { |
183 | my $c = shift; |
184 | my $appname = ref $c || $c; |
7f0397f8 |
185 | my $suffix = Catalyst::Utils::env_value( $c, 'CONFIG_LOCAL_SUFFIX' ) |
f004a98a |
186 | || $c->config->{ config_local_suffix } |
187 | || 'local'; |
188 | |
189 | return $suffix; |
190 | } |
191 | |
192 | sub _fix_syntax { |
193 | my $config = shift; |
194 | my @components = ( |
195 | map +{ |
196 | prefix => $_ eq 'Component' ? '' : $_ . '::', |
197 | values => delete $config->{ lc $_ } || delete $config->{ $_ } |
198 | }, |
199 | grep { |
200 | ref $config->{ lc $_ } || ref $config->{ $_ } |
201 | } |
202 | qw( Component Model M View V Controller C ) |
203 | ); |
204 | |
205 | foreach my $comp ( @components ) { |
206 | my $prefix = $comp->{ prefix }; |
207 | foreach my $element ( keys %{ $comp->{ values } } ) { |
208 | $config->{ "$prefix$element" } = $comp->{ values }->{ $element }; |
209 | } |
210 | } |
211 | } |
212 | |
213 | =head2 finalize_config |
214 | |
215 | This method is called after the config file is loaded. It can be |
216 | used to implement tuning of config values that can only be done |
217 | at runtime. If you need to do this to properly configure any |
218 | plugins, it's important to load ConfigLoader before them. |
219 | ConfigLoader provides a default finalize_config method which |
220 | walks through the loaded config hash and replaces any strings |
221 | beginning containing C<__HOME__> with the full path to |
222 | app's home directory (i.e. C<$c-E<gt>path_to('')> ). |
223 | You can also use C<__path_to(foo/bar)__> which translates to |
224 | C<$c-E<gt>path_to('foo', 'bar')> |
225 | |
226 | =cut |
227 | |
228 | sub finalize_config { |
229 | my $c = shift; |
230 | my $v = Data::Visitor::Callback->new( |
231 | plain_value => sub { |
232 | return unless defined $_; |
3af1b75e |
233 | s{__HOME__}{ $c->path_to( '' ) }eg; |
234 | s{__path_to\((.+?)\)__}{ $c->path_to( split( '/', $1 ) ) }eg; |
f004a98a |
235 | } |
236 | ); |
237 | $v->visit( $c->config ); |
238 | } |
239 | |
240 | =head1 AUTHOR |
241 | |
01be879a |
242 | Brian Cassidy E<lt>bricas@cpan.orgE<gt> |
f004a98a |
243 | |
244 | =head1 CONTRIBUTORS |
245 | |
246 | The following people have generously donated their time to the |
247 | development of this module: |
248 | |
249 | =over 4 |
250 | |
251 | =item * Joel Bernstein E<lt>rataxis@cpan.orgE<gt> - Rewrite to use L<Config::Any> |
252 | |
253 | =item * David Kamholz E<lt>dkamholz@cpan.orgE<gt> - L<Data::Visitor> integration |
254 | |
255 | =back |
256 | |
257 | Work to this module has been generously sponsored by: |
258 | |
259 | =over 4 |
260 | |
261 | =item * Portugal Telecom L<http://www.sapo.pt/> - Work done by Joel Bernstein |
262 | |
263 | =back |
264 | |
265 | =head1 COPYRIGHT AND LICENSE |
266 | |
80ffa192 |
267 | Copyright 2007 by Brian Cassidy |
f004a98a |
268 | |
269 | This library is free software; you can redistribute it and/or modify |
270 | it under the same terms as Perl itself. |
271 | |
272 | =head1 SEE ALSO |
273 | |
274 | =over 4 |
275 | |
276 | =item * L<Catalyst> |
277 | |
affbca23 |
278 | =item * L<Catalyst::Plugin::ConfigLoader::Manual> |
279 | |
f004a98a |
280 | =item * L<Config::Any> |
281 | |
282 | =back |
283 | |
284 | =cut |
285 | |
286 | 1; |