| 1 | package Catalyst::Controller::AutoAssets::Handler::CSS; | ||||||||||||||||||||||||||||
| 2 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | # VERSION | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 2 | ▶ | use Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use namespace::autoclean; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 2 | with 'Catalyst::Controller::AutoAssets::Handler'; | |||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 2 | ▶ | use Path::Class 0.32 qw( dir file ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 2 | ▶ | use Module::Runtime; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 2 | ▶ | use CSS::Scopifier 0.02; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 0 | ▶ | has 'minify', is => 'ro', isa => 'Bool', default => sub{0}; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||
| 17 | 1 | ▶ | has 'scopify', is => 'ro', isa => 'Maybe[ArrayRef]', default => sub{undef}; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | sub BUILD { | ||||||||||||||||||||||||||||
| 20 | 2 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
BUILD: called CC=3 SCAR=13.1
| |||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 2 | ▶ | Catalyst::Exception->throw("No minifier available") | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$self->minify and not $self->minifier
MC/DC0%0/2
$self->minify and not $self->minifier
$self->minify
not $self->minifier
Truth table33%1/3
A and not(B)
A= $self->minifyB= not $self->minifier
| |||||||||||||||||||||||||||||
| 23 | if($self->minify && ! $self->minifier); | ||||||||||||||||||||||||||||
| 24 | } | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | has 'minifier', is => 'ro', isa => 'Maybe[CodeRef]', lazy => 1, default => sub { | ||||||||||||||||||||||||||||
| 27 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 28 | 0 | Module::Runtime::require_module('CSS::Minifier'); | |||||||||||||||||||||||||||
| 29 | 0 | ▶ | return sub { CSS::Minifier::minify(@_) }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 30 | 2 | }; | |||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 2 | has 'asset_content_type', is => 'ro', isa => 'Str', default => 'text/css'; | |||||||||||||||||||||||||||
| 33 | 2 | has 'ext', is => 'ro', isa => 'Str', default => 'css'; | |||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | sub is_current_request_arg { | ||||||||||||||||||||||||||||
| 36 | 11 | ▶ | my ($self, $arg) = @_; | ||||||||||||||||||||||||||
|
Subroutine
is_current_request_arg: called CC=3 SCAR=11.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 37 | return ( | ||||||||||||||||||||||||||||
| 38 | 11 | ▶ | $arg eq $self->current_alias || | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$arg eq $self->current_alias or $arg eq $self->current_alias . '.' . $self->ext
MC/DC50%1/2
$arg eq $self->current_alias or $arg eq $self->current_alias . '.' . $self->ext
$arg eq $self->current_alias
$arg eq $self->current_alias . '.' . $self->ext
Truth table66%2/3
A or B
A= $arg eq $self->current_aliasB= $arg eq $self->current_alias . '.' . $self->ext
| |||||||||||||||||||||||||||||
| 39 | $arg eq $self->current_alias . '.' . $self->ext | ||||||||||||||||||||||||||||
| 40 | ) ? 1 : 0; | ||||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | sub asset_request { | ||||||||||||||||||||||||||||
| 44 | 9 | ▶ | my ( $self, $c, @args ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
asset_request: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 45 | 9 | my $want_asset = join('/',@args); | |||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | #$self->prepare_asset; | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 9 | ▶ | return $self->unknown_asset($c,$want_asset) unless ($self->asset_name eq $want_asset); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | # Let browsers cache forever because we're a CAS path! content will always be current | ||||||||||||||||||||||||||||
| 52 | 7 | $c->response->header( | |||||||||||||||||||||||||||
| 53 | 'Content-Type' => $self->asset_content_type, | ||||||||||||||||||||||||||||
| 54 | 'Cache-Control' => $self->cache_control_header | ||||||||||||||||||||||||||||
| 55 | ); | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 7 | return $c->response->body( $self->asset_fh ); | |||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | sub asset_name { | ||||||||||||||||||||||||||||
| 61 | 12 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
asset_name: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 62 | 12 | return $self->current_fingerprint . '.' . $self->ext; | |||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | sub asset_fh { | ||||||||||||||||||||||||||||
| 66 | 7 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
asset_fh: called CC=3 SCAR=11.3
Pod
undocumented
| |||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 7 | my $file = $self->built_file; | |||||||||||||||||||||||||||
| 69 | 7 | ▶ | return undef unless (-f $file); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 7 | ▶ | my $fh = file($file)->openr or die "$! : $file\n"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | 7 | return $fh; | |||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | sub html_head_tags { | ||||||||||||||||||||||||||||
| 76 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
html_head_tags: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 77 | return | ||||||||||||||||||||||||||||
| 78 | 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 | ▶ | my ($self, $fd, $files) = @_; | ||||||||||||||||||||||||||
|
Subroutine
write_built_file: called CC=8 SCAR=24.4
Pod
undocumented
| |||||||||||||||||||||||||||||
| 88 | 2 | ▶ | if($self->minify && $self->minifier) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$self->minify and $self->minifier
MC/DC0%0/2
$self->minify and $self->minifier
$self->minify
$self->minifier
Truth table33%1/3
A and B
A= $self->minifyB= $self->minifier
| |||||||||||||||||||||||||||||
| 89 | 0 | foreach my $file (@$files) { | |||||||||||||||||||||||||||
| 90 | 0 | ▶ | open(INFILE, $file) or die $!; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 91 | 0 | $self->minifier->( input => *INFILE, outfile => $fd ); | |||||||||||||||||||||||||||
| 92 | 0 | close INFILE; | |||||||||||||||||||||||||||
| 93 | 0 | $fd->write("\r\n"); | |||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | else { | ||||||||||||||||||||||||||||
| 97 | 2 | $fd->write($_) for ( map { $_->slurp(iomode => '<:raw') . "\r\n" } @$files ); | |||||||||||||||||||||||||||
| 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 | ▶ | if($self->scopify) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 105 | 1 | $fd->close; | |||||||||||||||||||||||||||
| 106 | 1 | my $CSS = CSS::Scopifier->new(); | |||||||||||||||||||||||||||
| 107 | 1 | $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 | my $err = $CSS->errstr; | |||||||||||||||||||||||||||
| 112 | 1 | ▶ | Catalyst::Exception->throw("CSS::Scopifer/CSS::Tiny error: $err") | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 113 | if($err); | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 1 | $CSS->scopify(@{$self->scopify}); | |||||||||||||||||||||||||||
| 116 | 1 | $self->built_file->spew(iomode => '>:raw', $CSS->write_string); | |||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 2 | 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 | |||||||||||||||||||||||||||||