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   18070 use Mojo::Base 'Mojolicious::Plugin::AssetPack::Pipe';
  3         8  
  3         17  
3 3     3   526 use Mojolicious::Plugin::AssetPack::Util qw(checksum diag DEBUG);
  3         6  
  3         187  
4 3     3   35 use IO::Compress::Gzip 'gzip';
  3         7  
  3         353  
5 3     3   25 use Mojo::Util qw(url_unescape decode);
  3         5  
  3         4250  
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 81 my $self = shift->SUPER::new(@_);
16 3         50 $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 6967 my ($self, $assets) = @_;
25 6         40 my $collect = Mojo::Collection->new;
26 6         57 my $topicURL = Mojo::URL->new($self->topic);
27 6         2047 my $topic = url_unescape($topicURL->path->to_string);#->charset('UTF-8')
28 6 50       1798 my $format = $topicURL->path->[-1] =~ /\.(\w+)$/ ? lc $1 : '';
29 6         179 my $checksum = checksum $topic.$self->revision;#name=>decode('UTF-8', $topic)
30 6         211 my $attrs = {key => "combine-file", url=>$topic, name=>decode('UTF-8', $topic), checksum=>$checksum, minified=>1, format=>$format,};# for store
31 6         146 my $store = $self->assetpack->store;
32            
33             return
34 6 100 100     63 unless $self->enabled || $format ~~ ['html', 'json'];
35            
36 5         46 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         14 for my $asset (@$assets) {
44             next
45 10 50       173 if $asset->isa('Mojolicious::Plugin::AssetPack::Asset::Null');
46            
47 10 50 50     29 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         93 @$assets = @other;
57            
58 5 50       17 if (@$collect) {
59             #~ my $format = $collect->[0]->format;
60            
61             #~ return
62             #~ unless $self->enabled || $format ~~ ['html', 'json'];
63            
64 5 50   10   25 $content = $collect->map('content')->map(sub { /\n$/ ? $_ : "$_\n" })->join;
  10         2387  
65             #~ my $checksum = checksum $topic.$self->revision;#
66            
67 5         1198 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         25 Mojo::File::path($self->app->static->paths->[0], 'cache', $topic)->dirname->make_path;
71 5         1395 $store->save(\$content, $attrs);
72            
73 5         2115 push @$assets,
74             Mojolicious::Plugin::AssetPack::Asset->new(url => $topic)->checksum($checksum)->minified(1)
75             ->content($content);
76             }
77              
78 5 100 33     419 if ($content && $self->config->{gzip} && ($self->config->{gzip}{min_size} || 1000) < $content->size) {
      50        
      66        
79 3         80 gzip \($content->to_string) => \(my $gzip), {-Level => 9};
80 3         18517 my $checksum_gzip = checksum $topic.$self->revision.'.gzip';
81 3         58 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   1626 my $self = shift;
92            
93             return sub {
94 6     6   58206 my $c = shift;
95 6         27 my $topic = $c->stash('topic');
96 6         70 utf8::encode($topic);
97             #~ my $checksum = checksum Mojo::Util::encode('UTF-8', $topic.$self->revision);
98 6         26 my $checksum = checksum $topic.$self->revision;
99             #~ my $checksum = checksum $topic.($self->app->config('version') // $self->app->config('версия') // '');
100 6         83 DEBUG && diag 'Routing combined topic=[%s] checksum=[%s]', $topic, $checksum;
101 6         22 $c->stash('name'=>$checksum);
102 6         109 $c->stash('checksum'=>$checksum);
103 6         110 return $self->serve->($c);
104 3         20 };
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