File Coverage

blib/lib/Mojolicious/Plugin/AssetPack/Che.pm
Criterion Covered Total %
statement 43 47 91.4
branch 14 26 53.8
condition 8 13 61.5
subroutine 8 9 88.8
pod 3 4 75.0
total 76 99 76.7


line stmt bran cond sub pod time code
1             package Mojolicious::Plugin::AssetPack::Che;
2 4     4   352015 use Mojo::Base 'Mojolicious::Plugin::AssetPack';
  4         193501  
  4         27  
3 4     4   142221 use Mojolicious::Plugin::AssetPack::Util qw( DEBUG diag checksum );#
  4         11  
  4         220  
4 4     4   25 use Mojo::URL;
  4         9  
  4         25  
5              
6             has [qw(config app)] => undef, weak=>1;
7             has revision => sub { my $app = shift->app; $app->config('revision') // $app->config('version') // $app->config('версия') // ''; };
8              
9             sub register {
10 3     3 1 273 my ($self, $app, $config) = @_;
11 3         12 $self->config($config);
12 3         37 $self->app($app);
13             #~ Scalar::Util::weaken($self->{app});
14 3         43 Mojo::File->new($app->home->rel_file('assets'), 'cache')->remove_tree({keep_root => 1});
15 3         956 $self->SUPER::register($app, $config);
16            
17             # Patch the asset route
18 3         622 $self->route;
19 3         1244 $app->routes->find('assetpack')->pattern->defaults->{cb} = $self->serve_cb();
20            
21 3         251 $self->store->_types->type(html => ['text/html;charset=UTF-8']);# Restore deleted Jan
22             $self->store->default_headers($config->{default_headers})
23 3 50       825 if $config->{default_headers};
24            
25 3         9 my $process = $config->{process};
26             $self->process(ref eq 'ARRAY' ? @$_ : $_) #($_->[0], map Mojo::URL->new($_), @$_[1..$#$_])
27 3 0       56 for ref $process eq 'HASH' ? map([$_=> ref $process->{$_} eq 'ARRAY' ? @{$process->{$_}} : $process->{$_}], keys %$process) : ref $process eq 'ARRAY' ? @$process : ();
  0 50       0  
    50          
    50          
28            
29 3         1821 return $self;
30             }
31              
32             # redefine for nested topics
33             sub process {
34 6     6 1 1480 my ($self, $topic, @input) = @_;
35 6         25 utf8::encode($topic);
36            
37 6 100       32 $self->route unless $self->{route_added}++;
38 6 50       25 return $self->_process_from_def($topic) unless @input;
39            
40 6         53 my $assets = Mojo::Collection->new;
41 6         43 for my $url (@input) {
42 12         32 utf8::encode($url);
43 12 50       27 if (my $nested = $self->processed($url)) {
44 0         0 push @$assets, @$nested;
45 0         0 next;
46             }
47 12 50       70 my $asset = Scalar::Util::blessed($url) ? $url : $self->store->asset($url);
48 12 50       2132 die qq(Could not find input asset "$url" .) unless Scalar::Util::blessed($asset);
49 12         40 push @$assets, $asset;
50             }
51            
52 6 50   0   26 return $self->tap(sub { $_->{input}{$topic} = $assets }) if $self->{lazy};
  0         0  
53 6         54 return $self->_process($topic => $assets);
54             }
55              
56             #!!! me frozen at parent version 2.02
57 12     12 1 35 sub processed { $_[0]->{by_topic}{$_[1]} }
58              
59             sub serve_cb {
60 6     6 0 38 my $self= shift;
61             return sub {
62 10     10   118401 my $c = shift;
63 10         30 my $checksum = $c->stash('checksum');
64 10 100 50     109 if (($c->req->headers->accept_encoding // '') =~ /gzip/i && (my $asset = $self->{by_checksum}{$checksum})) {
      66        
65 9         381 my $checksum_gzip = checksum($asset->url.$self->revision.'.gzip');
66 9 50 66     255 $asset = $self->{by_checksum}{$checksum_gzip}
      100        
      33        
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         67 Mojolicious::Plugin::AssetPack::_serve($c, @_);
73 6         72 };
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 pipe HTML files with L.
97              
98             Can pipe CSS, JS, JSON, HTML with L into disk cache. This pipe can also gzip and cache gzipped assets.
99              
100             Since version 1.28.
101              
102             =head1 VERSION
103              
104             Version 2.105 (test on base Mojolicious::Plugin::AssetPack v2.10)
105              
106             =cut
107              
108             our $VERSION = '2.105';
109              
110              
111             =head1 SYNOPSIS
112              
113             See parent module L for full documentation.
114              
115             On register the plugin C can contain additional optional argument B:
116              
117             $app->plugin('AssetPack::Che',
118             pipes => [qw(Sass Css JavaScriptPacker HTML CombineFile)],
119             CombineFile => { gzip => {min_size => 1000},}, # pipe options
120             HTML => {minify_opts=>{remove_newlines => 1,}},# pipe based on HTML::Packer
121             JavaScriptPacker => {minify_opts=>{}},# pipe based on JavaScript::Packer
122             process => [
123             ['foo.js'=>qw(path/to/foo1.js path/to/foo2.js)],
124             ['foo.html'=>qw(path/to/foo1.html path/to/foo2.html)],
125             ...
126             ],
127             );
128              
129              
130             =head1 SEE ALSO
131              
132             L
133              
134             =head1 AUTHOR
135              
136             Михаил Че (Mikhail Che), C<< >>
137              
138             =head1 BUGS / CONTRIBUTING
139              
140             Please report any bugs or feature requests at L. Pull requests also welcome.
141              
142             =head1 COPYRIGHT
143              
144             Copyright 2016-2020 Mikhail Che.
145              
146             This library is free software; you can redistribute it and/or modify
147             it under the same terms as Perl itself.
148              
149              
150              
151             =cut
152              
153             1; # End of Mojolicious::Plugin::AssetPack::Che
154