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 |
aac330eb |
13 | our $VERSION = '0.06';\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 |
aceb0443 |
33 | This module will attempt to load find and load a configuration\r |
b9e0a855 |
34 | file of various types. Currently it supports YAML, JSON, XML,\r |
35 | INI and Perl formats.\r |
36 | \r |
aceb0443 |
37 | To support the distinction between development and production environments,\r |
38 | this module will also attemp to load a local config (e.g. myapp_local.yaml)\r |
39 | which will override any duplicate settings.\r |
40 | \r |
b9e0a855 |
41 | =head1 METHODS\r |
42 | \r |
43 | =head2 setup( )\r |
44 | \r |
45 | This method is automatically called by Catalyst's setup routine. It will\r |
46 | attempt to use each plugin and, once a file has been successfully\r |
aceb0443 |
47 | loaded, set the C<config()> section. \r |
b9e0a855 |
48 | \r |
49 | =cut\r |
50 | \r |
51 | sub setup {\r |
52 | my $c = shift;\r |
53 | my $path = $c->config->{ file } || $c->path_to( Catalyst::Utils::appprefix( ref $c || $c ) );\r |
54 | \r |
55 | my( $extension ) = ( $path =~ /\.(.{1,4})$/ );\r |
56 | \r |
57 | for my $loader ( $c->_config_loaders ) {\r |
58 | my @files;\r |
59 | my @extensions = $loader->extensions;\r |
60 | if( $extension ) {\r |
61 | next unless grep { $_ eq $extension } @extensions;\r |
62 | push @files, $path;\r |
63 | }\r |
64 | else {\r |
aceb0443 |
65 | @files = map { ( "$path.$_", "${path}_local.$_" ) } @extensions;\r |
b9e0a855 |
66 | }\r |
67 | \r |
68 | for( @files ) {\r |
69 | next unless -f $_;\r |
70 | my $config = $loader->load( $_ );\r |
aac330eb |
71 | $c->config( $config ) if $config;\r |
b9e0a855 |
72 | }\r |
73 | }\r |
74 | \r |
2fb22e6e |
75 | $c->finalize_config;\r |
76 | \r |
b9e0a855 |
77 | $c->NEXT::setup( @_ );\r |
78 | }\r |
79 | \r |
2fb22e6e |
80 | =head2 finalize_config\r |
81 | \r |
82 | This method is called after the config file is loaded. It can be\r |
83 | used to implement tuning of config values that can only be done\r |
84 | at runtime. If you need to do this to properly configure any\r |
85 | plugins, it's important to load ConfigLoader before them.\r |
86 | ConfigLoader provides a default finalize_config method which\r |
87 | walks through the loaded config hash and replaces any strings\r |
aceb0443 |
88 | beginning containing C<__HOME__> with the full path to\r |
89 | app's home directory (i.e. C<$c-E<gt>path_to('')> ).\r |
90 | You can also use C<__path_to('foo/bar')__> which translates to\r |
91 | C<$c-E<gt>path_to('foo', 'bar')> \r |
2fb22e6e |
92 | \r |
93 | =cut\r |
94 | \r |
95 | sub finalize_config {\r |
96 | my $c = shift;\r |
97 | my $v = Data::Visitor::Callback->new(\r |
aceb0443 |
98 | plain_value => sub {\r |
99 | return unless defined $_;\r |
100 | s[__HOME__][ $c->path_to( '' ) ]e;\r |
101 | s[__path_to\((.+)\)__][ $c->path_to( split( '/', $1 ) ) ]e;\r |
102 | }\r |
2fb22e6e |
103 | );\r |
104 | $v->visit( $c->config );\r |
105 | }\r |
106 | \r |
b9e0a855 |
107 | =head1 AUTHOR\r |
108 | \r |
109 | =over 4 \r |
110 | \r |
111 | =item * Brian Cassidy E<lt>bricas@cpan.orgE<gt>\r |
112 | \r |
113 | =back\r |
114 | \r |
2fb22e6e |
115 | =head1 CONTRIBUTORS\r |
116 | \r |
117 | The following people have generously donated their time to the\r |
118 | development of this module:\r |
119 | \r |
120 | =over 4\r |
121 | \r |
122 | =item * David Kamholz E<lt>dkamholz@cpan.orgE<gt>\r |
123 | \r |
124 | =back\r |
125 | \r |
b9e0a855 |
126 | =head1 COPYRIGHT AND LICENSE\r |
127 | \r |
128 | Copyright 2006 by Brian Cassidy\r |
129 | \r |
130 | This library is free software; you can redistribute it and/or modify\r |
131 | it under the same terms as Perl itself. \r |
132 | \r |
133 | =head1 SEE ALSO\r |
134 | \r |
135 | =over 4 \r |
136 | \r |
137 | =item * L<Catalyst>\r |
138 | \r |
139 | =back\r |
140 | \r |
141 | =cut\r |
142 | \r |
aceb0443 |
143 | 1;\r |