package Catalyst::Plugin::ConfigLoader::Remote;

use warnings;
use strict;

use File::Fetch;
use Scalar::Util qw(blessed);
use File::Temp qw/tempdir/;

use MRO::Compat;

use base qw/Catalyst::Plugin::ConfigLoader/;

=head1 NAME

Catalyst::Plugin::ConfigLoader::Remote - Load (remote) URIs into config

=head1 VERSION

Version 0.01


our $VERSION = '0.02';


This module provides support for fetching remote configuration files over HTTP,
FTP or other remote methods.

It will fetch any blessed URI object in the C<files> config entry for the
package via L<File::Fetch>

 package MyApp;

 use Catalyst qw/ConfigLoader::Remote/;

 use URI;

    'Plugin::ConfigLoader::Remote' => {
        files => [

=head1 METHODS

=head2 find_files

find_files will download each file to a temporary directory that is purged on
program exit.  It is then passed to  L<Catalyst::Plugin::ConfigLoader> for 
actual configuration loading and processing.


sub find_files {
    my $c = shift;

    my $tempdir = tempdir( CLEANUP => 1 );

    # Load up the other files that are coming in.
    my @files = $c->next::method();

    my $config = $c->config->{'Plugin::ConfigLoader::Remote'};

    return @files unless ref $config eq 'HASH' and
                         ref $config->{files} eq 'ARRAY';

    my @incoming_files =
        @{ $config->{files} };

    # replace everything in @files that is a URI object with a downloaded copy
    foreach my $arg ( @incoming_files ) {
        if ( blessed $arg and $arg->isa('URI') ) {
            # Fetch a blessed URI
            my $ff = File::Fetch->new( uri => $arg->as_string );
            if ( $ff ) {
                my $file = $ff->fetch( to => $tempdir );
                if ( $file and -f $file ) {
                    push @files, $file;
        } else {
            push @files, $arg;

    return @files;

=head1 AUTHOR

J. Shirley, C<< <jshirley at> >>

Eden Cardim

=head1 BUGS

Please report any bugs or feature requests to
C<bug-catalyst-plugin-configloader-remote at>, or through the web interface at
I will be notified, and then you'll automatically be notified of progress on
your bug as I make changes.

=head1 SUPPORT

You can find documentation for this module with the perldoc command.

    perldoc Catalyst::Plugin::ConfigLoader::Remote

You can also look for information at:

=over 4

=item * AnnoCPAN: Annotated CPAN documentation


=item * CPAN Ratings


=item * RT: CPAN's request tracker


=item * Search CPAN





Copyright 2007 J. Shirley and

This program is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.


1; # End of Catalyst::Plugin::ConfigLoader::Remote