added is_supported() to see what plugins we can use
[p5sagit/Config-Any.git] / lib / Config / Any / General.pm
CommitLineData
f0e3c221 1package Config::Any::General;
2
3use strict;
4use warnings;
5
6=head1 NAME
7
8Config::Any::General - Load Config::General files
9
10=head1 DESCRIPTION
11
12Loads Config::General files. Example:
13
14 name = TestApp
15 <Component Controller::Foo>
16 foo bar
17 </Component>
18 <Model Baz>
19 qux xyzzy
20 </Model>
21
22=head1 METHODS
23
24=head2 extensions( )
25
26return an array of valid extensions (C<cnf>, C<conf>).
27
28=cut
29
30sub extensions {
31 return qw( cnf conf );
32}
33
34=head2 load( $file )
35
36Attempts to load C<$file> via Config::General.
37
38=cut
39
40sub load {
41 my $class = shift;
42 my $file = shift;
e0c0c283 43 my $args = shift || {};
f0e3c221 44
45 # work around bug (?) in Config::General
92a04e78 46 # return if $class->_test_perl($file);
f0e3c221 47
92a04e78 48 $args->{ -ConfigFile } = $file;
e0c0c283 49
f0e3c221 50 require Config::General;
e0c0c283 51 my $configfile = Config::General->new( %$args );
f0e3c221 52 my $config = { $configfile->getall };
92a04e78 53
f0e3c221 54 return $config;
55}
56
57# this is a bit of a hack but necessary, because Config::General is *far* too lax
58# about what it will load -- specifically, it seems to be quite happy to load a Perl
59# config file (ie, a file which is valid Perl and creates a hashref) as if it were
60# an Apache-style configuration file, presumably due to laziness on the part of the
61# developer.
62
63sub _test_perl {
92a04e78 64 my ( $class, $file ) = @_;
f0e3c221 65 my $is_perl_src;
66 eval { $is_perl_src = do "$file"; };
92a04e78 67 delete $INC{ $file }; # so we don't screw stuff later on
f0e3c221 68 return defined $is_perl_src;
69}
70
72628dc7 71=head2 is_supported( )
72
73Returns true if L<Config::General> is available.
74
75=cut
76
77sub is_supported {
78 eval { require Config::General; };
79 return $@ ? 0 : 1;
80}
81
f0e3c221 82=head1 AUTHOR
83
a918b0b8 84Brian Cassidy E<lt>bricas@cpan.orgE<gt>
f0e3c221 85
86=head1 CONTRIBUTORS
87
a918b0b8 88Joel Bernstein C<< <rataxis@cpan.org> >>
f0e3c221 89
90=head1 COPYRIGHT AND LICENSE
91
a918b0b8 92Copyright 2007 by Brian Cassidy
f0e3c221 93
94Portions Copyright 2006 Portugal Telecom
95
96This library is free software; you can redistribute it and/or modify
97it under the same terms as Perl itself.
98
99=head1 SEE ALSO
100
101=over 4
102
103=item * L<Catalyst>
104
105=item * L<Config::Any>
106
107=item * L<Config::General>
108
109=back
110
111=cut
112
1131;
114