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