File Coverage

blib/lib/PAGI/Utils.pm
Criterion Covered Total %
statement 44 44 100.0
branch 25 26 96.1
condition 3 5 60.0
subroutine 9 9 100.0
pod 2 2 100.0
total 83 86 96.5


line stmt bran cond sub pod time code
1             package PAGI::Utils;
2             $PAGI::Utils::VERSION = '0.002000';
3 26     26   358665 use strict;
  26         58  
  26         861  
4 26     26   104 use warnings;
  26         42  
  26         1008  
5 26     26   93 use Exporter 'import';
  26         32  
  26         588  
6 26     26   75 use Future::AsyncAwait;
  26         70  
  26         126  
7 26     26   1163 use Carp qw(croak);
  26         38  
  26         1166  
8 26     26   97 use Scalar::Util qw(blessed);
  26         28  
  26         1026  
9 26     26   9392 use PAGI::Lifespan;
  26         59  
  26         16919  
10              
11             our @EXPORT_OK = qw(handle_lifespan to_app);
12             our %EXPORT_TAGS = (all => \@EXPORT_OK);
13              
14 6     6 1 190717 async sub handle_lifespan {
15 6         14 my ($scope, $receive, $send, %opts) = @_;
16              
17 6   50     15 my $type = $scope->{type} // '';
18 6 100       188 croak "handle_lifespan called with scope type '$type' (expected 'lifespan'). "
19             . "Check scope type before calling: "
20             . "return await handle_lifespan(...) if \$scope->{type} eq 'lifespan'"
21             unless $type eq 'lifespan';
22              
23 5         19 my $manager = PAGI::Lifespan->for_scope($scope);
24 5 100 66     20 $manager->register(%opts) if $opts{startup} || $opts{shutdown};
25              
26 5         13 return await $manager->handle($scope, $receive, $send);
27             }
28              
29             sub to_app {
30 333     333 1 191037 my ($thing) = @_;
31              
32 333 100       836 croak "to_app() requires an app, component, or class name"
33             unless defined $thing;
34              
35 332 100       1867 return $thing if ref($thing) eq 'CODE';
36              
37 33 100       98 if (blessed($thing)) {
38 20 100       122 return $thing->to_app if $thing->can('to_app');
39 2 100       162 croak ref($thing) . " looks like middleware, not an app"
40             . " - pass it to enable(), or wrap an app with ->wrap(\$app)"
41             if $thing->can('wrap');
42 1         83 croak "Cannot coerce " . ref($thing) . " object to a PAGI app (no to_app method)";
43             }
44              
45 13 100       31 if (!ref($thing)) {
46 12 100       150 croak "Cannot coerce '$thing' to a PAGI app"
47             unless $thing =~ /\A\w+(?:::\w+)*\z/;
48 11 100       115 unless ($thing->can('to_app')) {
49 5         5 local $@;
50 5 100       471 eval "require $thing; 1" or croak "Failed to load '$thing': $@";
51             }
52 9 100       73 return $thing->to_app if $thing->can('to_app');
53 1 50       6 croak "'$thing' looks like middleware, not an app - pass it to enable()"
54             if $thing->can('wrap');
55 1         89 croak "'$thing' does not have a to_app() method";
56             }
57              
58 1         70 croak "Cannot coerce " . ref($thing) . " reference to a PAGI app";
59             }
60              
61             1;
62              
63             __END__