root/trunk/plagger/lib/Plagger/Plugin.pm

Revision 593 (checked in by miyagawa, 14 years ago)

WARNING: big directory organize revamp. Keep cases in plugin id and assets directory
Fixes #106

  • Property svn:keywords set to Id Revision
Line 
1 package Plagger::Plugin;
2 use strict;
3 use base qw( Class::Accessor::Fast );
4
5 __PACKAGE__->mk_accessors( qw(conf rule rule_hook cache) );
6
7 use Plagger::Crypt;
8 use Plagger::Rule;
9 use Plagger::Rules;
10
11 use FindBin;
12 use File::Spec;
13
14 sub new {
15     my($class, $opt) = @_;
16     my $self = bless {
17         conf => $opt->{config} || {},
18         rule => $opt->{rule},
19         rule_op => $opt->{rule_op} || 'AND',
20         rule_hook => '',
21         meta => {},
22     }, $class;
23     $self->init();
24     $self;
25 }
26
27 sub init {
28     my $self = shift;
29
30     if (my $rule = $self->{rule}) {
31         $rule = [ $rule ] if ref($rule) eq 'HASH';
32         my $op = $self->{rule_op};
33         $self->{rule} = Plagger::Rules->new($op, @$rule);
34     } else {
35         $self->{rule} = Plagger::Rule->new({ module => 'Always' });
36     }
37
38     $self->walk_config_encryption();
39 }
40
41 sub walk_config_encryption {
42     my $self = shift;
43     my $conf = $self->conf;
44
45     $self->do_walk($conf);
46 }
47
48 sub do_walk {
49     my($self, $data) = @_;
50     return unless defined($data) && ref $data;
51
52     if (ref($data) eq 'HASH') {
53         for my $key (keys %$data) {
54             if ($key =~ /password/) {
55                 $self->decrypt_config($data, $key);
56             }
57             $self->do_walk($data->{$key});
58         }
59     } elsif (ref($data) eq 'ARRAY') {
60         for my $value (@$data) {
61             $self->do_walk($value);
62         }
63     }
64 }
65
66 sub decrypt_config {
67     my($self, $data, $key) = @_;
68
69     my $decrypted = Plagger::Crypt->decrypt($data->{$key});
70     if ($decrypted eq $data->{$key}) {
71         Plagger->context->add_rewrite_task($key, $decrypted, Plagger::Crypt->encrypt($decrypted, 'base64'));
72     } else {
73         $data->{$key} = $decrypted;
74     }
75 }
76
77 sub conf { $_[0]->{conf} }
78 sub rule { $_[0]->{rule} }
79
80 sub dispatch_rule_on {
81     my($self, $hook) = @_;
82     $self->rule_hook && $self->rule_hook eq $hook;
83 }
84
85 sub class_id {
86     my $self = shift;
87
88     my $pkg = ref($self) || $self;
89        $pkg =~ s/Plagger::Plugin:://;
90     my @pkg = split /::/, $pkg;
91
92     return join '-', @pkg;
93 }
94
95 # subclasses may overload to avoid cache sharing
96 sub plugin_id {
97     my $self = shift;
98     $self->class_id;
99 }
100
101 sub assets_dir {
102     my $self = shift;
103
104     my $context = Plagger->context;
105     my $assets_base = $context->conf->{assets_path} || File::Spec->catfile($FindBin::Bin, "assets");
106     return File::Spec->catfile(
107         $assets_base, "plugins", $self->class_id,
108     );
109 }
110
111 sub log {
112     my $self = shift;
113     Plagger->context->log(@_, caller => ref($self));
114 }
115
116 1;
Note: See TracBrowser for help on using the browser.