lib/Dancer2/Core/MIME.pm

filter: Statementstmt 82.3% 42 / 51 Branchbran 83.3% 5 / 6 Conditioncond 83.3% 5 / 6 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 82.3% 14 / 17 Podpod 100% 7 / 7 totaltotal 81.1% 73 / 90 CC 6 SCAR 5.7
CC
6
Cov
83%
for_name
3.0
BEGIN
3.0
__ANON__
2.0
CRAP 1.8
« lib/Dancer2/Core/Role/HasConfig.pm ↑ Summary blib/lib/Dancer2/FileUtils.pm »
1 # ABSTRACT: Class to ease manipulation of MIME types
2  
3 package Dancer2::Core::MIME;
4 188 $Dancer2::Core::MIME::VERSION = '2.3.0';
5 188 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 188 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 188 ▶use Plack::MIME;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 188 ▶use Dancer2::Core::Types;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 188 ▶use Module::Runtime 'require_module';
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 # Initialise MIME::Types at compile time, to ensure it's done before
13 # the fork in a preforking webserver like mod_perl or Starman. Not
14 # doing this leads to all MIME types being returned as "text/plain",
15 # as MIME::Types fails to load its mappings from the DATA handle. See
16 # t/04_static_file/003_mime_types_reinit.t and GH#136.
17 188 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=10.9
18 0 ▶ if ( eval { require_module('MIME::Types'); 1; } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { &require_module("MIME::Types"); 1 } }) 0 188 188
19 0 my $mime_types = MIME::Types->new(only_complete => 1);
20 Plack::MIME->set_fallback(
21 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
22 0 $mime_types->mimeTypeOf($_[0])
23 }
24 0 );
25 }
26 }
27  
28 188 ▶use constant { 'DEFAULT_MIME_TYPE' => 'application/data' };
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 has custom_types => (
31 is => 'ro',
32 isa => HashRef,
33 568 ▶ default => sub { +{} },
Subroutine __ANON__: called CC=1 SCAR=0.0
34 188 );
35  
36 has default => (
37 is => 'rw',
38 isa => Str,
39 568 ▶ default => sub { DEFAULT_MIME_TYPE() },
Subroutine __ANON__: called CC=1 SCAR=0.0
40 188 );
41  
42 0 ▶sub reset_to_default {
Subroutine reset_to_default: not called CC=1 SCAR=6.9
Pod documented
43 0 my ($self) = @_;
44 0 $self->default( DEFAULT_MIME_TYPE() );
45 }
46  
47 0 ▶sub reset_default {
Subroutine reset_default: not called CC=1 SCAR=6.9
Pod documented
48 0 my ($self) = @_;
49 0 Carp::carp 'DEPRECATED: reset_default internal method, replaced by reset_to_default';
50 0 goto &reset_to_default;
51 }
52  
53 7 ▶sub add_type {
Subroutine add_type: called CC=1 SCAR=0.0
Pod documented
54 7 my ( $self, $name, $type ) = @_;
55 7 $self->custom_types->{$name} = $type;
56 7 return;
57 }
58  
59 5 ▶sub add_alias {
Subroutine add_alias: called CC=1 SCAR=0.0
Pod documented
60 5 my ( $self, $alias, $orig ) = @_;
61 5 my $type = $self->for_name($orig);
62 5 $self->add_type( $alias, $type );
63 5 return $type;
64 }
65  
66 29 ▶sub for_file {
Subroutine for_file: called CC=2 SCAR=6.9
Pod documented
67 29 my ( $self, $filename ) = @_;
68 29 my ($ext) = $filename =~ /\.([^.]+)$/;
69 29 ▶ return $self->default unless $ext;
Branch: 1 branch
BranchTrueFalseTotal
unless $ext 1 28 29
70 28 return $self->for_name($ext);
71 }
72  
73 1513 ▶sub name_or_type {
Subroutine name_or_type: called CC=2 SCAR=6.9
Pod documented
74 1513 my ( $self, $name ) = @_;
75  
76 1513 ▶ return $name if $name =~ m{/}; # probably a mime type
Branch: 1 branch
BranchTrueFalseTotal
if $name =~ m[/] 1512 1 1513
77 1 return $self->for_name($name);
78 }
79  
80 36 ▶sub for_name {
Subroutine for_name: called CC=3 SCAR=11.0
Pod documented
81 36 my ( $self, $name ) = @_;
82  
83 return
84 36 ▶ $self->custom_types->{ lc $name }
Condition83%5/6
$self->custom_types->{lc $name} || 'Plack::MIME'->mime_type(lc ".$name") || $self->default
l!l&&r!l&&!r
3510
$self->custom_types->{lc $name} or "Plack::MIME"->mime_type(lc ".$name")
l!l&&r!l&&!r
8271
MC/DC0%0/3
$self->custom_types->{lc $name} || 'Plack::MIME'->mime_type(lc ".$name") || $self->default
$self->custom_types->{lc $name} "Plack::MIME"->mime_type(lc ".$name") $self->default
Truth table75%3/4
A or B || C
A= $self->custom_types->{lc $name}B= "Plack::MIME"->mime_type(lc ".$name")C= $self->default
ABCresult
1XX1
01X1
0011
0000
85 || Plack::MIME->mime_type( lc ".$name" )
86 || $self->default;
87 }
88  
89 188 1;
90  
91 __END__
« lib/Dancer2/Core/Role/HasConfig.pm ↑ Summary blib/lib/Dancer2/FileUtils.pm »