Commit | Line | Data |
c80a0905 |
1 | package Config::Any; |
92a04e78 |
2 | |
c80a0905 |
3 | use strict; |
92a04e78 |
4 | use warnings; |
5 | |
c80a0905 |
6 | use Carp; |
7 | use Module::Pluggable::Object (); |
59a80452 |
8 | |
8af0a7cf |
9 | our $VERSION = '0.12'; |
c80a0905 |
10 | |
59a80452 |
11 | =head1 NAME |
12 | |
13 | Config::Any - Load configuration from different file formats, transparently |
14 | |
59a80452 |
15 | =head1 SYNOPSIS |
16 | |
17 | use Config::Any; |
18 | |
f0e3c221 |
19 | my $cfg = Config::Any->load_stems({stems => \@filepath_stems, ... }); |
20 | # or |
21 | my $cfg = Config::Any->load_files({files => \@filepaths, ... }); |
59a80452 |
22 | |
f0e3c221 |
23 | for (@$cfg) { |
0c6989c6 |
24 | my ($filename, $config) = %$_; |
f0e3c221 |
25 | $class->config($config); |
26 | warn "loaded config from file: $filename"; |
27 | } |
59a80452 |
28 | |
29 | =head1 DESCRIPTION |
30 | |
31 | L<Config::Any|Config::Any> provides a facility for Perl applications and libraries |
32 | to load configuration data from multiple different file formats. It supports XML, YAML, |
33 | JSON, Apache-style configuration, Windows INI files, and even Perl code. |
34 | |
35 | The rationale for this module is as follows: Perl programs are deployed on many different |
36 | platforms and integrated with many different systems. Systems administrators and end |
37 | users may prefer different configuration formats than the developers. The flexibility |
38 | inherent in a multiple format configuration loader allows different users to make |
39 | different choices, without generating extra work for the developers. As a developer |
40 | you only need to learn a single interface to be able to use the power of different |
41 | configuration formats. |
42 | |
43 | =head1 INTERFACE |
44 | |
45 | =cut |
46 | |
7c218182 |
47 | =head2 load_files( \%args ) |
59a80452 |
48 | |
4efab558 |
49 | Config::Any->load_files( { files => \@files } ); |
50 | Config::Any->load_files( { files => \@files, filter => \&filter } ); |
51 | Config::Any->load_files( { files => \@files, use_ext => 1 } ); |
59a80452 |
52 | |
53 | C<load_files()> attempts to load configuration from the list of files passed in |
54 | the C<files> parameter, if the file exists. |
55 | |
56 | If the C<filter> parameter is set, it is used as a callback to modify the configuration |
57 | data before it is returned. It will be passed a single hash-reference parameter which |
58 | it should modify in-place. |
59 | |
60 | If the C<use_ext> parameter is defined, the loader will attempt to parse the file |
61 | extension from each filename and will skip the file unless it matches a standard |
62 | extension for the loading plugins. Only plugins whose standard extensions match the |
63 | file extension will be used. For efficiency reasons, its use is encouraged, but |
64 | be aware that you will lose flexibility -- for example, a file called C<myapp.cfg> |
65 | containing YAML data will not be offered to the YAML plugin, whereas C<myapp.yml> |
66 | or C<myapp.yaml> would be. |
67 | |
41f47406 |
68 | C<load_files()> also supports a 'force_plugins' parameter, whose value should be an |
69 | arrayref of plugin names like C<Config::Any::INI>. Its intended use is to allow the use |
70 | of a non-standard file extension while forcing it to be offered to a particular parser. |
71 | It is not compatible with 'use_ext'. |
72 | |
ef87b7dc |
73 | You can supply a C<driver_args> hashref to pass special options to a particular |
74 | parser object. Example: |
75 | |
76 | Config::Any->load_files( { files => \@files, driver_args => { |
77 | General => { -LowerCaseNames => 1 } |
78 | } ) |
79 | |
59a80452 |
80 | =cut |
81 | |
c80a0905 |
82 | sub load_files { |
92a04e78 |
83 | my ( $class, $args ) = @_; |
4efab558 |
84 | |
85 | unless ( $args && exists $args->{ files } ) { |
86 | warn "No files specified!"; |
59a80452 |
87 | return; |
88 | } |
89 | |
4efab558 |
90 | return $class->_load( $args ); |
c80a0905 |
91 | } |
92 | |
7c218182 |
93 | =head2 load_stems( \%args ) |
59a80452 |
94 | |
bef9e9a5 |
95 | Config::Any->load_stems( { stems => \@stems } ); |
96 | Config::Any->load_stems( { stems => \@stems, filter => \&filter } ); |
97 | Config::Any->load_stems( { stems => \@stems, use_ext => 1 } ); |
59a80452 |
98 | |
99 | C<load_stems()> attempts to load configuration from a list of files which it generates |
100 | by combining the filename stems list passed in the C<stems> parameter with the |
101 | potential filename extensions from each loader, which you can check with the |
102 | C<extensions()> classmethod described below. Once this list of possible filenames is |
103 | built it is treated exactly as in C<load_files()> above, as which it takes the same |
104 | parameters. Please read the C<load_files()> documentation before using this method. |
105 | |
106 | =cut |
107 | |
c80a0905 |
108 | sub load_stems { |
92a04e78 |
109 | my ( $class, $args ) = @_; |
bef9e9a5 |
110 | |
111 | unless ( $args && exists $args->{ stems } ) { |
112 | warn "No stems specified!"; |
59a80452 |
113 | return; |
114 | } |
92a04e78 |
115 | |
bef9e9a5 |
116 | my $stems = delete $args->{ stems }; |
c80a0905 |
117 | my @files; |
92a04e78 |
118 | for my $s ( @$stems ) { |
92a04e78 |
119 | for my $ext ( $class->extensions ) { |
4efab558 |
120 | push @files, "$s.$ext"; |
c80a0905 |
121 | } |
122 | } |
c80a0905 |
123 | |
bef9e9a5 |
124 | $args->{ files } = \@files; |
125 | return $class->_load( $args ); |
126 | } |
41f47406 |
127 | |
c80a0905 |
128 | sub _load { |
92a04e78 |
129 | my ( $class, $args ) = @_; |
a918b0b8 |
130 | croak "_load requires a arrayref of file paths" unless $args->{ files }; |
131 | |
c84f1613 |
132 | my $force = defined $args->{ force_plugins }; |
133 | if ( !$force and !defined $args->{ use_ext } ) { |
72628dc7 |
134 | warn |
135 | "use_ext argument was not explicitly set, as of 0.09, this is true by default"; |
a918b0b8 |
136 | $args->{ use_ext } = 1; |
137 | } |
138 | |
139 | # figure out what plugins we're using |
ade8c46e |
140 | my @plugins = $force ? @{ $args->{ force_plugins } } : $class->plugins; |
a918b0b8 |
141 | |
142 | # map extensions if we have to |
72628dc7 |
143 | my ( %extension_lut, $extension_re ); |
a918b0b8 |
144 | my $use_ext_lut = !$force && $args->{ use_ext }; |
72628dc7 |
145 | if ( $use_ext_lut ) { |
a918b0b8 |
146 | for my $plugin ( @plugins ) { |
147 | $extension_lut{ $_ } = $plugin for $plugin->extensions; |
148 | } |
149 | |
150 | $extension_re = join( '|', keys %extension_lut ); |
151 | } |
59a80452 |
152 | |
a918b0b8 |
153 | # map args to plugins |
154 | my $base_class = __PACKAGE__; |
155 | my %loader_args; |
156 | for my $plugin ( @plugins ) { |
157 | $plugin =~ m{^$base_class\::(.+)}; |
158 | $loader_args{ $plugin } = $args->{ driver_args }->{ $1 } || {}; |
159 | } |
160 | |
161 | my @results; |
92c29326 |
162 | warn $@ if $@; |
a918b0b8 |
163 | |
164 | for my $filename ( @{ $args->{ files } } ) { |
72628dc7 |
165 | |
a918b0b8 |
166 | # don't even bother if it's not there |
167 | next unless -f $filename; |
168 | |
169 | my @try_plugins = @plugins; |
41f47406 |
170 | |
72628dc7 |
171 | if ( $use_ext_lut ) { |
a918b0b8 |
172 | $filename =~ m{\.($extension_re)\z}; |
173 | next unless $1; |
174 | @try_plugins = $extension_lut{ $1 }; |
175 | } |
176 | |
177 | for my $loader ( @try_plugins ) { |
ade8c46e |
178 | next unless $loader->is_supported; |
72628dc7 |
179 | my @configs |
180 | = eval { $loader->load( $filename, $loader_args{ $loader } ); }; |
41f47406 |
181 | |
a918b0b8 |
182 | # fatal error if we used extension matching |
92c29326 |
183 | croak "Error parsing $filename: $@" if $@ and $use_ext_lut; |
a918b0b8 |
184 | next if $@ or !@configs; |
185 | |
186 | # post-process config with a filter callback |
187 | if ( $args->{ filter } ) { |
188 | $args->{ filter }->( $_ ) for @configs; |
189 | } |
41f47406 |
190 | |
72628dc7 |
191 | push @results, |
192 | { $filename => @configs == 1 ? $configs[ 0 ] : \@configs }; |
a918b0b8 |
193 | last; |
c80a0905 |
194 | } |
195 | } |
7c218182 |
196 | |
a918b0b8 |
197 | return \@results; |
c80a0905 |
198 | } |
199 | |
59a80452 |
200 | =head2 finder( ) |
201 | |
202 | The C<finder()> classmethod returns the |
203 | L<Module::Pluggable::Object|Module::Pluggable::Object> |
204 | object which is used to load the plugins. See the documentation for that module for |
205 | more information. |
206 | |
207 | =cut |
208 | |
c80a0905 |
209 | sub finder { |
92a04e78 |
210 | my $class = shift; |
c80a0905 |
211 | my $finder = Module::Pluggable::Object->new( |
212 | search_path => [ __PACKAGE__ ], |
213 | require => 1 |
214 | ); |
bef9e9a5 |
215 | return $finder; |
c80a0905 |
216 | } |
217 | |
59a80452 |
218 | =head2 plugins( ) |
219 | |
220 | The C<plugins()> classmethod returns the names of configuration loading plugins as |
221 | found by L<Module::Pluggable::Object|Module::Pluggable::Object>. |
222 | |
223 | =cut |
224 | |
c80a0905 |
225 | sub plugins { |
226 | my $class = shift; |
227 | return $class->finder->plugins; |
228 | } |
229 | |
59a80452 |
230 | =head2 extensions( ) |
c80a0905 |
231 | |
59a80452 |
232 | The C<extensions()> classmethod returns the possible file extensions which can be loaded |
233 | by C<load_stems()> and C<load_files()>. This may be useful if you set the C<use_ext> |
234 | parameter to those methods. |
c80a0905 |
235 | |
59a80452 |
236 | =cut |
c80a0905 |
237 | |
59a80452 |
238 | sub extensions { |
239 | my $class = shift; |
240 | my @ext = map { $_->extensions } $class->plugins; |
4efab558 |
241 | return wantarray ? @ext : \@ext; |
59a80452 |
242 | } |
c80a0905 |
243 | |
244 | =head1 DIAGNOSTICS |
245 | |
c80a0905 |
246 | =over |
247 | |
7c218182 |
248 | =item C<No files specified!> or C<No stems specified!> |
c80a0905 |
249 | |
59a80452 |
250 | The C<load_files()> and C<load_stems()> methods will issue this warning if |
251 | called with an empty list of files/stems to load. |
c80a0905 |
252 | |
59a80452 |
253 | =item C<_load requires a arrayref of file paths> |
c80a0905 |
254 | |
59a80452 |
255 | This fatal error will be thrown by the internal C<_load> method. It should not occur |
256 | but is specified here for completeness. If your code dies with this error, please |
257 | email a failing test case to the authors below. |
c80a0905 |
258 | |
259 | =back |
260 | |
c80a0905 |
261 | =head1 CONFIGURATION AND ENVIRONMENT |
262 | |
c80a0905 |
263 | Config::Any requires no configuration files or environment variables. |
264 | |
c80a0905 |
265 | =head1 DEPENDENCIES |
266 | |
59a80452 |
267 | L<Module::Pluggable|Module::Pluggable> |
c80a0905 |
268 | |
59a80452 |
269 | And at least one of the following: |
270 | L<Config::General|Config::General> |
271 | L<Config::Tiny|Config::Tiny> |
272 | L<JSON|JSON> |
273 | L<YAML|YAML> |
274 | L<JSON::Syck|JSON::Syck> |
275 | L<YAML::Syck|YAML::Syck> |
276 | L<XML::Simple|XML::Simple> |
c80a0905 |
277 | |
278 | =head1 INCOMPATIBILITIES |
279 | |
c80a0905 |
280 | None reported. |
281 | |
c80a0905 |
282 | =head1 BUGS AND LIMITATIONS |
283 | |
c80a0905 |
284 | No bugs have been reported. |
285 | |
286 | Please report any bugs or feature requests to |
287 | C<bug-config-any@rt.cpan.org>, or through the web interface at |
288 | L<http://rt.cpan.org>. |
289 | |
c80a0905 |
290 | =head1 AUTHOR |
291 | |
f0e3c221 |
292 | Joel Bernstein E<lt>rataxis@cpan.orgE<gt> |
c80a0905 |
293 | |
59a80452 |
294 | =head1 CONTRIBUTORS |
295 | |
296 | This module was based on the original |
297 | L<Catalyst::Plugin::ConfigLoader|Catalyst::Plugin::ConfigLoader> |
298 | module by Brian Cassidy C<< <bricas@cpan.org> >>. |
299 | |
300 | With ideas and support from Matt S Trout C<< <mst@shadowcatsystems.co.uk> >>. |
c80a0905 |
301 | |
41f47406 |
302 | Further enhancements suggested by Evan Kaufman C<< <evank@cpan.org> >>. |
303 | |
c80a0905 |
304 | =head1 LICENCE AND COPYRIGHT |
305 | |
306 | Copyright (c) 2006, Portugal Telecom C<< http://www.sapo.pt/ >>. All rights reserved. |
41f47406 |
307 | Portions copyright 2007, Joel Bernstein C<< <rataxis@cpan.org> >>. |
c80a0905 |
308 | |
309 | This module is free software; you can redistribute it and/or |
310 | modify it under the same terms as Perl itself. See L<perlartistic>. |
311 | |
c80a0905 |
312 | =head1 DISCLAIMER OF WARRANTY |
313 | |
314 | BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY |
315 | FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN |
316 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES |
317 | PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER |
318 | EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
319 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE |
320 | ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH |
321 | YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL |
322 | NECESSARY SERVICING, REPAIR, OR CORRECTION. |
323 | |
324 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING |
325 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR |
326 | REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE |
327 | LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL, |
328 | OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE |
329 | THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING |
330 | RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A |
331 | FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF |
332 | SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF |
333 | SUCH DAMAGES. |
59a80452 |
334 | |
335 | =head1 SEE ALSO |
336 | |
337 | L<Catalyst::Plugin::ConfigLoader|Catalyst::Plugin::ConfigLoader> |
338 | -- now a wrapper around this module. |
339 | |
340 | =cut |
341 | |
41f47406 |
342 | "Drink more beer"; |