blib/lib/Mojolicious/Plugin/Inertia.pm

filter: Statementstmt 100% 50 / 50 Branchbran 100% 22 / 22 Conditioncond 76.4% 13 / 17 MC/DCmcdc 55.5% 5 / 9 Subroutinesub 100% 6 / 6 Podpod 100% 1 / 1 totaltotal 92.3% 97 / 105 CC 20 SCAR 23.0
CC
20
Cov
96%
__ANON__
14.0
register
7.0
BEGIN
1.0
CRAP 10.0
↑ Summary
1 package Mojolicious::Plugin::Inertia;
2 8 ▶use Mojo::Base 'Mojolicious::Plugin';
Subroutine BEGIN: called CC=1 SCAR=0.0
3 8 ▶use Mojo::JSON qw(encode_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
4 8 ▶use Scalar::Util qw(reftype);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 8 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 8 our $VERSION = "0.01";
8  
9 10 ▶sub register {
Subroutine register: called CC=7 SCAR=19.5
Pod documented
10 10 my ($self, $app, $conf) = @_;
11 10 ▶ $conf ||= {};
Condition50%1/2
$conf ||= {}
l!l
100
MC/DC0%0/1
$conf ||= {}
$conf
Truth table50%1/2
A
A= $conf
Aresult
11
00
12  
13 10 ▶ croak "Inertia plugin requires a 'version' configuration option" unless $conf->{version};
Branch: 1 branch
BranchTrueFalseTotal
unless $conf->{"version"} 1 9 10
14 9 ▶ croak "Inertia plugin requires a 'layout' configuration option" unless $conf->{layout};
Branch: 1 branch
BranchTrueFalseTotal
unless $conf->{"layout"} 1 8 9
15  
16 # Asset versioning. e.g. md5sum of your assets manifest file.
17 8 my $version = $conf->{version};
18  
19 # Layout template for non-Inertia requests.
20 # It must contain a <%= data_page %> placeholder at the application root.
21 8 ▶ my $layout = ref $conf->{layout} ? $conf->{layout}->slurp : $conf->{layout};
Branch: 1 branch
BranchTrueFalseTotal
ref $conf->{'layout'} ? : 1 7 8
22  
23 # History encryption settings (optional).
24 # Ref: https://inertiajs.com/history-encryption
25 8 ▶ my $default_encrypt_history = defined $conf->{encrypt_history} ? $conf->{encrypt_history} : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $conf->{'encrypt_history'} ? : 1 7 8
26 8 ▶ my $default_clear_history = defined $conf->{clear_history} ? $conf->{clear_history} : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $conf->{'clear_history'} ? : 1 7 8
27  
28 15 ▶ $app->helper(inertia => sub {
Subroutine __ANON__: called CC=14 SCAR=26.4
29 15 my ($c, $component, $props, $options) = @_;
30 15 ▶ $props ||= {};
Condition50%1/2
$props ||= {}
l!l
150
MC/DC0%0/1
$props ||= {}
$props
Truth table50%1/2
A
A= $props
Aresult
11
00
31 15 ▶ $options ||= {};
Condition100%2/2
$options ||= {}
l!l
114
MC/DC100%1/1
$options ||= {}
$options
Truth table100%2/2
A
A= $options
Aresult
11
00
32  
33 # Options
34 15 ▶ my $encrypt_history = defined $options->{encrypt_history} ? $options->{encrypt_history} : $default_encrypt_history;
Branch: 1 branch
BranchTrueFalseTotal
defined $options->{'encrypt_history'} ? : 1 14 15
35 15 ▶ my $clear_history = defined $options->{clear_history} ? $options->{clear_history} : $default_clear_history;
Branch: 1 branch
BranchTrueFalseTotal
defined $options->{'clear_history'} ? : 1 14 15
36  
37 # If the client's asset version does not match the server's version,
38 # then the client must do a full page reload.
39 # So, we respond with a 409 status and an X-Inertia-Location header
40 # Ref: https://inertiajs.com/the-protocol#asset-versioning
41 15 my $inertia_version = $c->req->headers->header('X-Inertia-Version');
42 15 ▶ if ($c->req->method eq 'GET' && $inertia_version && $inertia_version ne $version) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->req->method eq "GET" and $inertia_version and $inertia_version ne $version) 1 14 15
Condition83%5/6
$c->req->method eq "GET" and $inertia_version and $inertia_version ne $version
!ll&&!rl&&r
1311
$c->req->method eq "GET" and $inertia_version
!ll&&!rl&&r
0132
MC/DC66%2/3
$c->req->method eq "GET" and $inertia_version and $inertia_version ne $version
$c->req->method eq "GET" $inertia_version $inertia_version ne $version
Truth table75%3/4
A and B and C
A= $c->req->method eq "GET"B= $inertia_versionC= $inertia_version ne $version
ABCresult
0XX0
10X0
1100
1111
43 1 $c->res->headers->header('X-Inertia-Location' => $c->req->url->to_string);
44 1 return $c->rendered(409);
45 }
46  
47 # Partial reloads allows you to request a subset of the props (data) from the server on subsequent visits to the same page component.
48 # Ref: https://inertiajs.com/the-protocol#partial-reloads
49 14 my $partial_data = $c->req->headers->header('X-Inertia-Partial-Data');
50 14 my $partial_component = $c->req->headers->header('X-Inertia-Partial-Component');
51 14 ▶ if ($partial_data && $partial_component) {
Branch: 1 branch
BranchTrueFalseTotal
if ($partial_data and $partial_component) 4 10 14
Condition66%2/3
$partial_data and $partial_component
!ll&&!rl&&r
1004
MC/DC50%1/2
$partial_data and $partial_component
$partial_data $partial_component
Truth table66%2/3
A and B
A= $partial_dataB= $partial_component
ABresult
0X0
100
111
52 4 my @only_keys = split /,/, $partial_data;
53 4 $props = { map { $_ => $props->{$_} } @only_keys };
54 }
55  
56 # Resolve props that are coderefs by calling them with the current controller context.
57 # Code refs are useful for lazy loading data only when needed.
58 14 my $resolved_props = {};
59 14 for my $key (keys %$props) {
60 21 my $prop = $props->{$key};
61 21 ▶ $resolved_props->{$key} = (reftype($prop) || '') eq 'CODE' ? $prop->($c) : $prop;
Branch: 1 branch
BranchTrueFalseTotal
(builtin::reftype($prop) || '') eq 'CODE' ? : 3 18 21
Condition100%2/2
builtin::reftype($prop) || ''
l!l
138
MC/DC100%1/1
builtin::reftype($prop) || ''
builtin::reftype($prop)
Truth table100%2/2
A
A= builtin::reftype($prop)
Aresult
11
00
62 }
63  
64 # Construct the page object.
65 # Ref: https://inertiajs.com/the-protocol#the-page-object
66 14 my $page_object = {
67 component => $component,
68 props => $resolved_props,
69 url => $c->req->url->to_string,
70 version => $version,
71 encryptHistory => $encrypt_history,
72 clearHistory => $clear_history,
73 };
74  
75 # Check if the request is an Inertia request.
76 # If so, return a JSON response.
77 # Else, return an HTML response with embedded page object.
78 # Ref: https://inertiajs.com/the-protocol#inertia-responses
79 14 my $is_inertia = $c->req->headers->header('X-Inertia');
80  
81 14 ▶ if ($is_inertia) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_inertia) { } 11 3 14
82 11 $c->res->headers->header('X-Inertia' => 'true');
83 11 $c->res->headers->header('Vary' => 'X-Inertia');
84 11 return $c->render(json => $page_object);
85 }
86 else {
87 3 $c->res->headers->header('Vary' => 'X-Inertia');
88 3 return $c->render(
89 inline => $layout,
90 format => 'html',
91 data_page => encode_json($page_object)
92 );
93 }
94 8 });
95 }
96  
97 8 1;
98 __END__
↑ Summary