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 |
2fb22e6e |
11 | use Data::Visitor::Callback;\r |
b9e0a855 |
12 | \r |
2fb22e6e |
13 | our $VERSION = '0.04';\r |
b9e0a855 |
14 | \r |
15 | =head1 NAME\r |
16 | \r |
17 | Catalyst::Plugin::ConfigLoader - Load config files of various types\r |
18 | \r |
19 | =head1 SYNOPSIS\r |
20 | \r |
21 | package MyApp;\r |
22 | \r |
23 | # ConfigLoader should be first in your list so\r |
24 | # other plugins can get the config information\r |
25 | use Catalyst qw( ConfigLoader ... );\r |
26 | \r |
27 | # by default myapp.* will be loaded\r |
28 | # you can specify a file if you'd like\r |
2fb22e6e |
29 | __PACKAGE__->config( file = > 'config.yaml' ); \r |
b9e0a855 |
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 |
2fb22e6e |
74 | $c->finalize_config;\r |
75 | \r |
b9e0a855 |
76 | $c->NEXT::setup( @_ );\r |
77 | }\r |
78 | \r |
2fb22e6e |
79 | =head2 finalize_config\r |
80 | \r |
81 | This method is called after the config file is loaded. It can be\r |
82 | used to implement tuning of config values that can only be done\r |
83 | at runtime. If you need to do this to properly configure any\r |
84 | plugins, it's important to load ConfigLoader before them.\r |
85 | ConfigLoader provides a default finalize_config method which\r |
86 | walks through the loaded config hash and replaces any strings\r |
87 | beginning with C<< __HOME__/<path> >> with the full path to\r |
88 | the file inside the app's home directory.\r |
89 | \r |
90 | =cut\r |
91 | \r |
92 | sub finalize_config {\r |
93 | my $c = shift;\r |
94 | my $v = Data::Visitor::Callback->new(\r |
95 | plain_value => sub { s[^__HOME__/(.+)$][ $c->path_to($1) ]e }\r |
96 | );\r |
97 | $v->visit( $c->config );\r |
98 | }\r |
99 | \r |
b9e0a855 |
100 | =head1 AUTHOR\r |
101 | \r |
102 | =over 4 \r |
103 | \r |
104 | =item * Brian Cassidy E<lt>bricas@cpan.orgE<gt>\r |
105 | \r |
106 | =back\r |
107 | \r |
2fb22e6e |
108 | =head1 CONTRIBUTORS\r |
109 | \r |
110 | The following people have generously donated their time to the\r |
111 | development of this module:\r |
112 | \r |
113 | =over 4\r |
114 | \r |
115 | =item * David Kamholz E<lt>dkamholz@cpan.orgE<gt>\r |
116 | \r |
117 | =back\r |
118 | \r |
b9e0a855 |
119 | =head1 COPYRIGHT AND LICENSE\r |
120 | \r |
121 | Copyright 2006 by Brian Cassidy\r |
122 | \r |
123 | This library is free software; you can redistribute it and/or modify\r |
124 | it under the same terms as Perl itself. \r |
125 | \r |
126 | =head1 SEE ALSO\r |
127 | \r |
128 | =over 4 \r |
129 | \r |
130 | =item * L<Catalyst>\r |
131 | \r |
132 | =back\r |
133 | \r |
134 | =cut\r |
135 | \r |
136 | 1; |