blib/lib/Thunderhorse.pm

filter: Statementstmt 100% 53 / 53 Branchbran 91.6% 22 / 24 Conditioncond 100% 5 / 5 MC/DCmcdc 100% 3 / 3 Subroutinesub 100% 9 / 9 Podpod 0.0% 0 / 3 totaltotal 94.8% 92 / 97 CC 16 SCAR 17.5
CC
16
Cov
98%
__ANON__
10.0
__ANON__
4.0
pagi_loop
4.0
CRAP 5.8
↑ Summary blib/lib/Thunderhorse/Config.pm »
1 package Thunderhorse;
2 24 $Thunderhorse::VERSION = '0.107';
3 ##################################
4 # ~~~~~~~~~~~~ Ride ~~~~~~~~~~~~ #
5 # ~~~~~~~~~~~~ Ride ~~~~~~~~~~~~ #
6 # ~~~~~~~~~~~~ Ride ~~~~~~~~~~~~ #
7 # ~~~~~~~~~~~~ Ride ~~~~~~~~~~~~ #
8 # ~~~~~~~~ Thunderhorse ~~~~~~~~ #
9 # ~~~~~~~~ Thunderhorse ~~~~~~~~ #
10 # ~~~~~~~~ Thunderhorse ~~~~~~~~ #
11 # ~~~~~~~~ Thunderhorse ~~~~~~~~ #
12 # ~~~~~~~~~~~ Revenge ~~~~~~~~~~ #
13 # ~~~~~~~~~~~ Revenge ~~~~~~~~~~ #
14 # ~~~~~~~~~~~ Revenge ~~~~~~~~~~ #
15 ##################################
16  
17 24 ▶use v5.40;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 24 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
20 24 ▶use Future::AsyncAwait;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 24 ▶use Gears::X::Thunderhorse;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 24 our @EXPORT_OK = qw(
25 pagi_loop
26 adapt_pagi
27 build_handler
28 );
29  
30 114 ▶async sub pagi_loop ($ctx, @matches)
Subroutine pagi_loop: called CC=4 SCAR=13.9
Pod undocumented
31 114 {
32 114 my @pagi = $ctx->pagi->@*;
33  
34 114 foreach my $match (@matches) {
35 # is this a bridge? If yes, take first element (the bridge location).
36 # It is guaranteed to be a match, not an array
37 120 ▶ my $loc = (ref $match eq 'ARRAY' ? $match->[0] : $match)->location;
Branch: 1 branch
BranchTrueFalseTotal
ref $match eq 'ARRAY' ? : 11 109 120
38  
39 # $ctx->match may be an array if this is a bridge. Location handler
40 # takes care of that
41 120 $ctx->set_match($match);
42  
43 # execute location handler (PAGI application)
44 120 await $loc->pagi_app->(@pagi);
45 113 ▶ last if $ctx->is_consumed;
Branch: 1 branch
BranchTrueFalseTotal
if $ctx->is_consumed 100 13 113
46 }
47 }
48  
49 10 ▶sub adapt_pagi ($destination)
Subroutine adapt_pagi: called CC=1 SCAR=0.0
Pod undocumented
50 {
51 # no need to async here because we don't await - destination must return a promise anyway
52 # TODO: think of a proper way to enforce last placeholder being at the very end of the url
53 14 ▶ return sub ($scope, @args) {
Subroutine __ANON__: called CC=4 SCAR=13.9
54 Gears::X::Thunderhorse->raise('bad PAGI execution chain, not a Thunderhorse app')
55 14 ▶ unless my $ctx = $scope->{thunderhorse};
Branch: 1 branch
BranchTrueFalseTotal
unless my $ctx = $scope->{"thunderhorse"} 0 14 14
56  
57 # consume this context eagerly to keep further matches from firing
58 14 $ctx->consume;
59  
60 # take last matched element as the path
61 # pagi apps can't be bridges, so don't check if $ctx->match is an array
62 14 ▶ my $path = $ctx->match->matched->[-1] // '';
Condition100%2/2
$ctx->match->matched->[-1] // ""
l!l
311
MC/DC100%1/1
$ctx->match->matched->[-1] // ""
$ctx->match->matched->[-1]
Truth table100%2/2
A
A= $ctx->match->matched->[-1]
Aresult
11
00
63 14 ▶ my $trailing_slash = $scope->{path} =~ m{/$} ? '/' : '';
Branch: 1 branch
BranchTrueFalseTotal
$scope->{'path'} =~ m[/$]u ? : 6 8 14
64 14 $path =~ s{^/?}{/};
65 14 $path =~ s{/?$}{$trailing_slash};
66  
67 # modify the scope for the app
68 14 $scope = {$scope->%*};
69 14 $scope->{root_path} = ($scope->{root_path} . $scope->{path}) =~ s{\Q$path\E$}{}r;
70 14 $scope->{path} = $path;
71  
72 14 return $destination->($scope, @args);
73 }
74 10 }
75  
76 87 ▶sub build_handler ($controller, $destination)
Subroutine build_handler: called CC=1 SCAR=0.0
Pod undocumented
77 {
78 106 ▶ return async sub ($scope, $receive, $send) {
Subroutine __ANON__: called CC=10 SCAR=23.0
79 Gears::X::Thunderhorse->raise('bad PAGI execution chain, not a Thunderhorse app')
80 106 ▶ unless my $ctx = $scope->{thunderhorse};
Branch: 1 branch
BranchTrueFalseTotal
unless my $ctx = $scope->{"thunderhorse"} 0 106 106
81  
82 106 $ctx->update($scope, $receive, $send);
83  
84 106 my $match = $ctx->match;
85 106 my $bridge = ref $match eq 'ARRAY';
86  
87 # this location may be unimplemented when destination is undefined, but
88 # a full handler should be built anyway. Unimplemented destinations
89 # should still be wrappable in middleware.
90 106 ▶ if (defined $destination) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $destination) 104 2 106
91 104 try {
92 104 my $facade = $controller->make_facade($ctx);
93 104 ▶ my $result = $destination->($controller, $facade, ($bridge ? $match->[0] : $match)->matched->@*);
Branch: 1 branch
BranchTrueFalseTotal
$bridge ? : 10 94 104
94 91 ▶ $result = await $result
Branch: 1 branch
BranchTrueFalseTotal
if $result isa "Future" 18 73 91
95 if $result isa 'Future';
96  
97 89 ▶ if (!$ctx->is_consumed) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $ctx->is_consumed) 74 10 84
98 74 ▶ if (defined $result) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $result) { } 57 17 74
99 57 await $controller->render_response($ctx, $result);
100 }
101 else {
102 17 weaken $facade;
103 17 ▶ Gears::X::Thunderhorse->raise("context hasn't been given up - forgot await?")
Branch: 1 branch
BranchTrueFalseTotal
if defined $facade 1 16 17
104 if defined $facade;
105 }
106 }
107 }
108 catch ($ex) {
109 19 await $controller->_on_error($ctx, $ex);
110 }
111 }
112  
113 # NOTE: this method will not send the response if we already sent or if
114 # the response is not ready. It does not check whether the context is
115 # consumed altogether, so a manually consumed context with a non-ready
116 # response will not send anything, likely rendering some kind of error
117 # page (but not 404). Currently, a PAGI error is raised, informing
118 # about app returning without sending respnose.
119 # NOTE: this needs to be here, since we want to use $send from this context
120 104 await $ctx->try_send_res;
121  
122 # if this is a bridge and bridge did not render, it means we are
123 # free to go deeper. Avoid first match, as it was handled already
124 # above
125 99 ▶ if ($bridge && !$ctx->is_consumed) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bridge and not $ctx->is_consumed) 8 91 99
Condition100%3/3
$bridge and not $ctx->is_consumed
!ll&&!rl&&r
8838
MC/DC100%2/2
$bridge and not $ctx->is_consumed
$bridge not $ctx->is_consumed
Truth table100%3/3
A and not(B)
A= $bridgeB= not $ctx->is_consumed
ABresult
0X0
100
111
126 8 await pagi_loop($ctx, $match->@[1 .. $match->$#*]);
127 }
128 87 };
129 }
130  
131 __END__
↑ Summary blib/lib/Thunderhorse/Config.pm »