d4092cb51e0c294426482073a876168ded6ecc61
[catagits/Catalyst-Plugin-ConfigLoader.git] / lib / Catalyst / Plugin / ConfigLoader.pm
1 package Catalyst::Plugin::ConfigLoader;\r
2 \r
3 use strict;\r
4 use warnings;\r
5 \r
6 use NEXT;\r
7 use Module::Pluggable::Fast\r
8     name    => '_config_loaders',\r
9     search  => [ __PACKAGE__ ],\r
10     require => 1;\r
11 \r
12 our $VERSION = '0.02';\r
13 \r
14 =head1 NAME\r
15 \r
16 Catalyst::Plugin::ConfigLoader - Load config files of various types\r
17 \r
18 =head1 SYNOPSIS\r
19 \r
20     package MyApp;\r
21     \r
22     use Catalyst( ConfigLoader );\r
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
31 This mdoule will attempt to load find and load a configuration\r
32 file of various types. Currently it supports YAML, JSON, XML,\r
33 INI and Perl formats.\r
34 \r
35 =head1 METHODS\r
36 \r
37 =head2 setup( )\r
38 \r
39 This method is automatically called by Catalyst's setup routine. It will\r
40 attempt to use each plugin and set the C<config()> section once a file has been\r
41 successfully loaded.\r
42 \r
43 =cut\r
44 \r
45 sub setup {\r
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
50     \r
51     for my $loader ( $c->_config_loaders ) {\r
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
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
85 Copyright 2006 by Brian Cassidy\r
86 \r
87 This library is free software; you can redistribute it and/or modify\r
88 it 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
100 1;