tabs => 4 spaces
[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
1502c009 13our $VERSION = '0.07';\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
9742697f 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
c7413665 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
b2d85594 56 \r
57 for my $loader ( $c->_config_loaders ) {\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
1502c009 71 \r
72 _fix_syntax( $config );\r
73 \r
06b5f4a9 74 $c->config( $config ) if $config;\r
b2d85594 75 }\r
76 }\r
77\r
9742697f 78 $c->finalize_config;\r
79\r
b2d85594 80 $c->NEXT::setup( @_ );\r
81}\r
82\r
9742697f 83=head2 finalize_config\r
84\r
85This method is called after the config file is loaded. It can be\r
86used to implement tuning of config values that can only be done\r
87at runtime. If you need to do this to properly configure any\r
88plugins, it's important to load ConfigLoader before them.\r
89ConfigLoader provides a default finalize_config method which\r
90walks through the loaded config hash and replaces any strings\r
04ea923f 91beginning containing C<__HOME__> with the full path to\r
92app's home directory (i.e. C<$c-E<gt>path_to('')> ).\r
93You can also use C<__path_to('foo/bar')__> which translates to\r
94C<$c-E<gt>path_to('foo', 'bar')> \r
9742697f 95\r
96=cut\r
97\r
98sub finalize_config {\r
99 my $c = shift;\r
100 my $v = Data::Visitor::Callback->new(\r
da18b925 101 plain_value => sub {\r
102 return unless defined $_;\r
103 s[__HOME__][ $c->path_to( '' ) ]e;\r
104 s[__path_to\((.+)\)__][ $c->path_to( split( '/', $1 ) ) ]e;\r
105 }\r
9742697f 106 );\r
dc4daea5 107 $v->visit( $c->config );\r
9742697f 108}\r
109\r
1502c009 110sub _fix_syntax {\r
111 my $config = shift;\r
112 my @components = (\r
113 map +{\r
114 prefix => $_ eq 'Component' ? '' : $_ . '::',\r
115 values => delete $config->{ lc $_ } || delete $config->{ $_ }\r
116 }, qw( Component Model View Controller )\r
117 );\r
118\r
135fd39b 119 foreach my $comp ( @components ) {\r
1502c009 120 my $prefix = $comp->{ prefix };\r
121 foreach my $element ( keys %{ $comp->{ values } } ) {\r
122 $config->{ "$prefix$element" } = $comp->{ values }->{ $element };\r
123 }\r
124 }\r
125}\r
126\r
b2d85594 127=head1 AUTHOR\r
128\r
129=over 4 \r
130\r
131=item * Brian Cassidy E<lt>bricas@cpan.orgE<gt>\r
132\r
133=back\r
134\r
a8288af5 135=head1 CONTRIBUTORS\r
136\r
137The following people have generously donated their time to the\r
138development of this module:\r
139\r
140=over 4\r
141\r
142=item * David Kamholz E<lt>dkamholz@cpan.orgE<gt>\r
143\r
144=back\r
145\r
b2d85594 146=head1 COPYRIGHT AND LICENSE\r
147\r
148Copyright 2006 by Brian Cassidy\r
149\r
150This library is free software; you can redistribute it and/or modify\r
151it under the same terms as Perl itself. \r
152\r
153=head1 SEE ALSO\r
154\r
155=over 4 \r
156\r
157=item * L<Catalyst>\r
158\r
159=back\r
160\r
161=cut\r
162\r
a07967b8 1631;\r