| 1 | package PAGI::Endpoint::Router; | ||||||||||||||||||||||||||||||||
| 2 | 5 | $PAGI::Endpoint::Router::VERSION = '0.002000'; | |||||||||||||||||||||||||||||||
| 3 | 5 | ▶ | use strict; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 4 | 5 | ▶ | use warnings; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||
| 6 | 5 | ▶ | use Future::AsyncAwait; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 7 | 5 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 8 | 5 | ▶ | use Scalar::Util qw(blessed); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 9 | 5 | ▶ | use Module::Load qw(load); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||
| 12 | sub new { | ||||||||||||||||||||||||||||||||
| 13 | 22 | ▶ | my ($class, %args) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 14 | 22 | return bless { | |||||||||||||||||||||||||||||||
| 15 | _state => {}, | ||||||||||||||||||||||||||||||||
| 16 | }, $class; | ||||||||||||||||||||||||||||||||
| 17 | } | ||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||
| 19 | # Worker-local state (NOT shared across workers) | ||||||||||||||||||||||||||||||||
| 20 | sub state { | ||||||||||||||||||||||||||||||||
| 21 | 6 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
state: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 22 | 6 | return $self->{_state}; | |||||||||||||||||||||||||||||||
| 23 | } | ||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||
| 25 | # Override in subclass to use a custom context class | ||||||||||||||||||||||||||||||||
| 26 | 79 | ▶ | sub context_class { 'PAGI::Context' } | ||||||||||||||||||||||||||||||
|
Subroutine
context_class: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||
| 28 | # Override in subclass to define routes | ||||||||||||||||||||||||||||||||
| 29 | sub routes { | ||||||||||||||||||||||||||||||||
| 30 | 1 | ▶ | my ($self, $r) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
routes: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 31 | # Default: no routes | ||||||||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||
| 35 | sub to_app { | ||||||||||||||||||||||||||||||||
| 36 | 20 | ▶ | my ($class) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
to_app: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||
| 38 | # Create instance that lives for app lifetime | ||||||||||||||||||||||||||||||||
| 39 | 20 | ▶ | my $instance = blessed($class) ? $class : $class->new; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||
| 41 | # Build internal router | ||||||||||||||||||||||||||||||||
| 42 | 20 | load('PAGI::App::Router'); | |||||||||||||||||||||||||||||||
| 43 | 20 | my $internal_router = PAGI::App::Router->new; | |||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||
| 45 | # Let subclass define routes | ||||||||||||||||||||||||||||||||
| 46 | 20 | $instance->_build_routes($internal_router); | |||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||
| 48 | 19 | my $app = $internal_router->to_app; | |||||||||||||||||||||||||||||||
| 49 | 19 | my $state = $instance->{_state}; | |||||||||||||||||||||||||||||||
| 50 | 19 | my $context_class = $instance->context_class; | |||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||
| 52 | 30 | ▶ | return async sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||
| 53 | 30 | my ($scope, $receive, $send) = @_; | |||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||
| 55 | # Inject instance state into scope (allows $req->state to work) | ||||||||||||||||||||||||||||||||
| 56 | 30 | ▶ | $scope->{state} //= $state; | ||||||||||||||||||||||||||||||
|
Condition66%2/3
$scope->{"state"} //= $state
MC/DC0%0/2
$scope->{"state"} //= $state
$scope->{"state"}
$state
Truth table66%2/3
A //= B
A= $scope->{"state"}B= $state
| |||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||
| 58 | # HTTP routes return a response value that bubbles up through the | ||||||||||||||||||||||||||||||||
| 59 | # value-flow method middleware; send it once here. Other dispatch | ||||||||||||||||||||||||||||||||
| 60 | # outcomes (WS/SSE/mount/404/405) handle their own sending and yield | ||||||||||||||||||||||||||||||||
| 61 | # a non-respond-able value. | ||||||||||||||||||||||||||||||||
| 62 | 30 | my $res = await $app->($scope, $receive, $send); | |||||||||||||||||||||||||||||||
| 63 | 28 | ▶ | if (Scalar::Util::blessed($res) && $res->can('respond')) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
builtin::blessed($res) and $res->can("respond")
MC/DC50%1/2
builtin::blessed($res) and $res->can("respond")
builtin::blessed($res)
$res->can("respond")
Truth table66%2/3
A and B
A= builtin::blessed($res)B= $res->can("respond")
| |||||||||||||||||||||||||||||||||
| 64 | 20 | require PAGI::Context; | |||||||||||||||||||||||||||||||
| 65 | 20 | my $ctx = $context_class->new($scope, $receive, $send); | |||||||||||||||||||||||||||||||
| 66 | 20 | await $ctx->respond($res); | |||||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||||||
| 68 | 19 | }; | |||||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||
| 71 | sub _build_routes { | ||||||||||||||||||||||||||||||||
| 72 | 20 | ▶ | my ($self, $r) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
_build_routes: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||
| 74 | # Create a wrapper router that intercepts route registration | ||||||||||||||||||||||||||||||||
| 75 | 20 | my $wrapper = PAGI::Endpoint::Router::RouteBuilder->new($self, $r); | |||||||||||||||||||||||||||||||
| 76 | 20 | $self->routes($wrapper); | |||||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||
| 79 | # Internal route builder that wraps handlers | ||||||||||||||||||||||||||||||||
| 80 | package PAGI::Endpoint::Router::RouteBuilder; | ||||||||||||||||||||||||||||||||
| 81 | 5 | $PAGI::Endpoint::Router::RouteBuilder::VERSION = '0.002000'; | |||||||||||||||||||||||||||||||
| 82 | 5 | ▶ | use strict; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 83 | 5 | ▶ | use warnings; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 84 | 5 | ▶ | use Future::AsyncAwait; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 85 | 5 | ▶ | use Scalar::Util qw(blessed); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||
| 87 | sub new { | ||||||||||||||||||||||||||||||||
| 88 | 20 | ▶ | my ($class, $endpoint, $router) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 89 | 20 | return bless { | |||||||||||||||||||||||||||||||
| 90 | endpoint => $endpoint, | ||||||||||||||||||||||||||||||||
| 91 | router => $router, | ||||||||||||||||||||||||||||||||
| 92 | }, $class; | ||||||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||
| 95 | # HTTP methods | ||||||||||||||||||||||||||||||||
| 96 | 33 | ▶ | sub get { shift->_add_http_route('GET', @_) } | ||||||||||||||||||||||||||||||
|
Subroutine
get: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 97 | 1 | ▶ | sub post { shift->_add_http_route('POST', @_) } | ||||||||||||||||||||||||||||||
|
Subroutine
post: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 98 | 0 | ▶ | sub put { shift->_add_http_route('PUT', @_) } | ||||||||||||||||||||||||||||||
|
Subroutine
put: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 99 | 0 | ▶ | sub patch { shift->_add_http_route('PATCH', @_) } | ||||||||||||||||||||||||||||||
|
Subroutine
patch: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 100 | 0 | ▶ | sub delete { shift->_add_http_route('DELETE', @_) } | ||||||||||||||||||||||||||||||
|
Subroutine
delete: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 101 | 0 | ▶ | sub head { shift->_add_http_route('HEAD', @_) } | ||||||||||||||||||||||||||||||
|
Subroutine
head: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 102 | 0 | ▶ | sub options { shift->_add_http_route('OPTIONS', @_) } | ||||||||||||||||||||||||||||||
|
Subroutine
options: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||
| 104 | sub _add_http_route { | ||||||||||||||||||||||||||||||||
| 105 | 34 | ▶ | my ($self, $method, $path, @rest) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
_add_http_route: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||
| 107 | 34 | my ($middleware, $handler) = $self->_parse_route_args(@rest); | |||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||
| 109 | # Wrap middleware | ||||||||||||||||||||||||||||||||
| 110 | 34 | my @wrapped_mw = map { $self->_wrap_middleware($_) } @$middleware; | |||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||
| 112 | # Wrap handler | ||||||||||||||||||||||||||||||||
| 113 | 33 | my $wrapped = $self->_wrap_http_handler($handler); | |||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||
| 115 | # Register with internal router using the appropriate HTTP method | ||||||||||||||||||||||||||||||||
| 116 | 33 | my $router_method = lc($method); | |||||||||||||||||||||||||||||||
| 117 | 33 | ▶ | $self->{router}->$router_method($path, @wrapped_mw ? (\@wrapped_mw, $wrapped) : $wrapped); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||
| 119 | 33 | return $self; | |||||||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||
| 122 | sub _parse_route_args { | ||||||||||||||||||||||||||||||||
| 123 | 40 | ▶ | my ($self, @args) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
_parse_route_args: called CC=4 SCAR=14.5
| |||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||
| 125 | 40 | ▶ | if (@args == 2 && ref($args[0]) eq 'ARRAY') { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition66%2/3
@args == 2 and ref $args[0] eq "ARRAY"
MC/DC50%1/2
@args == 2 and ref $args[0] eq "ARRAY"
@args == 2
ref $args[0] eq "ARRAY"
Truth table66%2/3
A and B
A= @args == 2B= ref $args[0] eq "ARRAY"
| |||||||||||||||||||||||||||||||||
| 126 | 21 | return ($args[0], $args[1]); | |||||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||||||
| 128 | elsif (@args == 1) { | ||||||||||||||||||||||||||||||||
| 129 | 19 | return ([], $args[0]); | |||||||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||||||
| 131 | else { | ||||||||||||||||||||||||||||||||
| 132 | 0 | die "Invalid route arguments"; | |||||||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||
| 136 | sub _wrap_http_handler { | ||||||||||||||||||||||||||||||||
| 137 | 33 | ▶ | my ($self, $handler) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
_wrap_http_handler: called CC=3 SCAR=14.2
| |||||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||||
| 139 | 33 | my $endpoint = $self->{endpoint}; | |||||||||||||||||||||||||||||||
| 140 | 33 | my $context_class = $endpoint->context_class; | |||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||
| 142 | # If handler is a string, it's a method name | ||||||||||||||||||||||||||||||||
| 143 | 33 | ▶ | if (!ref($handler)) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 144 | 33 | my $method_name = $handler; | |||||||||||||||||||||||||||||||
| 145 | 33 | ▶ | my $method = $endpoint->can($method_name) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 146 | or die "No such method: $method_name in " . ref($endpoint); | ||||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||
| 148 | 18 | ▶ | return async sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.4
| |||||||||||||||||||||||||||||||||
| 149 | 18 | my ($scope, $receive, $send) = @_; | |||||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||||||
| 151 | 18 | require PAGI::Context; | |||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||||
| 153 | 18 | my $ctx = $context_class->new($scope, $receive, $send); | |||||||||||||||||||||||||||||||
| 154 | 18 | my $res = await $endpoint->$method($ctx); | |||||||||||||||||||||||||||||||
| 155 | 18 | ▶ | die "handler did not return a response\n" | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
builtin::blessed($res) and $res->can("respond")
MC/DC0%0/2
builtin::blessed($res) and $res->can("respond")
builtin::blessed($res)
$res->can("respond")
Truth table33%1/3
A and B
A= builtin::blessed($res)B= $res->can("respond")
| |||||||||||||||||||||||||||||||||
| 156 | unless Scalar::Util::blessed($res) && $res->can('respond'); | ||||||||||||||||||||||||||||||||
| 157 | 18 | return $res; | |||||||||||||||||||||||||||||||
| 158 | 33 | }; | |||||||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||||
| 161 | # Already a coderef - wrap it | ||||||||||||||||||||||||||||||||
| 162 | 0 | ▶ | return async sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||||
| 163 | 0 | my ($scope, $receive, $send) = @_; | |||||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||||||
| 165 | 0 | require PAGI::Context; | |||||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||||||
| 167 | 0 | my $ctx = $context_class->new($scope, $receive, $send); | |||||||||||||||||||||||||||||||
| 168 | 0 | my $res = await $handler->($ctx); | |||||||||||||||||||||||||||||||
| 169 | 0 | ▶ | die "handler did not return a response\n" | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
builtin::blessed($res) and $res->can("respond")
MC/DC0%0/2
builtin::blessed($res) and $res->can("respond")
builtin::blessed($res)
$res->can("respond")
Truth table0%0/3
A and B
A= builtin::blessed($res)B= $res->can("respond")
| |||||||||||||||||||||||||||||||||
| 170 | unless Scalar::Util::blessed($res) && $res->can('respond'); | ||||||||||||||||||||||||||||||||
| 171 | 0 | return $res; | |||||||||||||||||||||||||||||||
| 172 | 0 | }; | |||||||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||||||
| 175 | sub websocket { | ||||||||||||||||||||||||||||||||
| 176 | 3 | ▶ | my ($self, $path, @rest) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
websocket: called CC=2 SCAR=7.1
| |||||||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||||||
| 178 | 3 | my ($middleware, $handler) = $self->_parse_route_args(@rest); | |||||||||||||||||||||||||||||||
| 179 | 3 | my @wrapped_mw = map { $self->_wrap_middleware($_) } @$middleware; | |||||||||||||||||||||||||||||||
| 180 | 3 | my $wrapped = $self->_wrap_websocket_handler($handler); | |||||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||||||
| 182 | 3 | ▶ | $self->{router}->websocket($path, @wrapped_mw ? (\@wrapped_mw, $wrapped) : $wrapped); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||||||
| 184 | 3 | return $self; | |||||||||||||||||||||||||||||||
| 185 | } | ||||||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||||||
| 187 | sub _wrap_websocket_handler { | ||||||||||||||||||||||||||||||||
| 188 | 3 | ▶ | my ($self, $handler) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
_wrap_websocket_handler: called CC=3 SCAR=12.3
| |||||||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||||||
| 190 | 3 | my $endpoint = $self->{endpoint}; | |||||||||||||||||||||||||||||||
| 191 | 3 | my $context_class = $endpoint->context_class; | |||||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||||||
| 193 | 3 | ▶ | if (!ref($handler)) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 194 | 3 | my $method_name = $handler; | |||||||||||||||||||||||||||||||
| 195 | 3 | ▶ | my $method = $endpoint->can($method_name) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 196 | or die "No such method: $method_name"; | ||||||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||||||
| 198 | 3 | ▶ | return async sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 199 | 3 | my ($scope, $receive, $send) = @_; | |||||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||||||
| 201 | 3 | require PAGI::Context; | |||||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||||||
| 203 | 3 | my $ctx = $context_class->new($scope, $receive, $send); | |||||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||||||
| 205 | 3 | await $endpoint->$method($ctx); | |||||||||||||||||||||||||||||||
| 206 | 3 | }; | |||||||||||||||||||||||||||||||
| 207 | } | ||||||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||||||
| 209 | 0 | ▶ | return async sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 210 | 0 | my ($scope, $receive, $send) = @_; | |||||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||||||
| 212 | 0 | require PAGI::Context; | |||||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||||||
| 214 | 0 | my $ctx = $context_class->new($scope, $receive, $send); | |||||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||||||
| 216 | 0 | await $handler->($ctx); | |||||||||||||||||||||||||||||||
| 217 | 0 | }; | |||||||||||||||||||||||||||||||
| 218 | } | ||||||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||||||
| 220 | sub sse { | ||||||||||||||||||||||||||||||||
| 221 | 3 | ▶ | my ($self, $path, @rest) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
sse: called CC=2 SCAR=7.1
| |||||||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||||||
| 223 | 3 | my ($middleware, $handler) = $self->_parse_route_args(@rest); | |||||||||||||||||||||||||||||||
| 224 | 3 | my @wrapped_mw = map { $self->_wrap_middleware($_) } @$middleware; | |||||||||||||||||||||||||||||||
| 225 | 3 | my $wrapped = $self->_wrap_sse_handler($handler); | |||||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||||||
| 227 | 3 | ▶ | $self->{router}->sse($path, @wrapped_mw ? (\@wrapped_mw, $wrapped) : $wrapped); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||||||
| 229 | 3 | return $self; | |||||||||||||||||||||||||||||||
| 230 | } | ||||||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||||||
| 232 | sub _wrap_sse_handler { | ||||||||||||||||||||||||||||||||
| 233 | 3 | ▶ | my ($self, $handler) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
_wrap_sse_handler: called CC=3 SCAR=12.3
| |||||||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||||||
| 235 | 3 | my $endpoint = $self->{endpoint}; | |||||||||||||||||||||||||||||||
| 236 | 3 | my $context_class = $endpoint->context_class; | |||||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||||||
| 238 | 3 | ▶ | if (!ref($handler)) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 239 | 3 | my $method_name = $handler; | |||||||||||||||||||||||||||||||
| 240 | 3 | ▶ | my $method = $endpoint->can($method_name) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 241 | or die "No such method: $method_name"; | ||||||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||||||
| 243 | 3 | ▶ | return async sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 244 | 3 | my ($scope, $receive, $send) = @_; | |||||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||||||
| 246 | 3 | require PAGI::Context; | |||||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||||||
| 248 | 3 | my $ctx = $context_class->new($scope, $receive, $send); | |||||||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||||||
| 250 | 3 | await $endpoint->$method($ctx); | |||||||||||||||||||||||||||||||
| 251 | 3 | }; | |||||||||||||||||||||||||||||||
| 252 | } | ||||||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||||||
| 254 | 0 | ▶ | return async sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 255 | 0 | my ($scope, $receive, $send) = @_; | |||||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||||||
| 257 | 0 | require PAGI::Context; | |||||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||||||
| 259 | 0 | my $ctx = $context_class->new($scope, $receive, $send); | |||||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||||||
| 261 | 0 | await $handler->($ctx); | |||||||||||||||||||||||||||||||
| 262 | 0 | }; | |||||||||||||||||||||||||||||||
| 263 | } | ||||||||||||||||||||||||||||||||
| 264 | |||||||||||||||||||||||||||||||||
| 265 | sub _wrap_middleware { | ||||||||||||||||||||||||||||||||
| 266 | 22 | ▶ | my ($self, $mw) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
_wrap_middleware: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||||||
| 268 | 22 | my $endpoint = $self->{endpoint}; | |||||||||||||||||||||||||||||||
| 269 | 22 | my $context_class = $endpoint->context_class; | |||||||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||||||
| 271 | # String = endpoint method name → value-flow route middleware. | ||||||||||||||||||||||||||||||||
| 272 | 22 | ▶ | if (!ref($mw)) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 273 | 21 | ▶ | my $method = $endpoint->can($mw) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 274 | or die "No such middleware method: $mw"; | ||||||||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||||||
| 276 | 11 | ▶ | return async sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||
| 277 | 11 | my ($scope, $receive, $send, $next) = @_; | |||||||||||||||||||||||||||||||
| 278 | |||||||||||||||||||||||||||||||||
| 279 | 11 | require PAGI::Context; | |||||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||||||
| 281 | 11 | my $ctx = $context_class->new($scope, $receive, $send); | |||||||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||||||
| 283 | 11 | my $res = await $endpoint->$method($ctx, $next); | |||||||||||||||||||||||||||||||
| 284 | 11 | ▶ | die "route middleware '$mw' did not return a response\n" | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
builtin::blessed($res) and $res->can("respond")
MC/DC50%1/2
builtin::blessed($res) and $res->can("respond")
builtin::blessed($res)
$res->can("respond")
Truth table66%2/3
A and B
A= builtin::blessed($res)B= $res->can("respond")
| |||||||||||||||||||||||||||||||||
| 285 | unless blessed($res) && $res->can('respond'); | ||||||||||||||||||||||||||||||||
| 286 | 10 | return $res; | |||||||||||||||||||||||||||||||
| 287 | 21 | }; | |||||||||||||||||||||||||||||||
| 288 | } | ||||||||||||||||||||||||||||||||
| 289 | |||||||||||||||||||||||||||||||||
| 290 | # Coderefs/objects are standard (event) middleware: they belong at the | ||||||||||||||||||||||||||||||||
| 291 | # outer layer (App::Router mount/group), not inside an Endpoint route's | ||||||||||||||||||||||||||||||||
| 292 | # value-flow chain. | ||||||||||||||||||||||||||||||||
| 293 | 1 | die "Standard middleware (coderef/object) belong at the mount or group " | |||||||||||||||||||||||||||||||
| 294 | . "level, not in an Endpoint route's middleware list; route middleware " | ||||||||||||||||||||||||||||||||
| 295 | . "are value-flow endpoint methods that return a response.\n"; | ||||||||||||||||||||||||||||||||
| 296 | } | ||||||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||||||
| 298 | # Pass through mount to internal router | ||||||||||||||||||||||||||||||||
| 299 | sub mount { | ||||||||||||||||||||||||||||||||
| 300 | 2 | ▶ | my ($self, @args) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
mount: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 301 | 2 | $self->{router}->mount(@args); | |||||||||||||||||||||||||||||||
| 302 | 2 | return $self; | |||||||||||||||||||||||||||||||
| 303 | } | ||||||||||||||||||||||||||||||||
| 304 | |||||||||||||||||||||||||||||||||
| 305 | # Pass through name() to internal router | ||||||||||||||||||||||||||||||||
| 306 | sub name { | ||||||||||||||||||||||||||||||||
| 307 | 0 | ▶ | my ($self, $name) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
name: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 308 | 0 | $self->{router}->name($name); | |||||||||||||||||||||||||||||||
| 309 | 0 | return $self; | |||||||||||||||||||||||||||||||
| 310 | } | ||||||||||||||||||||||||||||||||
| 311 | |||||||||||||||||||||||||||||||||
| 312 | # Pass through as() to internal router | ||||||||||||||||||||||||||||||||
| 313 | sub as { | ||||||||||||||||||||||||||||||||
| 314 | 0 | ▶ | my ($self, $namespace) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
as: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 315 | 0 | $self->{router}->as($namespace); | |||||||||||||||||||||||||||||||
| 316 | 0 | return $self; | |||||||||||||||||||||||||||||||
| 317 | } | ||||||||||||||||||||||||||||||||
| 318 | |||||||||||||||||||||||||||||||||
| 319 | # Pass through uri_for() to internal router | ||||||||||||||||||||||||||||||||
| 320 | sub uri_for { | ||||||||||||||||||||||||||||||||
| 321 | 0 | ▶ | my ($self, @args) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
uri_for: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 322 | 0 | return $self->{router}->uri_for(@args); | |||||||||||||||||||||||||||||||
| 323 | } | ||||||||||||||||||||||||||||||||
| 324 | |||||||||||||||||||||||||||||||||
| 325 | # Pass through named_routes() to internal router | ||||||||||||||||||||||||||||||||
| 326 | sub named_routes { | ||||||||||||||||||||||||||||||||
| 327 | 0 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
named_routes: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 328 | 0 | return $self->{router}->named_routes; | |||||||||||||||||||||||||||||||
| 329 | } | ||||||||||||||||||||||||||||||||
| 330 | |||||||||||||||||||||||||||||||||
| 331 | 5 | 1; | |||||||||||||||||||||||||||||||
| 332 | |||||||||||||||||||||||||||||||||
| 333 | __END__ | ||||||||||||||||||||||||||||||||