Webqq/Plugin.pm untested

filter: Statementstmt 0.0% 0 / 56 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 15 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 0.0% 0 / 2 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 99 CC 10 SCAR 36.0
CC
10
Cov
0%
load
42.0
call
30.0
CRAP 36.5
1 package Mojo::Webqq::Plugin;
2 sub load {
3 my $self = shift;
4 my @module_name;
5 my %opt;
6 if(ref $_[0] eq "ARRAY"){
7 @module_name = @{shift @_};
8 }
9 else{
10 push @module_name,shift;
11 }
12 %opt= @_;
13
14 for my $module_name (@module_name){
15 my $module_function = undef;
16 my $module;
17 if(substr($module_name,0,1) eq '+'){
18 substr($module_name,0,1) = "";
19 $module = $module_name;
20 }
21 else{
22 $module = "Mojo::Webqq::Plugin::" . $module_name;
23 }
24 eval "require $module";
25 $self->die("加载插件[ $module ]失败: $@\n") if $@;
26 $module_function = *{"${module}::call"}{CODE};
27 $self->die("加载插件[ $module ]失败: 未获取到call函数引用\n") if ref $module_function ne 'CODE';
28 $self->debug("加载插件[ $module ]");
29 $self->plugins->{$module}{code} = $module_function;
30 $self->plugins->{$module}{name} = $module;
31 $self->plugins->{$module}{data} = $opt{data};
32 $self->plugins->{$module}{priority} = $opt{priority} || eval "\$${module}::PRIORITY" || 0;
33 $self->plugins->{$module}{call_on_load} = $opt{call_on_load} || eval "\$${module}::CALL_ON_LOAD" || 0;
34 if($self->plugins->{$module}{call_on_load}){
35 $self->emit("plugin_load",$module);
36 $self->call($module);
37 }
38 else{
39 $self->plugins->{$module}{auto_call} = $opt{auto_call} || eval "\$${module}::AUTO_CALL" || 1 ;
40 $self->emit("plugin_load",$module);
41 }
42 }
43 return $self;
44 }
45  
46 sub call{
47 my $self = shift;
48 my @plugins;
49 if(ref $_[0] eq 'ARRAY'){
50 @plugins = @{$_[0]};
51 shift;
52 }
53 else{
54 push @plugins,$_[0];
55 shift;
56 }
57 for(sort {$self->plugins->{$b}{priority} <=> $self->plugins->{$a}{priority}} @plugins){
58 if(exists $self->plugins->{$_}){
59 $self->info("执行插件[ $_ ]");
60 eval {
61 &{$self->plugins->{$_}{code}}($self,$self->plugins->{$_}{data},@_);
62 };
63 if($@){
64 $self->error("插件[ $_ ]执行错误: $@");
65 if($@ =~ /Can't create listen socket: Address already in use/){
66 $self->stop();
67 }
68 next;
69 }
70 $self->emit("plugin_call",$_);
71 }
72 else{
73 $self->error("运行插件[ $_ ]失败:找不到该插件");
74 }
75 }
76 return $self;
77 }
78  
79 1;