File Coverage

lib/Mojolicious/Plugin/AssetPack/Pipe/CombineFile.pm
Criterion Covered Total %
statement 40 45 88.8
branch 7 12 58.3
condition 5 10 50.0
subroutine 8 8 100.0
pod 2 2 100.0
total 62 77 80.5


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