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