CallBackery

 view release on metacpan or  search on metacpan

lib/CallBackery/GuiPlugin/Abstract.pm  view on Meta::CPAN


If the destination already exists, the method compares the current
content with the new one. It will only update the file if the content
differs.

The method returns 0 when there was no change and 1 when a new version
of the file was written.

These additional commands are available to the templates.

=over

=item *

slurp(file)

=back

=cut

has cfgHash => sub {
    my $self = shift;
    return $self->app->config->cfgHash;
}, weak => 1;

has template => sub {
    my $self = shift;
    my $mt = Mojo::Template->new();
    $self->dbHandle;   
    my $dbLookup = sub { $self->getConfigValue(@_) // ''};
 
    # don't use L, use dbLookup instead
    monkey_patch $mt->namespace,
        L => $dbLookup;

    monkey_patch $mt->namespace,
        dbLookup => $dbLookup;

    monkey_patch $mt->namespace,
        app => sub { $self->app };

    monkey_patch $mt->namespace,
        slurp => sub {
            my $filename = shift;
            return Mojo::File->new($filename)->slurp;
        };
    monkey_patch $mt->namespace,
        cfgHash => sub { $self->cfgHash };

    monkey_patch $mt->namespace,
        pluginCfg => sub { my $instance = shift;
            my $cfg = $self->cfgHash->{PLUGIN}{prototype}{$instance}->config;
            weaken $cfg;
            return $cfg;
        };
    return $mt;
};


has homeDir => sub {
    [getpwuid $>]->[7];
};

sub renderTemplate{
    my $self = shift;
    my $template = shift;
    my $destination = Mojo::File->new(shift);
    $self->log->debug('['.$self->name.'] processing template '.$template);
    my $newData = $self->template->render($self->app->home->rel_file('templates/system/'.$template)->slurp);
    if (-r $destination){
        my $oldData = Mojo::File->new($destination)->slurp;
        if ($newData eq $oldData){
            return 0
        }
    }
    my $dir = $destination->dirname;
    if (not -d $dir){
        Mojo::File->new($dir)->make_path({mode => 755});
    }

    $self->log->debug('['.$self->name."] writing $destination\n$newData");
    eval {
        local $SIG{__DIE__};
        $destination->spew($newData);
    };
    if ($@){
        if (blessed $@ and $@->isa('autodie::exception')){
            $self->log->error('['.$self->name."] writing $template -> $destination: ".$@->errno);
        }
        else {
            die $@;
        }
    }
    if ($self->controller and $self->controller->can('runEventActions')){
        $self->controller->runEventActions('changeConfig');
    }
    return 1;
}

=head2 getConfigValue(key)

Read a config value from the database.

=cut

sub getConfigValue {
    my $self = shift;
    my $key = shift;
    my $value = $self->dbHandle->getConfigValue($key);
    return undef if not defined $value;
    my $ret = eval { decode_json($value) };
    # warn "GET $key -> ".Dumper($ret);
    if ($@){
        die mkerror (3984,$@);
    }
    return $ret->[0];
}

=head2 setConfigValue(key)

Save a config value to the database.



( run in 1.681 second using v1.01-cache-2.11-cpan-39bf76dae61 )