--- /dev/null
+use strict;\r
+\r
+use Module::Build;\r
+\r
+my $build = Module::Build->new(\r
+ module_name => 'Catalyst::Plugin::ConfigLoader',\r
+ dist_author => 'Brian Cassidy <bricas@cpan.org>',\r
+ license => 'perl',\r
+ create_readme => 1,\r
+ create_makefile_pl => 'traditional',\r
+ requires => {\r
+ 'Catalyst' => 0,\r
+ },\r
+);\r
+\r
+$build->create_build_script;
\ No newline at end of file
--- /dev/null
+Revision history for Perl extension Catalyst::Plugin::ConfigLoader.\r
+\r
+0.01 Sat Jan 28 2006\r
+ - original version
\ No newline at end of file
--- /dev/null
+Build.PL\r
+Changes\r
+lib/Catalyst/Plugin/ConfigLoader.pm\r
+lib/Catalyst/Plugin/ConfigLoader/INI.pm\r
+lib/Catalyst/Plugin/ConfigLoader/JSON.pm\r
+lib/Catalyst/Plugin/ConfigLoader/Perl.pm\r
+lib/Catalyst/Plugin/ConfigLoader/XML.pm\r
+lib/Catalyst/Plugin/ConfigLoader/YAML.pm\r
+MANIFEST\r
+META.yml\r
+t/01-use.t\r
+t/10-live_auto.t\r
+t/98-pod_coverage.t\r
+t/99-pod.t\r
+t/lib/TestApp.pm\r
+t/lib/TestApp/testapp.pl\r
+t/lib/TestApp/Controller/Config.pm\r
+\r
--- /dev/null
+package Catalyst::Plugin::ConfigLoader;\r
+\r
+use strict;\r
+use warnings;\r
+\r
+use NEXT;\r
+use Module::Pluggable::Fast\r
+ name => '_config_loaders',\r
+ search => [ __PACKAGE__ ],\r
+ require => 1;\r
+\r
+our $VERSION = '0.01';\r
+\r
+=head1 NAME\r
+\r
+Catalyst::Plugin::ConfigLoader - Load config files of various types\r
+\r
+=head1 SYNOPSIS\r
+\r
+ package MyApp;\r
+ \r
+ use Catalyst( ConfigLoader );\r
+ \r
+ # by default myapp.* will be loaded\r
+ # you can specify a file if you'd like\r
+ __PACKAGE__->config( file = > 'config.yaml' );\r
+ \r
+\r
+=head1 DESCRIPTION\r
+\r
+This mdoule will attempt to load find and load a configuration\r
+file of various types. Currently it supports YAML, JSON, XML,\r
+INI and Perl formats.\r
+\r
+=head1 METHODS\r
+\r
+=head2 setup( )\r
+\r
+This method is automatically called by Catalyst's setup routine. It will\r
+attempt to use each plugin and set the C<config()> section once a file has been\r
+successfully loaded.\r
+\r
+=cut\r
+\r
+sub setup {\r
+ my $c = shift;\r
+ my $confpath = $c->config->{ file } || $c->path_to( Catalyst::Utils::appprefix( ref $c || $c ) );\r
+ \r
+ for my $loader ( $c->_config_loaders ) {\r
+ my $config = $loader->load( $confpath );\r
+ if( $config ) {\r
+ $c->config( $config );\r
+ last;\r
+ }\r
+ }\r
+\r
+ $c->NEXT::setup( @_ );\r
+}\r
+\r
+=head1 AUTHOR\r
+\r
+=over 4 \r
+\r
+=item * Brian Cassidy E<lt>bricas@cpan.orgE<gt>\r
+\r
+=back\r
+\r
+=head1 COPYRIGHT AND LICENSE\r
+\r
+Copyright 2006 by Brian Cassidy\r
+\r
+This library is free software; you can redistribute it and/or modify\r
+it under the same terms as Perl itself. \r
+\r
+=head1 SEE ALSO\r
+\r
+=over 4 \r
+\r
+=item * L<Catalyst>\r
+\r
+=back\r
+\r
+=cut\r
+\r
+1;
\ No newline at end of file
--- /dev/null
+package Catalyst::Plugin::ConfigLoader::INI;\r
+\r
+use strict;\r
+use warnings;\r
+\r
+=head1 NAME\r
+\r
+Catalyst::Plugin::ConfigLoader::INI - Load INI config files\r
+\r
+=head1 DESCRIPTION\r
+\r
+Loads INI files. Example:\r
+\r
+ name=TestApp\r
+ \r
+ [Controller::Config]\r
+ foo=bar\r
+\r
+=head1 METHODS\r
+\r
+=head2 load( $file )\r
+\r
+Attempts to load C<$file> as an INI file.\r
+\r
+=cut\r
+\r
+sub load {\r
+ my $class = shift;\r
+ my $confpath = shift;\r
+\r
+ my $file;\r
+ if( $confpath =~ /\.(.{3})$/ ) {\r
+ return unless $1 eq 'ini';\r
+ $file = $confpath;\r
+ }\r
+ else {\r
+ $file = "$confpath.ini";\r
+ }\r
+ \r
+ return unless -f $file;\r
+\r
+ require Config::Tiny;\r
+ my $config = Config::Tiny->read( $file );\r
+ my $main = delete $config->{ _ };\r
+ $config->{ $_ } = $main->{ $_ } for keys %$main;\r
+\r
+ return $config;\r
+}\r
+\r
+=head1 AUTHOR\r
+\r
+=over 4 \r
+\r
+=item * Brian Cassidy E<lt>bricas@cpan.orgE<gt>\r
+\r
+=back\r
+\r
+=head1 COPYRIGHT AND LICENSE\r
+\r
+Copyright 2006 by Brian Cassidy\r
+\r
+This library is free software; you can redistribute it and/or modify\r
+it under the same terms as Perl itself. \r
+\r
+=head1 SEE ALSO\r
+\r
+=over 4 \r
+\r
+=item * L<Catalyst>\r
+\r
+=back\r
+\r
+=cut\r
+\r
+1;
\ No newline at end of file
--- /dev/null
+package Catalyst::Plugin::ConfigLoader::JSON;\r
+\r
+use strict;\r
+use warnings;\r
+\r
+use File::Slurp;\r
+\r
+=head1 NAME\r
+\r
+Catalyst::Plugin::ConfigLoader::JSON - Load JSON config files\r
+\r
+=head1 DESCRIPTION\r
+\r
+Loads JSON files. Example:\r
+\r
+ {\r
+ "name": "TestApp",\r
+ "Controller::Config": {\r
+ "foo": "bar"\r
+ }\r
+ }\r
+\r
+=head1 METHODS\r
+\r
+=head2 load( $file )\r
+\r
+Attempts to load C<$file> as a JSON file.\r
+\r
+=cut\r
+\r
+sub load {\r
+ my $class = shift;\r
+ my $confpath = shift;\r
+\r
+ my @files;\r
+ if( $confpath =~ /\.(.{3,4})$/ ) {\r
+ return unless $1 =~ /^jso?n$/;\r
+ @files = $confpath;\r
+ }\r
+ else {\r
+ @files = map { "$confpath.$_" } qw( json jsn );\r
+ }\r
+ \r
+ for my $file ( @files ) {\r
+ next unless -f $file;\r
+ \r
+ my $content = read_file( $file );\r
+\r
+ eval { require JSON::Syck; };\r
+ if( $@ ) {\r
+ require JSON;\r
+ JSON->import;\r
+ return jsonToObj( $content );\r
+ }\r
+ else {\r
+ return JSON::Syck::Load( $content );\r
+ }\r
+ }\r
+}\r
+\r
+=head1 AUTHOR\r
+\r
+=over 4 \r
+\r
+=item * Brian Cassidy E<lt>bricas@cpan.orgE<gt>\r
+\r
+=back\r
+\r
+=head1 COPYRIGHT AND LICENSE\r
+\r
+Copyright 2006 by Brian Cassidy\r
+\r
+This library is free software; you can redistribute it and/or modify\r
+it under the same terms as Perl itself. \r
+\r
+=head1 SEE ALSO\r
+\r
+=over 4 \r
+\r
+=item * L<Catalyst>\r
+\r
+=back\r
+\r
+=cut\r
+\r
+1;
\ No newline at end of file
--- /dev/null
+package Catalyst::Plugin::ConfigLoader::Perl;\r
+\r
+use strict;\r
+use warnings;\r
+\r
+=head1 NAME\r
+\r
+Catalyst::Plugin::ConfigLoader::Perl - Load Perl config files\r
+\r
+=head1 DESCRIPTION\r
+\r
+Loads Perl files. Example:\r
+\r
+ {\r
+ name => 'TestApp',\r
+ Controller::Config => {\r
+ foo => 'bar'\r
+ }\r
+ }\r
+\r
+=head1 METHODS\r
+\r
+=head2 load( $file )\r
+\r
+Attempts to load C<$file> as a Perl file.\r
+\r
+=cut\r
+\r
+sub load {\r
+ my $class = shift;\r
+ my $confpath = shift;\r
+\r
+ my @files;\r
+ if( $confpath =~ /\.(.{2,4})$/ ) {\r
+ return unless $1 =~ /^p(er)?l$/;\r
+ @files = $confpath;\r
+ }\r
+ else {\r
+ @files = map { "$confpath.$_" } qw( pl perl );\r
+ }\r
+ \r
+ for my $file ( @files ) {\r
+ next unless -f $file;\r
+ return eval { require $file };\r
+ }\r
+}\r
+\r
+=head1 AUTHOR\r
+\r
+=over 4 \r
+\r
+=item * Brian Cassidy E<lt>bricas@cpan.orgE<gt>\r
+\r
+=back\r
+\r
+=head1 COPYRIGHT AND LICENSE\r
+\r
+Copyright 2006 by Brian Cassidy\r
+\r
+This library is free software; you can redistribute it and/or modify\r
+it under the same terms as Perl itself. \r
+\r
+=head1 SEE ALSO\r
+\r
+=over 4 \r
+\r
+=item * L<Catalyst>\r
+\r
+=back\r
+\r
+=cut\r
+\r
+1;
\ No newline at end of file
--- /dev/null
+package Catalyst::Plugin::ConfigLoader::XML;\r
+\r
+use strict;\r
+use warnings;\r
+\r
+=head1 NAME\r
+\r
+Catalyst::Plugin::ConfigLoader::XML - Load XML config files\r
+\r
+=head1 DESCRIPTION\r
+\r
+Loads XML files. Example:\r
+\r
+ <config>\r
+ <name>TestApp</name>\r
+ <component name="Controller::Config">\r
+ <foo>bar</foo>\r
+ </component>\r
+ </config>\r
+\r
+=head1 METHODS\r
+\r
+=head2 load( $file )\r
+\r
+Attempts to load C<$file> as an XML file.\r
+\r
+=cut\r
+\r
+sub load {\r
+ my $class = shift;\r
+ my $confpath = shift;\r
+\r
+ my $file;\r
+ if( $confpath =~ /\.(.{3})$/ ) {\r
+ return unless $1 eq 'xml';\r
+ $file = $confpath;\r
+ }\r
+ else {\r
+ $file = "$confpath.xml";\r
+ }\r
+ \r
+ return unless -f $file;\r
+\r
+ require XML::Simple;\r
+ XML::Simple->import;\r
+ my $config = XMLin( $file, ForceArray => [ 'component' ] );\r
+\r
+ my $components = delete $config->{ component };\r
+ foreach my $element ( keys %$components ) {\r
+ $config->{ $element } = $components->{ $element };\r
+ }\r
+\r
+ return $config;\r
+}\r
+\r
+=head1 AUTHOR\r
+\r
+=over 4 \r
+\r
+=item * Brian Cassidy E<lt>bricas@cpan.orgE<gt>\r
+\r
+=back\r
+\r
+=head1 COPYRIGHT AND LICENSE\r
+\r
+Copyright 2006 by Brian Cassidy\r
+\r
+This library is free software; you can redistribute it and/or modify\r
+it under the same terms as Perl itself. \r
+\r
+=head1 SEE ALSO\r
+\r
+=over 4 \r
+\r
+=item * L<Catalyst>\r
+\r
+=back\r
+\r
+=cut\r
+\r
+1;
\ No newline at end of file
--- /dev/null
+package Catalyst::Plugin::ConfigLoader::YAML;\r
+\r
+use strict;\r
+use warnings;\r
+\r
+use File::Slurp;\r
+\r
+=head1 NAME\r
+\r
+Catalyst::Plugin::ConfigLoader::YAML - Load YAML config files\r
+\r
+=head1 DESCRIPTION\r
+\r
+Loads YAML files. Example:\r
+\r
+ ---\r
+ name: TestApp\r
+ Controller::Config:\r
+ foo: bar\r
+\r
+=head1 METHODS\r
+\r
+=head2 load( $file )\r
+\r
+Attempts to load C<$file> as a YAML file.\r
+\r
+=cut\r
+\r
+sub load {\r
+ my $class = shift;\r
+ my $confpath = shift;\r
+\r
+ my @files;\r
+ if( $confpath =~ /\.(.{3,4})$/ ) {\r
+ return unless $1 =~ /^ya?ml$/;\r
+ @files = $confpath;\r
+ }\r
+ else {\r
+ @files = map { "$confpath.$_" } qw( yml yaml );\r
+ }\r
+ \r
+ for my $file ( @files ) {\r
+ next unless -f $file;\r
+\r
+ eval { require YAML::Syck; };\r
+ if( $@ ) {\r
+ require YAML;\r
+ return YAML::LoadFile( $file );\r
+ }\r
+ else {\r
+ my $content = read_file( $file );\r
+ return YAML::Syck::Load( $content );\r
+ }\r
+ }\r
+}\r
+\r
+=head1 AUTHOR\r
+\r
+=over 4 \r
+\r
+=item * Brian Cassidy E<lt>bricas@cpan.orgE<gt>\r
+\r
+=back\r
+\r
+=head1 COPYRIGHT AND LICENSE\r
+\r
+Copyright 2006 by Brian Cassidy\r
+\r
+This library is free software; you can redistribute it and/or modify\r
+it under the same terms as Perl itself. \r
+\r
+=head1 SEE ALSO\r
+\r
+=over 4 \r
+\r
+=item * L<Catalyst>\r
+\r
+=back\r
+\r
+=cut\r
+\r
+1;
\ No newline at end of file
--- /dev/null
+use Test::More tests => 6;\r
+\r
+BEGIN { \r
+ use_ok( 'Catalyst::Plugin::ConfigLoader' );\r
+ use_ok( 'Catalyst::Plugin::ConfigLoader::INI' );\r
+ use_ok( 'Catalyst::Plugin::ConfigLoader::JSON' );\r
+ use_ok( 'Catalyst::Plugin::ConfigLoader::Perl' );\r
+ use_ok( 'Catalyst::Plugin::ConfigLoader::XML' );\r
+ use_ok( 'Catalyst::Plugin::ConfigLoader::YAML' );\r
+}\r
--- /dev/null
+use strict;\r
+use warnings;\r
+\r
+use FindBin;\r
+use lib "$FindBin::Bin/lib";\r
+\r
+use Test::More tests => 2;\r
+\r
+use Catalyst::Test 'TestApp';\r
+\r
+{\r
+ ok( my $response = request('http://localhost/config/'), 'request ok' );\r
+ is( $response->content, 'foo', 'config ok' );\r
+}\r
--- /dev/null
+use Test::More;\r
+eval "use Test::Pod::Coverage 1.00";\r
+plan skip_all => "Test::Pod::Coverage 1.00 required for testing POD coverage" if $@;\r
+all_pod_coverage_ok();
\ No newline at end of file
--- /dev/null
+use Test::More;\r
+eval "use Test::Pod 1.00";\r
+plan skip_all => "Test::Pod 1.00 required for testing POD" if $@;\r
+all_pod_files_ok();
\ No newline at end of file
--- /dev/null
+package TestApp;
+
+use strict;
+use warnings;
+
+use Catalyst qw/ConfigLoader/;
+
+our $VERSION = '0.01';
+
+__PACKAGE__->setup;
+
+1;
--- /dev/null
+package TestApp::Controller::Config;\r
+\r
+use strict;\r
+use warnings;\r
+\r
+use base qw( Catalyst::Controller );\r
+\r
+sub index : Private {\r
+ my( $self, $c ) = @_;\r
+ $c->res->output( $self->{ foo } );\r
+}\r
+\r
+1;
\ No newline at end of file
--- /dev/null
+{
+ name => 'TestApp',
+ Controller::Config => {
+ foo => 'foo'
+ }
+}