From: Brian Cassidy Date: Mon, 30 Jan 2006 01:01:55 +0000 (+0000) Subject: refactored X-Git-Tag: v0.02^0 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=catagits%2FCatalyst-Plugin-ConfigLoader.git;a=commitdiff_plain;h=c7413665fa4fdabbd3d0e796070ef2f218eef333 refactored --- diff --git a/Changes b/Changes index d8cd4b7..6fe8b17 100644 --- a/Changes +++ b/Changes @@ -1,4 +1,7 @@ Revision history for Perl extension Catalyst::Plugin::ConfigLoader. +0.02 Sun Jan 29 2006 + - refactoring (suggested by Christian Hansen) + 0.01 Sat Jan 28 2006 - original version \ No newline at end of file diff --git a/lib/Catalyst/Plugin/ConfigLoader.pm b/lib/Catalyst/Plugin/ConfigLoader.pm index ef29444..d4092cb 100644 --- a/lib/Catalyst/Plugin/ConfigLoader.pm +++ b/lib/Catalyst/Plugin/ConfigLoader.pm @@ -9,7 +9,7 @@ use Module::Pluggable::Fast search => [ __PACKAGE__ ], require => 1; -our $VERSION = '0.01'; +our $VERSION = '0.02'; =head1 NAME @@ -17,9 +17,9 @@ Catalyst::Plugin::ConfigLoader - Load config files of various types =head1 SYNOPSIS - package MyApp; - - use Catalyst( ConfigLoader ); + package MyApp; + + use Catalyst( ConfigLoader ); # by default myapp.* will be loaded # you can specify a file if you'd like @@ -43,14 +43,29 @@ successfully loaded. =cut sub setup { - my $c = shift; - my $confpath = $c->config->{ file } || $c->path_to( Catalyst::Utils::appprefix( ref $c || $c ) ); + my $c = shift; + my $path = $c->config->{ file } || $c->path_to( Catalyst::Utils::appprefix( ref $c || $c ) ); + + my( $extension ) = ( $path =~ /\.(.{1,4})$/ ); for my $loader ( $c->_config_loaders ) { - my $config = $loader->load( $confpath ); - if( $config ) { - $c->config( $config ); - last; + my @files; + my @extensions = $loader->extensions; + if( $extension ) { + next unless grep { $_ eq $extension } @extensions; + push @files, $path; + } + else { + push @files, "$path.$_" for @extensions; + } + + for( @files ) { + next unless -f $_; + my $config = $loader->load( $_ ); + if( $config ) { + $c->config( $config ); + last; + } } } diff --git a/lib/Catalyst/Plugin/ConfigLoader/INI.pm b/lib/Catalyst/Plugin/ConfigLoader/INI.pm index df263da..64d04d7 100644 --- a/lib/Catalyst/Plugin/ConfigLoader/INI.pm +++ b/lib/Catalyst/Plugin/ConfigLoader/INI.pm @@ -13,11 +13,21 @@ Loads INI files. Example: name=TestApp - [Controller::Config] + [Controller::Foo] foo=bar =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. @@ -25,23 +35,13 @@ 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; + 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; @@ -68,6 +68,8 @@ it under the same terms as Perl itself. =item * L +=item * + =back =cut diff --git a/lib/Catalyst/Plugin/ConfigLoader/JSON.pm b/lib/Catalyst/Plugin/ConfigLoader/JSON.pm index 02cdced..041e55f 100644 --- a/lib/Catalyst/Plugin/ConfigLoader/JSON.pm +++ b/lib/Catalyst/Plugin/ConfigLoader/JSON.pm @@ -15,13 +15,23 @@ Loads JSON files. Example: { "name": "TestApp", - "Controller::Config": { + "Controller::Foo": { "foo": "bar" } } =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. @@ -29,32 +39,19 @@ Attempts to load C<$file> as a JSON file. =cut sub load { - my $class = shift; - my $confpath = shift; + my $class = shift; + my $file = shift; - my @files; - if( $confpath =~ /\.(.{3,4})$/ ) { - return unless $1 =~ /^jso?n$/; - @files = $confpath; + my $content = read_file( $file ); + + eval { require JSON::Syck; }; + if( $@ ) { + require JSON; + JSON->import; + return jsonToObj( $content ); } 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 ); - } + return JSON::Syck::Load( $content ); } } @@ -79,6 +76,8 @@ it under the same terms as Perl itself. =item * L +=item * + =back =cut diff --git a/lib/Catalyst/Plugin/ConfigLoader/Perl.pm b/lib/Catalyst/Plugin/ConfigLoader/Perl.pm index a7beb2a..23e7142 100644 --- a/lib/Catalyst/Plugin/ConfigLoader/Perl.pm +++ b/lib/Catalyst/Plugin/ConfigLoader/Perl.pm @@ -13,13 +13,23 @@ Loads Perl files. Example: { name => 'TestApp', - Controller::Config => { + Controller::Foo => { foo => 'bar' } } =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. @@ -27,22 +37,10 @@ Attempts to load C<$file> as a Perl file. =cut sub load { - my $class = shift; - my $confpath = shift; + my $class = shift; + my $file = 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 }; - } + return eval { require $file }; } =head1 AUTHOR @@ -66,6 +64,8 @@ it under the same terms as Perl itself. =item * L +=item * + =back =cut diff --git a/lib/Catalyst/Plugin/ConfigLoader/XML.pm b/lib/Catalyst/Plugin/ConfigLoader/XML.pm index e44bcae..e960e6a 100644 --- a/lib/Catalyst/Plugin/ConfigLoader/XML.pm +++ b/lib/Catalyst/Plugin/ConfigLoader/XML.pm @@ -13,13 +13,23 @@ Loads XML files. Example: TestApp - + bar =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. @@ -27,23 +37,12 @@ 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; + my $class = shift; + my $file = shift; require XML::Simple; XML::Simple->import; - my $config = XMLin( $file, ForceArray => [ 'component' ] ); + my $config = XMLin( $file, ForceArray => [ 'component' ] ); my $components = delete $config->{ component }; foreach my $element ( keys %$components ) { @@ -74,6 +73,8 @@ it under the same terms as Perl itself. =item * L +=item * + =back =cut diff --git a/lib/Catalyst/Plugin/ConfigLoader/YAML.pm b/lib/Catalyst/Plugin/ConfigLoader/YAML.pm index ec660bd..3d0d538 100644 --- a/lib/Catalyst/Plugin/ConfigLoader/YAML.pm +++ b/lib/Catalyst/Plugin/ConfigLoader/YAML.pm @@ -15,11 +15,21 @@ Loads YAML files. Example: --- name: TestApp - Controller::Config: + Controller::Foo: foo: bar =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. @@ -27,30 +37,17 @@ Attempts to load C<$file> as a YAML file. =cut sub load { - my $class = shift; - my $confpath = shift; + my $class = shift; + my $file = shift; - my @files; - if( $confpath =~ /\.(.{3,4})$/ ) { - return unless $1 =~ /^ya?ml$/; - @files = $confpath; + eval { require YAML::Syck; }; + if( $@ ) { + require YAML; + return YAML::LoadFile( $file ); } 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 ); - } + my $content = read_file( $file ); + return YAML::Syck::Load( $content ); } } @@ -75,6 +72,8 @@ it under the same terms as Perl itself. =item * L +=item * + =back =cut