Commit | Line | Data |
b2d85594 |
1 | package Catalyst::Plugin::ConfigLoader;\r |
2 | \r |
3 | use strict;\r |
4 | use warnings;\r |
5 | \r |
6 | use NEXT;\r |
3b6330a4 |
7 | use Module::Pluggable::Object ();\r |
9742697f |
8 | use Data::Visitor::Callback;\r |
b2d85594 |
9 | \r |
935441da |
10 | our $VERSION = '0.1';\r |
b2d85594 |
11 | \r |
12 | =head1 NAME\r |
13 | \r |
14 | Catalyst::Plugin::ConfigLoader - Load config files of various types\r |
15 | \r |
16 | =head1 SYNOPSIS\r |
17 | \r |
c7413665 |
18 | package MyApp;\r |
19 | \r |
d2afa071 |
20 | # ConfigLoader should be first in your list so\r |
21 | # other plugins can get the config information\r |
22 | use Catalyst qw( ConfigLoader ... );\r |
135fd39b |
23 | \r |
b2d85594 |
24 | # by default myapp.* will be loaded\r |
25 | # you can specify a file if you'd like\r |
87dd9ae0 |
26 | __PACKAGE__->config( file => 'config.yaml' ); \r |
b2d85594 |
27 | \r |
28 | =head1 DESCRIPTION\r |
29 | \r |
04ea923f |
30 | This module will attempt to load find and load a configuration\r |
b2d85594 |
31 | file of various types. Currently it supports YAML, JSON, XML,\r |
32 | INI and Perl formats.\r |
33 | \r |
04ea923f |
34 | To support the distinction between development and production environments,\r |
35 | this module will also attemp to load a local config (e.g. myapp_local.yaml)\r |
36 | which will override any duplicate settings.\r |
37 | \r |
b2d85594 |
38 | =head1 METHODS\r |
39 | \r |
40 | =head2 setup( )\r |
41 | \r |
42 | This method is automatically called by Catalyst's setup routine. It will\r |
d6277728 |
43 | attempt to use each plugin and, once a file has been successfully\r |
04ea923f |
44 | loaded, set the C<config()> section. \r |
b2d85594 |
45 | \r |
46 | =cut\r |
47 | \r |
48 | sub setup {\r |
87dd9ae0 |
49 | my $c = shift;\r |
50 | my( $path, $extension ) = $c->get_config_path;\r |
3b6330a4 |
51 | \r |
52 | my $finder = Module::Pluggable::Object->new(\r |
53 | search_path => [ __PACKAGE__ ],\r |
54 | require => 1\r |
55 | );\r |
56 | \r |
57 | for my $loader ( $finder->plugins ) {\r |
c7413665 |
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 |
04ea923f |
65 | @files = map { ( "$path.$_", "${path}_local.$_" ) } @extensions;\r |
c7413665 |
66 | }\r |
67 | \r |
68 | for( @files ) {\r |
69 | next unless -f $_;\r |
70 | my $config = $loader->load( $_ );\r |
87dd9ae0 |
71 | \r |
172de8aa |
72 | $c->log->debug( qq(Loaded Config "$_") ) if $c->debug;\r |
7b57af98 |
73 | \r |
74 | next if !$config;\r |
75 | \r |
76 | _fix_syntax( $config );\r |
77 | \r |
7b57af98 |
78 | $c->config( $config );\r |
b2d85594 |
79 | }\r |
80 | }\r |
81 | \r |
9742697f |
82 | $c->finalize_config;\r |
83 | \r |
b2d85594 |
84 | $c->NEXT::setup( @_ );\r |
85 | }\r |
86 | \r |
9742697f |
87 | =head2 finalize_config\r |
88 | \r |
89 | This method is called after the config file is loaded. It can be\r |
90 | used to implement tuning of config values that can only be done\r |
91 | at runtime. If you need to do this to properly configure any\r |
92 | plugins, it's important to load ConfigLoader before them.\r |
93 | ConfigLoader provides a default finalize_config method which\r |
94 | walks through the loaded config hash and replaces any strings\r |
04ea923f |
95 | beginning containing C<__HOME__> with the full path to\r |
96 | app's home directory (i.e. C<$c-E<gt>path_to('')> ).\r |
97 | You can also use C<__path_to('foo/bar')__> which translates to\r |
98 | C<$c-E<gt>path_to('foo', 'bar')> \r |
9742697f |
99 | \r |
100 | =cut\r |
101 | \r |
102 | sub finalize_config {\r |
103 | my $c = shift;\r |
104 | my $v = Data::Visitor::Callback->new(\r |
da18b925 |
105 | plain_value => sub {\r |
106 | return unless defined $_;\r |
3b6330a4 |
107 | s{__HOME__}{ $c->path_to( '' ) }e;\r |
108 | s{__path_to\((.+)\)__}{ $c->path_to( split( '/', $1 ) ) }e;\r |
da18b925 |
109 | }\r |
9742697f |
110 | );\r |
dc4daea5 |
111 | $v->visit( $c->config );\r |
9742697f |
112 | }\r |
113 | \r |
87dd9ae0 |
114 | =head2 get_config_path\r |
115 | \r |
116 | This method determines the path, filename prefix and file extension to be used\r |
117 | for config loading. It returns the path (up to the filename less the\r |
118 | extension) to check and the specific extension to use (if it was specified).\r |
119 | \r |
120 | The order of preference is specified as:\r |
121 | \r |
122 | =over 4\r |
123 | \r |
124 | =item * C<$ENV{ MYAPP_CONFIG }>\r |
125 | \r |
126 | =item * C<$c->config->{ file }>\r |
127 | \r |
128 | =item * C<$c->path_to( $application_prefix )>\r |
129 | \r |
130 | =back\r |
131 | \r |
132 | If either of the first two user-specified options are directories, the\r |
133 | application prefix will be added on to the end of the path.\r |
134 | \r |
135 | =cut\r |
136 | \r |
137 | sub get_config_path {\r |
138 | my $c = shift;\r |
139 | my $appname = ref $c || $c;\r |
140 | my $prefix = Catalyst::Utils::appprefix( $appname );\r |
141 | my $path = $ENV{ Catalyst::Utils::class2env( $appname ) . '_CONFIG' }\r |
142 | || $c->config->{ file }\r |
143 | || $c->path_to( $prefix );\r |
144 | \r |
3b6330a4 |
145 | my( $extension ) = ( $path =~ m{\.(.{1,4})$} );\r |
87dd9ae0 |
146 | \r |
147 | if( -d $path ) {\r |
3b6330a4 |
148 | $path =~ s{[\/\\]$}{};\r |
87dd9ae0 |
149 | $path .= "/$prefix";\r |
150 | }\r |
151 | \r |
152 | return( $path, $extension );\r |
153 | }\r |
154 | \r |
1502c009 |
155 | sub _fix_syntax {\r |
156 | my $config = shift;\r |
157 | my @components = (\r |
158 | map +{\r |
159 | prefix => $_ eq 'Component' ? '' : $_ . '::',\r |
160 | values => delete $config->{ lc $_ } || delete $config->{ $_ }\r |
d1b52b01 |
161 | },\r |
162 | grep {\r |
163 | ref $config->{ lc $_ } || ref $config->{ $_ }\r |
164 | }\r |
6e7f9907 |
165 | qw( Component Model M View V Controller C )\r |
1502c009 |
166 | );\r |
167 | \r |
135fd39b |
168 | foreach my $comp ( @components ) {\r |
1502c009 |
169 | my $prefix = $comp->{ prefix };\r |
170 | foreach my $element ( keys %{ $comp->{ values } } ) {\r |
171 | $config->{ "$prefix$element" } = $comp->{ values }->{ $element };\r |
172 | }\r |
173 | }\r |
174 | }\r |
175 | \r |
b2d85594 |
176 | =head1 AUTHOR\r |
177 | \r |
178 | =over 4 \r |
179 | \r |
180 | =item * Brian Cassidy E<lt>bricas@cpan.orgE<gt>\r |
181 | \r |
182 | =back\r |
183 | \r |
a8288af5 |
184 | =head1 CONTRIBUTORS\r |
185 | \r |
186 | The following people have generously donated their time to the\r |
187 | development of this module:\r |
188 | \r |
189 | =over 4\r |
190 | \r |
191 | =item * David Kamholz E<lt>dkamholz@cpan.orgE<gt>\r |
192 | \r |
193 | =back\r |
194 | \r |
b2d85594 |
195 | =head1 COPYRIGHT AND LICENSE\r |
196 | \r |
197 | Copyright 2006 by Brian Cassidy\r |
198 | \r |
199 | This library is free software; you can redistribute it and/or modify\r |
200 | it under the same terms as Perl itself. \r |
201 | \r |
202 | =head1 SEE ALSO\r |
203 | \r |
204 | =over 4 \r |
205 | \r |
206 | =item * L<Catalyst>\r |
207 | \r |
208 | =back\r |
209 | \r |
210 | =cut\r |
211 | \r |
a07967b8 |
212 | 1;\r |