lib/PAGI/App/Loader.pm

filter: Statementstmt 84.4% 38 / 45 Branchbran 60.0% 12 / 20 Conditioncond 25.0% 2 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 100% 7 / 7 Podpod 0.0% 0 / 2 totaltotal 67.8% 59 / 87 CC 14 SCAR 27.0
CC
14
Cov
71%
_get_app
23.8
new
2.1
__ANON__
2.0
CRAP 14.9
« blib/lib/PAGI/Middleware/Maintenance.pm ↑ Summary blib/lib/PAGI/App/Router.pm »
1 package PAGI::App::Loader;
2 1 $PAGI::App::Loader::VERSION = '0.002002';
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Future::AsyncAwait;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 =head1 NAME
8  
9 PAGI::App::Loader - Load PAGI app from file
10  
11 =head1 SYNOPSIS
12  
13 use PAGI::App::Loader;
14  
15 my $app = PAGI::App::Loader->new(
16 file => 'app.pl',
17 )->to_app;
18  
19 =cut
20  
21 2 ▶sub new {
Subroutine new: called CC=2 SCAR=7.2
Pod undocumented
22 2 my ($class, %args) = @_;
23  
24 return bless {
25 file => $args{file},
26 2 ▶ reload => $args{reload} // 0,
Condition50%1/2
$args{"reload"} // 0
l!l
02
MC/DC0%0/1
$args{"reload"} // 0
$args{"reload"}
Truth table50%1/2
A
A= $args{"reload"}
Aresult
11
00
27 _app => undef,
28 _mtime => 0,
29 }, $class;
30 }
31  
32 2 ▶sub to_app {
Subroutine to_app: called CC=1 SCAR=0.0
Pod undocumented
33 2 my ($self) = @_;
34  
35 2 ▶ return async sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
36 2 my ($scope, $receive, $send) = @_;
37 2 my $app = $self->_get_app();
38  
39 2 ▶ unless ($app) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($app) 1 1 2
40 1 await $send->({
41 type => 'http.response.start',
42 status => 500,
43 headers => [['content-type', 'text/plain']],
44 });
45 1 await $send->({ type => 'http.response.body', body => 'App failed to load', more => 0 });
46 1 return;
47 }
48  
49 1 await $app->($scope, $receive, $send);
50 2 };
51 }
52  
53 2 ▶sub _get_app {
Subroutine _get_app: called CC=12 SCAR=31.7
54 2 my ($self) = @_;
55  
56 2 my $file = $self->{file};
57  
58 # Check if reload needed
59 2 ▶ if ($self->{reload} && $self->{_app}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"reload"} and $self->{"_app"}) 0 2 2
Condition33%1/3
$self->{"reload"} and $self->{"_app"}
!ll&&!rl&&r
200
MC/DC0%0/2
$self->{"reload"} and $self->{"_app"}
$self->{"reload"} $self->{"_app"}
Truth table33%1/3
A and B
A= $self->{"reload"}B= $self->{"_app"}
ABresult
0X0
100
111
60 0 my @stat = stat($file);
61 0 ▶ if (@stat && $stat[9] > $self->{_mtime}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@stat and $stat[9] > $self->{"_mtime"}) 0 0 0
Condition0%0/3
@stat and $stat[9] > $self->{"_mtime"}
!ll&&!rl&&r
000
MC/DC0%0/2
@stat and $stat[9] > $self->{"_mtime"}
@stat $stat[9] > $self->{"_mtime"}
Truth table0%0/3
A and B
A= @statB= $stat[9] > $self->{"_mtime"}
ABresult
0X0
100
111
62 0 $self->{_app} = undef;
63 }
64 }
65  
66 2 ▶ return $self->{_app} if $self->{_app};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_app"} 0 2 2
67  
68 # Load app
69 2 my @stat = stat($file);
70 2 ▶ $self->{_mtime} = $stat[9] if @stat;
Branch: 1 branch
BranchTrueFalseTotal
if @stat 1 1 2
71  
72 2 my $app = do $file;
73 2 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 2 2
74 0 warn "Error loading $file: $@\n";
75 0 return;
76 }
77 2 ▶ unless ($app) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($app) 1 1 2
78 1 ▶ warn "Error loading $file: $!\n" if $!;
Branch: 1 branch
BranchTrueFalseTotal
if $! 1 0 1
79 1 ▶ warn "$file did not return a coderef\n" unless $app;
Branch: 1 branch
BranchTrueFalseTotal
unless $app 1 0 1
80 1 return;
81 }
82 1 ▶ unless (ref $app eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $app eq "CODE") 0 1 1
83 0 warn "$file did not return a coderef\n";
84 0 return;
85 }
86  
87 1 $self->{_app} = $app;
88 1 return $app;
89 }
90  
91 1 1;
92  
93 __END__
« blib/lib/PAGI/Middleware/Maintenance.pm ↑ Summary blib/lib/PAGI/App/Router.pm »