File Coverage

blib/lib/File/Assets/Util.pm
Criterion Covered Total %
statement 104 115 90.4
branch 39 56 69.6
condition 24 33 72.7
subroutine 20 21 95.2
pod 0 9 0.0
total 187 234 79.9


line stmt bran cond sub pod time code
1             package File::Assets::Util;
2              
3 24     24   129 use strict;
  24         70  
  24         724  
4 24     24   120 use warnings;
  24         39  
  24         552  
5              
6 24     24   128 use File::Assets::Carp;
  24         47  
  24         227  
7              
8 24     24   28539 use MIME::Types();
  24         184561  
  24         638  
9 24     24   268 use Scalar::Util qw/blessed/;
  24         50  
  24         1332  
10 24     24   23427 use Module::Pluggable search_path => q/File::Assets::Filter/, require => 1, sub_name => q/filter_load/;
  24         273107  
  24         179  
11 24     24   26647 use Digest;
  24         11926  
  24         694  
12 24     24   161 use File::Assets::Asset;
  24         90  
  24         3715  
13              
14             {
15             my $types;
16             sub types {
17 363   66 363 0 67664 return $types ||= MIME::Types->new(only_complete => 1);
18             }
19             }
20              
21             sub digest {
22 118     118 0 725 return Digest->new("MD5");
23             }
24              
25             sub parse_name {
26 0     0 0 0 my $class = shift;
27 0         0 my $name = shift;
28 0 0       0 $name = "" unless defined $name;
29 0         0 $name = $name."";
30 0 0       0 return undef unless length $name;
31 0         0 return $name;
32             }
33              
34             sub same_type {
35 24     24   130 no warnings 'uninitialized';
  24         45  
  24         4515  
36 200     200 0 338 my $class = shift;
37 200 50       536 my $aa = $class->parse_type($_[0]) or confess "Couldn't parse: $_[0]";
38 200 50       8225 my $bb = $class->parse_type($_[1]) or confess "Couldn't parse: $_[1]";
39            
40 200         1485 return $aa->simplified eq $bb->simplified;
41             }
42              
43             sub type_extension {
44 195     195 0 386 my $class = shift;
45 195         503 my $type = $class->parse_type($_[0]);
46 195 50       701 croak "Couldn't parse @_" unless $type;
47 195         1610 return ($type->extensions)[0];
48             }
49              
50             sub parse_type {
51 24     24   133 no warnings 'uninitialized';
  24         55  
  24         23313  
52 864     864 0 1440 my $class = shift;
53 864         1164 my $type = shift;
54 864 100       2050 return unless defined $type;
55 808 100 66     5836 return $type if blessed $type && $type->isa("MIME::Type");
56 352 100       1587 $type = ".$type" if $type !~ m/\W+/;
57             # Make sure we get stringified version of $type, whatever it is
58 352         612 $type .= "";
59 352 100       808 $type = "application/javascript" if $type =~ m{^text/javascript$}i;
60 352         611 $type = lc $type;
61 352   66     992 return $class->types->mimeTypeOf($type) || $class->types->type($type);
62             }
63              
64             sub parse_rsc {
65 44     44 0 98 my $class = shift;
66 44         93 my $resource = shift;
67 44         103 my ($uri, $dir, $path) = @_;
68 44 100       209 if (ref $resource eq "ARRAY") {
    50          
    0          
69 41         132 ($uri, $dir, $path) = @$resource;
70             }
71             elsif (ref $resource eq "HASH") {
72 3         15 ($uri, $dir, $path) = @$resource{qw/uri dir path/};
73             }
74             elsif (blessed $resource) {
75 0 0       0 if ($resource->isa("Path::Resource")) {
    0          
76 0         0 return $resource->clone;
77             }
78             elsif ($resource->isa("URI::ToDisk")) {
79 0         0 $uri = $resource->URI;
80 0         0 $dir = $resource->path;
81             }
82             }
83 44         423 return Path::Resource->new(uri => $uri, dir => $dir, path => $path);
84             }
85              
86             my @_filters;
87             sub _filters {
88             return @_filters ||
89 27   33 27   272 grep { ! m/::SUPER$/ } reverse sort __PACKAGE__->filter_load();
90             }
91              
92             sub parse_filter {
93 27     27 0 84 my $class = shift;
94 27         49 my $filter = shift;
95              
96 27         45 my $_filter;
97 27         125 for my $possible ($class->_filters) {
98 300 100       2530 last if $_filter = $possible->new_parse($filter, @_);
99             }
100              
101 27         244 return $_filter;
102             }
103              
104             sub _substitute($$$;$$) {
105 630     630   845 my $target = shift;
106 630         749 my $character = shift;
107 630         769 my $value = shift;
108 630         649 my $deprecated = shift;
109 630         670 my $original_path = shift;
110              
111 630 100       1431 $value = "" unless defined $value;
112              
113 630         604 my $found;
114 630   66     7542 $found ||= $$target =~ s/\%$character/$value/g;
115 630 100 100     5906 $found ||= $$target =~ s/\%\.$character/$value ? "\.$value" : ""/ge;
  31         194  
116 630 100 100     5307 $found ||= $$target =~ s/\%\-$character/$value ? "\-$value" : ""/ge;
  85         437  
117 630 0 66     4515 $found ||= $$target =~ s/\%\/$character/$value ? "\/$value" : ""/ge;
  0         0  
118              
119 630 100 100     2701 carp "\%$character is deprecated as a path pattern (in \"$original_path\")" if $found && $deprecated;
120             }
121              
122             sub build_output_path {
123 72     72 0 5692 my $class = shift;
124 72         124 my $template = shift;
125 72         110 my $filter = shift;
126              
127 72         119 my $path = $template;
128 72 100       310 $path = $path->{path} if ref $path eq "HASH";
129              
130 72 100       281 return $$path if ref $path eq "SCALAR";
131              
132 70 100       182 $path = '%n%-l%-f%.e' unless $path;
133 70 100 66     334 $path = "$path/" if blessed $path && $path->isa("Path::Class::Dir");
134 70 100 66     1397 $path .= '%n%-l%-f%.e' if $path && $path =~ m/\/$/;
135 70 100       483 $path .= '%.e' if $path =~ m/(?:^|\/)[^.]+$/;
136              
137 70         138 local %_;
138 70 100       228 if (ref $filter eq "HASH") {
139 14         96 %_ = %$filter;
140             }
141             else {
142 56         325 %_ = (
143             fingerprint => $filter->fingerprint,
144             name => $filter->assets->name,
145             kind => $filter->kind->kind,
146             head => $filter->kind->head,
147             tail => $filter->kind->tail,
148             extension => $filter->kind->extension,
149             );
150             }
151              
152 70         968 my $original_path = $path;
153              
154 70 50       239 $path =~ s/%b/%-l/g and carp "\%b is deprecated as a path pattern (in \"$original_path\")";
155              
156 70         291 _substitute \$path, e => $_{extension};
157 70         223 _substitute \$path, f => $_{fingerprint};
158 70         238 _substitute \$path, n => $_{name};
159 70         205 _substitute \$path, k => $_{kind};
160 70         216 _substitute \$path, h => $_{head};
161 70         215 _substitute \$path, l => $_{tail};
162              
163 70         264 _substitute \$path, d => $_{fingerprint}, 1 => $original_path;
164 70         769 _substitute \$path, D => $_{fingerprint}, 1 => $original_path;
165 70         217 _substitute \$path, a => $_{tail}, 1 => $original_path;
166              
167 70         171 $path =~ s/%%/%/g;
168              
169 70         604 return $path;
170             }
171              
172             1;