| 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"
MC/DC0%0/1
$source //= "Application"
$source
Truth table50%1/2
A
A= $source
| |||||||||||||||||||||
| 60 | |||||||||||||||||||||
| 61 | 692 | ▶ | return $candidate if (reftype($candidate) // '') eq 'CODE'; | ||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
builtin::reftype($candidate) // ""
MC/DC100%1/1
builtin::reftype($candidate) // ""
builtin::reftype($candidate)
Truth table100%2/2
A
A= builtin::reftype($candidate)
| |||||||||||||||||||||
| 62 | |||||||||||||||||||||
| 63 | 9 | ▶ | my $type = ref($candidate) || 'non-reference'; | ||||||||||||||||||
|
Condition100%2/2
ref $candidate || 'non-reference'
MC/DC100%1/1
ref $candidate || 'non-reference'
ref $candidate
Truth table100%2/2
A
A= ref $candidate
| |||||||||||||||||||||
| 64 | 9 | ▶ | die "$source must be a coderef or instantiated application-provider object, got: $type\n" | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 65 | unless blessed($candidate); | ||||||||||||||||||||
| 66 | |||||||||||||||||||||
| 67 | 6 | ▶ | die "$source returned an application-provider object without to_app()\n" | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 72 | |||||||||||||||||||||
| 73 | 6 | ▶ | my $app_type = ref($app) || 'non-reference'; | ||||||||||||||||||
|
Condition50%1/2
ref $app || 'non-reference'
MC/DC0%0/1
ref $app || 'non-reference'
ref $app
Truth table50%1/2
A
A= ref $app
| |||||||||||||||||||||
| 74 | 6 | ▶ | die "$source to_app() must return a coderef, got: $app_type\n" | ||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
builtin::reftype($app) // ""
MC/DC0%0/1
builtin::reftype($app) // ""
builtin::reftype($app)
Truth table50%1/2
A
A= builtin::reftype($app)
| |||||||||||||||||||||
| 75 | unless (reftype($app) // '') eq 'CODE'; | ||||||||||||||||||||
| 76 | |||||||||||||||||||||
| 77 | 6 | return $app; | |||||||||||||||||||
| 78 | } | ||||||||||||||||||||
| 79 | |||||||||||||||||||||
| 80 | 164 | 1; | |||||||||||||||||||