1 package Catalyst::Plugin::ConfigLoader;
\r
7 use Module::Pluggable::Fast
\r
8 name => '_config_loaders',
\r
9 search => [ __PACKAGE__ ],
\r
11 use Data::Visitor::Callback;
\r
13 our $VERSION = '0.07';
\r
17 Catalyst::Plugin::ConfigLoader - Load config files of various types
\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
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
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
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
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
53 my $path = $c->config->{ file } || $c->path_to( Catalyst::Utils::appprefix( ref $c || $c ) );
\r
55 my( $extension ) = ( $path =~ /\.(.{1,4})$/ );
\r
57 for my $loader ( $c->_config_loaders ) {
\r
59 my @extensions = $loader->extensions;
\r
61 next unless grep { $_ eq $extension } @extensions;
\r
65 @files = map { ( "$path.$_", "${path}_local.$_" ) } @extensions;
\r
70 my $config = $loader->load( $_ );
\r
72 _fix_syntax( $config );
\r
74 $c->config( $config ) if $config;
\r
78 $c->finalize_config;
\r
80 $c->NEXT::setup( @_ );
\r
83 =head2 finalize_config
\r
85 This method is called after the config file is loaded. It can be
\r
86 used to implement tuning of config values that can only be done
\r
87 at runtime. If you need to do this to properly configure any
\r
88 plugins, it's important to load ConfigLoader before them.
\r
89 ConfigLoader provides a default finalize_config method which
\r
90 walks through the loaded config hash and replaces any strings
\r
91 beginning containing C<__HOME__> with the full path to
\r
92 app's home directory (i.e. C<$c-E<gt>path_to('')> ).
\r
93 You can also use C<__path_to('foo/bar')__> which translates to
\r
94 C<$c-E<gt>path_to('foo', 'bar')>
\r
98 sub finalize_config {
\r
100 my $v = Data::Visitor::Callback->new(
\r
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
107 $v->visit( $c->config );
\r
111 my $config = shift;
\r
114 prefix => $_ eq 'Component' ? '' : $_ . '::',
\r
115 values => delete $config->{ lc $_ } || delete $config->{ $_ }
\r
116 }, qw( Component Model View Controller )
\r
119 foreach my $comp ( @components ) {
\r
120 my $prefix = $comp->{ prefix };
\r
121 foreach my $element ( keys %{ $comp->{ values } } ) {
\r
122 $config->{ "$prefix$element" } = $comp->{ values }->{ $element };
\r
131 =item * Brian Cassidy E<lt>bricas@cpan.orgE<gt>
\r
135 =head1 CONTRIBUTORS
\r
137 The following people have generously donated their time to the
\r
138 development of this module:
\r
142 =item * David Kamholz E<lt>dkamholz@cpan.orgE<gt>
\r
146 =head1 COPYRIGHT AND LICENSE
\r
148 Copyright 2006 by Brian Cassidy
\r
150 This library is free software; you can redistribute it and/or modify
\r
151 it under the same terms as Perl itself.
\r
157 =item * L<Catalyst>
\r