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