blib/lib/Plack/Middleware/CSS/Compressor.pm

filter: Statementstmt 93.1% 41 / 44 Branchbran 50.0% 6 / 12 Conditioncond 33.3% 2 / 6 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 100% 9 / 9 Podpod 100% 2 / 2 totaltotal 78.9% 60 / 76 CC 9 SCAR 15.2
CC
9
Cov
79%
call
8.8
prepare_app
3.1
BEGIN
1.0
CRAP 4.6
↑ Summary
1 package Plack::Middleware::CSS::Compressor;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 ▶use parent qw( Plack::Middleware );
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 ▶use CSS::Compressor qw( css_compress );
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 2 ▶use Plack::Util::Accessor qw( suffix );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Plack::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 2 our $VERSION = '0.01';
14  
15 sub prepare_app
16 1 ▶{
Subroutine prepare_app: called CC=3 SCAR=11.3
Pod documented
17 1 my $self = shift;
18 1 my $suffix = $self->suffix();
19  
20 1 ▶ unless ( defined( $suffix ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $suffix) 1 0 1
21 1 $suffix = '-min';
22 }
23  
24 # make the suffix usable in regular expressions
25 1 ▶ $self->suffix( ref( $suffix )
Branch: 1 branch
BranchTrueFalseTotal
ref $suffix ? : 0 1 1
26 ? $suffix
27 : quotemeta( $suffix )
28 );
29 }
30  
31 sub call
32 3 ▶{
Subroutine call: called CC=7 SCAR=21.8
Pod documented
33 3 my ( $self, $env ) = @_;
34 3 my $suffix = $self->suffix();
35 3 my $path_info = $env->{PATH_INFO};
36  
37 # match and adjust file name and extension
38 3 ▶ unless ( $env->{PATH_INFO} =~ s!\A (.*) $suffix ([.]css) \z!$1$2!x ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($env->{"PATH_INFO"} =~ s/\A (.*) $suffix ([.]css) \z/$1$2/x) 2 1 3
39 # otherwise pass through
40 2 return $self->app->( $env );
41 }
42  
43 # call underlying handler with adjusted path
44 1 my $res = $self->app->( $env );
45  
46 # make sure the response is an array and not streamed
47 1 ▶ unless ( ref( $res ) eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $res eq "ARRAY") 0 1 1
48 0 return $res;
49 }
50  
51 1 my $h = Plack::Util::headers( $res->[1] );
52 1 my $ct = $h->get( 'Content-Type' );
53  
54 # got a stylesheet as response
55 1 ▶ if ( $res->[0] == 200 && $ct && $ct =~ m!\A text/css \b!x ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($res->[0] == 200 and $ct and $ct =~ m[\A text/css \b]x) { } 1 0 1
elsif ($res->[0] == 404) { } 0 0 0
Condition33%2/6
$res->[0] == 200 and $ct and $ct =~ m[\A text/css \b]x
!ll&&!rl&&r
001
$res->[0] == 200 and $ct
!ll&&!rl&&r
001
MC/DC0%0/3
$res->[0] == 200 and $ct and $ct =~ m[\A text/css \b]x
$res->[0] == 200 $ct $ct =~ m[\A text/css \b]x
Truth table25%1/4
A and B and C
A= $res->[0] == 200B= $ctC= $ct =~ m[\A text/css \b]x
ABCresult
0XX0
10X0
1100
1111
56 1 my $css = '';
57  
58 # collect all css chunks
59 1 ▶ Plack::Util::foreach( $res->[2], sub { $css .= $_[0] } );
Subroutine __ANON__: called CC=1 SCAR=0.0
60  
61 # store compressed css as new body
62 1 $res->[2] = [ css_compress( $css ) ];
63  
64 # adjust length
65 1 $h->set( 'Content-Length' => length( $res->[2][0] ) );
66 }
67 # nothing found with the adjusted path
68 elsif ( $res->[0] == 404 ) {
69 # restore original path
70 0 $env->{PATH_INFO} = $path_info;
71 # and try again
72 0 $res = $self->app->( $env );
73 }
74  
75 1 return $res;
76 }
77  
78 2 1;
79  
80 __END__
↑ Summary