blib/lib/PAGI/Server/AppNormalizer.pm

filter: Statementstmt 100% 23 / 23 Branchbran 70.0% 7 / 10 Conditioncond 70.0% 7 / 10 MC/DCmcdc 40.0% 2 / 5 Subroutinesub 100% 4 / 4 Podpod 100% 1 / 1 totaltotal 83.0% 44 / 53 CC 11 SCAR 22.5
CC
11
Cov
86%
normalize_app
11.8
BEGIN
1.0
BEGIN
1.0
CRAP 9.5
« blib/lib/PAGI/Server/EventValidator.pm ↑ Summary examples/07-extension-fullflush/app.pl »
1 package PAGI::Server::AppNormalizer;
2  
3 164 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 164 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 164 our $VERSION = '0.002013';
7  
8 164 ▶use Scalar::Util qw(blessed reftype);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 =head1 NAME
11  
12 PAGI::Server::AppNormalizer - Normalizes PAGI 0.4 application providers to a coderef
13  
14 =head1 SYNOPSIS
15  
16 use PAGI::Server::AppNormalizer ();
17  
18 my $app = PAGI::Server::AppNormalizer::normalize_app($candidate, 'app');
19  
20 =head1 DESCRIPTION
21  
22 B<Note:> This is a PAGI::Server internal module, not part of the public
23 PAGI::Server API, and is not indexed on CPAN.
24  
25 PAGI 0.4 allows an application source (an app file, a module constructor, an
26 C<-e> expression, direct C<PAGI::Server> construction, or C<configure(app =>
27 ...)>) to hand PAGI::Server either a native PAGI coderef or an instantiated
28 application-provider object exposing a C<to_app> method. This module is the
29 single normalization point every one of those sources calls through, so a
30 provider is normalized to its coderef exactly once regardless of which
31 source produced it. A blessed coderef is returned as-is (native-app
32 precedence); a package-name string is never treated as a provider.
33  
34 =head1 FUNCTIONS
35  
36 =head2 normalize_app($candidate, $source)
37  
38 my $app = PAGI::Server::AppNormalizer::normalize_app($candidate, $source);
39  
40 Returns C<$candidate> unchanged if it is already a coderef (including a
41 blessed one). Otherwise C<$candidate> must be a blessed object providing
42 C<to_app>; that method is called once and its coderef return value is
43 returned. C<$source> is a human-readable label (e.g. C<'App file'>, C<'-e
44 code'>) used only to identify the origin in die messages; it defaults to
45 C<'Application'>.
46  
47 Dies if C<$candidate> is neither a coderef nor a blessed object, if the
48 object has no C<to_app> method, if C<to_app> raises, or if C<to_app>'s
49 return value is not itself a coderef.
50  
51 =head1 SEE ALSO
52  
53 L<PAGI::Server>, L<PAGI::Server::Runner>
54  
55 =cut
56  
57 692 ▶sub normalize_app {
Subroutine normalize_app: called CC=11 SCAR=24.7
Pod documented
58 692 my ($candidate, $source) = @_;
59 692 ▶ $source //= 'Application';
Condition50%1/2
$source //= "Application"
l!l
6920
MC/DC0%0/1
$source //= "Application"
$source
Truth table50%1/2
A
A= $source
Aresult
11
00
60  
61 692 ▶ return $candidate if (reftype($candidate) // '') eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::reftype($candidate) // "") eq "CODE" 683 9 692
Condition100%2/2
builtin::reftype($candidate) // ""
l!l
6911
MC/DC100%1/1
builtin::reftype($candidate) // ""
builtin::reftype($candidate)
Truth table100%2/2
A
A= builtin::reftype($candidate)
Aresult
11
00
62  
63 9 ▶ my $type = ref($candidate) || 'non-reference';
Condition100%2/2
ref $candidate || 'non-reference'
l!l
81
MC/DC100%1/1
ref $candidate || 'non-reference'
ref $candidate
Truth table100%2/2
A
A= ref $candidate
Aresult
11
00
64 9 ▶ die "$source must be a coderef or instantiated application-provider object, got: $type\n"
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($candidate) 3 6 9
65 unless blessed($candidate);
66  
67 6 ▶ die "$source returned an application-provider object without to_app()\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $candidate->can("to_app") 0 6 6
68 unless $candidate->can('to_app');
69  
70 6 my $app = eval { $candidate->to_app };
71 6 ▶ die "Error normalizing $source through to_app(): $@\n" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 6 6
72  
73 6 ▶ my $app_type = ref($app) || 'non-reference';
Condition50%1/2
ref $app || 'non-reference'
l!l
60
MC/DC0%0/1
ref $app || 'non-reference'
ref $app
Truth table50%1/2
A
A= ref $app
Aresult
11
00
74 6 ▶ die "$source to_app() must return a coderef, got: $app_type\n"
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::reftype($app) // "") eq "CODE" 0 6 6
Condition50%1/2
builtin::reftype($app) // ""
l!l
60
MC/DC0%0/1
builtin::reftype($app) // ""
builtin::reftype($app)
Truth table50%1/2
A
A= builtin::reftype($app)
Aresult
11
00
75 unless (reftype($app) // '') eq 'CODE';
76  
77 6 return $app;
78 }
79  
80 164 1;
« blib/lib/PAGI/Server/EventValidator.pm ↑ Summary examples/07-extension-fullflush/app.pl »