File Coverage

blib/lib/Plack/Middleware/CSS/Compressor.pm
Criterion Covered Total %
statement 39 42 92.8
branch 6 12 50.0
condition 2 6 33.3
subroutine 9 9 100.0
pod 2 2 100.0
total 58 71 81.6


line stmt bran cond sub pod time code
1             package Plack::Middleware::CSS::Compressor;
2              
3 2     2   1881979 use strict;
  2         5  
  2         77  
4 2     2   10 use warnings;
  2         3  
  2         63  
5              
6 2     2   1240 use parent qw( Plack::Middleware );
  2         488  
  2         15  
7              
8 2     2   33947 use CSS::Compressor qw( css_compress );
  2         6754  
  2         166  
9              
10 2     2   18 use Plack::Util::Accessor qw( suffix );
  2         6  
  2         25  
11 2     2   80 use Plack::Util;
  2         4  
  2         879  
12              
13             our $VERSION = '0.01';
14              
15             sub prepare_app
16             {
17 1     1 1 86 my $self = shift;
18 1         8 my $suffix = $self->suffix();
19              
20 1 50       111 unless ( defined( $suffix ) ) {
21 1         3 $suffix = '-min';
22             }
23              
24             # make the suffix usable in regular expressions
25 1 50       8 $self->suffix( ref( $suffix )
26             ? $suffix
27             : quotemeta( $suffix )
28             );
29             }
30              
31             sub call
32             {
33 3     3 1 197483 my ( $self, $env ) = @_;
34 3         21 my $suffix = $self->suffix();
35 3         31 my $path_info = $env->{PATH_INFO};
36              
37             # match and adjust file name and extension
38 3 100       62 unless ( $env->{PATH_INFO} =~ s!\A (.*) $suffix ([.]css) \z!$1$2!x ) {
39             # otherwise pass through
40 2         40 return $self->app->( $env );
41             }
42              
43             # call underlying handler with adjusted path
44 1         6 my $res = $self->app->( $env );
45              
46             # make sure the response is an array and not streamed
47 1 50       428 unless ( ref( $res ) eq 'ARRAY' ) {
48 0         0 return $res;
49             }
50              
51 1         8 my $h = Plack::Util::headers( $res->[1] );
52 1         46 my $ct = $h->get( 'Content-Type' );
53              
54             # got a stylesheet as response
55 1 50 33     127 if ( $res->[0] == 200 && $ct && $ct =~ m!\A text/css \b!x ) {
    0 33        
56 1         3 my $css = '';
57              
58             # collect all css chunks
59 1     1   10 Plack::Util::foreach( $res->[2], sub { $css .= $_[0] } );
  1         153  
60              
61             # store compressed css as new body
62 1         72 $res->[2] = [ css_compress( $css ) ];
63              
64             # adjust length
65 1         675 $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         0 $env->{PATH_INFO} = $path_info;
71             # and try again
72 0         0 $res = $self->app->( $env );
73             }
74              
75 1         95 return $res;
76             }
77              
78             1;
79              
80             __END__