From: Brian Cassidy Date: Sun, 29 Jan 2006 01:40:49 +0000 (+0000) Subject: initial import X-Git-Tag: v0.01^0 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=catagits%2FCatalyst-Plugin-ConfigLoader.git;a=commitdiff_plain;h=b2d855940c68a8bec09c76f6235b804ef9090dc6 initial import --- diff --git a/Build.PL b/Build.PL new file mode 100644 index 0000000..14c4530 --- /dev/null +++ b/Build.PL @@ -0,0 +1,16 @@ +use strict; + +use Module::Build; + +my $build = Module::Build->new( + module_name => 'Catalyst::Plugin::ConfigLoader', + dist_author => 'Brian Cassidy ', + license => 'perl', + create_readme => 1, + create_makefile_pl => 'traditional', + requires => { + 'Catalyst' => 0, + }, +); + +$build->create_build_script; \ No newline at end of file diff --git a/Changes b/Changes new file mode 100644 index 0000000..d8cd4b7 --- /dev/null +++ b/Changes @@ -0,0 +1,4 @@ +Revision history for Perl extension Catalyst::Plugin::ConfigLoader. + +0.01 Sat Jan 28 2006 + - original version \ No newline at end of file diff --git a/MANIFEST b/MANIFEST new file mode 100644 index 0000000..4da8e07 --- /dev/null +++ b/MANIFEST @@ -0,0 +1,18 @@ +Build.PL +Changes +lib/Catalyst/Plugin/ConfigLoader.pm +lib/Catalyst/Plugin/ConfigLoader/INI.pm +lib/Catalyst/Plugin/ConfigLoader/JSON.pm +lib/Catalyst/Plugin/ConfigLoader/Perl.pm +lib/Catalyst/Plugin/ConfigLoader/XML.pm +lib/Catalyst/Plugin/ConfigLoader/YAML.pm +MANIFEST +META.yml +t/01-use.t +t/10-live_auto.t +t/98-pod_coverage.t +t/99-pod.t +t/lib/TestApp.pm +t/lib/TestApp/testapp.pl +t/lib/TestApp/Controller/Config.pm + diff --git a/lib/Catalyst/Plugin/ConfigLoader.pm b/lib/Catalyst/Plugin/ConfigLoader.pm new file mode 100644 index 0000000..ef29444 --- /dev/null +++ b/lib/Catalyst/Plugin/ConfigLoader.pm @@ -0,0 +1,85 @@ +package Catalyst::Plugin::ConfigLoader; + +use strict; +use warnings; + +use NEXT; +use Module::Pluggable::Fast + name => '_config_loaders', + search => [ __PACKAGE__ ], + require => 1; + +our $VERSION = '0.01'; + +=head1 NAME + +Catalyst::Plugin::ConfigLoader - Load config files of various types + +=head1 SYNOPSIS + + package MyApp; + + use Catalyst( ConfigLoader ); + + # by default myapp.* will be loaded + # you can specify a file if you'd like + __PACKAGE__->config( file = > 'config.yaml' ); + + +=head1 DESCRIPTION + +This mdoule will attempt to load find and load a configuration +file of various types. Currently it supports YAML, JSON, XML, +INI and Perl formats. + +=head1 METHODS + +=head2 setup( ) + +This method is automatically called by Catalyst's setup routine. It will +attempt to use each plugin and set the C section once a file has been +successfully loaded. + +=cut + +sub setup { + my $c = shift; + my $confpath = $c->config->{ file } || $c->path_to( Catalyst::Utils::appprefix( ref $c || $c ) ); + + for my $loader ( $c->_config_loaders ) { + my $config = $loader->load( $confpath ); + if( $config ) { + $c->config( $config ); + last; + } + } + + $c->NEXT::setup( @_ ); +} + +=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 + +=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 new file mode 100644 index 0000000..df263da --- /dev/null +++ b/lib/Catalyst/Plugin/ConfigLoader/INI.pm @@ -0,0 +1,75 @@ +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::Config] + foo=bar + +=head1 METHODS + +=head2 load( $file ) + +Attempts to load C<$file> as an INI file. + +=cut + +sub load { + my $class = shift; + my $confpath = shift; + + my $file; + if( $confpath =~ /\.(.{3})$/ ) { + return unless $1 eq 'ini'; + $file = $confpath; + } + else { + $file = "$confpath.ini"; + } + + return unless -f $file; + + 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 + +=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 new file mode 100644 index 0000000..02cdced --- /dev/null +++ b/lib/Catalyst/Plugin/ConfigLoader/JSON.pm @@ -0,0 +1,86 @@ +package Catalyst::Plugin::ConfigLoader::JSON; + +use strict; +use warnings; + +use File::Slurp; + +=head1 NAME + +Catalyst::Plugin::ConfigLoader::JSON - Load JSON config files + +=head1 DESCRIPTION + +Loads JSON files. Example: + + { + "name": "TestApp", + "Controller::Config": { + "foo": "bar" + } + } + +=head1 METHODS + +=head2 load( $file ) + +Attempts to load C<$file> as a JSON file. + +=cut + +sub load { + my $class = shift; + my $confpath = shift; + + my @files; + if( $confpath =~ /\.(.{3,4})$/ ) { + return unless $1 =~ /^jso?n$/; + @files = $confpath; + } + else { + @files = map { "$confpath.$_" } qw( json jsn ); + } + + for my $file ( @files ) { + next unless -f $file; + + my $content = read_file( $file ); + + 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 + +=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 new file mode 100644 index 0000000..a7beb2a --- /dev/null +++ b/lib/Catalyst/Plugin/ConfigLoader/Perl.pm @@ -0,0 +1,73 @@ +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::Config => { + foo => 'bar' + } + } + +=head1 METHODS + +=head2 load( $file ) + +Attempts to load C<$file> as a Perl file. + +=cut + +sub load { + my $class = shift; + my $confpath = shift; + + my @files; + if( $confpath =~ /\.(.{2,4})$/ ) { + return unless $1 =~ /^p(er)?l$/; + @files = $confpath; + } + else { + @files = map { "$confpath.$_" } qw( pl perl ); + } + + for my $file ( @files ) { + next unless -f $file; + 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 + +=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 new file mode 100644 index 0000000..e44bcae --- /dev/null +++ b/lib/Catalyst/Plugin/ConfigLoader/XML.pm @@ -0,0 +1,81 @@ +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 + + + +=head1 METHODS + +=head2 load( $file ) + +Attempts to load C<$file> as an XML file. + +=cut + +sub load { + my $class = shift; + my $confpath = shift; + + my $file; + if( $confpath =~ /\.(.{3})$/ ) { + return unless $1 eq 'xml'; + $file = $confpath; + } + else { + $file = "$confpath.xml"; + } + + return unless -f $file; + + require XML::Simple; + XML::Simple->import; + my $config = XMLin( $file, ForceArray => [ 'component' ] ); + + my $components = delete $config->{ component }; + foreach my $element ( keys %$components ) { + $config->{ $element } = $components->{ $element }; + } + + 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 + +=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 new file mode 100644 index 0000000..ec660bd --- /dev/null +++ b/lib/Catalyst/Plugin/ConfigLoader/YAML.pm @@ -0,0 +1,82 @@ +package Catalyst::Plugin::ConfigLoader::YAML; + +use strict; +use warnings; + +use File::Slurp; + +=head1 NAME + +Catalyst::Plugin::ConfigLoader::YAML - Load YAML config files + +=head1 DESCRIPTION + +Loads YAML files. Example: + + --- + name: TestApp + Controller::Config: + foo: bar + +=head1 METHODS + +=head2 load( $file ) + +Attempts to load C<$file> as a YAML file. + +=cut + +sub load { + my $class = shift; + my $confpath = shift; + + my @files; + if( $confpath =~ /\.(.{3,4})$/ ) { + return unless $1 =~ /^ya?ml$/; + @files = $confpath; + } + else { + @files = map { "$confpath.$_" } qw( yml yaml ); + } + + for my $file ( @files ) { + next unless -f $file; + + eval { require YAML::Syck; }; + if( $@ ) { + require YAML; + return YAML::LoadFile( $file ); + } + else { + my $content = read_file( $file ); + 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 + +=back + +=cut + +1; \ No newline at end of file diff --git a/t/01-use.t b/t/01-use.t new file mode 100644 index 0000000..0399513 --- /dev/null +++ b/t/01-use.t @@ -0,0 +1,10 @@ +use Test::More tests => 6; + +BEGIN { + use_ok( 'Catalyst::Plugin::ConfigLoader' ); + use_ok( 'Catalyst::Plugin::ConfigLoader::INI' ); + use_ok( 'Catalyst::Plugin::ConfigLoader::JSON' ); + use_ok( 'Catalyst::Plugin::ConfigLoader::Perl' ); + use_ok( 'Catalyst::Plugin::ConfigLoader::XML' ); + use_ok( 'Catalyst::Plugin::ConfigLoader::YAML' ); +} diff --git a/t/10-live_auto.t b/t/10-live_auto.t new file mode 100644 index 0000000..56997e8 --- /dev/null +++ b/t/10-live_auto.t @@ -0,0 +1,14 @@ +use strict; +use warnings; + +use FindBin; +use lib "$FindBin::Bin/lib"; + +use Test::More tests => 2; + +use Catalyst::Test 'TestApp'; + +{ + ok( my $response = request('http://localhost/config/'), 'request ok' ); + is( $response->content, 'foo', 'config ok' ); +} diff --git a/t/98-pod_coverage.t b/t/98-pod_coverage.t new file mode 100644 index 0000000..73a83b0 --- /dev/null +++ b/t/98-pod_coverage.t @@ -0,0 +1,4 @@ +use Test::More; +eval "use Test::Pod::Coverage 1.00"; +plan skip_all => "Test::Pod::Coverage 1.00 required for testing POD coverage" if $@; +all_pod_coverage_ok(); \ No newline at end of file diff --git a/t/99-pod.t b/t/99-pod.t new file mode 100644 index 0000000..3afe2fa --- /dev/null +++ b/t/99-pod.t @@ -0,0 +1,4 @@ +use Test::More; +eval "use Test::Pod 1.00"; +plan skip_all => "Test::Pod 1.00 required for testing POD" if $@; +all_pod_files_ok(); \ No newline at end of file diff --git a/t/lib/TestApp.pm b/t/lib/TestApp.pm new file mode 100644 index 0000000..6d4b336 --- /dev/null +++ b/t/lib/TestApp.pm @@ -0,0 +1,12 @@ +package TestApp; + +use strict; +use warnings; + +use Catalyst qw/ConfigLoader/; + +our $VERSION = '0.01'; + +__PACKAGE__->setup; + +1; diff --git a/t/lib/TestApp/Controller/Config.pm b/t/lib/TestApp/Controller/Config.pm new file mode 100644 index 0000000..1951de2 --- /dev/null +++ b/t/lib/TestApp/Controller/Config.pm @@ -0,0 +1,13 @@ +package TestApp::Controller::Config; + +use strict; +use warnings; + +use base qw( Catalyst::Controller ); + +sub index : Private { + my( $self, $c ) = @_; + $c->res->output( $self->{ foo } ); +} + +1; \ No newline at end of file diff --git a/t/lib/TestApp/testapp.pl b/t/lib/TestApp/testapp.pl new file mode 100644 index 0000000..0a775f1 --- /dev/null +++ b/t/lib/TestApp/testapp.pl @@ -0,0 +1,6 @@ +{ + name => 'TestApp', + Controller::Config => { + foo => 'foo' + } +}