File Coverage

blib/lib/Catalyst/Controller/AutoAssets/Handler/CSS.pm
Criterion Covered Total %
statement 51 58 87.9
branch 10 16 62.5
condition 3 6 50.0
subroutine 13 14 92.8
pod 0 6 0.0
total 77 100 77.0


line stmt bran cond sub pod time code
1             package Catalyst::Controller::AutoAssets::Handler::CSS;
2 3     3   1504 use strict;
  3         5  
  3         114  
3 3     3   11 use warnings;
  3         4  
  3         76  
4              
5             # VERSION
6              
7 3     3   10 use Moose;
  3         4  
  3         17  
8 3     3   12387 use namespace::autoclean;
  3         5  
  3         23  
9              
10             with 'Catalyst::Controller::AutoAssets::Handler';
11              
12 3     3   209 use Path::Class 0.32 qw( dir file );
  3         77  
  3         186  
13 3     3   12 use Module::Runtime;
  3         2  
  3         17  
14 3     3   1276 use CSS::Scopifier 0.04;
  3         67083  
  3         90  
15 3     3   1207 use CSS::Scopifier::Group;
  3         263099  
  3         1924  
16              
17             has 'minify', is => 'ro', isa => 'Bool', default => sub{0};
18             has 'scopify', is => 'ro', isa => 'Maybe[ArrayRef]', default => sub{undef};
19              
20             sub BUILD {
21             my $self = shift;
22            
23             Catalyst::Exception->throw("No minifier available")
24             if($self->minify && ! $self->minifier);
25             }
26              
27             has 'minifier', is => 'ro', isa => 'Maybe[CodeRef]', lazy => 1, default => sub {
28             my $self = shift;
29             Module::Runtime::require_module('CSS::Minifier');
30             return sub { CSS::Minifier::minify(@_) };
31             };
32              
33             has 'asset_content_type', is => 'ro', isa => 'Str', default => 'text/css';
34             has 'ext', is => 'ro', isa => 'Str', default => 'css';
35              
36             sub is_current_request_arg {
37 13     13 0 20 my ($self, $arg) = @_;
38             return (
39 13 100 66     452 $arg eq $self->current_alias ||
40             $arg eq $self->current_alias . '.' . $self->ext
41             ) ? 1 : 0;
42             }
43              
44             sub asset_request {
45 11     11 0 22 my ( $self, $c, @args ) = @_;
46 11         26 my $want_asset = join('/',@args);
47              
48             #$self->prepare_asset;
49              
50 11 100       33 return $self->unknown_asset($c,$want_asset) unless ($self->asset_name eq $want_asset);
51            
52             # Let browsers cache forever because we're a CAS path! content will always be current
53 9         210 $c->response->header(
54             'Content-Type' => $self->asset_content_type,
55             'Cache-Control' => $self->cache_control_header
56             );
57            
58 9         2491 return $c->response->body( $self->asset_fh );
59             }
60              
61             sub asset_name {
62 14     14 0 257 my $self = shift;
63 14         40 return $self->current_fingerprint . '.' . $self->ext;
64             }
65              
66             sub asset_fh {
67 9     9 0 55 my $self = shift;
68              
69 9         294 my $file = $self->built_file;
70 9 50       38 return undef unless (-f $file);
71            
72 9 50       690 my $fh = file($file)->openr or die "$! : $file\n";
73 9         2249 return $fh;
74             }
75              
76             sub html_head_tags {
77 0     0 0 0 my $self = shift;
78             return
79 0         0 "<!-- AUTO GENERATED BY " . ref($self->Controller) . " (/" .
80             $self->action_namespace($self->_app) . ") -->\r\n" .
81             '<link rel="stylesheet" type="text/css" href="' .
82             $self->asset_path .
83             '" />' .
84             "\r\n<!-- ---- END AUTO GENERATED ASSETS ---- -->\r\n";
85             }
86              
87             sub write_built_file {
88 3     3 0 6 my ($self, $fd, $files) = @_;
89 3 50 33     126 if($self->minify && $self->minifier) {
90 0         0 foreach my $file (@$files) {
91 0 0       0 open(INFILE, $file) or die $!;
92 0         0 $self->minifier->( input => *INFILE, outfile => $fd );
93 0         0 close INFILE;
94 0         0 $fd->write("\r\n");
95             }
96             }
97             else {
98 3         11 $fd->write($_) for ( map { $_->slurp(iomode => '<:raw') . "\r\n" } @$files );
  4         165  
99             }
100            
101             # TODO/FIXME: if we minified this will effectively reverse it. I left
102             # it this way for now because of the annoying, cumbersome CSS::Minifier
103             # API which wants to work with filehandles. Best solution is probably
104             # to use another temporary file (and also do the scopify before the minify)
105 3 100       748 if($self->scopify) {
106 1         3 $fd->close;
107 1         92 my $CSS = CSS::Scopifier::Group->new();
108 1         3772 $CSS->read($self->built_file);
109            
110             # New: If there was an error parsing the CSS, we're better off
111             # dying right away rather than allowing bad code through
112 1         1564 my $err = $CSS->errstr;
113 1 50       7 Catalyst::Exception->throw("CSS::Scopifer::Group/CSS::Tiny error: $err")
114             if($err);
115            
116 1         1 $CSS->scopify(@{$self->scopify});
  1         43  
117 1         87 $self->built_file->spew(iomode => '>:raw', $CSS->write_string);
118             }
119             }
120              
121             1;
122              
123             __END__
124              
125             =pod
126              
127             =head1 NAME
128              
129             Catalyst::Controller::AutoAssets::Handler::CSS - CSS type handler
130              
131             =head1 DESCRIPTION
132              
133             This is the Handler class for the 'CSS' asset type. This is a core type and is
134             documented in L<Catalyst::Controller::AutoAssets>.
135              
136             =head1 SEE ALSO
137              
138             =over
139              
140             =item L<Catalyst::Controller::AutoAssets::Handler>
141              
142             =back
143              
144             =head1 AUTHOR
145              
146             Henry Van Styn <vanstyn@cpan.org>
147              
148             =head1 COPYRIGHT AND LICENSE
149              
150             This software is copyright (c) 2013 by IntelliTree Solutions llc.
151              
152             This is free software; you can redistribute it and/or modify it under
153             the same terms as the Perl 5 programming language system itself.
154              
155             =cut
156