8fe2df57e3c551195349b93f3fbf0f6c4f01d74c
[catagits/Catalyst-Plugin-ConfigLoader.git] / lib / Catalyst / Plugin / ConfigLoader.pm
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 use Data::Visitor::Callback;\r
12 \r
13 our $VERSION = '0.08';\r
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
29     __PACKAGE__->config( file => 'config.yaml' );    \r
30 \r
31 =head1 DESCRIPTION\r
32 \r
33 This module 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 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
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
47 loaded, set the C<config()> section. \r
48 \r
49 =cut\r
50 \r
51 sub setup {\r
52     my $c = shift;\r
53     my( $path, $extension ) = $c->get_config_path;\r
54     \r
55     for my $loader ( $c->_config_loaders ) {\r
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
63             @files = map { ( "$path.$_", "${path}_local.$_" ) } @extensions;\r
64         }\r
65 \r
66         for( @files ) {\r
67             next unless -f $_;\r
68             my $config = $loader->load( $_ );\r
69 \r
70             $c->debug( "Loaded Config $_" ) if $c->debug;\r
71            _fix_syntax( $config );\r
72             $c->config( $config ) if $config;\r
73         }\r
74     }\r
75 \r
76     $c->finalize_config;\r
77 \r
78     $c->NEXT::setup( @_ );\r
79 }\r
80 \r
81 =head2 finalize_config\r
82 \r
83 This method is called after the config file is loaded. It can be\r
84 used to implement tuning of config values that can only be done\r
85 at runtime. If you need to do this to properly configure any\r
86 plugins, it's important to load ConfigLoader before them.\r
87 ConfigLoader provides a default finalize_config method which\r
88 walks through the loaded config hash and replaces any strings\r
89 beginning containing C<__HOME__> with the full path to\r
90 app's home directory (i.e. C<$c-E<gt>path_to('')> ).\r
91 You can also use C<__path_to('foo/bar')__> which translates to\r
92 C<$c-E<gt>path_to('foo', 'bar')> \r
93 \r
94 =cut\r
95 \r
96 sub finalize_config {\r
97     my $c = shift;\r
98     my $v = Data::Visitor::Callback->new(\r
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
104     );\r
105     $v->visit( $c->config );\r
106 }\r
107 \r
108 =head2 get_config_path\r
109 \r
110 This method determines the path, filename prefix and file extension to be used\r
111 for config loading. It returns the path (up to the filename less the\r
112 extension) to check and the specific extension to use (if it was specified).\r
113 \r
114 The 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
126 If either of the first two user-specified options are directories, the\r
127 application prefix will be added on to the end of the path.\r
128 \r
129 =cut\r
130 \r
131 sub 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
149 sub _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
158     foreach my $comp ( @components ) {\r
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
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
174 =head1 CONTRIBUTORS\r
175 \r
176 The following people have generously donated their time to the\r
177 development 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
185 =head1 COPYRIGHT AND LICENSE\r
186 \r
187 Copyright 2006 by Brian Cassidy\r
188 \r
189 This library is free software; you can redistribute it and/or modify\r
190 it 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
202 1;\r