File Coverage

lib/Mojolicious/Plugin/AssetPack/Pipe/CombineFile.pm
Criterion Covered Total %
statement 52 53 98.1
branch 9 14 64.2
condition 8 13 61.5
subroutine 9 9 100.0
pod 2 2 100.0
total 80 91 87.9


line stmt bran cond sub pod time code
1             package Mojolicious::Plugin::AssetPack::Pipe::CombineFile;
2 3     3   18256 use Mojo::Base 'Mojolicious::Plugin::AssetPack::Pipe';
  3         7  
  3         20  
3 3     3   740 use Mojolicious::Plugin::AssetPack::Util qw(checksum diag DEBUG);
  3         6  
  3         190  
4 3     3   34 use IO::Compress::Gzip 'gzip';
  3         7  
  3         343  
5 3     3   34 use Mojo::Util qw(url_unescape decode);
  3         7  
  3         3924  
6            
7             has enabled => sub { shift->assetpack->minify };
8             has app => sub { shift->assetpack->app };
9             has config => sub { my $self = shift; my $config = $self->assetpack->config || $self->assetpack->config({}); $config->{CombineFile} ||= {}; };
10             has serve => sub { shift->assetpack->serve_cb };
11             has revision => sub { shift->assetpack->revision // '' };
12              
13              
14             sub new {
15 3     3 1 86 my $self = shift->SUPER::new(@_);
16 3         52 $self->app->routes->any('/assets/*topic')
17             ->methods(qw(HEAD GET))
18             ->name('assetpack by topic')
19             ->to(cb => $self->_cb_route_by_topic);
20 3         91 $self;
21             }
22              
23             sub process {
24 6     6 1 7238 my ($self, $assets) = @_;
25 6         41 my $collect = Mojo::Collection->new;
26 6         59 my $topicURL = Mojo::URL->new($self->topic);
27 6         2071 my $topic = url_unescape($topicURL->path->to_string);#->charset('UTF-8')
28 6 50       1780 my $format = $topicURL->path->[-1] =~ /\.(\w+)$/ ? lc $1 : '';
29 6         180 my $checksum = checksum $topic.$self->revision;#name=>decode('UTF-8', $topic)
30 6         255 my $attrs = {key => "combine-file", url=>$topic, name=>decode('UTF-8', $topic), checksum=>$checksum, minified=>1, format=>$format,};# for store
31 6         143 my $store = $self->assetpack->store;
32            
33             return
34 6 100 100     73 unless $self->enabled || $format ~~ ['html', 'json'];
35            
36 5         44 DEBUG && diag "Process topic [%s]", $topic;
37             #~ return unless $self->enabled;!!! below
38            
39             #~ diag "Load [%s]", $self->assetpack->app->dumper($store->load($attrs));
40            
41 5         12 my (@other, $content) = ();
42            
43 5         12 for my $asset (@$assets) {
44             next
45 10 50       174 if $asset->isa('Mojolicious::Plugin::AssetPack::Asset::Null');
46            
47 10 50 50     31 push @$collect, $asset
48             and next
49             if grep $asset->format eq $_, qw(css js json html);
50            
51 0         0 push @other, $asset;
52            
53             }
54            
55             # preserve assets such as images and font files
56 5         92 @$assets = @other;
57            
58 5 50       16 if (@$collect) {
59             #~ my $format = $collect->[0]->format;
60            
61             #~ return
62             #~ unless $self->enabled || $format ~~ ['html', 'json'];
63            
64 5 50   10   27 $content = $collect->map('content')->map(sub { /\n$/ ? $_ : "$_\n" })->join;
  10         2321  
65             #~ my $checksum = checksum $topic.$self->revision;#
66            
67 5         1121 DEBUG && diag 'Combined [%s] assets into "%s" with revision=[%s] checksum[%s] and format[%s].', scalar @$collect, $topic, $self->revision, $checksum, $format;
68            
69             #~ push @process,
70 5         22 Mojo::File::path($self->app->static->paths->[0], 'cache', $topic)->dirname->make_path;
71 5         1368 $store->save(\$content, $attrs);
72            
73 5         2250 push @$assets,
74             Mojolicious::Plugin::AssetPack::Asset->new(url => $topic)->checksum($checksum)->minified(1)
75             ->content($content);
76             }
77              
78 5 100 33     421 if ($content && $self->config->{gzip} && ($self->config->{gzip}{min_size} || 1000) < $content->size) {
      50        
      66        
79 3         81 gzip \($content->to_string) => \(my $gzip), {-Level => 9};
80 3         18948 my $checksum_gzip = checksum $topic.$self->revision.'.gzip';
81 3         53 DEBUG && diag 'GZIP combined topic=[%s] with revision=[%s] checksum=[%s] and format=[%s] and rate=[%s/%s].', $topic, $self->revision, $checksum, $format, $content->size, length($gzip);
82 3         22 $self->assetpack->{by_checksum}{$checksum_gzip} = $store->save(\$gzip, {key => "combine-file-gzip", url=>$topic.'.gzip', name=>decode('UTF-8', $topic.'.gzip'), checksum=>$checksum_gzip, minified=>1, format=>$format,});# name=>decode('UTF-8', $topic.'.gzip')
83             #~ push @$assets,
84             #~ Mojolicious::Plugin::AssetPack::Asset->new(url => $topic.'.gzip')->checksum($checksum_gzip)->minified(1)
85             #~ ->content($gzip);
86            
87             }
88             }
89              
90             sub _cb_route_by_topic {
91 3     3   1638 my $self = shift;
92            
93             return sub {
94 6     6   58956 my $c = shift;
95 6         22 my $topic = $c->stash('topic');
96 6         77 utf8::encode($topic);
97             #~ my $checksum = checksum Mojo::Util::encode('UTF-8', $topic.$self->revision);
98 6         25 my $checksum = checksum $topic.$self->revision;
99             #~ my $checksum = checksum $topic.($self->app->config('version') // $self->app->config('версия') // '');
100 6         86 DEBUG && diag 'Routing combined topic=[%s] checksum=[%s]', $topic, $checksum;
101 6         24 $c->stash('name'=>$checksum);
102 6         111 $c->stash('checksum'=>$checksum);
103 6         106 return $self->serve->($c);
104 3         21 };
105             }
106            
107             1;
108              
109             =pod
110              
111             =encoding utf8
112              
113             Доброго всем
114              
115             ¡ ¡ ¡ ALL GLORY TO GLORIA ! ! !
116              
117             =head1 NAME
118              
119             Mojolicious::Plugin::AssetPack::Pipe::CombineFile - Store combined and gzipped asset to cache file instead of memory.
120              
121              
122             =head1 SYNOPSIS
123              
124             $app->plugin('AssetPack::Che' => {
125             pipes => [qw(Sass Css JavaScript CombineFile)],
126             CombineFile => { gzip => {min_size => 1000},}, # pipe options
127             process => [
128             ['foo.html'=>qw(templates/foo.html templates/bar.html)],
129             ...,
130             ],
131             });
132              
133             =head1 CONFIG
134              
135             B determine config for this pipe module. Hashref keys:
136              
137             B - hashref options.
138              
139              
140             =head1 ROUTE
141              
142             B will auto place.
143              
144             Get combined asset by url
145              
146             ///assets/foo.html
147              
148             =head1 SEE ALSO
149              
150             L
151              
152             =head1 AUTHOR
153              
154             Михаил Че (Mikhail Che), C<< >>
155              
156             =head1 BUGS / CONTRIBUTING
157              
158             Please report any bugs or feature requests at L. Pull requests also welcome.
159              
160             =head1 COPYRIGHT
161              
162             Copyright 2016-2020 Mikhail Che.
163              
164             This library is free software; you can redistribute it and/or modify
165             it under the same terms as Perl itself.
166              
167             =cut