File Coverage

blib/lib/File/Assets/Util.pm
Criterion Covered Total %
statement 103 115 89.5
branch 37 56 66.0
condition 23 33 69.7
subroutine 20 21 95.2
pod 0 9 0.0
total 183 234 78.2


line stmt bran cond sub pod time code
1             package File::Assets::Util;
2              
3 23     23   121 use strict;
  23         44  
  23         721  
4 23     23   118 use warnings;
  23         42  
  23         1213  
5              
6 23     23   126 use File::Assets::Carp;
  23         37  
  23         221  
7              
8 23     23   27370 use MIME::Types();
  23         155393  
  23         735  
9 23     23   275 use Scalar::Util qw/blessed/;
  23         50  
  23         1498  
10 23     23   30614 use Module::Pluggable search_path => q/File::Assets::Filter/, require => 1, sub_name => q/filter_load/;
  23         263104  
  23         184  
11 23     23   31643 use Digest;
  23         12488  
  23         9371  
12 23     23   182 use File::Assets::Asset;
  23         48  
  23         3651  
13              
14             {
15             my $types;
16             sub types {
17 346   66 346 0 51009 return $types ||= MIME::Types->new(only_complete => 1);
18             }
19             }
20              
21             sub digest {
22 118     118 0 775 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 23     23   133 no warnings 'uninitialized';
  23         48  
  23         4735  
36 193     193 0 314 my $class = shift;
37 193 50       505 my $aa = $class->parse_type($_[0]) or confess "Couldn't parse: $_[0]";
38 193 50       7942 my $bb = $class->parse_type($_[1]) or confess "Couldn't parse: $_[1]";
39            
40 193         1598 return $aa->simplified eq $bb->simplified;
41             }
42              
43             sub type_extension {
44 188     188 0 290 my $class = shift;
45 188         498 my $type = $class->parse_type($_[0]);
46 188 50       670 croak "Couldn't parse @_" unless $type;
47 188         1431 return ($type->extensions)[0];
48             }
49              
50             sub parse_type {
51 23     23   142 no warnings 'uninitialized';
  23         47  
  23         24930  
52 831     831 0 1299 my $class = shift;
53 831         1201 my $type = shift;
54 831 100       2136 return unless defined $type;
55 777 100 66     5574 return $type if blessed $type && $type->isa("MIME::Type");
56 335 100       1513 $type = ".$type" if $type !~ m/\W+/;
57             # Make sure we get stringified version of $type, whatever it is
58 335         613 $type .= "";
59 335 100       781 $type = "application/javascript" if $type =~ m{^text/javascript$}i;
60 335         621 $type = lc $type;
61 335   66     940 return $class->types->mimeTypeOf($type) || $class->types->type($type);
62             }
63              
64             sub parse_rsc {
65 39     39 0 188 my $class = shift;
66 39         87 my $resource = shift;
67 39         111 my ($uri, $dir, $path) = @_;
68 39 100       203 if (ref $resource eq "ARRAY") {
    50          
    0          
69 36         135 ($uri, $dir, $path) = @$resource;
70             }
71             elsif (ref $resource eq "HASH") {
72 3         14 ($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 39         418 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   297 grep { ! m/::SUPER$/ } reverse sort __PACKAGE__->filter_load();
90             }
91              
92             sub parse_filter {
93 27     27 0 61 my $class = shift;
94 27         56 my $filter = shift;
95              
96 27         56 my $_filter;
97 27         111 for my $possible ($class->_filters) {
98 300 100       2628 last if $_filter = $possible->new_parse($filter, @_);
99             }
100              
101 27         251 return $_filter;
102             }
103              
104             sub _substitute($$$;$$) {
105 567     567   840 my $target = shift;
106 567         829 my $character = shift;
107 567         792 my $value = shift;
108 567         692 my $deprecated = shift;
109 567         705 my $original_path = shift;
110              
111 567 100       1130 $value = "" unless defined $value;
112              
113 567         557 my $found;
114 567   66     6847 $found ||= $$target =~ s/\%$character/$value/g;
115 567 0 66     5577 $found ||= $$target =~ s/\%\.$character/$value ? "\.$value" : ""/ge;
  0         0  
116 567 100 100     5731 $found ||= $$target =~ s/\%\-$character/$value ? "\-$value" : ""/ge;
  71         390  
117 567 0 66     7491 $found ||= $$target =~ s/\%\/$character/$value ? "\/$value" : ""/ge;
  0         0  
118              
119 567 100 100     2433 carp "\%$character is deprecated as a path pattern (in \"$original_path\")" if $found && $deprecated;
120             }
121              
122             sub build_output_path {
123 65     65 0 5456 my $class = shift;
124 65         117 my $template = shift;
125 65         86 my $filter = shift;
126              
127 65         223 my $path = $template;
128 65 100       275 $path = $path->{path} if ref $path eq "HASH";
129              
130 65 100       234 return $$path if ref $path eq "SCALAR";
131              
132 63 100       156 $path = '%n%-l%-f.%e' unless $path;
133 63 100 66     289 $path = "$path/" if blessed $path && $path->isa("Path::Class::Dir");
134 63 100 66     427 $path .= '%n%-l%-f.%e' if $path && $path =~ m/\/$/;
135 63 100       410 $path .= '.%e' if $path =~ m/(?:^|\/)[^.]+$/;
136              
137 63         133 local %_;
138 63 100       205 if (ref $filter eq "HASH") {
139 7         30 %_ = %$filter;
140             }
141             else {
142 56         301 %_ = (
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 63         968 my $original_path = $path;
153              
154 63 50       238 $path =~ s/%b/%-l/g and carp "\%b is deprecated as a path pattern (in \"$original_path\")";
155              
156 63         832 _substitute \$path, e => $_{extension};
157 63         205 _substitute \$path, f => $_{fingerprint};
158 63         205 _substitute \$path, n => $_{name};
159 63         191 _substitute \$path, k => $_{kind};
160 63         200 _substitute \$path, h => $_{head};
161 63         206 _substitute \$path, l => $_{tail};
162              
163 63         204 _substitute \$path, d => $_{fingerprint}, 1 => $original_path;
164 63         841 _substitute \$path, D => $_{fingerprint}, 1 => $original_path;
165 63         339 _substitute \$path, a => $_{tail}, 1 => $original_path;
166              
167 63         158 $path =~ s/%%/%/g;
168              
169 63         601 return $path;
170             }
171              
172             1;