blib/lib/Mojolicious/Plugin/TagHelpers/NoCaching.pm

filter: Statementstmt 93.3% 70 / 75 Branchbran 75.0% 24 / 32 Conditioncond 50.0% 1 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 100% 12 / 12 Podpod 100% 1 / 1 totaltotal 87.8% 108 / 123 CC 19 SCAR 17.3
CC
19
Cov
87%
_href2filepath
11.7
_nc_href
6.0
_href2absolute
3.0
CRAP 5.6
↑ Summary
1 package Mojolicious::Plugin::TagHelpers::NoCaching;
2  
3 2 ▶use Mojo::Base 'Mojolicious::Plugin';
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use Mojo::URL;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Cwd;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 our $VERSION = '0.05';
9  
10 2 ▶sub register {
Subroutine register: called CC=2 SCAR=6.9
Pod documented
11 2 my ($plugin, $app, $cfg) = @_;
12
13 2 ▶ $cfg->{key} = 'nc' unless defined $cfg->{key};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $cfg->{"key"} 1 1 2
14
15 2 $plugin->{url2path} = {};
16 2 $plugin->{path2key} = {};
17 2 $plugin->{cfg} = $cfg;
18
19 6 ▶ $app->helper(stylesheet_nc => sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
20 6 my $self = shift;
21
22 6 ▶ if (@_ % 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ % 2) 6 0 6
23 # this is css url
24 6 my $href = $plugin->_nc_href($self, shift);
25 6 unshift @_, $href;
26 }
27
28 6 $self->stylesheet(@_);
29 2 });
30
31 5 ▶ $app->helper(javascript_nc => sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
32 5 my $self = shift;
33
34 5 ▶ if (@_ % 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ % 2) 5 0 5
35 # this is script url
36 5 my $href = $plugin->_nc_href($self, shift);
37 5 unshift @_, $href;
38 }
39
40 5 $self->javascript(@_);
41 2 });
42
43 4 ▶ $app->helper(image_nc => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
44 4 my $self = shift;
45
46 4 my $href = $plugin->_nc_href($self, shift);
47 4 unshift @_, $href;
48
49 4 $self->image(@_);
50 2 });
51 }
52  
53 15 ▶sub _href2absolute {
Subroutine _href2absolute: called CC=3 SCAR=11.0
54 15 my ($controller, $href) = @_;
55
56 15 $href =~ s/\?.+//; # query params
57 15 ▶ if ($href =~ m!^(?:/|[a-z]+://)!i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($href =~ m[^(?:/|[a-z]+://)]iu) 6 9 15
58 # absolute
59 6 return $href;
60 }
61
62 9 my $path = $controller->req->url->path;
63 9 $path =~ s![^/]+$!!;
64 9 ▶ $path = '/' if $path eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $path eq "" 0 9 9
65 9 $href = $path . $href;
66
67 9 return $href;
68 }
69  
70 12 ▶sub _href2filepath {
Subroutine _href2filepath: called CC=9 SCAR=24.6
71 12 my ($controller, $href) = @_;
72
73 12 ▶ if ($href =~ m!^[a-z]+://!i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($href =~ m[^[a-z]+://]iu) 0 12 12
74 0 my $url = Mojo::URL->new($href);
75 0 my $c_url = $controller->req->url->to_abs;
76
77 0 ▶ if ($url->host ne $c_url->host) {
Branch: 1 branch
BranchTrueFalseTotal
if ($url->host ne $c_url->host) 0 0 0
78 # external url
79 0 return;
80 }
81
82 0 $href = $url->path;
83 }
84
85 12 my $static =$controller->app->static;
86 12 ▶ my $asset = $static->file($href)
Branch: 1 branch
BranchTrueFalseTotal
my $asset = $static->file($href) or return 8 4 12
87 or return;
88
89 8 ▶ $asset->is_file
Branch: 1 branch
BranchTrueFalseTotal
$asset->is_file or return 8 0 8
90 or return;
91
92 8 ▶ my $path = File::Spec->canonpath(Cwd::realpath($asset->path)||return);
Condition50%1/2
Cwd::realpath($asset->path) || (return)
l!l
80
MC/DC0%0/1
Cwd::realpath($asset->path) || (return)
Cwd::realpath($asset->path)
Truth table50%1/2
A
A= Cwd::realpath($asset->path)
Aresult
11
00
93 8 my $ok;
94 8 for my $p (@{$static->paths}) {
95 8 ▶ $ok = index($path, $p) == 0
Branch: 1 branch
BranchTrueFalseTotal
$ok = index($path, $p) == 0 and last 7 1 8
96 and last;
97 }
98 # check is found file is inside public directory
99 8 ▶ $ok or return;
Branch: 1 branch
BranchTrueFalseTotal
$ok or return 7 1 8
100
101 7 return $path;
102 }
103  
104 7 ▶sub _nc_key {
Subroutine _nc_key: called CC=1 SCAR=0.0
105 7 my ($self, $path) = @_;
106 7 return (stat($path))[9];
107 }
108  
109 15 ▶sub _nc_href {
Subroutine _nc_href: called CC=6 SCAR=17.9
110 15 my ($self, $controller, $href) = @_;
111
112 15 my $abs_href = _href2absolute($controller, $href);
113
114 15 ▶ unless (exists $self->{url2path}{$abs_href}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"url2path"}{$abs_href}) 12 3 15
115 12 $self->{url2path}{$abs_href} = _href2filepath($controller, $abs_href);
116 }
117
118 15 ▶ my $path = $self->{url2path}{$abs_href}
Branch: 1 branch
BranchTrueFalseTotal
my $path = $self->{"url2path"}{$abs_href} or return $href 8 7 15
119 or return $href;
120
121 8 ▶ unless (exists $self->{path2key}{$path}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"path2key"}{$path}) 7 1 8
122 7 $self->{path2key}{$path} = $self->_nc_key($path);
123 }
124
125 8 ▶ my $key = $self->{path2key}{$path}
Branch: 1 branch
BranchTrueFalseTotal
my $key = $self->{"path2key"}{$path} or return $href 8 0 8
126 or return $href;
127
128 8 ▶ $href .= index($href, '?') == -1 ? '?' : '&';
Branch: 1 branch
BranchTrueFalseTotal
index($href, '?') == -1 ? : 6 2 8
129 8 $href .= $self->{cfg}{key} . '=' . $key;
130
131 # fix for https://github.com/kraih/mojo/issues/565
132 8 return Mojo::URL->new($href);
133 }
134  
135 2 1;
136  
137 __END__
↑ Summary