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.08';
\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, $extension ) = $c->get_config_path;
\r
55 for my $loader ( $c->_config_loaders ) {
\r
57 my @extensions = $loader->extensions;
\r
59 next unless grep { $_ eq $extension } @extensions;
\r
63 @files = map { ( "$path.$_", "${path}_local.$_" ) } @extensions;
\r
68 my $config = $loader->load( $_ );
\r
70 $c->log->debug( "Loaded Config $_" ) if $c->debug;
\r
74 _fix_syntax( $config );
\r
76 # merge hashes 1 level down
\r
77 for my $key ( keys %$config ) {
\r
78 if( exists $c->config->{ $key } ) {
\r
79 my $isa_ref = ref $config->{ $key };
\r
81 next if !$isa_ref or $isa_ref ne 'HASH';
\r
83 my %temp = ( %{ $c->config->{ $key } }, %{ $config->{ $key } } );
\r
84 $config->{ $key } = \%temp;
\r
88 $c->config( $config );
\r
92 $c->finalize_config;
\r
94 $c->NEXT::setup( @_ );
\r
97 =head2 finalize_config
\r
99 This method is called after the config file is loaded. It can be
\r
100 used to implement tuning of config values that can only be done
\r
101 at runtime. If you need to do this to properly configure any
\r
102 plugins, it's important to load ConfigLoader before them.
\r
103 ConfigLoader provides a default finalize_config method which
\r
104 walks through the loaded config hash and replaces any strings
\r
105 beginning containing C<__HOME__> with the full path to
\r
106 app's home directory (i.e. C<$c-E<gt>path_to('')> ).
\r
107 You can also use C<__path_to('foo/bar')__> which translates to
\r
108 C<$c-E<gt>path_to('foo', 'bar')>
\r
112 sub finalize_config {
\r
114 my $v = Data::Visitor::Callback->new(
\r
115 plain_value => sub {
\r
116 return unless defined $_;
\r
117 s[__HOME__][ $c->path_to( '' ) ]e;
\r
118 s[__path_to\((.+)\)__][ $c->path_to( split( '/', $1 ) ) ]e;
\r
121 $v->visit( $c->config );
\r
124 =head2 get_config_path
\r
126 This method determines the path, filename prefix and file extension to be used
\r
127 for config loading. It returns the path (up to the filename less the
\r
128 extension) to check and the specific extension to use (if it was specified).
\r
130 The order of preference is specified as:
\r
134 =item * C<$ENV{ MYAPP_CONFIG }>
\r
136 =item * C<$c->config->{ file }>
\r
138 =item * C<$c->path_to( $application_prefix )>
\r
142 If either of the first two user-specified options are directories, the
\r
143 application prefix will be added on to the end of the path.
\r
147 sub get_config_path {
\r
149 my $appname = ref $c || $c;
\r
150 my $prefix = Catalyst::Utils::appprefix( $appname );
\r
151 my $path = $ENV{ Catalyst::Utils::class2env( $appname ) . '_CONFIG' }
\r
152 || $c->config->{ file }
\r
153 || $c->path_to( $prefix );
\r
155 my( $extension ) = ( $path =~ /\.(.{1,4})$/ );
\r
158 $path =~ s/[\/\\]$//;
\r
159 $path .= "/$prefix";
\r
162 return( $path, $extension );
\r
166 my $config = shift;
\r
169 prefix => $_ eq 'Component' ? '' : $_ . '::',
\r
170 values => delete $config->{ lc $_ } || delete $config->{ $_ }
\r
171 }, qw( Component Model View Controller )
\r
174 foreach my $comp ( @components ) {
\r
175 my $prefix = $comp->{ prefix };
\r
176 foreach my $element ( keys %{ $comp->{ values } } ) {
\r
177 $config->{ "$prefix$element" } = $comp->{ values }->{ $element };
\r
186 =item * Brian Cassidy E<lt>bricas@cpan.orgE<gt>
\r
190 =head1 CONTRIBUTORS
\r
192 The following people have generously donated their time to the
\r
193 development of this module:
\r
197 =item * David Kamholz E<lt>dkamholz@cpan.orgE<gt>
\r
201 =head1 COPYRIGHT AND LICENSE
\r
203 Copyright 2006 by Brian Cassidy
\r
205 This library is free software; you can redistribute it and/or modify
\r
206 it under the same terms as Perl itself.
\r
212 =item * L<Catalyst>
\r