From: Joel Bernstein Date: Wed, 9 Aug 2006 16:57:11 +0000 (+0000) Subject: Initial import of Config::Any (refactored from Catalyst::Plugin::ConfigLoader), and... X-Git-Tag: v0.13~4 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=2b402f346273ef43d95e12a1807dceab4ec67064;hp=b773e0786fdcf35af131fc795b2df4fcc85e4e8b;p=catagits%2FCatalyst-Plugin-ConfigLoader.git Initial import of Config::Any (refactored from Catalyst::Plugin::ConfigLoader), and rewrite of C::P::CL to use C::A --- diff --git a/Build.PL b/Build.PL index 23ead95..69c319a 100644 --- a/Build.PL +++ b/Build.PL @@ -4,15 +4,15 @@ use Module::Build; my $build = Module::Build->new( module_name => 'Catalyst::Plugin::ConfigLoader', - dist_author => 'Brian Cassidy ', + dist_author => 'Joel Bernstein ', license => 'perl', create_readme => 1, create_makefile_pl => 'traditional', requires => { 'Catalyst::Runtime' => '0', 'Data::Visitor' => '0.02', - 'Module::Pluggable' => '3.01' + 'Config::Any' => '0.04' }, ); -$build->create_build_script; \ No newline at end of file +$build->create_build_script; diff --git a/README b/README new file mode 100644 index 0000000..56b2b6a --- /dev/null +++ b/README @@ -0,0 +1,5 @@ +Catalyst::Plugin::ConfigLoader - Load class configuration from configuration files + +This module was originally written by Brian Cassidy - . +Portions have been refactored into Config::Any and this module has been rewritten to use +that module, by Joel Bernstein - under the sponsorship of Portugal Telecom. diff --git a/lib/Catalyst/Plugin/ConfigLoader.pm b/lib/Catalyst/Plugin/ConfigLoader.pm index ab332da..5442a71 100644 --- a/lib/Catalyst/Plugin/ConfigLoader.pm +++ b/lib/Catalyst/Plugin/ConfigLoader.pm @@ -1,242 +1,205 @@ -package Catalyst::Plugin::ConfigLoader; - -use strict; -use warnings; - -use NEXT; -use Module::Pluggable::Object (); -use Data::Visitor::Callback; - -our $VERSION = '0.12'; - -=head1 NAME - -Catalyst::Plugin::ConfigLoader - Load config files of various types - -=head1 SYNOPSIS - - package MyApp; - - # ConfigLoader should be first in your list so - # other plugins can get the config information - use Catalyst qw( ConfigLoader ... ); - - # by default myapp.* will be loaded - # you can specify a file if you'd like - __PACKAGE__->config( file => 'config.yaml' ); - -=head1 DESCRIPTION - -This module will attempt to load find and load a configuration -file of various types. Currently it supports YAML, JSON, XML, -INI and Perl formats. - -To support the distinction between development and production environments, -this module will also attemp to load a local config (e.g. myapp_local.yaml) -which will override any duplicate settings. - -=head1 METHODS - -=head2 setup( ) - -This method is automatically called by Catalyst's setup routine. It will -attempt to use each plugin and, once a file has been successfully -loaded, set the C section. - -=cut - -sub setup { - my $c = shift; - my( $path, $extension ) = $c->get_config_path; - my $suffix = $c->get_config_local_suffix; - - my $finder = Module::Pluggable::Object->new( - search_path => [ __PACKAGE__ ], - require => 1 - ); - - for my $loader ( $finder->plugins ) { - my @files; - my @extensions = $loader->extensions; - if( $extension ) { - next unless grep { $_ eq $extension } @extensions; - push @files, $path; - } - else { - @files = map { ( "$path.$_", "${path}_${suffix}.$_" ) } @extensions; - } - - for( @files ) { - next unless -f $_; - my $config = $loader->load( $_ ); - - $c->log->debug( qq(Loaded Config "$_") ) if $c->debug; - - next if !$config; - - _fix_syntax( $config ); - - $c->config( $config ); - } - } - - $c->finalize_config; - - $c->NEXT::setup( @_ ); -} - -=head2 finalize_config - -This method is called after the config file is loaded. It can be -used to implement tuning of config values that can only be done -at runtime. If you need to do this to properly configure any -plugins, it's important to load ConfigLoader before them. -ConfigLoader provides a default finalize_config method which -walks through the loaded config hash and replaces any strings -beginning containing C<__HOME__> with the full path to -app's home directory (i.e. C<$c-Epath_to('')> ). -You can also use C<__path_to(foo/bar)__> which translates to -C<$c-Epath_to('foo', 'bar')> - -=cut - -sub finalize_config { - my $c = shift; - my $v = Data::Visitor::Callback->new( - plain_value => sub { - return unless defined $_; - s{__HOME__}{ $c->path_to( '' ) }e; - s{__path_to\((.+)\)__}{ $c->path_to( split( '/', $1 ) ) }e; - } - ); - $v->visit( $c->config ); -} - -=head2 get_config_path - -This method determines the path, filename prefix and file extension to be used -for config loading. It returns the path (up to the filename less the -extension) to check and the specific extension to use (if it was specified). - -The order of preference is specified as: - -=over 4 - -=item * C<$ENV{ MYAPP_CONFIG }> - -=item * C<$c-Econfig-E{ file }> - -=item * C<$c-Epath_to( $application_prefix )> - -=back - -If either of the first two user-specified options are directories, the -application prefix will be added on to the end of the path. - -=cut - -sub get_config_path { - my $c = shift; - my $appname = ref $c || $c; - my $prefix = Catalyst::Utils::appprefix( $appname ); - my $path = $ENV{ Catalyst::Utils::class2env( $appname ) . '_CONFIG' } - || $c->config->{ file } - || $c->path_to( $prefix ); - - my( $extension ) = ( $path =~ m{\.(.{1,4})$} ); - - if( -d $path ) { - $path =~ s{[\/\\]$}{}; - $path .= "/$prefix"; - } - - return( $path, $extension ); -} - -=head2 get_config_local_suffix - -Determines the suffix of files used to override the main config. By default -this value is C, but it can be specified in the following order of preference: - -=over 4 - -=item * C<$ENV{ CATALYST_CONFIG_LOCAL_SUFFIX }> - -=item * C<$ENV{ MYAPP_CONFIG_LOCAL_SUFFIX }> - -=item * C<$c-Econfig-E{ config_local_suffix }> - - -=back - -=cut - -sub get_config_local_suffix { - my $c = shift; - my $appname = ref $c || $c; - my $suffix = $ENV{ CATALYST_CONFIG_LOCAL_SUFFIX } - || $ENV{ Catalyst::Utils::class2env( $appname ) . '_CONFIG_LOCAL_SUFFIX' } - || $c->config->{ config_local_suffix } - || 'local'; - - return $suffix; -} - -sub _fix_syntax { - my $config = shift; - my @components = ( - map +{ - prefix => $_ eq 'Component' ? '' : $_ . '::', - values => delete $config->{ lc $_ } || delete $config->{ $_ } - }, - grep { - ref $config->{ lc $_ } || ref $config->{ $_ } - } - qw( Component Model M View V Controller C ) - ); - - foreach my $comp ( @components ) { - my $prefix = $comp->{ prefix }; - foreach my $element ( keys %{ $comp->{ values } } ) { - $config->{ "$prefix$element" } = $comp->{ values }->{ $element }; - } - } -} - -=head1 AUTHOR - -=over 4 - -=item * Brian Cassidy Ebricas@cpan.orgE - -=back - -=head1 CONTRIBUTORS - -The following people have generously donated their time to the -development of this module: - -=over 4 - -=item * David Kamholz Edkamholz@cpan.orgE - -=back - -=head1 COPYRIGHT AND LICENSE - -Copyright 2006 by Brian Cassidy - -This library is free software; you can redistribute it and/or modify -it under the same terms as Perl itself. - -=head1 SEE ALSO - -=over 4 - -=item * L - -=back - -=cut - -1; +package Catalyst::Plugin::ConfigLoader; + +use strict; +use warnings; +use Config::Any; +use NEXT; +use Data::Visitor::Callback; +our $VERSION = '0.13'; + +=head1 NAME + +Catalyst::Plugin::ConfigLoader - Load config files of various types + +=head1 SYNOPSIS + + package MyApp; + + # ConfigLoader should be first in your list so + # other plugins can get the config information + use Catalyst qw( ConfigLoader ... ); + + # by default myapp.* will be loaded + # you can specify a file if you'd like + __PACKAGE__->config( file => 'config.yaml' ); + +=head1 DESCRIPTION + +This module will attempt to load find and load a configuration +file of various types. Currently it supports YAML, JSON, XML, +INI and Perl formats. + +To support the distinction between development and production environments, +this module will also attemp to load a local config (e.g. myapp_local.yaml) +which will override any duplicate settings. + +=head1 METHODS + +=head2 setup( ) + +This method is automatically called by Catalyst's setup routine. It will +attempt to use each plugin and, once a file has been successfully +loaded, set the C section. + +=cut + +sub setup { + my $c = shift; + + my @files = $c->find_files; + my $cfg = Config::Any->load_stems({stems => \@files, filter => \&_fix_syntax}); + + for my $ref (@$cfg) { + my ($file, $config) = each %$ref; + $c->config($config); + $c->log->debug( qq(Loaded Config "$file") ) + if $c->debug; + } + + $c->finalize_config; + $c->NEXT::setup( @_ ); +} + +=head2 find_files + +This method determines the potential file paths to be used for config loading. +It returns an array of paths (up to the filename less the extension) to pass to +L for loading. + +=cut + +sub find_files { + my $c = shift; + my ($path, $extension) = $c->get_config_path; + my $suffix = $c->get_config_local_suffix; + my @extensions = @{ Config::Any->extensions }; + + my @files; + if ($extension) { + next unless grep { $_ eq $extension } @extensions; + push @files, $path; + } else { + @files = map { ( "$path.$_", "${path}_${suffix}.$_" ) } @extensions; + } + @files; +} + +=head2 get_config_path + +This method determines the path, filename prefix and file extension to be used +for config loading. It returns the path (up to the filename less the +extension) to check and the specific extension to use (if it was specified). + +The order of preference is specified as: + +=over 4 + +=item * C<$ENV{ MYAPP_CONFIG }> + +=item * C<$c-Econfig-E{ file }> + +=item * C<$c-Epath_to( $application_prefix )> + +=back + +If either of the first two user-specified options are directories, the +application prefix will be added on to the end of the path. + +=cut + +sub get_config_path { + my $c = shift; + my $appname = ref $c || $c; + my $prefix = Catalyst::Utils::appprefix( $appname ); + my $path = $ENV{ Catalyst::Utils::class2env( $appname ) . '_CONFIG' } + || $c->config->{ file } + || $c->path_to( $prefix ); + + my( $extension ) = ( $path =~ m{\.(.{1,4})$} ); + + if( -d $path ) { + $path =~ s{[\/\\]$}{}; + $path .= "/$prefix"; + } + + return( $path, $extension ); +} + +=head2 get_config_local_suffix + +Determines the suffix of files used to override the main config. By default +this value is C, but it can be specified in the following order of preference: + +=over 4 + +=item * C<$ENV{ CATALYST_CONFIG_LOCAL_SUFFIX }> + +=item * C<$ENV{ MYAPP_CONFIG_LOCAL_SUFFIX }> + +=item * C<$c-Econfig-E{ config_local_suffix }> + + +=back + +=cut + +sub get_config_local_suffix { + my $c = shift; + my $appname = ref $c || $c; + my $suffix = $ENV{ CATALYST_CONFIG_LOCAL_SUFFIX } + || $ENV{ Catalyst::Utils::class2env( $appname ) . '_CONFIG_LOCAL_SUFFIX' } + || $c->config->{ config_local_suffix } + || 'local'; + + return $suffix; +} + +sub _fix_syntax { + my $config = shift; + my @components = ( + map +{ + prefix => $_ eq 'Component' ? '' : $_ . '::', + values => delete $config->{ lc $_ } || delete $config->{ $_ } + }, + grep { + ref $config->{ lc $_ } || ref $config->{ $_ } + } + qw( Component Model M View V Controller C ) + ); + + foreach my $comp ( @components ) { + my $prefix = $comp->{ prefix }; + foreach my $element ( keys %{ $comp->{ values } } ) { + $config->{ "$prefix$element" } = $comp->{ values }->{ $element }; + } + } +} + +=head2 finalize_config + +This method is called after the config file is loaded. It can be +used to implement tuning of config values that can only be done +at runtime. If you need to do this to properly configure any +plugins, it's important to load ConfigLoader before them. +ConfigLoader provides a default finalize_config method which +walks through the loaded config hash and replaces any strings +beginning containing C<__HOME__> with the full path to +app's home directory (i.e. C<$c-Epath_to('')> ). +You can also use C<__path_to(foo/bar)__> which translates to +C<$c-Epath_to('foo', 'bar')> + +=cut + +sub finalize_config { + my $c = shift; + my $v = Data::Visitor::Callback->new( + plain_value => sub { + return unless defined $_; + s{__HOME__}{ $c->path_to( '' ) }e; + s{__path_to\((.+)\)__}{ $c->path_to( split( '/', $1 ) ) }e; + } + ); + $v->visit( $c->config ); +} + +1; diff --git a/lib/Catalyst/Plugin/ConfigLoader/General.pm b/lib/Catalyst/Plugin/ConfigLoader/General.pm deleted file mode 100644 index f6e8d16..0000000 --- a/lib/Catalyst/Plugin/ConfigLoader/General.pm +++ /dev/null @@ -1,80 +0,0 @@ -package Catalyst::Plugin::ConfigLoader::General; - -use strict; -use warnings; - -=head1 NAME - -Catalyst::Plugin::ConfigLoader::General - Load Config::General files - -=head1 DESCRIPTION - -Loads Config::General files. Example: - - name = TestApp - - foo bar - - - qux xyzzy - - -=head1 METHODS - -=head2 extensions( ) - -return an array of valid extensions (C, C). - -=cut - -sub extensions { - return qw( cnf conf ); -} - -=head2 load( $file ) - -Attempts to load C<$file> via Config::General. - -=cut - -sub load { - my $class = shift; - my $file = shift; - - require Config::General; - my $configfile = Config::General->new( $file ); - my $config = { $configfile->getall }; - - return $config; -} - -=head1 AUTHOR - -=over 4 - -=item * Brian Cassidy Ebricas@cpan.orgE - -=back - -=head1 COPYRIGHT AND LICENSE - -Copyright 2006 by Brian Cassidy - -This library is free software; you can redistribute it and/or modify -it under the same terms as Perl itself. - -=head1 SEE ALSO - -=over 4 - -=item * L - -=item * L - -=item * L - -=back - -=cut - -1; \ No newline at end of file diff --git a/lib/Catalyst/Plugin/ConfigLoader/INI.pm b/lib/Catalyst/Plugin/ConfigLoader/INI.pm deleted file mode 100644 index 9c24cab..0000000 --- a/lib/Catalyst/Plugin/ConfigLoader/INI.pm +++ /dev/null @@ -1,82 +0,0 @@ -package Catalyst::Plugin::ConfigLoader::INI; - -use strict; -use warnings; - -=head1 NAME - -Catalyst::Plugin::ConfigLoader::INI - Load INI config files - -=head1 DESCRIPTION - -Loads INI files. Example: - - name=TestApp - - [Controller::Foo] - foo=bar - - [Model::Baz] - qux=xyzzy - -=head1 METHODS - -=head2 extensions( ) - -return an array of valid extensions (C). - -=cut - -sub extensions { - return qw( ini ); -} - -=head2 load( $file ) - -Attempts to load C<$file> as an INI file. - -=cut - -sub load { - my $class = shift; - my $file = shift; - - require Config::Tiny; - my $config = Config::Tiny->read( $file ); - my $main = delete $config->{ _ }; - - $config->{ $_ } = $main->{ $_ } for keys %$main; - - return $config; -} - -=head1 AUTHOR - -=over 4 - -=item * Brian Cassidy Ebricas@cpan.orgE - -=back - -=head1 COPYRIGHT AND LICENSE - -Copyright 2006 by Brian Cassidy - -This library is free software; you can redistribute it and/or modify -it under the same terms as Perl itself. - -=head1 SEE ALSO - -=over 4 - -=item * L - -=item * L - -=item * L - -=back - -=cut - -1; \ No newline at end of file diff --git a/lib/Catalyst/Plugin/ConfigLoader/JSON.pm b/lib/Catalyst/Plugin/ConfigLoader/JSON.pm deleted file mode 100644 index 1c100ce..0000000 --- a/lib/Catalyst/Plugin/ConfigLoader/JSON.pm +++ /dev/null @@ -1,92 +0,0 @@ -package Catalyst::Plugin::ConfigLoader::JSON; - -use strict; -use warnings; - -=head1 NAME - -Catalyst::Plugin::ConfigLoader::JSON - Load JSON config files - -=head1 DESCRIPTION - -Loads JSON files. Example: - - { - "name": "TestApp", - "Controller::Foo": { - "foo": "bar" - }, - "Model::Baz": { - "qux": "xyzzy" - } - } - -=head1 METHODS - -=head2 extensions( ) - -return an array of valid extensions (C, C). - -=cut - -sub extensions { - return qw( json jsn ); -} - -=head2 load( $file ) - -Attempts to load C<$file> as a JSON file. - -=cut - -sub load { - my $class = shift; - my $file = shift; - - open( my $fh, $file ) or die $!; - my $content = do { local $/; <$fh> }; - close $fh; - - eval { require JSON::Syck; }; - if( $@ ) { - require JSON; - JSON->import; - return jsonToObj( $content ); - } - else { - return JSON::Syck::Load( $content ); - } -} - -=head1 AUTHOR - -=over 4 - -=item * Brian Cassidy Ebricas@cpan.orgE - -=back - -=head1 COPYRIGHT AND LICENSE - -Copyright 2006 by Brian Cassidy - -This library is free software; you can redistribute it and/or modify -it under the same terms as Perl itself. - -=head1 SEE ALSO - -=over 4 - -=item * L - -=item * L - -=item * L - -=item * L - -=back - -=cut - -1; \ No newline at end of file diff --git a/lib/Catalyst/Plugin/ConfigLoader/Perl.pm b/lib/Catalyst/Plugin/ConfigLoader/Perl.pm deleted file mode 100644 index e94edba..0000000 --- a/lib/Catalyst/Plugin/ConfigLoader/Perl.pm +++ /dev/null @@ -1,76 +0,0 @@ -package Catalyst::Plugin::ConfigLoader::Perl; - -use strict; -use warnings; - -=head1 NAME - -Catalyst::Plugin::ConfigLoader::Perl - Load Perl config files - -=head1 DESCRIPTION - -Loads Perl files. Example: - - { - name => 'TestApp', - 'Controller::Foo' => { - foo => 'bar' - }, - 'Model::Baz' => { - qux => 'xyzzy' - } - } - -=head1 METHODS - -=head2 extensions( ) - -return an array of valid extensions (C, C). - -=cut - -sub extensions { - return qw( pl perl ); -} - -=head2 load( $file ) - -Attempts to load C<$file> as a Perl file. - -=cut - -sub load { - my $class = shift; - my $file = shift; - - return eval { require $file }; -} - -=head1 AUTHOR - -=over 4 - -=item * Brian Cassidy Ebricas@cpan.orgE - -=back - -=head1 COPYRIGHT AND LICENSE - -Copyright 2006 by Brian Cassidy - -This library is free software; you can redistribute it and/or modify -it under the same terms as Perl itself. - -=head1 SEE ALSO - -=over 4 - -=item * L - -=item * L - -=back - -=cut - -1; \ No newline at end of file diff --git a/lib/Catalyst/Plugin/ConfigLoader/XML.pm b/lib/Catalyst/Plugin/ConfigLoader/XML.pm deleted file mode 100644 index f44346c..0000000 --- a/lib/Catalyst/Plugin/ConfigLoader/XML.pm +++ /dev/null @@ -1,82 +0,0 @@ -package Catalyst::Plugin::ConfigLoader::XML; - -use strict; -use warnings; - -=head1 NAME - -Catalyst::Plugin::ConfigLoader::XML - Load XML config files - -=head1 DESCRIPTION - -Loads XML files. Example: - - - TestApp - - bar - - - xyzzy - - - -=head1 METHODS - -=head2 extensions( ) - -return an array of valid extensions (C). - -=cut - -sub extensions { - return qw( xml ); -} - -=head2 load( $file ) - -Attempts to load C<$file> as an XML file. - -=cut - -sub load { - my $class = shift; - my $file = shift; - - require XML::Simple; - XML::Simple->import; - my $config = XMLin( $file, ForceArray => [ qw( component model view controller ) ] ); - - return $config; -} - -=head1 AUTHOR - -=over 4 - -=item * Brian Cassidy Ebricas@cpan.orgE - -=back - -=head1 COPYRIGHT AND LICENSE - -Copyright 2006 by Brian Cassidy - -This library is free software; you can redistribute it and/or modify -it under the same terms as Perl itself. - -=head1 SEE ALSO - -=over 4 - -=item * L - -=item * L - -=item * L - -=back - -=cut - -1; \ No newline at end of file diff --git a/lib/Catalyst/Plugin/ConfigLoader/YAML.pm b/lib/Catalyst/Plugin/ConfigLoader/YAML.pm deleted file mode 100644 index f1e6a86..0000000 --- a/lib/Catalyst/Plugin/ConfigLoader/YAML.pm +++ /dev/null @@ -1,88 +0,0 @@ -package Catalyst::Plugin::ConfigLoader::YAML; - -use strict; -use warnings; - -=head1 NAME - -Catalyst::Plugin::ConfigLoader::YAML - Load YAML config files - -=head1 DESCRIPTION - -Loads YAML files. Example: - - --- - name: TestApp - Controller::Foo: - foo: bar - Model::Baz: - qux: xyzzy - - -=head1 METHODS - -=head2 extensions( ) - -return an array of valid extensions (C, C). - -=cut - -sub extensions { - return qw( yml yaml ); -} - -=head2 load( $file ) - -Attempts to load C<$file> as a YAML file. - -=cut - -sub load { - my $class = shift; - my $file = shift; - - eval { require YAML::Syck; }; - if( $@ ) { - require YAML; - return YAML::LoadFile( $file ); - } - else { - open( my $fh, $file ) or die $!; - my $content = do { local $/; <$fh> }; - close $fh; - return YAML::Syck::Load( $content ); - } -} - -=head1 AUTHOR - -=over 4 - -=item * Brian Cassidy Ebricas@cpan.orgE - -=back - -=head1 COPYRIGHT AND LICENSE - -Copyright 2006 by Brian Cassidy - -This library is free software; you can redistribute it and/or modify -it under the same terms as Perl itself. - -=head1 SEE ALSO - -=over 4 - -=item * L - -=item * L - -=item * L - -=item * L - -=back - -=cut - -1; \ No newline at end of file