| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Weixin::Client::Plugin; | 
| 2 |  |  |  |  |  |  | sub new{ | 
| 3 | 0 |  |  | 0 | 0 |  | bless { | 
| 4 |  |  |  |  |  |  | plugin_num          => 0, | 
| 5 |  |  |  |  |  |  | plugins             =>  {}, | 
| 6 |  |  |  |  |  |  | }; | 
| 7 |  |  |  |  |  |  | } | 
| 8 |  |  |  |  |  |  |  | 
| 9 |  |  |  |  |  |  | sub load { | 
| 10 | 0 |  |  | 0 | 0 |  | my $self = shift; | 
| 11 | 0 |  |  |  |  |  | my @module_name = @_; | 
| 12 | 0 |  |  |  |  |  | for my $module_name (@module_name){ | 
| 13 | 0 |  |  |  |  |  | my $module_function = undef; | 
| 14 | 0 | 0 |  |  |  |  | if(substr($module_name,0,1) eq '+'){ | 
| 15 | 0 |  |  |  |  |  | substr($module_name,0,1) = ""; | 
| 16 | 0 |  |  |  |  |  | $module = $module_name; | 
| 17 |  |  |  |  |  |  | } | 
| 18 |  |  |  |  |  |  | else{ | 
| 19 | 0 |  |  |  |  |  | $module = "Weixin::Client::Plugin::" . $module_name; | 
| 20 |  |  |  |  |  |  | } | 
| 21 | 0 |  |  |  |  |  | eval "require $module"; | 
| 22 | 0 | 0 |  |  |  |  | die "加载插件[ $module ]失败: $@\n" if $@; | 
| 23 | 0 |  |  |  |  |  | $module_function = *{"${module}::call"}{CODE}; | 
|  | 0 |  |  |  |  |  |  | 
| 24 | 0 | 0 |  |  |  |  | die "加载插件[ $module ]失败: 未获取到call函数引用\n" if ref $module_function ne 'CODE'; | 
| 25 | 0 |  |  |  |  |  | $self->{plugin_num}++; | 
| 26 | 0 |  |  |  |  |  | $self->{plugins}{$module_name} = { | 
| 27 |  |  |  |  |  |  | id=>$self->{plugin_num}, | 
| 28 |  |  |  |  |  |  | code=>$module_function, | 
| 29 |  |  |  |  |  |  | }; | 
| 30 |  |  |  |  |  |  | } | 
| 31 |  |  |  |  |  |  | } | 
| 32 |  |  |  |  |  |  |  | 
| 33 |  |  |  |  |  |  | sub call_all{ | 
| 34 | 0 |  |  | 0 | 0 |  | my $self = shift; | 
| 35 | 0 |  |  |  |  |  | for(sort {$self->{plugins}{$a}{id}<=>$self->{plugins}{$b}{id}} keys  %{$self->{plugins}}){ | 
|  | 0 |  |  |  |  |  |  | 
|  | 0 |  |  |  |  |  |  | 
| 36 | 0 |  |  |  |  |  | &{$self->{plugins}{$_}{code}}($self,@_); | 
|  | 0 |  |  |  |  |  |  | 
| 37 |  |  |  |  |  |  | } | 
| 38 |  |  |  |  |  |  | } | 
| 39 |  |  |  |  |  |  |  | 
| 40 |  |  |  |  |  |  | sub call{ | 
| 41 | 0 |  |  | 0 | 0 |  | my $self = shift; | 
| 42 | 0 |  |  |  |  |  | my @plugins; | 
| 43 | 0 | 0 |  |  |  |  | if(ref $_[0] eq 'ARRAY'){ | 
| 44 | 0 |  |  |  |  |  | @plugins = @{$_[0]}; | 
|  | 0 |  |  |  |  |  |  | 
| 45 | 0 |  |  |  |  |  | shift; | 
| 46 |  |  |  |  |  |  | } | 
| 47 |  |  |  |  |  |  | else{ | 
| 48 | 0 |  |  |  |  |  | push @plugins,$_[0]; | 
| 49 | 0 |  |  |  |  |  | shift; | 
| 50 |  |  |  |  |  |  | } | 
| 51 |  |  |  |  |  |  |  | 
| 52 | 0 |  |  |  |  |  | for(@plugins){ | 
| 53 | 0 | 0 |  |  |  |  | if(exists $self->{plugins}{$_}){ | 
| 54 | 0 |  |  |  |  |  | eval { | 
| 55 | 0 |  |  |  |  |  | &{$self->{plugins}{$_}{code}}($self,@_); | 
|  | 0 |  |  |  |  |  |  | 
| 56 |  |  |  |  |  |  | }; | 
| 57 | 0 | 0 |  |  |  |  | print $@,"\n" if $@; | 
| 58 |  |  |  |  |  |  | } | 
| 59 |  |  |  |  |  |  | else{ | 
| 60 | 0 |  |  |  |  |  | die "运行插件[ $_ ]失败:找不到该插件\n"; | 
| 61 |  |  |  |  |  |  | } | 
| 62 |  |  |  |  |  |  | } | 
| 63 |  |  |  |  |  |  | } | 
| 64 |  |  |  |  |  |  |  | 
| 65 |  |  |  |  |  |  | sub plugin{ | 
| 66 | 0 |  |  | 0 | 0 |  | my $self = shift; | 
| 67 | 0 |  |  |  |  |  | my $plugin = shift; | 
| 68 | 0 | 0 |  |  |  |  | if(exists $self->{plugins}{$plugin}){ | 
| 69 | 0 |  |  |  |  |  | return $self->{plugins}{$plugin}{code}; | 
| 70 |  |  |  |  |  |  | } | 
| 71 |  |  |  |  |  |  | else{ | 
| 72 | 0 |  |  |  |  |  | die "查找插件[ $_ ]失败:找不到该插件\n"; | 
| 73 |  |  |  |  |  |  | } | 
| 74 |  |  |  |  |  |  | } | 
| 75 |  |  |  |  |  |  |  | 
| 76 |  |  |  |  |  |  | sub clear { | 
| 77 | 0 |  |  | 0 | 0 |  | my $self = shift; | 
| 78 | 0 |  |  |  |  |  | $self->{plugins} = []; | 
| 79 |  |  |  |  |  |  | } | 
| 80 |  |  |  |  |  |  | 1; |