Commit | Line | Data |
f0e3c221 |
1 | package Config::Any::General; |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
dcfb1d1d |
6 | use base 'Config::Any::Base'; |
7 | |
f0e3c221 |
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; |
e0c0c283 |
45 | my $args = shift || {}; |
f0e3c221 |
46 | |
47 | # work around bug (?) in Config::General |
92a04e78 |
48 | # return if $class->_test_perl($file); |
f0e3c221 |
49 | |
92a04e78 |
50 | $args->{ -ConfigFile } = $file; |
e0c0c283 |
51 | |
f0e3c221 |
52 | require Config::General; |
e0c0c283 |
53 | my $configfile = Config::General->new( %$args ); |
f0e3c221 |
54 | my $config = { $configfile->getall }; |
92a04e78 |
55 | |
f0e3c221 |
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 { |
92a04e78 |
66 | my ( $class, $file ) = @_; |
f0e3c221 |
67 | my $is_perl_src; |
68 | eval { $is_perl_src = do "$file"; }; |
92a04e78 |
69 | delete $INC{ $file }; # so we don't screw stuff later on |
f0e3c221 |
70 | return defined $is_perl_src; |
71 | } |
72 | |
dcfb1d1d |
73 | =head2 requires_all_of( ) |
72628dc7 |
74 | |
dcfb1d1d |
75 | Specifies that this module requires L<Config::General> in order to work. |
72628dc7 |
76 | |
77 | =cut |
78 | |
dcfb1d1d |
79 | sub requires_all_of { 'Config::General' } |
72628dc7 |
80 | |
f0e3c221 |
81 | =head1 AUTHOR |
82 | |
a918b0b8 |
83 | Brian Cassidy E<lt>bricas@cpan.orgE<gt> |
f0e3c221 |
84 | |
85 | =head1 CONTRIBUTORS |
86 | |
a918b0b8 |
87 | Joel Bernstein C<< <rataxis@cpan.org> >> |
f0e3c221 |
88 | |
89 | =head1 COPYRIGHT AND LICENSE |
90 | |
c793253e |
91 | Copyright 2006-2009 by Brian Cassidy |
f0e3c221 |
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 | |