--- /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.03';\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
+ # ConfigLoader should be first in your list so\r
+ # other plugins can get the config information\r
+ use Catalyst qw( 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, once a file has been successfully\r
+loaded, set the C<config()> section.\r
+\r
+=cut\r
+\r
+sub setup {\r
+ my $c = shift;\r
+ my $path = $c->config->{ file } || $c->path_to( Catalyst::Utils::appprefix( ref $c || $c ) );\r
+\r
+ my( $extension ) = ( $path =~ /\.(.{1,4})$/ );\r
+ \r
+ for my $loader ( $c->_config_loaders ) {\r
+ my @files;\r
+ my @extensions = $loader->extensions;\r
+ if( $extension ) {\r
+ next unless grep { $_ eq $extension } @extensions;\r
+ push @files, $path;\r
+ }\r
+ else {\r
+ push @files, "$path.$_" for @extensions;\r
+ }\r
+\r
+ for( @files ) {\r
+ next unless -f $_;\r
+ my $config = $loader->load( $_ );\r
+ if( $config ) {\r
+ $c->config( $config );\r
+ last;\r
+ }\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::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::Foo:\r
+ foo: bar\r
+\r
+=head1 METHODS\r
+\r
+=head2 extensions( )\r
+\r
+return an array of valid extensions (C<yml>, C<yaml>).\r
+\r
+=cut\r
+\r
+sub extensions {\r
+ return qw( yml yaml );\r
+}\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 $file = shift;\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
+=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
+=item * L<Catalyst::Plugin::ConfigLoader>\r
+\r
+=back\r
+\r
+=cut\r
+\r
+1;
\ No newline at end of file