| 1 | package Devel::Agent::Proxy; | ||||||||||||||||||
| 2 | |||||||||||||||||||
| 3 | =head1 NAME | ||||||||||||||||||
| 4 | |||||||||||||||||||
| 5 | Devel::Agent::Proxy - instance proxy layer | ||||||||||||||||||
| 6 | |||||||||||||||||||
| 7 | =head1 SYNOPSIS | ||||||||||||||||||
| 8 | |||||||||||||||||||
| 9 | use Devel::Agent::Proxy; | ||||||||||||||||||
| 10 | |||||||||||||||||||
| 11 | my $self=new SomeClass(...); | ||||||||||||||||||
| 12 | my $proxy=new Devel::Agent::Proxy( | ||||||||||||||||||
| 13 | proxy_class_name=>'SomeClass', | ||||||||||||||||||
| 14 | proxied_object=>$self, | ||||||||||||||||||
| 15 | ); | ||||||||||||||||||
| 16 | |||||||||||||||||||
| 17 | =head1 DESCRIPTION | ||||||||||||||||||
| 18 | |||||||||||||||||||
| 19 | Devel::Agent::Proxy | ||||||||||||||||||
| 20 | |||||||||||||||||||
| 21 | This class was created to act as an agent aware wrapper for classes that internals need to be kept of the radar of the agent debugger. | ||||||||||||||||||
| 22 | |||||||||||||||||||
| 23 | =cut | ||||||||||||||||||
| 24 | |||||||||||||||||||
| 25 | # would love to use Moo, but artifacts are bad! | ||||||||||||||||||
| 26 | 0 | ▶ | use strict; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.4
| |||||||||||||||||||
| 27 | 0 | ▶ | use warnings; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.4
| |||||||||||||||||||
| 28 | 1 | require Carp; | |||||||||||||||||
| 29 | 1 | require Devel::Agent; | |||||||||||||||||
| 30 | 1 | our $VERSION=$Devel::Agent::VERSION; | |||||||||||||||||
| 31 | |||||||||||||||||||
| 32 | # COMMENT THIS OUT AFTER TESTING!! | ||||||||||||||||||
| 33 | #use Data::Dumper; | ||||||||||||||||||
| 34 | |||||||||||||||||||
| 35 | 0 | our $AUTOLOAD; | |||||||||||||||||
| 36 | |||||||||||||||||||
| 37 | 0 | our %BUILD_ARGS; | |||||||||||||||||
| 38 | |||||||||||||||||||
| 39 | 6 | ▶ | my $has=sub { | ||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.0
| |||||||||||||||||||
| 40 | 6 | my ($method,%args)=@_; | |||||||||||||||||
| 41 | |||||||||||||||||||
| 42 | 6 | my $ref=ref $args{default}; | |||||||||||||||||
| 43 | 6 | $BUILD_ARGS{$method}=\%args; | |||||||||||||||||
| 44 | 6 | ▶ | unless($ref) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 45 | 4 | my $default=$args{default}; | |||||||||||||||||
| 46 | 4 | ▶ | $args{default}=sub { $default }; | ||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 47 | } | ||||||||||||||||||
| 48 | 38 | ▶ | my $sub=sub { | ||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||
| 49 | 38 | my $self=shift; | |||||||||||||||||
| 50 | 38 | ▶ | if($#_==-1) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 51 | 24 | ▶ | if(exists $self->{$method}) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 52 | 0 | return $self->{$method}; | |||||||||||||||||
| 53 | } else { | ||||||||||||||||||
| 54 | 5 | my $def=$args{default}; | |||||||||||||||||
| 55 | 5 | my $value=$self->{$method}=$self->$def(); | |||||||||||||||||
| 56 | 5 | return $value; | |||||||||||||||||
| 57 | } | ||||||||||||||||||
| 58 | } else { | ||||||||||||||||||
| 59 | 14 | return $self->{$method}=$_[0]; | |||||||||||||||||
| 60 | } | ||||||||||||||||||
| 61 | 6 | }; | |||||||||||||||||
| 62 | 6 | my $method_name=__PACKAGE__."::___$method"; | |||||||||||||||||
| 63 | 0 | ▶ | no strict 'refs'; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.4
| |||||||||||||||||||
| 64 | 0 | *{$method_name}=$sub; | |||||||||||||||||
| 65 | 1 | }; | |||||||||||||||||
| 66 | |||||||||||||||||||
| 67 | =head1 Constructor/Method Accssor | ||||||||||||||||||
| 68 | |||||||||||||||||||
| 69 | This section documents the constructor methods and class accessors. All constructor arguments are passed by thier name, all class accessors are prefixed with___. For Example wrap_result_methods becomes $self->___wrap_result_methods in an accessor context. | ||||||||||||||||||
| 70 | |||||||||||||||||||
| 71 | =over 4 | ||||||||||||||||||
| 72 | |||||||||||||||||||
| 73 | =item * wrap_result_methods=>HashRef[CodeRef] | ||||||||||||||||||
| 74 | |||||||||||||||||||
| 75 | This is a hashref of code refs that can be used to post process the results of a proxied method. | ||||||||||||||||||
| 76 | |||||||||||||||||||
| 77 | =cut | ||||||||||||||||||
| 78 | |||||||||||||||||||
| 79 | $has->(wrap_result_methods=>( | ||||||||||||||||||
| 80 | #isa=>HashRef[CodeRef], | ||||||||||||||||||
| 81 | is=>'ro', | ||||||||||||||||||
| 82 | lazy=>1, | ||||||||||||||||||
| 83 | 2 | ▶ | default=>sub { | ||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 84 | 2 | return {}; | |||||||||||||||||
| 85 | }, | ||||||||||||||||||
| 86 | 1 | )); | |||||||||||||||||
| 87 | |||||||||||||||||||
| 88 | =item * proxy_class_name=>Str | ||||||||||||||||||
| 89 | |||||||||||||||||||
| 90 | This should be the name of the class we want the class_method and raw_method to appear as in the agent stack trace. | ||||||||||||||||||
| 91 | |||||||||||||||||||
| 92 | =cut | ||||||||||||||||||
| 93 | |||||||||||||||||||
| 94 | 1 | $has->(proxy_class_name=>( | |||||||||||||||||
| 95 | #isa=>Str, | ||||||||||||||||||
| 96 | is=>'ro', | ||||||||||||||||||
| 97 | required=>1, | ||||||||||||||||||
| 98 | )); | ||||||||||||||||||
| 99 | |||||||||||||||||||
| 100 | =item * replace_name=>Str | ||||||||||||||||||
| 101 | |||||||||||||||||||
| 102 | This is autogenerated as needed, it will be set to $self->___proxy_class_name.'::' on first access. | ||||||||||||||||||
| 103 | |||||||||||||||||||
| 104 | =cut | ||||||||||||||||||
| 105 | |||||||||||||||||||
| 106 | $has->(replace_name=>( | ||||||||||||||||||
| 107 | #isa=>Str, | ||||||||||||||||||
| 108 | is=>'rw', | ||||||||||||||||||
| 109 | lazy=>1, | ||||||||||||||||||
| 110 | 1 | ▶ | default=>sub { | ||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 111 | 1 | my ($self)=@_; | |||||||||||||||||
| 112 | 1 | return $self->___proxy_class_name.'::'; | |||||||||||||||||
| 113 | }, | ||||||||||||||||||
| 114 | 1 | )); | |||||||||||||||||
| 115 | |||||||||||||||||||
| 116 | =item * proxied_object=>Object | ||||||||||||||||||
| 117 | |||||||||||||||||||
| 118 | This is the object to be proxied. | ||||||||||||||||||
| 119 | |||||||||||||||||||
| 120 | =cut | ||||||||||||||||||
| 121 | |||||||||||||||||||
| 122 | 1 | $has->(proxied_object=>( | |||||||||||||||||
| 123 | #isa=>Object, | ||||||||||||||||||
| 124 | required=>1, | ||||||||||||||||||
| 125 | is=>'ro', | ||||||||||||||||||
| 126 | )); | ||||||||||||||||||
| 127 | |||||||||||||||||||
| 128 | =item * current_method=>Str | ||||||||||||||||||
| 129 | |||||||||||||||||||
| 130 | This represents the current method being proxied and is set dynamically at runtime. | ||||||||||||||||||
| 131 | |||||||||||||||||||
| 132 | =cut | ||||||||||||||||||
| 133 | |||||||||||||||||||
| 134 | 1 | $has->(current_method=>( | |||||||||||||||||
| 135 | is=>'rw', | ||||||||||||||||||
| 136 | required=>1, | ||||||||||||||||||
| 137 | )); | ||||||||||||||||||
| 138 | |||||||||||||||||||
| 139 | =item * in_can=>0 | ||||||||||||||||||
| 140 | |||||||||||||||||||
| 141 | Used by the internals of this object for state. | ||||||||||||||||||
| 142 | |||||||||||||||||||
| 143 | =cut | ||||||||||||||||||
| 144 | |||||||||||||||||||
| 145 | 1 | $has->(in_can=>( | |||||||||||||||||
| 146 | is=>'rw', | ||||||||||||||||||
| 147 | default=>0, | ||||||||||||||||||
| 148 | lazy=>1, | ||||||||||||||||||
| 149 | )); | ||||||||||||||||||
| 150 | |||||||||||||||||||
| 151 | 1 | undef $has; | |||||||||||||||||
| 152 | |||||||||||||||||||
| 153 | 2 | ▶ | sub new { | ||||||||||||||||
|
Subroutine
new: called CC=5 SCAR=18.1
Pod
undocumented
| |||||||||||||||||||
| 154 | 2 | my ($class,%args)=@_; | |||||||||||||||||
| 155 | 2 | my $self=bless {},$class; | |||||||||||||||||
| 156 | 2 | ▶ | while(my ($key,$args)=each %BUILD_ARGS) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 157 | |||||||||||||||||||
| 158 | 12 | ▶ | if(exists $args{$key}) { | ||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||
| 159 | 0 | $self->{$key}=$args{$key}; | |||||||||||||||||
| 160 | } elsif($args->{required}) { | ||||||||||||||||||
| 161 | 0 | &Carp::croak("$key is a required argument"); | |||||||||||||||||
| 162 | } elsif(!$args->{lazy}) { | ||||||||||||||||||
| 163 | 0 | my $cb=$args->{default}; | |||||||||||||||||
| 164 | 0 | $self->{$key}=$self->$cb(); | |||||||||||||||||
| 165 | } | ||||||||||||||||||
| 166 | } | ||||||||||||||||||
| 167 | 2 | return $self; | |||||||||||||||||
| 168 | } | ||||||||||||||||||
| 169 | |||||||||||||||||||
| 170 | =back | ||||||||||||||||||
| 171 | |||||||||||||||||||
| 172 | =head1 Object Methods | ||||||||||||||||||
| 173 | |||||||||||||||||||
| 174 | This section documents the Methods created in this class. | ||||||||||||||||||
| 175 | |||||||||||||||||||
| 176 | =head2 $self->___db_stack_filter($agent,$frame,$args,$raw_caller) | ||||||||||||||||||
| 177 | |||||||||||||||||||
| 178 | This method is used by some plugin classes for Deve::Agent to handle frame alterations. Basicaly this method is used to re-write a frame in tracing to pevent this object from showing up as iteself and appear as the object it is proxying. | ||||||||||||||||||
| 179 | |||||||||||||||||||
| 180 | =cut | ||||||||||||||||||
| 181 | |||||||||||||||||||
| 182 | 2 | ▶ | sub ___db_stack_filter { | ||||||||||||||||
|
Subroutine
___db_stack_filter: called CC=3 SCAR=11.0
| |||||||||||||||||||
| 183 | 2 | my ($self,$agent,$frame,$args,$raw_caller)=@_; | |||||||||||||||||
| 184 | 2 | ▶ | return 1 unless ref $self; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 185 | |||||||||||||||||||
| 186 | # hide all the calls we make | ||||||||||||||||||
| 187 | 2 | ▶ | return 0 if $frame->{caller_class} eq __PACKAGE__; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 188 | 1 | my $replace=$self->___replace_name; | |||||||||||||||||
| 189 | 1 | $frame->{class_method}=~ s/^(.*)::/$replace/s; | |||||||||||||||||
| 190 | 1 | $frame->{raw_method}=~ s/^(.*)::/$replace/s; | |||||||||||||||||
| 191 | 1 | return 1; | |||||||||||||||||
| 192 | } | ||||||||||||||||||
| 193 | |||||||||||||||||||
| 194 | =head2 my $cb=$self->can('method') | ||||||||||||||||||
| 195 | |||||||||||||||||||
| 196 | Overload of UNIVERSAL->can method | ||||||||||||||||||
| 197 | |||||||||||||||||||
| 198 | =cut | ||||||||||||||||||
| 199 | |||||||||||||||||||
| 200 | 5 | ▶ | sub can { | ||||||||||||||||
|
Subroutine
can: called CC=2 SCAR=7.8
Pod
documented
| |||||||||||||||||||
| 201 | 5 | my ($self,$method)=@_; | |||||||||||||||||
| 202 | 5 | my $p=$self->___proxied_object; | |||||||||||||||||
| 203 | |||||||||||||||||||
| 204 | 5 | my $cb=$p->can($method); | |||||||||||||||||
| 205 | 5 | ▶ | return $cb unless $cb; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 206 | 5 | $self->___current_method($method); | |||||||||||||||||
| 207 | |||||||||||||||||||
| 208 | 0 | ▶ | return sub { | ||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||
| 209 | 0 | $self->___in_can(1); | |||||||||||||||||
| 210 | 0 | $AUTOLOAD=$method; | |||||||||||||||||
| 211 | 0 | $self->$method(@_); | |||||||||||||||||
| 212 | 5 | }; | |||||||||||||||||
| 213 | } | ||||||||||||||||||
| 214 | |||||||||||||||||||
| 215 | =head2 if($self->isa("class")) { ... } | ||||||||||||||||||
| 216 | |||||||||||||||||||
| 217 | Overload of UNIVERSAL->isa("class") | ||||||||||||||||||
| 218 | |||||||||||||||||||
| 219 | =cut | ||||||||||||||||||
| 220 | |||||||||||||||||||
| 221 | 2 | ▶ | sub isa { | ||||||||||||||||
|
Subroutine
isa: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||
| 222 | 2 | my ($self,$class)=@_; | |||||||||||||||||
| 223 | 2 | return $self->___proxied_object->isa($class); | |||||||||||||||||
| 224 | } | ||||||||||||||||||
| 225 | |||||||||||||||||||
| 226 | =head2 if($self->DOES($object|$class)) { ... } | ||||||||||||||||||
| 227 | |||||||||||||||||||
| 228 | Overload of UNIVERSAL->DOES($object|$class) | ||||||||||||||||||
| 229 | |||||||||||||||||||
| 230 | =cut | ||||||||||||||||||
| 231 | |||||||||||||||||||
| 232 | 0 | ▶ | sub DOES { | ||||||||||||||||
|
Subroutine
DOES: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||
| 233 | 0 | my ($self,$role)=@_; | |||||||||||||||||
| 234 | 0 | return $self->___proxied_object->DOES($role); | |||||||||||||||||
| 235 | } | ||||||||||||||||||
| 236 | |||||||||||||||||||
| 237 | =head2 Maybe[Any]=$self->AUTOLOAD(@args) | ||||||||||||||||||
| 238 | |||||||||||||||||||
| 239 | The guts of how this proxy class works, basically it auto loads methods to look like the object class it is proxying. | ||||||||||||||||||
| 240 | |||||||||||||||||||
| 241 | =cut | ||||||||||||||||||
| 242 | |||||||||||||||||||
| 243 | 3 | ▶ | sub AUTOLOAD { | ||||||||||||||||
|
Subroutine
AUTOLOAD: called CC=4 SCAR=16.1
| |||||||||||||||||||
| 244 | # do this so we can pass @_ to our target function | ||||||||||||||||||
| 245 | 3 | my $self=shift; | |||||||||||||||||
| 246 | 3 | my $method=$AUTOLOAD; | |||||||||||||||||
| 247 | 3 | $method=~ s/^.*:://s; | |||||||||||||||||
| 248 | 3 | $self->___current_method($method); | |||||||||||||||||
| 249 | 3 | my $p=$self->___proxied_object; | |||||||||||||||||
| 250 | |||||||||||||||||||
| 251 | 3 | my $cb; | |||||||||||||||||
| 252 | 3 | ▶ | if($self->___in_can) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 253 | 0 | return $self->___exec_method($self->__in_can,@_); | |||||||||||||||||
| 254 | 3 | ▶ | } if($cb=$p->can($method)) { | ||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||
| 255 | 3 | $self->___current_method($method); | |||||||||||||||||
| 256 | 3 | return $self->___exec_method($cb,@_); | |||||||||||||||||
| 257 | } elsif($cb=$p->can('AUTOLOAD')) { | ||||||||||||||||||
| 258 | 0 | $self->___current_method('AUTOLOAD'); | |||||||||||||||||
| 259 | 0 | return $self->___exec_method($cb,@_); | |||||||||||||||||
| 260 | } | ||||||||||||||||||
| 261 | |||||||||||||||||||
| 262 | 0 | Carp::croak(sprintf q{Can't locate object method "%s" via package "%s"},$method,$self->___proxy_class_name); | |||||||||||||||||
| 263 | } | ||||||||||||||||||
| 264 | |||||||||||||||||||
| 265 | =head2 Maybe[Any]=$self->___exec_method($cb) | ||||||||||||||||||
| 266 | |||||||||||||||||||
| 267 | Handles wrapper logic for a given code reference. | ||||||||||||||||||
| 268 | |||||||||||||||||||
| 269 | =cut | ||||||||||||||||||
| 270 | |||||||||||||||||||
| 271 | 3 | ▶ | sub ___exec_method { | ||||||||||||||||
|
Subroutine
___exec_method: called CC=4 SCAR=14.0
| |||||||||||||||||||
| 272 | 3 | my $self=shift; | |||||||||||||||||
| 273 | 3 | my $cb=shift; | |||||||||||||||||
| 274 | 3 | my $wrap=$self->___wrap_result_methods; | |||||||||||||||||
| 275 | 3 | my $method=$self->___current_method; | |||||||||||||||||
| 276 | 3 | $self->___in_can(0); | |||||||||||||||||
| 277 | 3 | my $p=$self->___proxied_object; | |||||||||||||||||
| 278 | 3 | local $@; | |||||||||||||||||
| 279 | 3 | ▶ | if(wantarray) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 280 | 1 | my @res; | |||||||||||||||||
| 281 | 1 | ▶ | if(exists $wrap->{$method}) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 282 | 0 | @res=$wrap->{$method}->($self,1,$cb); | |||||||||||||||||
| 283 | } else { | ||||||||||||||||||
| 284 | 1 | @res=$p->$cb(@_); | |||||||||||||||||
| 285 | } | ||||||||||||||||||
| 286 | 1 | return @res; | |||||||||||||||||
| 287 | } else { | ||||||||||||||||||
| 288 | 2 | my $res; | |||||||||||||||||
| 289 | 2 | ▶ | if(exists $wrap->{$method}) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 290 | 0 | $res=$wrap->{$method}->($self,0,$cb); | |||||||||||||||||
| 291 | } else { | ||||||||||||||||||
| 292 | 2 | $res=$p->$cb(@_); | |||||||||||||||||
| 293 | } | ||||||||||||||||||
| 294 | 2 | return $res; | |||||||||||||||||
| 295 | } | ||||||||||||||||||
| 296 | } | ||||||||||||||||||
| 297 | |||||||||||||||||||
| 298 | # manditory in the case of auto load!! | ||||||||||||||||||
| 299 | 2 | ▶ | sub DESTROY { | ||||||||||||||||
|
Subroutine
DESTROY: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 300 | 2 | my ($self)=@_; | |||||||||||||||||
| 301 | 2 | %$self=(); | |||||||||||||||||
| 302 | 2 | undef $self; | |||||||||||||||||
| 303 | } | ||||||||||||||||||
| 304 | |||||||||||||||||||
| 305 | 1 | 1; | |||||||||||||||||
| 306 | |||||||||||||||||||
| 307 | __END__ | ||||||||||||||||||