Commit | Line | Data |
b9e0a855 |
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.03';\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 | # ConfigLoader should be first in your list so\r |
23 | # other plugins can get the config information\r |
24 | use Catalyst qw( ConfigLoader ... );\r |
25 | \r |
26 | # by default myapp.* will be loaded\r |
27 | # you can specify a file if you'd like\r |
28 | __PACKAGE__->config( file = > 'config.yaml' );\r |
29 | \r |
30 | \r |
31 | =head1 DESCRIPTION\r |
32 | \r |
33 | This mdoule will attempt to load find and load a configuration\r |
34 | file of various types. Currently it supports YAML, JSON, XML,\r |
35 | INI and Perl formats.\r |
36 | \r |
37 | =head1 METHODS\r |
38 | \r |
39 | =head2 setup( )\r |
40 | \r |
41 | This method is automatically called by Catalyst's setup routine. It will\r |
42 | attempt to use each plugin and, once a file has been successfully\r |
43 | loaded, set the C<config()> section.\r |
44 | \r |
45 | =cut\r |
46 | \r |
47 | sub setup {\r |
48 | my $c = shift;\r |
49 | my $path = $c->config->{ file } || $c->path_to( Catalyst::Utils::appprefix( ref $c || $c ) );\r |
50 | \r |
51 | my( $extension ) = ( $path =~ /\.(.{1,4})$/ );\r |
52 | \r |
53 | for my $loader ( $c->_config_loaders ) {\r |
54 | my @files;\r |
55 | my @extensions = $loader->extensions;\r |
56 | if( $extension ) {\r |
57 | next unless grep { $_ eq $extension } @extensions;\r |
58 | push @files, $path;\r |
59 | }\r |
60 | else {\r |
61 | push @files, "$path.$_" for @extensions;\r |
62 | }\r |
63 | \r |
64 | for( @files ) {\r |
65 | next unless -f $_;\r |
66 | my $config = $loader->load( $_ );\r |
67 | if( $config ) {\r |
68 | $c->config( $config );\r |
69 | last;\r |
70 | }\r |
71 | }\r |
72 | }\r |
73 | \r |
74 | $c->NEXT::setup( @_ );\r |
75 | }\r |
76 | \r |
77 | =head1 AUTHOR\r |
78 | \r |
79 | =over 4 \r |
80 | \r |
81 | =item * Brian Cassidy E<lt>bricas@cpan.orgE<gt>\r |
82 | \r |
83 | =back\r |
84 | \r |
85 | =head1 COPYRIGHT AND LICENSE\r |
86 | \r |
87 | Copyright 2006 by Brian Cassidy\r |
88 | \r |
89 | This library is free software; you can redistribute it and/or modify\r |
90 | it under the same terms as Perl itself. \r |
91 | \r |
92 | =head1 SEE ALSO\r |
93 | \r |
94 | =over 4 \r |
95 | \r |
96 | =item * L<Catalyst>\r |
97 | \r |
98 | =back\r |
99 | \r |
100 | =cut\r |
101 | \r |
102 | 1; |