« blib/lib/PAGI/Middleware/ConditionalGet.pm
↑ Summary
blib/lib/PAGI/Middleware/WebSocket/RateLimit.pm »
| 1 | package PAGI::Utils; | ||||||||||||||||||||||||||||
| 2 | 26 | $PAGI::Utils::VERSION = '0.002000'; | |||||||||||||||||||||||||||
| 3 | 26 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 26 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 26 | ▶ | use Exporter 'import'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 26 | ▶ | use Future::AsyncAwait; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 26 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 26 | ▶ | use Scalar::Util qw(blessed); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 26 | ▶ | use PAGI::Lifespan; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 26 | our @EXPORT_OK = qw(handle_lifespan to_app); | |||||||||||||||||||||||||||
| 12 | 26 | our %EXPORT_TAGS = (all => \@EXPORT_OK); | |||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 6 | ▶ | async sub handle_lifespan { | ||||||||||||||||||||||||||
|
Subroutine
handle_lifespan: called CC=5 SCAR=16.2
Pod
documented
| |||||||||||||||||||||||||||||
| 15 | 6 | my ($scope, $receive, $send, %opts) = @_; | |||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 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"}
| |||||||||||||||||||||||||||||
| 18 | 6 | ▶ | croak "handle_lifespan called with scope type '$type' (expected 'lifespan'). " | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 19 | . "Check scope type before calling: " | ||||||||||||||||||||||||||||
| 20 | . "return await handle_lifespan(...) if \$scope->{type} eq 'lifespan'" | ||||||||||||||||||||||||||||
| 21 | unless $type eq 'lifespan'; | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 5 | my $manager = PAGI::Lifespan->for_scope($scope); | |||||||||||||||||||||||||||
| 24 | 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"}
| |||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 5 | return await $manager->handle($scope, $receive, $send); | |||||||||||||||||||||||||||
| 27 | 26 | } | |||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | sub to_app { | ||||||||||||||||||||||||||||
| 30 | 333 | ▶ | my ($thing) = @_; | ||||||||||||||||||||||||||
|
Subroutine
to_app: called CC=12 SCAR=24.9
Pod
documented
| |||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 333 | ▶ | croak "to_app() requires an app, component, or class name" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 33 | unless defined $thing; | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 332 | ▶ | return $thing if ref($thing) eq 'CODE'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 33 | ▶ | if (blessed($thing)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 38 | 20 | ▶ | return $thing->to_app if $thing->can('to_app'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 39 | 2 | ▶ | croak ref($thing) . " looks like middleware, not an app" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 40 | . " - pass it to enable(), or wrap an app with ->wrap(\$app)" | ||||||||||||||||||||||||||||
| 41 | if $thing->can('wrap'); | ||||||||||||||||||||||||||||
| 42 | 1 | croak "Cannot coerce " . ref($thing) . " object to a PAGI app (no to_app method)"; | |||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 13 | ▶ | if (!ref($thing)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 46 | 12 | ▶ | croak "Cannot coerce '$thing' to a PAGI app" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 47 | unless $thing =~ /\A\w+(?:::\w+)*\z/; | ||||||||||||||||||||||||||||
| 48 | 11 | ▶ | unless ($thing->can('to_app')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 49 | 5 | local $@; | |||||||||||||||||||||||||||
| 50 | 5 | ▶ | eval "require $thing; 1" or croak "Failed to load '$thing': $@"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | 9 | ▶ | return $thing->to_app if $thing->can('to_app'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 53 | 1 | ▶ | croak "'$thing' looks like middleware, not an app - pass it to enable()" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 54 | if $thing->can('wrap'); | ||||||||||||||||||||||||||||
| 55 | 1 | croak "'$thing' does not have a to_app() method"; | |||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 1 | croak "Cannot coerce " . ref($thing) . " reference to a PAGI app"; | |||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 26 | 1; | |||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | __END__ | ||||||||||||||||||||||||||||