File Coverage

blib/lib/Catalyst/Controller/AutoAssets/Handler/CSS.pm
Criterion Covered Total %
statement 48 55 87.2
branch 10 16 62.5
condition 3 6 50.0
subroutine 12 13 92.3
pod 0 6 0.0
total 73 96 76.0


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