line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Mojo::Weixin::Plugin; |
2
|
|
|
|
|
|
|
sub load { |
3
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
4
|
0
|
|
|
|
|
|
my @module_name; |
5
|
|
|
|
|
|
|
my %opt; |
6
|
0
|
0
|
|
|
|
|
if(ref $_[0] eq "ARRAY"){ |
7
|
0
|
|
|
|
|
|
@module_name = @{shift @_}; |
|
0
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
} |
9
|
|
|
|
|
|
|
else{ |
10
|
0
|
|
|
|
|
|
push @module_name,shift; |
11
|
|
|
|
|
|
|
} |
12
|
0
|
|
|
|
|
|
%opt= @_; |
13
|
|
|
|
|
|
|
|
14
|
0
|
|
|
|
|
|
for my $module_name (@module_name){ |
15
|
0
|
|
|
|
|
|
my $module_function = undef; |
16
|
|
|
|
|
|
|
# 有+号的是绝对命名空间,没有+号的是相对命名空间。 step5 插件的设计结构 |
17
|
0
|
|
|
|
|
|
my $module; |
18
|
0
|
0
|
|
|
|
|
if(substr($module_name,0,1) eq '+'){ |
19
|
0
|
|
|
|
|
|
substr($module_name,0,1) = ""; |
20
|
0
|
|
|
|
|
|
$module = $module_name; |
21
|
|
|
|
|
|
|
} |
22
|
|
|
|
|
|
|
else{ |
23
|
0
|
|
|
|
|
|
$module = __PACKAGE__ . "::" . $module_name; |
24
|
|
|
|
|
|
|
} |
25
|
0
|
|
|
|
|
|
eval "require $module"; |
26
|
0
|
0
|
|
|
|
|
$self->die("加载插件[ $module ]失败: $@\n") if $@; |
27
|
0
|
|
|
|
|
|
$module_function = *{"${module}::call"}{CODE}; |
|
0
|
|
|
|
|
|
|
28
|
0
|
0
|
|
|
|
|
$self->die("加载插件[ $module ]失败: 未获取到call函数引用\n") if ref $module_function ne 'CODE'; |
29
|
0
|
|
|
|
|
|
$self->debug("加载插件[ $module ]"); |
30
|
0
|
|
|
|
|
|
$self->plugins->{$module}{code} = $module_function; |
31
|
0
|
|
|
|
|
|
$self->plugins->{$module}{name} = $module; |
32
|
0
|
|
|
|
|
|
$self->plugins->{$module}{data} = $opt{data}; |
33
|
0
|
|
0
|
|
|
|
$self->plugins->{$module}{priority} = $opt{priority} || eval "\$${module}::PRIORITY" || 0; |
34
|
0
|
|
0
|
|
|
|
$self->plugins->{$module}{call_on_load} = $opt{call_on_load} || eval "\$${module}::CALL_ON_LOAD" || 0; |
35
|
0
|
0
|
|
|
|
|
if($self->plugins->{$module}{call_on_load}){ |
36
|
0
|
|
|
|
|
|
$self->emit("plugin_load",$module); |
37
|
0
|
|
|
|
|
|
$self->call($module); |
38
|
|
|
|
|
|
|
} |
39
|
|
|
|
|
|
|
else{ |
40
|
0
|
|
0
|
|
|
|
$self->plugins->{$module}{auto_call} = $opt{auto_call} || eval "\$${module}::AUTO_CALL" || 1 ; |
41
|
0
|
|
|
|
|
|
$self->emit("plugin_load",$module); |
42
|
|
|
|
|
|
|
} |
43
|
|
|
|
|
|
|
} |
44
|
0
|
|
|
|
|
|
return $self; |
45
|
|
|
|
|
|
|
} |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
sub call{ |
48
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
49
|
0
|
|
|
|
|
|
my @plugins; |
50
|
0
|
0
|
|
|
|
|
if(ref $_[0] eq 'ARRAY'){ |
51
|
0
|
|
|
|
|
|
@plugins = @{$_[0]}; |
|
0
|
|
|
|
|
|
|
52
|
0
|
|
|
|
|
|
shift; |
53
|
|
|
|
|
|
|
} |
54
|
|
|
|
|
|
|
else{ |
55
|
0
|
|
|
|
|
|
push @plugins,$_[0]; |
56
|
0
|
|
|
|
|
|
shift; |
57
|
|
|
|
|
|
|
} |
58
|
0
|
|
|
|
|
|
for(sort {$self->plugins->{$b}{priority} <=> $self->plugins->{$a}{priority}} @plugins){ |
|
0
|
|
|
|
|
|
|
59
|
0
|
0
|
|
|
|
|
if(exists $self->plugins->{$_}){ |
60
|
0
|
|
|
|
|
|
$self->info("执行插件[ $_ ]"); |
61
|
0
|
|
|
|
|
|
eval { |
62
|
0
|
|
|
|
|
|
&{$self->plugins->{$_}{code}}($self,$self->plugins->{$_}{data},@_); |
|
0
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
}; |
64
|
0
|
0
|
|
|
|
|
if($@){ |
65
|
0
|
|
|
|
|
|
$self->error("插件[ $_ ]执行错误: $@"); |
66
|
0
|
0
|
|
|
|
|
if($@ =~ /Can't create listen socket: Address already in use/){ |
67
|
0
|
|
|
|
|
|
$self->stop(); |
68
|
|
|
|
|
|
|
} |
69
|
0
|
|
|
|
|
|
next; |
70
|
|
|
|
|
|
|
} |
71
|
0
|
|
|
|
|
|
$self->emit("plugin_call",$_); |
72
|
|
|
|
|
|
|
} |
73
|
|
|
|
|
|
|
else{ |
74
|
0
|
|
|
|
|
|
$self->error("运行插件[ $_ ]失败:找不到该插件"); |
75
|
|
|
|
|
|
|
} |
76
|
|
|
|
|
|
|
} |
77
|
0
|
|
|
|
|
|
return $self; |
78
|
|
|
|
|
|
|
} |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
1; |