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