refactored
[catagits/Catalyst-Plugin-ConfigLoader.git] / lib / Catalyst / Plugin / ConfigLoader.pm
CommitLineData
b2d85594 1package Catalyst::Plugin::ConfigLoader;\r
2\r
3use strict;\r
4use warnings;\r
5\r
6use NEXT;\r
7use Module::Pluggable::Fast\r
8 name => '_config_loaders',\r
9 search => [ __PACKAGE__ ],\r
10 require => 1;\r
11\r
c7413665 12our $VERSION = '0.02';\r
b2d85594 13\r
14=head1 NAME\r
15\r
16Catalyst::Plugin::ConfigLoader - Load config files of various types\r
17\r
18=head1 SYNOPSIS\r
19\r
c7413665 20 package MyApp;\r
21 \r
22 use Catalyst( ConfigLoader );\r
b2d85594 23 \r
24 # by default myapp.* will be loaded\r
25 # you can specify a file if you'd like\r
26 __PACKAGE__->config( file = > 'config.yaml' );\r
27 \r
28\r
29=head1 DESCRIPTION\r
30\r
31This mdoule will attempt to load find and load a configuration\r
32file of various types. Currently it supports YAML, JSON, XML,\r
33INI and Perl formats.\r
34\r
35=head1 METHODS\r
36\r
37=head2 setup( )\r
38\r
39This method is automatically called by Catalyst's setup routine. It will\r
40attempt to use each plugin and set the C<config()> section once a file has been\r
41successfully loaded.\r
42\r
43=cut\r
44\r
45sub setup {\r
c7413665 46 my $c = shift;\r
47 my $path = $c->config->{ file } || $c->path_to( Catalyst::Utils::appprefix( ref $c || $c ) );\r
48\r
49 my( $extension ) = ( $path =~ /\.(.{1,4})$/ );\r
b2d85594 50 \r
51 for my $loader ( $c->_config_loaders ) {\r
c7413665 52 my @files;\r
53 my @extensions = $loader->extensions;\r
54 if( $extension ) {\r
55 next unless grep { $_ eq $extension } @extensions;\r
56 push @files, $path;\r
57 }\r
58 else {\r
59 push @files, "$path.$_" for @extensions;\r
60 }\r
61\r
62 for( @files ) {\r
63 next unless -f $_;\r
64 my $config = $loader->load( $_ );\r
65 if( $config ) {\r
66 $c->config( $config );\r
67 last;\r
68 }\r
b2d85594 69 }\r
70 }\r
71\r
72 $c->NEXT::setup( @_ );\r
73}\r
74\r
75=head1 AUTHOR\r
76\r
77=over 4 \r
78\r
79=item * Brian Cassidy E<lt>bricas@cpan.orgE<gt>\r
80\r
81=back\r
82\r
83=head1 COPYRIGHT AND LICENSE\r
84\r
85Copyright 2006 by Brian Cassidy\r
86\r
87This library is free software; you can redistribute it and/or modify\r
88it under the same terms as Perl itself. \r
89\r
90=head1 SEE ALSO\r
91\r
92=over 4 \r
93\r
94=item * L<Catalyst>\r
95\r
96=back\r
97\r
98=cut\r
99\r
1001;