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