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