blib/lib/WebDyne/Chain.pm untested

filter: Statementstmt 0.0% 0 / 216 Branchbran 0.0% 0 / 44 Conditioncond 0.0% 0 / 119 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 18 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 403 CC 23 SCAR 44.3
CC
23
Cov
0%
handler
156.0
UNIVERSAL::AUTOLOAD
132.0
DESTROY
6.0
CRAP 84.2
1 #
2 # This file is part of WebDyne.
3 #
4 # This software is copyright (c) 2026 by Andrew Speer <andrew.speer@isolutions.com.au>.
5 #
6 # This is free software; you can redistribute it and/or modify it under
7 # the same terms as the Perl 5 programming language system itself.
8 #
9 # Full license text is available at:
10 #
11 # <http://dev.perl.org/licenses/>
12 #
13 package WebDyne::Chain;
14  
15  
16 # Compiler Pragma
17 #
18 use strict qw(vars);
19 use vars qw($VERSION);
20 use warnings;
21 no warnings qw(uninitialized);
22  
23  
24 # WebDyne Modules.
25 #
26 use WebDyne;
27 use WebDyne::Constant;
28 use WebDyne::Chain::Constant;
29 use WebDyne::Util;
30  
31  
32 # Other modules
33 #
34 use Data::Dumper;
35  
36  
37 # Version information in a formate suitable for CPAN etc. Must be
38 # all on one line
39 #
40 $VERSION='3.027';
41  
42  
43 # Debug using WebDyne debug handler
44 #
45 0 && debug("%s loaded, version $VERSION", __PACKAGE__);
46  
47  
48 # Shortcut error handler, save using ISA;
49 #
50 require WebDyne::Err;
51 *err_html=\&WebDyne::Err::err_html || *err_html;
52  
53  
54 # Package wide hash ref for data storage
55 #
56 my %Package;
57  
58  
59 # Make all errors non-fatal
60 #
61 errnofatal(1);
62  
63  
64 # And done
65 #
66 1;
67  
68  
69 #------------------------------------------------------------------------------
70  
71  
72 # Packace init, attempt to load optional Time::HiRes module
73 #
74 BEGIN {
75 eval {require Time::HiRes; Time::HiRes->import('time')};
76 eval {require Devel::Confess; Devel::Confess->import(qw(no_warnings))};
77 }
78  
79  
80 sub import {
81  
82  
83 # Will only work if called from within a __PERL__ block in WebDyne
84 #
85 my ($class, @import)=@_;
86 my $self_cr=UNIVERSAL::can(scalar caller, 'self') || return;
87 my $self=$self_cr->() || return;
88 $self->set_handler($class);
89 my $meta_hr=$self->meta();
90 push @{$meta_hr->{'webdynechain'}}, @import;
91  
92  
93 }
94  
95  
96 sub handler : method {
97  
98  
99 # Get class, request object
100 #
101 my ($self, $r, $param_hr)=@_;
102 my $class=ref($self) || do {
103  
104  
105 # Need new self ref
106 #
107 my %self=(
108  
109 _time => time(),
110 _r => $r,
111 %{delete $self->{'_self'}},
112  
113 );
114 $self=bless \%self, $self;
115 ref($self);
116  
117  
118 };
119  
120  
121 # Setup error handlers
122 #
123 local $SIG{__DIE__}=sub {return $self->err_html(@_) unless $^S};
124 local $SIG{__WARN__}=sub {return $self->err_html(@_)}
125 if $WEBDYNE_WARNINGS_FATAL;
126  
127  
128 # Debug
129 #
130 0 && debug(
131 "in WebDyne::Chain::handler, class $class, r $r, self $self, param_hr %s",
132 Dumper($param_hr));
133  
134  
135 # Log URI
136 #
137 0 && debug("URI %s", $r->uri());
138  
139  
140 # Get string of modules to chain
141 #
142 my @module;
143 if (my $module_ar=$param_hr->{'meta'}{'webdynechain'}) {
144 0 && debug("using module_ar $module_ar %s from meta", Dumper($module_ar));
145 @module=@{$module_ar};
146 }
147 elsif (my $module=$r->dir_config('WebDyneChain')) {
148 0 && debug("using module $module dir_config");
149 @module=split(/\s+/, $module);
150 }
151 else {
152 0 && debug('could not find any module chain info');
153 }
154 0 && debug('module: %s', Dumper(\@module));
155  
156  
157  
158 # WebDyne::Chain must be the first handler in line, Webdyne the last
159 #
160 unshift @module, __PACKAGE__ unless ($module[0] eq +__PACKAGE__);
161 push @module, 'WebDyne' unless ($module[$#module] eq 'WebDyne');
162 0 && debug('final module chain %s', join('*', @module));
163  
164  
165 # Store current chain
166 #
167 $Package{'_chain_ar'}=\@module;
168  
169  
170 # If only two modules (WebDyne::Chain, WebDyne) something is wrong
171 #
172 if (@module == 2) {
173 return
174 $self->err_html('unable to determine module chain - have you set WebDyneChain var ?');
175 }
176  
177  
178 # Get location. Used to use r->location, now use module array to generate pseudo
179 # location data;
180 #
181 my $location=join(undef, @module);
182 0 && debug("location $location");
183 unless ($Package{'_chain_loaded_hr'}{$location}++) {
184 0 && debug("modules not loaded, doing now");
185 local $SIG{'__DIE__'};
186 foreach my $package (@module) {
187 eval("require $package") ||
188 return $self->err_html("unable to load package $package, " . lcfirst($@));
189 0 && debug("loaded $package");
190 }
191 }
192  
193  
194 # If location not same as last time we were run, then unload chain
195 #
196 if ((my $location_current=$Package{'_location_current'}) ne $location) {
197  
198  
199 # Need to unload cached code refs
200 #
201 0 && debug("location_current '$location_current' is ne this location ('$location'). restoring cr's");
202 &ISA_restore();
203  
204  
205 # Update location
206 #
207 $Package{'_location_current'}=$location;
208  
209  
210 # If code ref's cached, load up now
211 #
212 if (my $chain_hr=$Package{'_chain_hr'}{$location}) {
213  
214  
215 # Debug
216 #
217 0 && debug("found cached code ref's for location $location loading");
218  
219  
220 # Yes found, load up
221 #
222 while (my ($method, $cr)=each %{$chain_hr}) {
223  
224  
225 # Debug
226 #
227 0 && debug("loading cr $cr for method $method");
228  
229  
230 # Install code ref
231 #
232 *{$method}=$cr;
233  
234 }
235  
236  
237 # Update current pointer
238 #
239 $Package{'_chain_current_hr'}=$chain_hr;
240  
241  
242 }
243 }
244 else {
245  
246 0 && debug('location chain same as last request, caching');
247  
248 }
249  
250  
251 # Debug
252 #
253 0 && debug('module array %s', Dumper(\@module));
254  
255  
256 # All done, pass onto next handler in chain. NOTE no error handler (eg || $self->err_html). It is
257 # not our job to check for errors here, we should just pass back whatever the next handler does.
258 #
259 return $self->SUPER::handler($r, @_[2..$#_]);
260  
261  
262 # Only get here if error handler invoked
263 #
264 RENDER_ERROR:
265 return $self->err_html();
266  
267  
268 # Only get here if subrequest invoked.
269 HANDLER_COMPLETE:
270 return $MP2 ? &Apache::OK : HTTP_OK;
271  
272  
273 }
274  
275  
276 sub ISA_restore {
277  
278  
279 # Get cuurent chain hash
280 #
281 my $chain_hr=delete $Package{'_chain_current_hr'};
282 0 && debug('in ISA_restore, chain %s', Dumper($chain_hr));
283  
284  
285 # Go through each module, restoring
286 #
287 foreach my $method (keys %{$chain_hr}) {
288  
289  
290 # Free up
291 #
292 0 && debug("free $method");
293 undef *{$method};
294  
295  
296 }
297  
298  
299 }
300  
301  
302 sub DESTROY {
303  
304  
305 # Get chain array ref
306 #
307 my $self=shift();
308 my $chain_ar=$Package{'_chain_ar'};
309 0 && debug("self $self, going through DESTROY chain %s", Dumper($chain_ar));
310  
311  
312 # Handle destroys specially, mini version of AUTOLOAD code below
313 #
314 foreach my $i (1..$#{$chain_ar}) {
315 my $package_chain=$chain_ar->[$i];
316 0 && debug("looking for DESTROY $package_chain");
317 if (my $cr=UNIVERSAL::can($package_chain, 'DESTROY')) {
318 0 && debug("DESTROY hit on $package_chain");
319 $cr->($self);
320 }
321 }
322  
323  
324 # Destroy object
325 #
326 %{$self}=();
327 undef $self;
328  
329  
330 }
331  
332  
333 sub UNIVERSAL::AUTOLOAD {
334  
335  
336 # Get self ref, calling class, autoloaded method
337 #
338 my $self=$_[0];
339 my $autoload=$UNIVERSAL::AUTOLOAD || return;
340  
341  
342 # Do not handle DESTROY's
343 #
344 return if $autoload=~/::DESTROY$/;
345  
346  
347 # Debug
348 #
349 0 && debug(
350 "in UNIVERSAL::AUTOLOAD, self $self, autoload $autoload, caller %s",
351 Dumper([(caller(1))[0..3]]));
352  
353  
354 # Get apache request ref, location. If not present means called by non-WebDyne class, not supported
355 #
356 my $r; {
357 local $SIG{'__DIE__'}=undef;
358  
359 #unless (eval{ ref($self) && ($r=$self->{'_r'}) }) {
360 unless (eval {ref($self) && ($r=$self->{'_r'})} || UNIVERSAL::can($self, $autoload)) {
361 err ("call to run %s UNIVERSAL::AUTOLOAD for non chained method '$autoload', self ref '$self'.", +__PACKAGE__);
362 goto RENDER_ERROR;
363 }
364 }
365  
366  
367 # Get method user was looking for, keep full package name.
368 #
369 my ($package_autoload, $method_autoload)=($autoload=~/(.*)::(.*?)$/);
370 0 && debug("package_autoload $package_autoload, method_autoload $method_autoload");
371  
372  
373 # And chain for this location
374 #
375 my $chain_ar=$Package{'_chain_ar'};
376 my $location=join(undef, @{$chain_ar});
377 0 && debug('going through chain %s', Dumper($chain_ar));
378  
379  
380 # Caller information
381 #
382 my $subroutine_caller=(caller(1))[3];
383 my $subroutine_caller_cr=\&{"$subroutine_caller"};
384 my ($package_caller, $method_caller)=($subroutine_caller=~/(.*)::(.*?)$/);
385 0 && debug("package_caller $package_caller, method_caller $method_caller");
386  
387  
388 # If SUPER method trawl through chain to find the package it was called from, make sure we start
389 # from there in iteration code below
390 #
391 my $i=0;
392 if ($autoload=~/\QSUPER::$method_autoload\E$/) {
393 0 && debug("SUPER method");
394 for (1; $i < @{$chain_ar}; $i++) {
395 if (UNIVERSAL::can($chain_ar->[$i], $method_caller) eq $subroutine_caller_cr) {
396 $i++;
397 last;
398 }
399 else {
400 0 && debug("miss on package $chain_ar->[$i], $_ ne $subroutine_caller_cr");
401 }
402 }
403 0 && debug("loop finished, i $i, chain_ar %s", $#{$chain_ar});
404 }
405  
406  
407 # Iterate through the chain (in order) looking for the method
408 #
409 foreach $i ($i..$#{$chain_ar}) {
410  
411  
412 # Can this package in the chain support the calling method ?
413 #
414 0 && debug("look for $method_autoload in package $chain_ar->[$i]");
415 if (my $cr=UNIVERSAL::can($chain_ar->[$i], $method_autoload)) {
416  
417  
418 # Yes. Check for loops
419 #
420 if ($cr eq $subroutine_caller_cr) {
421 err (
422 "detected AUTOLOAD loop for method '$method_autoload' " .
423 "package $package_caller. Current chain: %s", join(', ', @{$chain_ar}));
424 goto RENDER_ERROR;
425 }
426  
427  
428 # Update
429 #
430 0 && debug('hit');
431 *{$autoload}=$cr;
432  
433  
434 # And keep a record
435 #
436 $Package{'_chain_hr'}{$location}{$autoload}=$cr;
437 $Package{'_chain_current_hr'} ||= $Package{'_chain_hr'}{$location};
438  
439  
440 # And dispatch. The commented out code is good for debugging internal
441 # server errors, esp if comment out *{$autoload} above and turn on
442 # debugging
443 #
444 goto &{$cr};
445  
446 }
447 else {
448  
449  
450 # Debug
451 #
452 0 && debug("unable to find method $method_autoload in package $chain_ar->[$i]");
453  
454 }
455  
456 }
457  
458  
459 # Last resort - look back through call chain
460 #
461 0 && debug("checking back through callstack for method $method_autoload");
462 my %chain=map {$_ => 1} @{$chain_ar};
463 my @caller;
464 for ($i=0; my $caller=(caller($i))[0]; $i++) {
465 next if $chain{$caller}++; #already looked there
466 push @caller, $caller;
467 if (my $cr=UNIVERSAL::can($caller, $method_autoload)) {
468 if ($cr eq $subroutine_caller_cr) {
469 err (
470 "detected AUTOLOAD loop for method '$method_autoload' " .
471 "package $package_caller. Current chain: %s", join(', ', @{$chain_ar}));
472 goto RENDER_ERROR;
473 }
474 if ($WEBDYNE_AUTOLOAD_POLLUTE) {
475 *{$autoload}=$cr;
476 $Package{'_chain_hr'}{$location}{$autoload}=$cr;
477 }
478 goto &{$cr}
479 }
480 }
481  
482  
483 # Return err
484 #
485 err ("method '$method_autoload' not found in call chain: %s", join(',', @caller));
486 goto RENDER_ERROR;
487  
488 }
489 1;
490  
491 __END__