Catalyst-Model-HTML-FormFu

 view release on metacpan or  search on metacpan

lib/Catalyst/Model/HTML/FormFu.pm  view on Meta::CPAN

    my $self = shift;
    $self->context($_[0]);
    return $self;
}

sub load_form
{
    my $self   = shift;
    my $c      = eval { $_[0]->isa('Catalyst') } ? shift : $self->context;
    my $config = shift;

    my $method = 
        (eval { $config->isa('Path::Class::File') } || ! ref $config) ?
            '_load_from_file' :
            '_load_from_hash'
    ;
    my $form =  $self->$method($c, $config);

    # Actuall process the form
    $form->process( $c->request );
    if (my $key = $self->stash_key) {
        $c->stash->{$key} = $form;
    }
    return $form;
}

sub cache
{
    my ($self, $c) = @_;

    my $cache = $self->_cache;
    if (! $cache) {
        my $backend = $self->cache_backend;
        if ($c->can('cache') && $backend) {
            $cache = $c->cache( $backend );
            $self->_cache($cache);
        }
    }
    return $cache;
}

sub _load_from_file
{
    my ($self, $c, $file) = @_;

    if (! ref $file) {
        $file = Path::Class::File->new($file);
    }

    if (! $file->is_absolute) {
        # if this exists in the config...
        my $dir = $self->config_dir;
        $file = $dir ?
            Path::Class::Dir->new($dir)->file($file) :
            $c->path_to(qw(root formfu), $file)
        ;
    }

    # finally
    $file = $file->stringify;
    my $mtime = (stat($file))[9];

    my $cached_data;
    my $cache = $self->cache($c);
    if ($cache) {
        $cached_data = $cache->get($file);
        # If the cached data is older than the file, reload
        if ($cached_data) {
            if ($cached_data->{mtime} < $mtime) {
                $c->log->debug("HTML::FormFu config file $file has been modified since last load time. Throwing away cache and reloading") if $c->log->is_debug;
                undef $cached_data;
            } else {
                # No changes, return the cached form
                return $cached_data->{form};
            }
        }
    }

    my $loaded = Config::Any->load_files({ files => [$file], use_ext => 1 });
    my $config = $loaded->[0]->{$file} ||
        Catalyst::Exception->throw("Could not load form $file");
    $config    = $self->_filter_dynamic_values($c, $config);
    my $form      = $self->_construct_formfu($c, $config);
    $cache->set($file, { form => $form, mtime => $mtime }) if $cache;
    return $form;
}

sub _load_from_hash
{
    my ($self, $c, $config) = @_;

    my $form;
    my $cache = $self->_cache($c);
    my $key;
    if ($cache) {
        $key = do {
            require Data::Dumper;
            require Digest::MD5;
            local $Data::Dumper::Indent   = 1;
            local $Data::Dumper::Sortkeys = 1;
            local $Data::Dumper::Terse    = 1;

            Digest::MD5::md5_hex( Data::Dumper::Dumper($config) );
        };
        $form = $cache->get($key);
    }

    if (! $form) {
        $config = $self->_filter_dynamic_values($c, $config);
        $form   = $self->_construct_formfu($c, $config);

        $cache->set($key, $form) if $cache;
    }

    return $form;
}

sub _construct_formfu
{
    my ($self, $c, $config) = @_;



( run in 1.831 second using v1.01-cache-2.11-cpan-5b529ec07f3 )