Commit | Line | Data |
f0e3c221 |
1 | package Config::Any::XML; |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
dcfb1d1d |
6 | use base 'Config::Any::Base'; |
7 | |
f0e3c221 |
8 | =head1 NAME |
9 | |
10 | Config::Any::XML - Load XML config files |
11 | |
12 | =head1 DESCRIPTION |
13 | |
14 | Loads XML files. Example: |
15 | |
16 | <config> |
17 | <name>TestApp</name> |
18 | <component name="Controller::Foo"> |
19 | <foo>bar</foo> |
20 | </component> |
21 | <model name="Baz"> |
22 | <qux>xyzzy</qux> |
23 | </model> |
24 | </config> |
25 | |
26 | =head1 METHODS |
27 | |
28 | =head2 extensions( ) |
29 | |
30 | return an array of valid extensions (C<xml>). |
31 | |
32 | =cut |
33 | |
34 | sub extensions { |
35 | return qw( xml ); |
36 | } |
37 | |
38 | =head2 load( $file ) |
39 | |
40 | Attempts to load C<$file> as an XML file. |
41 | |
42 | =cut |
43 | |
44 | sub load { |
45 | my $class = shift; |
46 | my $file = shift; |
7d9d2803 |
47 | my $args = shift || {}; |
f0e3c221 |
48 | |
49 | require XML::Simple; |
92a04e78 |
50 | my $config = XML::Simple::XMLin( |
51 | $file, |
f0e3c221 |
52 | ForceArray => [ qw( component model view controller ) ], |
7d9d2803 |
53 | %$args |
f0e3c221 |
54 | ); |
55 | |
92a04e78 |
56 | return $class->_coerce( $config ); |
f0e3c221 |
57 | } |
58 | |
59 | sub _coerce { |
60 | # coerce the XML-parsed config into the correct format |
92a04e78 |
61 | my $class = shift; |
f0e3c221 |
62 | my $config = shift; |
63 | my $out; |
92a04e78 |
64 | for my $k ( keys %$config ) { |
65 | my $ref = $config->{ $k }; |
bb941906 |
66 | my $name = ref $ref eq 'HASH' ? delete $ref->{ name } : undef; |
92a04e78 |
67 | if ( defined $name ) { |
68 | $out->{ $k }->{ $name } = $ref; |
69 | } |
70 | else { |
71 | $out->{ $k } = $ref; |
f0e3c221 |
72 | } |
73 | } |
74 | $out; |
75 | } |
76 | |
dcfb1d1d |
77 | =head2 requires_all_of( ) |
72628dc7 |
78 | |
e7be073a |
79 | Specifies that this module requires L<XML::Simple> and L<XML::NamespaceSupport> |
80 | in order to work. |
72628dc7 |
81 | |
82 | =cut |
83 | |
e7be073a |
84 | sub requires_all_of { 'XML::Simple', 'XML::NamespaceSupport' } |
72628dc7 |
85 | |
11501cf7 |
86 | =head1 CAVEATS |
87 | |
88 | =head2 Strict Mode |
89 | |
90 | If, by some chance, L<XML::Simple> has already been loaded with the strict |
91 | flag turned on, then you will likely get errors as warnings will become |
92 | fatal exceptions and certain arguments to XMLin() will no longer be optional. |
93 | |
94 | See L<XML::Simple's strict mode documentation|XML::Simple/STRICT_MODE> for |
95 | more information. |
96 | |
a918b0b8 |
97 | =head1 AUTHORS |
f0e3c221 |
98 | |
a918b0b8 |
99 | Brian Cassidy E<lt>bricas@cpan.orgE<gt> |
f0e3c221 |
100 | |
a918b0b8 |
101 | Joel Bernstein E<lt>rataxis@cpan.orgE<gt> |
f0e3c221 |
102 | |
103 | =head1 COPYRIGHT AND LICENSE |
104 | |
f9f3d682 |
105 | Copyright 2006-2015 by Brian Cassidy |
f0e3c221 |
106 | |
107 | This library is free software; you can redistribute it and/or modify |
108 | it under the same terms as Perl itself. |
109 | |
110 | =head1 SEE ALSO |
111 | |
112 | =over 4 |
113 | |
114 | =item * L<Catalyst> |
115 | |
116 | =item * L<Config::Any> |
117 | |
118 | =item * L<XML::Simple> |
119 | |
120 | =back |
121 | |
122 | =cut |
123 | |
124 | 1; |