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