| 1 | package RPC::ExtDirect::API::Hook; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 27 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 27 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 27 | ▶ | no warnings 'uninitialized'; ## no critic | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 27 | ▶ | use B; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 27 | ▶ | use RPC::ExtDirect::Util::Accessor; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | ### PUBLIC CLASS METHOD (ACCESSOR) ### | ||||||||||||||||||||||||||||
| 12 | # | ||||||||||||||||||||||||||||
| 13 | # Return the list of supported hook types | ||||||||||||||||||||||||||||
| 14 | # | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 55 | ▶ | sub HOOK_TYPES { qw/ before instead after / } | ||||||||||||||||||||||||||
|
Subroutine
HOOK_TYPES: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | ### PUBLIC CLASS METHOD (CONSTRUCTOR) ### | ||||||||||||||||||||||||||||
| 19 | # | ||||||||||||||||||||||||||||
| 20 | # Instantiate a new Hook object | ||||||||||||||||||||||||||||
| 21 | # | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 136 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 24 | 136 | my ($class, %arg) = @_; | |||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 136 | my ($type, $coderef) = @arg{qw/ type code /}; | |||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | # If we're passed an undef or 'NONE' instead of a coderef, | ||||||||||||||||||||||||||||
| 29 | # then the hook is not runnable. Otherwise, try resolving | ||||||||||||||||||||||||||||
| 30 | # package if we have a coderef. | ||||||||||||||||||||||||||||
| 31 | 136 | ▶ | my $runnable = !('NONE' eq $coderef || !defined $coderef); | ||||||||||||||||||||||||||
|
Condition66%2/3
'NONE' eq $coderef || !defined($coderef)
MC/DC50%1/2
'NONE' eq $coderef || !defined($coderef)
'NONE' eq $coderef
!defined($coderef)
Truth table66%2/3
A || not(B)
A= 'NONE' eq $coderefB= !defined($coderef)
| |||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 136 | my ($package, $sub_name); | |||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 136 | ▶ | if ( 'CODE' eq ref $coderef ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 36 | 126 | $package = _package_from_coderef($coderef); | |||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||
| 38 | else { | ||||||||||||||||||||||||||||
| 39 | 10 | my @parts = split /::/, $coderef; | |||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 10 | $sub_name = pop @parts; | |||||||||||||||||||||||||||
| 42 | 10 | $package = join '::', @parts; | |||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | # We've got to have at least the sub_name part | ||||||||||||||||||||||||||||
| 45 | 10 | ▶ | die "Can't resolve '$type' hook $coderef" unless $sub_name; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 136 | my $self = bless { | |||||||||||||||||||||||||||
| 49 | package => $package, | ||||||||||||||||||||||||||||
| 50 | type => $type, | ||||||||||||||||||||||||||||
| 51 | code => $coderef, | ||||||||||||||||||||||||||||
| 52 | sub_name => $sub_name, | ||||||||||||||||||||||||||||
| 53 | runnable => $runnable, | ||||||||||||||||||||||||||||
| 54 | }, $class; | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 136 | return $self; | |||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | ### PUBLIC INSTANCE METHOD ### | ||||||||||||||||||||||||||||
| 60 | # | ||||||||||||||||||||||||||||
| 61 | # Run the hook | ||||||||||||||||||||||||||||
| 62 | # | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 55 | ▶ | sub run { | ||||||||||||||||||||||||||
|
Subroutine
run: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 65 | 55 | my ($self, %args) = @_; | |||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 55 | my $method_ref = $args{method_ref}; | |||||||||||||||||||||||||||
| 68 | 55 | my $action_name = $method_ref->action; | |||||||||||||||||||||||||||
| 69 | 55 | my $method_name = $method_ref->name; | |||||||||||||||||||||||||||
| 70 | 55 | my $method_pkg = $method_ref->package; | |||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 55 | my %hook_arg = $method_ref->get_api_definition_compat(); | |||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 55 | $hook_arg{method_ref} = $method_ref; | |||||||||||||||||||||||||||
| 75 | 55 | $hook_arg{code} = $method_ref->code; | |||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | @hook_arg{qw/arg env metadata aux_data/} | ||||||||||||||||||||||||||||
| 78 | 55 | = @args{qw/arg env metadata aux_data/}; | |||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | # Result and exception are passed to "after" hook only | ||||||||||||||||||||||||||||
| 81 | 55 | ▶ | if ( $self->type eq 'after' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 82 | @hook_arg{ qw/result exception method_called/ } | ||||||||||||||||||||||||||||
| 83 | 26 | = @args{ qw/result exception callee/ } | |||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 55 | for my $type ( $self->HOOK_TYPES ) { | |||||||||||||||||||||||||||
| 87 | my $hook = $args{api}->get_hook( | ||||||||||||||||||||||||||||
| 88 | 165 | action => $action_name, | |||||||||||||||||||||||||||
| 89 | method => $method_name, | ||||||||||||||||||||||||||||
| 90 | type => $type, | ||||||||||||||||||||||||||||
| 91 | ); | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 165 | $hook_arg{ $type.'_ref' } = $hook; | |||||||||||||||||||||||||||
| 94 | 165 | ▶ | $hook_arg{ $type } = $hook ? $hook->code : undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 55 | my $arg = $args{arg}; | |||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | # A drop of sugar | ||||||||||||||||||||||||||||
| 100 | 55 | ▶ | $hook_arg{orig} = sub { $method_pkg->$method_name(@$arg) }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 55 | my $hook_coderef = $self->code; | |||||||||||||||||||||||||||
| 103 | 55 | my $hook_sub_name = $self->sub_name; | |||||||||||||||||||||||||||
| 104 | 55 | my $hook_pkg = $self->package; | |||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | # By convention, hooks are called as class methods. If we were passed | ||||||||||||||||||||||||||||
| 107 | # a method name instead of a coderef, call it indirectly on the package | ||||||||||||||||||||||||||||
| 108 | # so that inheritance works properly | ||||||||||||||||||||||||||||
| 109 | 55 | ▶ | return $hook_pkg && $hook_sub_name ? $hook_pkg->$hook_sub_name(%hook_arg) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$hook_pkg and $hook_sub_name
MC/DC50%1/2
$hook_pkg and $hook_sub_name
$hook_pkg
$hook_sub_name
Truth table66%2/3
A and B
A= $hook_pkgB= $hook_sub_name
| |||||||||||||||||||||||||||||
| 110 | : $hook_coderef->($hook_pkg, %hook_arg) | ||||||||||||||||||||||||||||
| 111 | ; | ||||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | ### PUBLIC INSTANCE METHODS ### | ||||||||||||||||||||||||||||
| 115 | # | ||||||||||||||||||||||||||||
| 116 | # Simple read-write accessors | ||||||||||||||||||||||||||||
| 117 | # | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | 27 | RPC::ExtDirect::Util::Accessor::mk_accessors( | |||||||||||||||||||||||||||
| 120 | simple => [qw/ type code package sub_name runnable /], | ||||||||||||||||||||||||||||
| 121 | ); | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | ############## PRIVATE METHODS BELOW ############## | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | ### PRIVATE PACKAGE SUBROUTINE ### | ||||||||||||||||||||||||||||
| 126 | # | ||||||||||||||||||||||||||||
| 127 | # Return package name from coderef | ||||||||||||||||||||||||||||
| 128 | # | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | 126 | ▶ | sub _package_from_coderef { | ||||||||||||||||||||||||||
|
Subroutine
_package_from_coderef: called CC=3 SCAR=12.0
| |||||||||||||||||||||||||||||
| 131 | 126 | my ($code) = @_; | |||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | 126 | my $pkg = eval { B::svref_2object($code)->GV->STASH->NAME }; | |||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | 126 | ▶ | return defined $pkg && $pkg ne '' ? $pkg : undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $pkg and $pkg ne ""
MC/DC0%0/2
defined $pkg and $pkg ne ""
defined $pkg
$pkg ne ""
Truth table33%1/3
A and B
A= defined $pkgB= $pkg ne ""
| |||||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | 27 | 1; | |||||||||||||||||||||||||||