blib/lib/Mojolicious/Plugin/AssetPack/Che.pm

filter: Statementstmt 92.4% 49 / 53 Branchbran 57.6% 15 / 26 Conditioncond 57.1% 12 / 21 MC/DCmcdc 25.0% 2 / 8 Subroutinesub 90.0% 9 / 10 Podpod 75.0% 3 / 4 totaltotal 73.7% 90 / 122 CC 24 SCAR 19.4
CC
24
Cov
76%
process
9.5
register
8.4
__ANON__
8.3
CRAP 7.0
« lib/Mojolicious/Plugin/AssetPack/Pipe/CombineFile.pm ↑ Summary lib/Mojolicious/Plugin/AssetPack/Pipe/HTML.pm »
1 package Mojolicious::Plugin::AssetPack::Che;
2 4 ▶use Mojo::Base 'Mojolicious::Plugin::AssetPack';
Subroutine BEGIN: called CC=1 SCAR=0.0
3 4 ▶use Mojolicious::Plugin::AssetPack::Util qw( DEBUG diag checksum );#
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use Mojo::URL;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 4 has [qw(config app)] => undef, weak=>1;
7 3 ▶has revision => sub { my $app = shift->app; $app->config('revision') // $app->config('version') // $app->config('версия') // ''; };
Subroutine __ANON__: called CC=4 SCAR=17.1
Condition37%3/8
(($app->config("revision") // $app->config("version")) // $app->config("\x{432}\x{435}\x{440}\x{441}\x{438}\x{44f}")) // ""
l!l
03
($app->config("revision") // $app->config("version")) // $app->config("\x{432}\x{435}\x{440}\x{441}\x{438}\x{44f}")
l!l&&r!l&&!r
003
$app->config("revision") // $app->config("version")
l!l&&r!l&&!r
003
MC/DC0%0/3
(($app->config("revision") // $app->config("version")) // $app->config("\x{432}\x{435}\x{440}\x{441}\x{438}\x{44f}")) // ""
$app->config("revision") $app->config("version") $app->config("\x{432}\x{435}\x{440}\x{441}\x{438}\x{44f}")
Truth table25%1/4
A // B // C
A= $app->config("revision")B= $app->config("version")C= $app->config("\x{432}\x{435}\x{440}\x{441}\x{438}\x{44f}")
ABCresult
1XX1
01X1
0011
0000
8  
9 3 ▶sub register {
Subroutine register: called CC=7 SCAR=21.3
Pod documented
10 3 my ($self, $app, $config) = @_;
11 3 $self->config($config);
12 3 $self->app($app);
13 #~ Scalar::Util::weaken($self->{app});
14 3 Mojo::File->new($app->home->rel_file('assets'), 'cache')->remove_tree({keep_root => 1});
15 3 $self->SUPER::register($app, $config);
16
17 # Patch the asset route
18 3 $self->route;
19 3 $app->routes->find('assetpack')->pattern->defaults->{cb} = $self->serve_cb();
20
21 3 $self->store->_types->type(html => ['text/html;charset=UTF-8']);# Restore deleted Jan
22 $self->store->default_headers($config->{default_headers})
23 3 ▶ if $config->{default_headers};
Branch: 1 branch
BranchTrueFalseTotal
if $config->{"default_headers"} 0 3 3
24
25 3 my $process = $config->{process};
26 $self->process(ref eq 'ARRAY' ? @$_ : $_) #($_->[0], map Mojo::URL->new($_), @$_[1..$#$_])
27 0 ▶ for ref $process eq 'HASH' ? map([$_=> ref $process->{$_} eq 'ARRAY' ? @{$process->{$_}} : $process->{$_}], keys %$process) : ref $process eq 'ARRAY' ? @$process : ();
Branch: 4 branches
BranchTrueFalseTotal
ref $process eq 'HASH' ? : 0 3 3
ref $process->{$_} eq 'ARRAY' ? : 0 0 0
ref $process eq 'ARRAY' ? : 3 0 3
ref $_ eq 'ARRAY' ? : 6 0 6
28
29 3 return $self;
30 }
31  
32 # redefine for nested topics
33 6 ▶sub process {
Subroutine process: called CC=8 SCAR=22.5
Pod documented
34 6 my ($self, $topic, @input) = @_;
35 6 utf8::encode($topic);
36
37 6 ▶ $self->route unless $self->{route_added}++;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"route_added"}++ 3 3 6
38 6 ▶ return $self->_process_from_def($topic) unless @input;
Branch: 1 branch
BranchTrueFalseTotal
unless @input 0 6 6
39
40 6 my $assets = Mojo::Collection->new;
41 6 for my $url (@input) {
42 12 utf8::encode($url);
43 12 ▶ if (my $nested = $self->processed($url)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $nested = $self->processed($url)) 0 12 12
44 0 push @$assets, @$nested;
45 0 next;
46 }
47 12 ▶ my $asset = Scalar::Util::blessed($url) ? $url : $self->store->asset($url);
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($url) ? : 0 12 12
48 12 ▶ die qq(Could not find input asset "$url" .) unless Scalar::Util::blessed($asset);
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($asset) 0 12 12
49 12 push @$assets, $asset;
50 }
51
52 0 ▶ return $self->tap(sub { $_->{input}{$topic} = $assets }) if $self->{lazy};
Subroutine __ANON__: not called CC=1 SCAR=1.2
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"lazy"} 0 6 6
53 6 return $self->_process($topic => $assets);
54 }
55  
56 #!!! me frozen at parent version 2.02
57 12 ▶sub processed { $_[0]->{by_topic}{$_[1]} }
Subroutine processed: called CC=1 SCAR=0.0
Pod documented
58  
59 6 ▶sub serve_cb {
Subroutine serve_cb: called CC=1 SCAR=0.0
Pod undocumented
60 6 my $self= shift;
61 10 ▶ return sub {
Subroutine __ANON__: called CC=8 SCAR=21.2
62 10 my $c = shift;
63 10 my $checksum = $c->stash('checksum');
64 10 ▶ if (($c->req->headers->accept_encoding // '') =~ /gzip/i && (my $asset = $self->{by_checksum}{$checksum})) {
Branch: 1 branch
BranchTrueFalseTotal
if (($c->req->headers->accept_encoding // "") =~ /gzip/iu and my $asset = $self->{"by_checksum"}{$checksum}) 9 1 10
Condition60%3/5
($c->req->headers->accept_encoding // "") =~ /gzip/iu and my $asset = $self->{"by_checksum"}{$checksum}
!ll&&!rl&&r
019
$c->req->headers->accept_encoding // ""
l!l
100
MC/DC50%1/2
($c->req->headers->accept_encoding // "") =~ /gzip/iu and my $asset = $self->{"by_checksum"}{$checksum}
$c->req->headers->accept_encoding my $asset = $self->{"by_checksum"}{$checksum}
Truth table66%2/3
A and B
A= $c->req->headers->accept_encodingB= my $asset = $self->{"by_checksum"}{$checksum}
ABresult
0X0
100
111
65 9 my $checksum_gzip = checksum($asset->url.$self->revision.'.gzip');
66 9 ▶ $asset = $self->{by_checksum}{$checksum_gzip}
Branch: 1 branch
BranchTrueFalseTotal
$asset = $self->{"by_checksum"}{$checksum_gzip} and $c->res->headers->content_encoding("gzip") and 1 and $self->store->serve_asset($c, $asset) and return $c->rendered 3 6 9
Condition75%6/8
$asset = $self->{"by_checksum"}{$checksum_gzip} and $c->res->headers->content_encoding("gzip") and 1 and $self->store->serve_asset($c, $asset)
!ll&&!rl&&r
603
$asset = $self->{"by_checksum"}{$checksum_gzip} and $c->res->headers->content_encoding("gzip") and 1
!ll
63
$asset = $self->{"by_checksum"}{$checksum_gzip} and $c->res->headers->content_encoding("gzip")
!ll&&!rl&&r
603
MC/DC33%1/3
$asset = $self->{"by_checksum"}{$checksum_gzip} and $c->res->headers->content_encoding("gzip") and 1 and $self->store->serve_asset($c, $asset)
$asset = $self->{"by_checksum"}{$checksum_gzip} $c->res->headers->content_encoding("gzip") $self->store->serve_asset($c, $asset)
Truth table50%2/4
A and B and C
A= $asset = $self->{"by_checksum"}{$checksum_gzip}B= $c->res->headers->content_encoding("gzip")C= $self->store->serve_asset($c, $asset)
ABCresult
0XX0
10X0
1100
1111
67 and $c->res->headers->content_encoding('gzip')
68 and (DEBUG ? diag("Sent GZIPed topic [%s]", $asset->url) : 1)#$self->minify ? $self->app->log->info('') :
69 and $self->store->serve_asset($c, $asset)
70 and return $c->rendered;
71 }
72 7 Mojolicious::Plugin::AssetPack::_serve($c, @_);
73 6 };
74
75 }
76  
77  
78 =pod
79  
80 =encoding utf8
81  
82 Доброго всем
83  
84 =head1 Mojolicious::Plugin::AssetPack::Che
85  
86 ¡ ¡ ¡ ALL GLORY TO GLORIA ! ! !
87  
88 =head1 NAME
89  
90 Mojolicious::Plugin::AssetPack::Che - Child of Mojolicious::Plugin::AssetPack for little bit code.
91  
92 =head1 DESCRIPTION
93  
94 Can process assets during register plugin.
95  
96 Can nested assets.
97  
98 Can pipe HTML files with L<Mojolicious::Plugin::AssetPack::Pipe::HTML>.
99  
100 Can compile Vue templates L<Mojolicious::Plugin::AssetPack::Pipe::VueTemplateCompiler>.
101  
102 Can pipe CSS, JS, JSON, HTML with L<Mojolicious::Plugin::AssetPack::Pipe::CombineFile> into disk cache. This pipe can also gzip and cache gzipped assets.
103  
104 Since parent version 1.28.
105  
106 =head1 VERSION
107  
108 Version 2.106 (test on base Mojolicious::Plugin::AssetPack v2.10)
109  
110 =cut
111  
112 4 our $VERSION = '2.106';
113  
114  
115 =head1 SYNOPSIS
116  
117 See parent module L<Mojolicious::Plugin::AssetPack> for full documentation.
118  
119 On register the plugin C<config> can contain additional optional argument B<process>:
120  
121 $app->plugin('AssetPack::Che',
122 pipes => [qw(Sass Css JavaScriptPacker HTML CombineFile)],
123 CombineFile => { gzip => {min_size => 1000},}, # pipe options
124 HTML => {minify_opts=>{remove_newlines => 1,}},# pipe based on HTML::Packer
125 JavaScriptPacker => {minify_opts=>{}},# pipe based on JavaScript::Packer
126 process => [
127 ['foo.js' => qw(path/to/foo1.js path/to/foo2.js)],
128 ['bar.js' => qw(foo.js path/to/bar1.js path/to/bar2.js)], # nested topic
129 ['foo.html' => qw(path/to/foo1.html path/to/foo2.html)],
130 ...
131 ],
132 );
133  
134  
135 =head1 SEE ALSO
136  
137 L<Mojolicious::Plugin::AssetPack>
138  
139 =head1 AUTHOR
140  
141 Михаил Че (Mikhail Che), C<< <mche[-at-]cpan.org> >>
142  
143 =head1 BUGS / CONTRIBUTING
144  
145 Please report any bugs or feature requests at L<https://github.com/mche/Mojolicious-Plugin-AssetPack-Che/issues>. Pull requests also welcome.
146  
147 =head1 COPYRIGHT
148  
149 Copyright 2016-2020 Mikhail Che.
150  
151 This library is free software; you can redistribute it and/or modify
152 it under the same terms as Perl itself.
153  
154  
155  
156 =cut
157  
158 4 1; # End of Mojolicious::Plugin::AssetPack::Che
159  
« lib/Mojolicious/Plugin/AssetPack/Pipe/CombineFile.pm ↑ Summary lib/Mojolicious/Plugin/AssetPack/Pipe/HTML.pm »