blib/lib/Mojolicious/Plugin/LinkedContent/v9.pm

filter: Statementstmt 93.3% 127 / 136 Branchbran 56.8% 25 / 44 Conditioncond 100% 5 / 5 MC/DCmcdc 100% 3 / 3 Subroutinesub 100% 21 / 21 Podpod 66.6% 4 / 6 totaltotal 86.0% 185 / 215 CC 31 SCAR 16.0
CC
31
Cov
85%
store_items
11.3
include_css
7.6
store_items_reg
6.0
CRAP 4.9
↑ Summary
1 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 package Mojolicious::Plugin::LinkedContent::v9;
4 2 $Mojolicious::Plugin::LinkedContent::v9::VERSION = '0.10';
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 require Mojo::URL;
8 2 ▶use Mojolicious::Plugin::Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use LWP::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use File::Temp;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 2 ▶use base 'Mojolicious::Plugin';
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 2 my %defaults = (
15 'js_base' => '/js',
16 'css_base' => '/css',
17 'reg_config' => 'https://raw.githubusercontent.com/EmilianoBruni/MPLConfig/main/linked_content.cfg',
18 );
19  
20 2 our $reverse = 0;
21  
22 2 my $stashkey = '$linked_store';
23  
24 2 ▶sub register {
Subroutine register: called CC=3 SCAR=11.0
Pod documented
25 2 my ($self, $app, $params) = @_;
26 2 for (qw/js_base css_base reg_config/) {
27 $self->{$_} =
28 6 ▶ defined($params->{$_}) ? delete($params->{$_}) : $defaults{$_};
Branch: 1 branch
BranchTrueFalseTotal
defined $params->{$_} ? : 0 6 6
29 }
30  
31 2 $self->loaded_reg_items($app);
32  
33 2 push @{$app->renderer->classes}, __PACKAGE__;
34  
35 $app->renderer->add_helper(
36 3 ▶ require_js => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
37 3 $self->store_items('js', @_);
38 }
39 2 );
40 $app->renderer->add_helper(
41 3 ▶ require_css => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
42 3 $self->store_items('css', @_);
43 }
44 2 );
45 $app->renderer->add_helper(
46 1 ▶ require_reg => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
47 1 $self->store_items_reg(@_);
48 }
49 2 );
50 $app->renderer->add_helper(
51 4 ▶ include_css => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
52 4 $self->include_css(@_);
53 }
54 2 );
55 $app->renderer->add_helper(
56 4 ▶ include_js => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
57 4 $self->include_js(@_);
58 }
59 2 );
60  
61 2 $app->log->debug("Plugin " . __PACKAGE__ . " registred!");
62 }
63  
64 2 ▶sub loaded_reg_items {
Subroutine loaded_reg_items: called CC=4 SCAR=14.0
Pod undocumented
65 2 my $s = shift;
66 2 my $app = shift;
67 2 $s->{reg_items} = {};
68 2 ▶ return unless ($s->{reg_config});
Branch: 1 branch
BranchTrueFalseTotal
unless $s->{"reg_config"} 0 2 2
69  
70 2 my $file = $s->{reg_config};
71  
72 2 my $tmp;
73 2 ▶ if ($s->_is_remote($file)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s->_is_remote($file)) 2 0 2
74 # download
75 2 my $content = get($file);
76 2 $tmp = File::Temp->new(SUFFIX => '.cfg' );
77 2 print $tmp $content;
78 2 $file = $tmp->filename;
79 2 close($tmp);
80 }
81  
82 2 my $cfg = new Mojolicious::Plugin::Config->new->load($file);
83  
84 2 ▶ $s->{reg_items} = $cfg->{linkedcontent} if (exists $cfg->{linkedcontent});
Branch: 1 branch
BranchTrueFalseTotal
if exists $cfg->{"linkedcontent"} 2 0 2
85 2 $app->log->debug("Registry library loaded at " . $s->{reg_config});
86 }
87  
88 2 ▶sub store_items_reg {
Subroutine store_items_reg: called CC=6 SCAR=17.9
Pod undocumented
89 2 my ($s, $c, @items) = @_;
90 2 foreach my $item (@items) {
91 2 ▶ if (exists $s->{reg_items}->{$item}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $s->{"reg_items"}{$item}) 2 0 2
92 2 my $item_info = $s->{reg_items}->{$item};
93 2 ▶ if (exists $item_info->{deps}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $item_info->{"deps"}) 1 1 2
94 1 $s->store_items_reg($c,@{$item_info->{deps}});
95 }
96 2 foreach (qw/js css/) {
97 3 $s->store_items($_,$c,@{$item_info->{$_}})
98 4 ▶ if exists $item_info->{$_};
Branch: 1 branch
BranchTrueFalseTotal
if exists $item_info->{$_} 3 1 4
99 }
100 }
101 }
102 }
103  
104 9 ▶sub store_items {
Subroutine store_items: called CC=7 SCAR=24.2
Pod documented
105 9 my ($self, $target, $c, @items) = @_;
106  
107 9 my $upd;
108 9 ▶ my $store = $c->stash($stashkey) || {};
Condition100%2/2
$c->stash($stashkey) || {}
l!l
27
MC/DC100%1/1
$c->stash($stashkey) || {}
$c->stash($stashkey)
Truth table100%2/2
A
A= $c->stash($stashkey)
Aresult
11
00
109 9 ▶ for ($reverse ? reverse(@items) : @items) {
Branch: 1 branch
BranchTrueFalseTotal
$Mojolicious::Plugin::LinkedContent::v9::reverse ? : 0 9 9
110 10 ▶ if (exists $store->{'garage'}{$target}{$_}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $store->{"garage"}{$target}{$_}) 0 10 10
111 0 ▶ next unless $reverse;
Branch: 1 branch
BranchTrueFalseTotal
unless $Mojolicious::Plugin::LinkedContent::v9::reverse 0 0 0
112 0 my $x = $_;
113 0 @{$store->{'box'}{$target}} = grep $_ ne $x,
114 0 @{$store->{'box'}{$target}};
115 }
116 10 $store->{'garage'}{$target}{$_} = 1;
117 10 ▶ if (!$reverse) { push(@{$store->{'box'}{$target}}, $_) }
Branch: 1 branch
BranchTrueFalseTotal
if (not $Mojolicious::Plugin::LinkedContent::v9::reverse) { } 10 0 10
118 0 else { unshift(@{$store->{'box'}{$target}}, $_); }
119 }
120 9 $c->stash($stashkey => $store);
121 }
122  
123 4 ▶sub include_js {
Subroutine include_js: called CC=5 SCAR=16.1
Pod documented
124 4 my $self = shift;
125 4 my $c = shift;
126 4 local $reverse = 1;
127 4 ▶ $self->store_items('js', $c, @_) if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 4 4
128 4 my $store = $c->stash($stashkey);
129 4 ▶ return '' unless $store->{'box'}{'js'};
Branch: 1 branch
BranchTrueFalseTotal
unless $store->{"box"}{"js"} 0 4 4
130 4 my @ct;
131 4 for (@{$store->{'box'}{'js'}}) {
132  
133 5 ▶ $_ .= '.js' unless (/\.js$/);
Branch: 1 branch
BranchTrueFalseTotal
unless /\.js$/ 2 3 5
134  
135 5 $c->stash('$linked_item' => $self->_prepend_path($_, 'js_base'));
136  
137 5 push @ct, $c->render_to_string(
138 template => 'LinkedContent/js',
139 format => 'html',
140 handler => 'ep',
141  
142 # template_class is deprecated since Mojolicious 2.62
143 # was removed at some point which broke my code.
144 # But it'll live here for a while
145 template_class => __PACKAGE__
146 );
147 }
148 4 $c->stash('$linked_item', undef);
149 4 return join '', @ct;
150 }
151  
152 4 ▶sub include_css {
Subroutine include_css: called CC=7 SCAR=20.3
Pod documented
153 4 my $self = shift;
154 4 my $c = shift;
155 4 local $reverse = 1;
156 4 ▶ $self->store_items('css', $c, @_) if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 4 4
157 4 my $store = $c->stash($stashkey);
158 4 ▶ return '' unless $store->{'box'}{'css'};
Branch: 1 branch
BranchTrueFalseTotal
unless $store->{"box"}{"css"} 0 4 4
159 4 my @ct;
160 4 for (@{$store->{'box'}{'css'}}) {
161  
162 5 my $stash = {
163 '$linked_media' => 'screen'
164 };
165  
166 5 ▶ if (ref($_) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_ eq 'HASH') { } 0 5 5
167 0 $stash->{'$linked_item'} = $_->{href};
168 0 ▶ $stash->{'$linked_media'} = $_->{media} if (exists $_->{media});
Branch: 1 branch
BranchTrueFalseTotal
if exists $_->{"media"} 0 0 0
169 } else {
170 5 $stash->{'$linked_item'} = $_;
171 }
172  
173 5 ▶ $stash->{'$linked_item'} .= '.css' unless ($stash->{'$linked_item'} =~/\.css$/);
Branch: 1 branch
BranchTrueFalseTotal
unless $stash->{"\$linked_item"} =~ /\.css$/ 2 3 5
174 5 $stash->{'$linked_item'} = $self->_prepend_path($stash->{'$linked_item'}, 'css_base');
175  
176 5 $c->stash($stash);
177  
178 5 push @ct, $c->render_to_string(
179 template => 'LinkedContent/css',
180 format => 'html',
181 handler => 'ep',
182  
183 # template_class is deprecated since Mojolicious 2.62
184 # was removed at some point which broke my code.
185 # But it'll live here for a while
186 template_class => __PACKAGE__
187 );
188 }
189 4 $c->stash('$linked_item', undef);
190 4 return join '', @ct;
191 }
192  
193 10 ▶sub _prepend_path {
Subroutine _prepend_path: called CC=4 SCAR=13.9
194 10 my ($self, $path, $base) = @_;
195  
196 10 my $url = Mojo::URL->new($path);
197 10 ▶ if ($url->is_abs || $url->path->leading_slash) {
Branch: 1 branch
BranchTrueFalseTotal
if ($url->is_abs or $url->path->leading_slash) 8 2 10
Condition100%3/3
$url->is_abs or $url->path->leading_slash
l!l&&r!l&&!r
622
MC/DC100%2/2
$url->is_abs or $url->path->leading_slash
$url->is_abs $url->path->leading_slash
Truth table100%3/3
A or B
A= $url->is_absB= $url->path->leading_slash
ABresult
1X1
011
000
198  
199 # Absolute path or absolute url returned as is
200 8 return $path;
201 }
202  
203 # Basepath not defined
204 2 ▶ return unless $self->{$base};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{$base} 0 2 2
205  
206 # Prepend path with base
207 2 my $basepath = Mojo::Path->new($self->{$base});
208 2 unshift @{$url->path->parts}, @{$basepath->parts};
209  
210 # Inherit leading slash from basepath
211 2 $url->path->leading_slash($basepath->leading_slash);
212  
213 2 return $url->to_string;
214 }
215  
216 2 ▶sub _is_remote {
Subroutine _is_remote: called CC=2 SCAR=7.2
217 2 my ($s, $file) = (shift, shift);
218  
219 2 ▶ return 1 if ($file =~ /^http/);
Branch: 1 branch
BranchTrueFalseTotal
if $file =~ /^http/ 2 0 2
220 }
221 2 1;
222  
223 =pod
224  
225 =head1 NAME
226  
227 Mojolicious::Plugin::LinkedContent::v9 - manage linked css and js
228  
229 =head1 VERSION
230  
231 version 0.10
232  
233 =head1 AUTHOR
234  
235 Emiliano Bruni <info@ebruni.it>
236  
237 =head1 COPYRIGHT AND LICENSE
238  
239 This software is copyright (c) 2021 by Emiliano Bruni.
240  
241 This is free software; you can redistribute it and/or modify it under
242 the same terms as the Perl 5 programming language system itself.
243  
244 =cut
245  
246 __DATA__
↑ Summary