| 1 | package PAGI::Utils; | ||||||||||||||||||||||||||||
| 2 | 35 | $PAGI::Utils::VERSION = '0.002002'; | |||||||||||||||||||||||||||
| 3 | 35 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 35 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 35 | ▶ | use Exporter 'import'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 35 | ▶ | use Future::AsyncAwait; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 35 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 35 | ▶ | use Scalar::Util qw(blessed); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 35 | ▶ | use PAGI::Lifespan; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 35 | our @EXPORT_OK = qw(handle_lifespan to_app is_response); | |||||||||||||||||||||||||||
| 12 | 35 | our %EXPORT_TAGS = (all => \@EXPORT_OK); | |||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | # True if $x is a PAGI response value: a blessed object that can respond($send). | ||||||||||||||||||||||||||||
| 15 | # The single source of truth for the "did the handler return a response?" check | ||||||||||||||||||||||||||||
| 16 | # (used by the endpoint and router dispatch paths). | ||||||||||||||||||||||||||||
| 17 | 53 | ▶ | sub is_response { | ||||||||||||||||||||||||||
|
Subroutine
is_response: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 18 | 53 | my ($x) = @_; | |||||||||||||||||||||||||||
| 19 | 53 | ▶ | return blessed($x) && $x->can('respond') ? 1 : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
builtin::blessed($x) and $x->can("respond")
MC/DC100%2/2
builtin::blessed($x) and $x->can("respond")
builtin::blessed($x)
$x->can("respond")
Truth table100%3/3
A and B
A= builtin::blessed($x)B= $x->can("respond")
| |||||||||||||||||||||||||||||
| 20 | } | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 6 | ▶ | async sub handle_lifespan { | ||||||||||||||||||||||||||
|
Subroutine
handle_lifespan: called CC=5 SCAR=16.2
Pod
documented
| |||||||||||||||||||||||||||||
| 23 | 6 | my ($scope, $receive, $send, %opts) = @_; | |||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 6 | ▶ | my $type = $scope->{type} // ''; | ||||||||||||||||||||||||||
|
Condition50%1/2
$scope->{"type"} // ""
MC/DC0%0/1
$scope->{"type"} // ""
$scope->{"type"}
Truth table50%1/2
A
A= $scope->{"type"}
| |||||||||||||||||||||||||||||
| 26 | 6 | ▶ | croak "handle_lifespan called with scope type '$type' (expected 'lifespan'). " | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 27 | . "Check scope type before calling: " | ||||||||||||||||||||||||||||
| 28 | . "return await handle_lifespan(...) if \$scope->{type} eq 'lifespan'" | ||||||||||||||||||||||||||||
| 29 | unless $type eq 'lifespan'; | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 5 | my $manager = PAGI::Lifespan->for_scope($scope); | |||||||||||||||||||||||||||
| 32 | 5 | ▶ | $manager->register(%opts) if $opts{startup} || $opts{shutdown}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$opts{"startup"} or $opts{"shutdown"}
MC/DC50%1/2
$opts{"startup"} or $opts{"shutdown"}
$opts{"startup"}
$opts{"shutdown"}
Truth table66%2/3
A or B
A= $opts{"startup"}B= $opts{"shutdown"}
| |||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 5 | return await $manager->handle($scope, $receive, $send); | |||||||||||||||||||||||||||
| 35 | 35 | } | |||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 339 | ▶ | sub to_app { | ||||||||||||||||||||||||||
|
Subroutine
to_app: called CC=12 SCAR=24.9
Pod
documented
| |||||||||||||||||||||||||||||
| 38 | 339 | my ($thing) = @_; | |||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 339 | ▶ | croak "to_app() requires an app, component, or class name" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 41 | unless defined $thing; | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 338 | ▶ | return $thing if ref($thing) eq 'CODE'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 33 | ▶ | if (blessed($thing)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 46 | 20 | ▶ | return $thing->to_app if $thing->can('to_app'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 47 | 2 | ▶ | croak ref($thing) . " looks like middleware, not an app" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 48 | . " - pass it to enable(), or wrap an app with ->wrap(\$app)" | ||||||||||||||||||||||||||||
| 49 | if $thing->can('wrap'); | ||||||||||||||||||||||||||||
| 50 | 1 | croak "Cannot coerce " . ref($thing) . " object to a PAGI app (no to_app method)"; | |||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 13 | ▶ | if (!ref($thing)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 54 | 12 | ▶ | croak "Cannot coerce '$thing' to a PAGI app" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 55 | unless $thing =~ /\A\w+(?:::\w+)*\z/; | ||||||||||||||||||||||||||||
| 56 | 11 | ▶ | unless ($thing->can('to_app')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 57 | 5 | local $@; | |||||||||||||||||||||||||||
| 58 | 5 | ▶ | eval "require $thing; 1" or croak "Failed to load '$thing': $@"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | 9 | ▶ | return $thing->to_app if $thing->can('to_app'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 61 | 1 | ▶ | croak "'$thing' looks like middleware, not an app - pass it to enable()" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 62 | if $thing->can('wrap'); | ||||||||||||||||||||||||||||
| 63 | 1 | croak "'$thing' does not have a to_app() method"; | |||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 1 | croak "Cannot coerce " . ref($thing) . " reference to a PAGI app"; | |||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 35 | 1; | |||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | __END__ | ||||||||||||||||||||||||||||