File Coverage

blib/lib/Mojolicious/Plugin/AssetPack/Che.pm
Criterion Covered Total %
statement 44 47 93.6
branch 13 26 50.0
condition 5 11 45.4
subroutine 8 9 88.8
pod 3 4 75.0
total 73 97 75.2


line stmt bran cond sub pod time code
1             package Mojolicious::Plugin::AssetPack::Che;
2 4     4   318736 use Mojo::Base 'Mojolicious::Plugin::AssetPack';
  4         195844  
  4         24  
3 4     4   135359 use Mojolicious::Plugin::AssetPack::Util qw( checksum );
  4         10  
  4         161  
4 4     4   40 use Mojo::URL;
  4         7  
  4         28  
5              
6             has [qw(app config)];
7             has revision => sub { my $app = shift->app; $app->config('revision') // $app->config('version') // $app->config('версия') // ''; };
8              
9             sub register {
10 3     3 1 134 my ($self, $app, $config) = @_;
11 3         10 $self->config($config);
12 3         32 $self->app($app);
13 3         27 Scalar::Util::weaken($self->{app});
14 3         31 $self->SUPER::register($app, $config);
15            
16             # Patch the asset route
17 3         622 $self->route;
18 3         2999 $app->routes->find('assetpack')->pattern->defaults->{cb} = $self->serve_cb();
19            
20 3         217 $self->store->_types->type(html => ['text/html;charset=UTF-8']);# Restore deleted Jan
21             $self->store->default_headers($config->{default_headers})
22 3 50       775 if $config->{default_headers};
23            
24 3         8 my $process = $config->{process};
25             $self->process(ref eq 'ARRAY' ? @$_ : $_) #($_->[0], map Mojo::URL->new($_), @$_[1..$#$_])
26 3 50       39 for ref $process eq 'HASH' ? map([$_=> ref $process->{$_} eq 'ARRAY' ? @{$process->{$_}} : $process->{$_}], keys %$process) : ref $process eq 'ARRAY' ? @$process : ();
  6 0       37  
    50          
    50          
27            
28 3         1004 return $self;
29             }
30              
31             # redefine for nested topics
32             sub process {
33 6     6 1 1207 my ($self, $topic, @input) = @_;
34 6         22 utf8::encode($topic);
35            
36 6 100       35 $self->route unless $self->{route_added}++;
37 6 50       24 return $self->_process_from_def($topic) unless @input;
38            
39 6         231 my $assets = Mojo::Collection->new;
40 6         48 for my $url (@input) {
41 12         35 utf8::encode($url);
42 12 50       27 if (my $nested = $self->processed($url)) {
43 0         0 push @$assets, @$nested;
44 0         0 next;
45             }
46 12 50       48 my $asset = Scalar::Util::blessed($url) ? $url : $self->store->asset($url);
47 12 50       2009 die qq(Could not find input asset "$url".) unless Scalar::Util::blessed($asset);
48 12         35 push @$assets, $asset;
49             }
50            
51 6 50   0   21 return $self->tap(sub { $_->{input}{$topic} = $assets }) if $self->{lazy};
  0         0  
52 6         40 return $self->_process($topic => $assets);
53             }
54              
55             #!!! me frozen at parent version 2.02
56 12     12 1 35 sub processed { $_[0]->{by_topic}{$_[1]} }
57              
58             sub serve_cb {
59 6     6 0 37 my $self= shift;
60             return sub {
61 10     10   109377 my $c = shift;
62 10         41 my $checksum = $c->stash('checksum');
63 10 100 50     105 if (($c->req->headers->accept_encoding // '') =~ /gzip/i && (my $asset = $self->{by_checksum}{$checksum})) {
      66        
64 9         361 my $checksum_gzip = checksum($asset->url.$self->revision.'.gzip');
65 9 0 33     199 $asset = $self->{by_checksum}{$checksum_gzip}
      33        
66             and $c->res->headers->content_encoding('gzip')
67             and $self->store->serve_asset($c, $asset)
68             and return $c->rendered;
69             }
70 10         89 Mojolicious::Plugin::AssetPack::_serve($c, @_);
71 6         45 };
72            
73             }
74              
75              
76             =pod
77              
78             =encoding utf8
79              
80             Доброго всем
81              
82             =head1 Mojolicious::Plugin::AssetPack::Che
83              
84             ¡ ¡ ¡ ALL GLORY TO GLORIA ! ! !
85              
86             =head1 NAME
87              
88             Mojolicious::Plugin::AssetPack::Che - Child of Mojolicious::Plugin::AssetPack for little bit code.
89              
90             =head1 DESCRIPTION
91              
92             Can process assets during register plugin.
93              
94             Can pipe HTML files with L.
95              
96             Can pipe CSS, JS, JSON, HTML with L into disk cache. This pipe can also gzip and cache gzipped assets.
97              
98             Since version 1.28.
99              
100             =head1 VERSION
101              
102             Version 2.091 (test on base Mojolicious::Plugin::AssetPack v2.09)
103              
104             =cut
105              
106             our $VERSION = '2.091';
107              
108              
109             =head1 SYNOPSIS
110              
111             See parent module L for full documentation.
112              
113             On register the plugin C can contain additional optional argument B:
114              
115             $app->plugin('AssetPack::Che',
116             pipes => [qw(Sass Css JavaScriptPacker HTML CombineFile)],
117             CombineFile => { gzip => {min_size => 1000},}, # pipe options
118             HTML => {minify_opts=>{remove_newlines => 1,}},# pipe based on HTML::Packer
119             JavaScriptPacker => {minify_opts=>{}},# pipe based on JavaScript::Packer
120             process => [
121             ['foo.js'=>qw(path/to/foo1.js path/to/foo2.js)],
122             ['foo.html'=>qw(path/to/foo1.html path/to/foo2.html)],
123             ...
124             ],
125             );
126              
127              
128             =head1 SEE ALSO
129              
130             L
131              
132             =head1 AUTHOR
133              
134             Михаил Че (Mikhail Che), C<< >>
135              
136             =head1 BUGS / CONTRIBUTING
137              
138             Please report any bugs or feature requests at L. Pull requests also welcome.
139              
140             =head1 COPYRIGHT
141              
142             Copyright 2016+ Mikhail Che.
143              
144             This library is free software; you can redistribute it and/or modify
145             it under the same terms as Perl itself.
146              
147              
148              
149             =cut
150              
151             1; # End of Mojolicious::Plugin::AssetPack::Che
152