Controller/AutoAssets/Handler.pm

filter: Statementstmt 81.0% 240 / 296 Branchbran 51.0% 48 / 94 Conditioncond 40.8% 29 / 71 MC/DCmcdc 5.2% 2 / 38 Subroutinesub 80.8% 59 / 73 Podpod 0.0% 0 / 26 totaltotal 63.2% 378 / 598 SCAR 55.5
CC
77
Cov
69%
build_asset
14.6
client_current_etag
12.0
__ANON__
12.0
CRAP 257.7
↑ Summary Controller/AutoAssets/Handler/Directory.pm »
1 package Catalyst::Controller::AutoAssets::Handler;
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::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 3 requires qw(
11 asset_request
12 write_built_file
13 );
14  
15 3 ▶use Cwd;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 3 ▶use Path::Class 0.32 qw( dir file );
Subroutine BEGIN: called CC=1 SCAR=0.0
17 3 ▶use Fcntl qw( :DEFAULT :flock );
Subroutine BEGIN: called CC=1 SCAR=0.0
18 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 3 ▶use File::stat qw(stat);
Subroutine BEGIN: called CC=1 SCAR=0.0
20 3 ▶use Catalyst::Utils;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 3 ▶use Time::HiRes qw(gettimeofday tv_interval);
Subroutine BEGIN: called CC=1 SCAR=0.0
22 3 ▶use Storable qw(store retrieve);
Subroutine BEGIN: called CC=1 SCAR=0.0
23 3 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 3 ▶use Data::Dumper::Concise 'Dumper';
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 3 require Digest::SHA1;
27 3 require MIME::Types;
28 3 require Module::Runtime;
29  
30 3 has 'Controller' => (
31 is => 'ro', required => 1,
32 isa => 'Catalyst::Controller::AutoAssets',
33 handles => [qw(type _app action_namespace unknown_asset _build_params _module_version)],
34 );
35  
36 # Directories to include
37 3 has 'include', is => 'ro', isa => 'Str|ArrayRef[Str]', required => 1;
38  
39 # Optional regex to require files to match to be included
40 3 has 'include_regex', is => 'ro', isa => 'Maybe[Str]', default => undef;
41  
42 # Optional regex to exclude files
43 3 has 'exclude_regex', is => 'ro', isa => 'Maybe[Str]', default => undef;
44  
45 # Whether or not to use qr/$regex/i or qr/$regex/
46 3 has 'regex_ignore_case', is => 'ro', isa => 'Bool', default => 0;
47  
48 # Whether or not to make the current asset available via 307 redirect to the
49 # real, current checksum/fingerprint asset path
50 3 has 'current_redirect', is => 'ro', isa => 'Bool', default => 1;
51  
52 # What string to use for the 'current' redirect
53 3 has 'current_alias', is => 'ro', isa => 'Str', default => 'current';
54  
55 # Whether or not to make the current asset available via a static path
56 # with no benefit of caching
57 3 has 'allow_static_requests', is => 'ro', isa => 'Bool', default => 0;
58  
59 # What string to use for the 'static' path
60 3 has 'static_alias', is => 'ro', isa => 'Str', default => 'static';
61  
62 # Extra custom response headers for current/static requests
63 3 ▶has 'current_response_headers', is => 'ro', isa => 'HashRef', default => sub {{}};
Subroutine __ANON__: called CC=1 SCAR=0.0
64 3 ▶has 'static_response_headers', is => 'ro', isa => 'HashRef', default => sub {{}};
Subroutine __ANON__: called CC=1 SCAR=0.0
65  
66 # Whether or not to set 'Etag' response headers and check 'If-None-Match' request headers
67 # Very useful when using 'static' paths
68 3 has 'use_etags', is => 'ro', isa => 'Bool', default => 0;
69  
70 # Max number of seconds before recalculating the fingerprint (sha1 checksum)
71 # regardless of whether or not the mtime has changed. 0 means infinite/disabled
72 3 ▶has 'max_fingerprint_calc_age', is => 'ro', isa => 'Int', default => sub {0};
Subroutine __ANON__: called CC=1 SCAR=0.0
73  
74 # Max number of seconds to wait to obtain a lock (to be thread safe)
75 3 has 'max_lock_wait', is => 'ro', isa => 'Int', default => 120;
76  
77 has 'cache_control_header', is => 'ro', isa => 'Str',
78 3 ▶ default => sub { 'public, max-age=31536000, s-max-age=31536000' }; # 31536000 = 1 year
Subroutine __ANON__: called CC=1 SCAR=0.0
79  
80 # Whether or not to use stored state data across restarts to avoid rebuilding.
81 3 ▶has 'persist_state', is => 'ro', isa => 'Bool', default => sub{0};
Subroutine __ANON__: called CC=1 SCAR=0.0
82  
83 # Optional shorter checksum
84 2 ▶has 'sha1_string_length', is => 'ro', isa => 'Int', default => sub{40};
Subroutine __ANON__: called CC=1 SCAR=0.0
85  
86 # directory to use for relative includes (defaults to the Catalyst home dir);
87 # TODO: coerce from Str
88 has 'include_relative_dir', isa => 'Path::Class::Dir', is => 'ro', lazy => 1, default => sub {
89 3 ▶ my $self = shift;
Subroutine __ANON__: called CC=3 SCAR=12.0
90 3 my $home = $self->_app->config->{home};
91 3 ▶ $home = $home && -d $home ? $self->_app->config->{home} : cwd();
Branch: 1 branch
BranchTrueFalseTotal
$home && -d $home ? : 0 3 3
Condition33%1/3
$home and -d $home
!ll&&!rl&&r
300
MC/DC0%0/2
$home and -d $home
$home -d $home
Truth table33%1/3
A and B
A= $homeB= -d $home
ABresult
0X0
100
111
92 3 return dir( $home );
93 3 };
94  
95  
96  
97 ######################################
98  
99  
100 ▶sub BUILD {}
Subroutine BUILD: not called CC=1 SCAR=0.0
Pod undocumented
101 before BUILD => sub {
102 3 ▶ my $self = shift;
Subroutine __ANON__: called CC=5 SCAR=17.1
103
104 # optionally initialize state data from the copy stored on disk for fast
105 # startup (avoids having to always rebuild after every app restart):
106 3 ▶ $self->_restore_state if($self->persist_state);
Branch: 1 branch
BranchTrueFalseTotal
if $self->persist_state 0 3 3
107  
108 # init includes
109 3 $self->includes;
110
111 Catalyst::Exception->throw("Must include at least one file/directory")
112 3 ▶ unless (scalar @{$self->includes} > 0);
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @{$self->includes;} > 0 0 3 3
113  
114 # if the user picks something lower than 5 it is probably a mistake (really, anything
115 # lower than 8 is probably not a good idea. But the full 40 is probably way overkill)
116 3 ▶ Catalyst::Exception->throw("sha1_string_length must be between 5 and 40")
Branch: 1 branch
BranchTrueFalseTotal
unless $self->sha1_string_length >= 5 and $self->sha1_string_length <= 40 0 3 3
Condition33%1/3
$self->sha1_string_length >= 5 and $self->sha1_string_length <= 40
!ll&&!rl&&r
003
MC/DC0%0/2
$self->sha1_string_length >= 5 and $self->sha1_string_length <= 40
$self->sha1_string_length >= 5 $self->sha1_string_length <= 40
Truth table33%1/3
A and B
A= $self->sha1_string_length >= 5B= $self->sha1_string_length <= 40
ABresult
0X0
100
111
117 unless ($self->sha1_string_length >= 5 && $self->sha1_string_length <= 40);
118  
119 # init work_dir:
120 3 $self->work_dir->mkpath($self->_app->debug);
121 3 $self->work_dir->resolve;
122
123 3 $self->prepare_asset;
124 3 };
125  
126 # Main code entry point:
127 sub request {
128 16 ▶ my ( $self, $c, @args ) = @_;
Subroutine request: called CC=4 SCAR=14.5
Pod undocumented
129 16 my $sha1 = $args[0];
130
131 16 ▶ return $self->current_request($c, @args) if (
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_current_request_arg(@args) 4 12 16
132 $self->is_current_request_arg(@args)
133 );
134
135 12 ▶ return $self->static_request($c, @args) if (
Branch: 1 branch
BranchTrueFalseTotal
if $self->allow_static_requests and $self->static_alias eq $sha1 0 12 12
Condition33%1/3
$self->allow_static_requests and $self->static_alias eq $sha1
!ll&&!rl&&r
1200
MC/DC0%0/2
$self->allow_static_requests and $self->static_alias eq $sha1
$self->allow_static_requests $self->static_alias eq $sha1
Truth table33%1/3
A and B
A= $self->allow_static_requestsB= $self->static_alias eq $sha1
ABresult
0X0
100
111
136 $self->allow_static_requests
137 && $self->static_alias eq $sha1
138 );
139
140 12 return $self->handle_asset_request($c, @args);
141 }
142  
143 sub is_current_request_arg {
144 5 ▶ my ($self, $arg) = @_;
Subroutine is_current_request_arg: called CC=2 SCAR=6.9
Pod undocumented
145 5 ▶ return $arg eq $self->current_alias ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$arg eq $self->current_alias ? : 2 3 5
146 }
147  
148 sub current_request {
149 4 ▶ my ( $self, $c, $arg, @args ) = @_;
Subroutine current_request: called CC=2 SCAR=6.9
Pod undocumented
150 my %headers = (
151 'Cache-Control' => 'no-cache',
152 4 %{$self->current_response_headers}
153 );
154 4 $c->response->header( $_ => $headers{$_} ) for (keys %headers);
155 4 $c->response->redirect(join('/',$self->asset_path,@args), 307);
156 4 return $c->detach;
157 }
158  
159 sub static_request {
160 0 ▶ my ( $self, $c, $arg, @args ) = @_;
Subroutine static_request: not called CC=2 SCAR=17.9
Pod undocumented
161 my %headers = (
162 'Cache-Control' => 'no-cache',
163 0 %{$self->static_response_headers}
164 );
165 0 $c->response->header( $_ => $headers{$_} ) for (keys %headers);
166 # Simulate a request to the current sha1 checksum:
167 0 return $self->handle_asset_request($c, $self->asset_name, @args);
168 }
169  
170  
171 sub handle_asset_request {
172 12 ▶ my ( $self, $c, $arg, @args ) = @_;
Subroutine handle_asset_request: called CC=3 SCAR=12.3
Pod undocumented
173
174 12 $self->prepare_asset(@args);
175
176 12 ▶ if($self->use_etags && $self->client_current_etag($c, $arg, @args)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->use_etags and $self->client_current_etag($c, $arg, @args)) { } 0 12 12
Condition33%1/3
$self->use_etags and $self->client_current_etag($c, $arg, @args)
!ll&&!rl&&r
1200
MC/DC0%0/2
$self->use_etags and $self->client_current_etag($c, $arg, @args)
$self->use_etags $self->client_current_etag($c, $arg, @args)
Truth table33%1/3
A and B
A= $self->use_etagsB= $self->client_current_etag($c, $arg, @args)
ABresult
0X0
100
111
177 # Set 304 Not Modified:
178 0 $c->response->status(304);
179 }
180 else {
181 12 $self->asset_request($c, $arg, @args);
182 }
183 10 return $c->detach;
184 }
185  
186 sub client_current_etag {
187 0 ▶ my ( $self, $c, $arg, @args ) = @_;
Subroutine client_current_etag: not called CC=3 SCAR=24.8
Pod undocumented
188
189 0 my $etag = $self->etag_value(@args);
190 0 $c->response->header( Etag => $etag );
191 0 my $client_etag = $c->request->headers->{'if-none-match'};
192 0 ▶ return ($client_etag && $client_etag eq $etag) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$client_etag && $client_etag eq $etag ? : 0 0 0
Condition0%0/3
$client_etag and $client_etag eq $etag
!ll&&!rl&&r
000
MC/DC0%0/2
$client_etag and $client_etag eq $etag
$client_etag $client_etag eq $etag
Truth table0%0/3
A and B
A= $client_etagB= $client_etag eq $etag
ABresult
0X0
100
111
193 }
194  
195 sub etag_value {
196 0 ▶ my $self = shift;
Subroutine etag_value: not called CC=1 SCAR=6.9
Pod undocumented
197 0 return '"' . join('/',$self->asset_name,@_) . '"';
198 }
199  
200  
201 ############################
202  
203  
204 has 'work_dir', is => 'ro', isa => 'Path::Class::Dir', lazy => 1, default => sub {
205 0 ▶ my $self = shift;
Subroutine __ANON__: not called CC=2 SCAR=17.9
206 0 my $c = $self->_app;
207
208 0 ▶ my $tmpdir = Catalyst::Utils::class2tempdir($c)
Condition0%0/3
Catalyst::Utils::class2tempdir($c) || 'Catalyst::Exception'->throw("Can't determine tempdir for $c")
l!l&&r!l&&!r
000
MC/DC0%0/2
Catalyst::Utils::class2tempdir($c) || 'Catalyst::Exception'->throw("Can't determine tempdir for $c")
Catalyst::Utils::class2tempdir($c) 'Catalyst::Exception'->throw("Can't determine tempdir for $c")
Truth table0%0/3
A || B
A= Catalyst::Utils::class2tempdir($c)B= 'Catalyst::Exception'->throw("Can't determine tempdir for $c")
ABresult
1X1
011
000
209 || Catalyst::Exception->throw("Can't determine tempdir for $c");
210
211 0 return dir($tmpdir, "AutoAssets", $self->action_namespace($c));
212 3 };
213  
214 has 'built_file', is => 'ro', isa => 'Path::Class::File', lazy => 1, default => sub {
215 3 ▶ my $self = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
216 3 my $filename = 'built_file';
217 3 return file($self->work_dir,$filename);
218 3 };
219  
220 has 'fingerprint_file', is => 'ro', isa => 'Path::Class::File', lazy => 1, default => sub {
221 3 ▶ my $self = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
222 3 return file($self->work_dir,'fingerprint');
223 3 };
224  
225 has 'lock_file', is => 'ro', isa => 'Path::Class::File', lazy => 1, default => sub {
226 3 ▶ my $self = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
227 3 return file($self->work_dir,'lockfile');
228 3 };
229  
230 has 'includes', is => 'ro', isa => 'ArrayRef', lazy => 1, default => sub {
231 3 ▶ my $self = shift;
Subroutine __ANON__: called CC=4 SCAR=14.0
232 3 my $rel = $self->include_relative_dir;
233 3 ▶ my @list = ref $self->include ? @{$self->include} : $self->include;
Branch: 1 branch
BranchTrueFalseTotal
ref $self->include ? : 3 0 3
234 return [ map {
235 3 my $inc = file($_);
236 3 ▶ $inc = $rel->file($inc) unless ($inc->is_absolute);
Branch: 1 branch
BranchTrueFalseTotal
unless $inc->is_absolute 0 3 3
237 3 ▶ $inc = dir($inc) if (-d $inc); #<-- convert to Path::Class::Dir
Branch: 1 branch
BranchTrueFalseTotal
if -d $inc 2 1 3
238 3 $inc->resolve
239 } @list ];
240 3 };
241  
242  
243  
244  
245 sub get_include_files {
246 15 ▶ my $self = shift;
Subroutine get_include_files: called CC=4 SCAR=13.9
Pod undocumented
247
248 15 my @excluded = ();
249 15 my @files = ();
250 15 for my $inc (@{$self->includes}) {
251 15 ▶ if($inc->is_dir) {
Branch: 1 branch
BranchTrueFalseTotal
if ($inc->is_dir) { } 12 3 15
252 $inc->recurse(
253 preorder => 1,
254 depthfirst => 1,
255 callback => sub {
256 41 ▶ my $child = shift;
Subroutine __ANON__: called CC=2 SCAR=6.9
257 41 ▶ $self->_valid_include_file($child)
Branch: 1 branch
BranchTrueFalseTotal
$self->_valid_include_file($child) ? : 27 14 41
258 ? push @files, $child->absolute
259 : push @excluded, $child->absolute;
260 }
261 12 );
262 }
263 else {
264 3 ▶ $self->_valid_include_file($inc)
Branch: 1 branch
BranchTrueFalseTotal
$self->_valid_include_file($inc) ? : 3 0 3
265 ? push @files, $inc->absolute
266 : push @excluded, $inc->absolute;
267 }
268 }
269
270 # Some handlers (like Directory) need to know about excluded files
271 15 $self->_record_excluded_files(\@excluded);
272
273 # force consistent ordering of files:
274 15 return [sort @files];
275 }
276  
277 # optional hook for excluded files:
278 ▶sub _record_excluded_files {}
Subroutine _record_excluded_files: called CC=1 SCAR=0.0
279  
280  
281 has '_include_regexp', is => 'ro', isa => 'Maybe[RegexpRef]',
282 lazy => 1, init_arg => undef, default => sub {
283 0 ▶ my $self = shift;
Subroutine __ANON__: not called CC=3 SCAR=24.8
284 0 ▶ my $str = $self->include_regex or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $str = $self->include_regex or return undef 0 0 0
285 0 ▶ return $self->regex_ignore_case ? qr/$str/i : qr/$str/;
Branch: 1 branch
BranchTrueFalseTotal
$self->regex_ignore_case ? : 0 0 0
286 3 };
287 has '_exclude_regexp', is => 'ro', isa => 'Maybe[RegexpRef]',
288 lazy => 1, init_arg => undef, default => sub {
289 0 ▶ my $self = shift;
Subroutine __ANON__: not called CC=3 SCAR=24.8
290 0 ▶ my $str = $self->exclude_regex or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $str = $self->exclude_regex or return undef 0 0 0
291 0 ▶ return $self->regex_ignore_case ? qr/$str/i : qr/$str/;
Branch: 1 branch
BranchTrueFalseTotal
$self->regex_ignore_case ? : 0 0 0
292 3 };
293  
294 sub _valid_include_file {
295 44 ▶ my ($self, $file) = @_;
Subroutine _valid_include_file: called CC=6 SCAR=20.7
296 return (
297 44 ▶ $file->is_dir
Branch: 1 branch
BranchTrueFalseTotal
$file->is_dir || $self->include_regex && !($file =~ $self->_include_regexp) || $self->exclude_regex && $file =~ $self->_exclude_regexp ? : 14 30 44
Condition50%6/12
$file->is_dir or $self->include_regex and not $file =~ $self->_include_regexp or $self->exclude_regex and $file =~ $self->_exclude_regexp
l!l&&r!l&&!r
14030
$file->is_dir or $self->include_regex and not $file =~ $self->_include_regexp
l!l&&r!l&&!r
14030
$self->include_regex and not $file =~ $self->_include_regexp
!ll&&!rl&&r
3000
$self->exclude_regex and $file =~ $self->_exclude_regexp
!ll&&!rl&&r
3000
MC/DC20%1/5
$file->is_dir or $self->include_regex and not $file =~ $self->_include_regexp or $self->exclude_regex and $file =~ $self->_exclude_regexp
$file->is_dir $self->include_regex not $file =~ $self->_include_regexp $self->exclude_regex $file =~ $self->_exclude_regexp
Truth table25%2/8
A or B and not(C) or D and E
A= $file->is_dirB= $self->include_regexC= not $file =~ $self->_include_regexpD= $self->exclude_regexE= $file =~ $self->_exclude_regexp
ABCDEresult
1XXXX1
011XX1
00X111
010111
00X0X0
00X100
0100X0
010100
298 || ($self->include_regex && ! ($file =~ $self->_include_regexp))
299 || ($self->exclude_regex && $file =~ $self->_exclude_regexp)
300 ) ? 0 : 1;
301 }
302  
303 3 ▶has 'last_fingerprint_calculated', is => 'rw', isa => 'Maybe[Int]', default => sub{undef};
Subroutine __ANON__: called CC=1 SCAR=0.0
304  
305 3 ▶has 'built_mtime', is => 'rw', isa => 'Maybe[Str]', default => sub{undef};
Subroutine __ANON__: called CC=1 SCAR=0.0
306 sub get_built_mtime {
307 32 ▶ my $self = shift;
Subroutine get_built_mtime: called CC=2 SCAR=6.9
Pod undocumented
308 32 ▶ return -f $self->built_file ? $self->built_file->stat->mtime : undef;
Branch: 1 branch
BranchTrueFalseTotal
-f $self->built_file ? : 26 6 32
309 }
310  
311 # inc_mtimes are the mtime(s) of the include files. For directory assets
312 # this is *only* the mtime of the top directory (see subfile_meta below)
313 3 has 'inc_mtimes', is => 'rw', isa => 'Maybe[Str]', default => undef;
314 sub get_inc_mtime_concat {
315 26 ▶ my $self = shift;
Subroutine get_inc_mtime_concat: called CC=1 SCAR=0.0
Pod undocumented
316 26 my $list = shift;
317 26 return join('-', map { $_->stat->mtime } @$list );
318 }
319  
320  
321 sub calculate_fingerprint {
322 6 ▶ my $self = shift;
Subroutine calculate_fingerprint: called CC=2 SCAR=7.0
Pod undocumented
323 6 my $list = shift;
324 # include both the include (source) and built (output) in the fingerprint:
325 6 my $sha1 = $self->file_checksum(@$list,$self->built_file);
326 6 ▶ $self->last_fingerprint_calculated(time) if ($sha1);
Branch: 1 branch
BranchTrueFalseTotal
if $sha1 6 0 6
327 6 return $sha1;
328 }
329  
330 sub current_fingerprint {
331 23 ▶ my $self = shift;
Subroutine current_fingerprint: called CC=2 SCAR=6.9
Pod undocumented
332 23 ▶ return undef unless (-f $self->fingerprint_file);
Branch: 1 branch
BranchTrueFalseTotal
unless -f $self->fingerprint_file 3 20 23
333 20 my $fingerprint = $self->fingerprint_file->slurp(iomode => '<:raw');
334 20 return $fingerprint;
335 }
336  
337 sub save_fingerprint {
338 3 ▶ my $self = shift;
Subroutine save_fingerprint: called CC=2 SCAR=7.1
Pod undocumented
339 3 ▶ my $fingerprint = shift or die "Expected fingerprint/checksum argument";
Branch: 1 branch
BranchTrueFalseTotal
my $fingerprint = shift() or die "Expected fingerprint/checksum argument" 0 3 3
340 3 return $self->fingerprint_file->spew(iomode => '>:raw', $fingerprint);
341 }
342  
343 sub calculate_save_fingerprint {
344 3 ▶ my $self = shift;
Subroutine calculate_save_fingerprint: called CC=2 SCAR=7.1
Pod undocumented
345 3 ▶ my $fingerprint = $self->calculate_fingerprint(@_) or return 0;
Branch: 1 branch
BranchTrueFalseTotal
my $fingerprint = $self->calculate_fingerprint(@_) or return 0 0 3 3
346 3 return $self->save_fingerprint($fingerprint);
347 }
348  
349 sub fingerprint_calc_current {
350 23 ▶ my $self = shift;
Subroutine fingerprint_calc_current: called CC=4 SCAR=18.9
Pod undocumented
351 23 ▶ my $last = $self->last_fingerprint_calculated or return 0;
Branch: 1 branch
BranchTrueFalseTotal
my $last = $self->last_fingerprint_calculated or return 0 0 23 23
352 23 ▶ return 1 if ($self->max_fingerprint_calc_age == 0); # <-- 0 means infinite
Branch: 1 branch
BranchTrueFalseTotal
if $self->max_fingerprint_calc_age == 0 23 0 23
353 0 ▶ return 1 if (time - $last < $self->max_fingerprint_calc_age);
Branch: 1 branch
BranchTrueFalseTotal
if time - $last < $self->max_fingerprint_calc_age 0 0 0
354 0 return 0;
355 }
356  
357 # -----
358 # Quick and dirty state persistence for faster startup
359 has 'persist_state_file', is => 'ro', isa => 'Path::Class::File', lazy => 1, default => sub {
360 0 ▶ my $self = shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
361 0 return file($self->work_dir,'state.dat');
362 3 };
363  
364 2 ▶has '_persist_attrs', is => 'ro', isa => 'ArrayRef', default => sub{[qw(
Subroutine __ANON__: called CC=1 SCAR=0.0
365 built_mtime
366 inc_mtimes
367 last_fingerprint_calculated
368 3 )]};
369  
370 sub _persist_state {
371 3 ▶ my $self = shift;
Subroutine _persist_state: called CC=2 SCAR=12.6
372 3 ▶ return undef unless ($self->persist_state);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->persist_state 3 0 3
373 0 my $data = { map { $_ => $self->$_ } @{$self->_persist_attrs} };
374 0 $data->{_module_version} = $self->_module_version;
375 0 $data->{_build_params} = $self->_build_params;
376 0 store $data, $self->persist_state_file;
377 0 return $data;
378 }
379  
380 sub _restore_state {
381 0 ▶ my $self = shift;
Subroutine _restore_state: not called CC=2 SCAR=17.9
382 0 ▶ return 0 unless (-f $self->persist_state_file);
Branch: 1 branch
BranchTrueFalseTotal
unless -f $self->persist_state_file 0 0 0
383 0 my $data;
384 try {
385 0 ▶ $data = retrieve $self->persist_state_file;
Subroutine __ANON__: not called CC=3 SCAR=24.8
386 0 ▶ if($self->_valid_state_data($data)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_valid_state_data($data)) 0 0 0
387 0 $self->$_($data->{$_}) for (@{$self->_persist_attrs});
388 }
389 }
390 catch {
391 0 ▶ $self->clear_asset; #<-- make sure no partial state data is used
Subroutine __ANON__: not called CC=1 SCAR=6.9
392 0 $self->_app->log->warn(
393 'Failed to restore state from ' . $self->persist_state_file
394 );
395 0 };
396 0 return $data;
397 }
398  
399 sub _valid_state_data {
400 0 ▶ my ($self, $data) = @_;
Subroutine _valid_state_data: not called CC=3 SCAR=24.8
401
402 # Make sure the version and config params hasn't changed
403 return (
404 $self->_module_version eq $data->{_module_version}
405 && Dumper($self->_build_params) eq Dumper($data->{_build_params})
406 0 ▶ ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->_module_version eq $data->{'_module_version'} && Dumper($self->_build_params) eq Dumper($data->{'_build_params'}) ? : 0 0 0
Condition0%0/3
$self->_module_version eq $data->{"_module_version"} and Dumper($self->_build_params) eq Dumper($data->{"_build_params"})
!ll&&!rl&&r
000
MC/DC0%0/2
$self->_module_version eq $data->{"_module_version"} and Dumper($self->_build_params) eq Dumper($data->{"_build_params"})
$self->_module_version eq $data->{"_module_version"} Dumper($self->_build_params) eq Dumper($data->{"_build_params"})
Truth table0%0/3
A and B
A= $self->_module_version eq $data->{"_module_version"}B= Dumper($self->_build_params) eq Dumper($data->{"_build_params"})
ABresult
0X0
100
111
407 }
408 # -----
409  
410  
411 # force rebuild on next request/prepare_asset
412 sub clear_asset {
413 0 ▶ my $self = shift;
Subroutine clear_asset: not called CC=1 SCAR=6.9
Pod undocumented
414 0 $self->inc_mtimes(undef);
415 }
416  
417 sub _build_required {
418 26 ▶ my ($self, $d) = @_;
Subroutine _build_required: called CC=8 SCAR=22.3
419 return (
420 $self->inc_mtimes && $self->built_mtime &&
421 $d->{inc_mtimes} && $d->{built_mtime} &&
422 $self->inc_mtimes eq $d->{inc_mtimes} &&
423 $self->built_mtime eq $d->{built_mtime} &&
424 26 ▶ $self->fingerprint_calc_current
Branch: 1 branch
BranchTrueFalseTotal
$self->inc_mtimes && $self->built_mtime && $d->{'inc_mtimes'} && $d->{'built_mtime'} && $self->inc_mtimes eq $d->{'inc_mtimes'} && $self->built_mtime eq $d->{'built_mtime'} && $self->fingerprint_calc_current ? : 23 3 26
Condition66%12/18
$self->inc_mtimes and $self->built_mtime and $d->{"inc_mtimes"} and $d->{"built_mtime"} and $self->inc_mtimes eq $d->{"inc_mtimes"} and $self->built_mtime eq $d->{"built_mtime"} and $self->fingerprint_calc_current
!ll&&!rl&&r
3023
$self->inc_mtimes and $self->built_mtime and $d->{"inc_mtimes"} and $d->{"built_mtime"} and $self->inc_mtimes eq $d->{"inc_mtimes"} and $self->built_mtime eq $d->{"built_mtime"}
!ll&&!rl&&r
3023
$self->inc_mtimes and $self->built_mtime and $d->{"inc_mtimes"} and $d->{"built_mtime"} and $self->inc_mtimes eq $d->{"inc_mtimes"}
!ll&&!rl&&r
3023
$self->inc_mtimes and $self->built_mtime and $d->{"inc_mtimes"} and $d->{"built_mtime"}
!ll&&!rl&&r
3023
$self->inc_mtimes and $self->built_mtime and $d->{"inc_mtimes"}
!ll&&!rl&&r
3023
$self->inc_mtimes and $self->built_mtime
!ll&&!rl&&r
3023
MC/DC14%1/7
$self->inc_mtimes and $self->built_mtime and $d->{"inc_mtimes"} and $d->{"built_mtime"} and $self->inc_mtimes eq $d->{"inc_mtimes"} and $self->built_mtime eq $d->{"built_mtime"} and $self->fingerprint_calc_current
$self->inc_mtimes $self->built_mtime $d->{"inc_mtimes"} $d->{"built_mtime"} $self->inc_mtimes eq $d->{"inc_mtimes"} $self->built_mtime eq $d->{"built_mtime"} $self->fingerprint_calc_current
Truth table25%2/8
A and B and C and D and E and F and G
A= $self->inc_mtimesB= $self->built_mtimeC= $d->{"inc_mtimes"}D= $d->{"built_mtime"}E= $self->inc_mtimes eq $d->{"inc_mtimes"}F= $self->built_mtime eq $d->{"built_mtime"}G= $self->fingerprint_calc_current
ABCDEFGresult
0XXXXXX0
10XXXXX0
110XXXX0
1110XXX0
11110XX0
111110X0
11111100
11111111
425 ) ? 0 : 1;
426 }
427  
428  
429 # Gets the data used throughout the prepare_asset process:
430 sub get_prepare_data {
431 14 ▶ my $self = shift;
Subroutine get_prepare_data: called CC=1 SCAR=0.0
Pod undocumented
432
433 14 my $files = $self->get_include_files;
434 14 my $inc_mtimes = $self->get_inc_mtime_concat($files);
435 14 my $built_mtime = $self->get_built_mtime;
436
437 return {
438 14 files => $files,
439 inc_mtimes => $inc_mtimes,
440 built_mtime => $built_mtime
441 };
442 }
443  
444 ▶sub before_prepare_asset {}
Subroutine before_prepare_asset: called CC=1 SCAR=0.0
Pod undocumented
445  
446 sub prepare_asset {
447 26 ▶ my $self = shift;
Subroutine prepare_asset: called CC=3 SCAR=11.0
Pod undocumented
448 26 my $start = [gettimeofday];
449  
450 # Optional hook:
451 26 $self->before_prepare_asset(@_);
452  
453 26 my $opt = $self->get_prepare_data;
454 26 ▶ return 1 unless $self->_build_required($opt);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_build_required($opt) 23 3 26
455  
456 #### -----
457 #### The code above this line happens on every request and is designed
458 #### to be as fast as possible
459 ####
460 #### The code below this line is (comparatively) expensive and only
461 #### happens when a rebuild is needed which should be rare--only when
462 #### content is modified, or on app startup (unless 'persist_state' is set)
463 #### -----
464  
465 ### Do a rebuild:
466  
467 # --- Blocks for up to max_lock_wait seconds waiting to get an exclusive lock
468 # The lock is held until it goes out of scope.
469 # If we fail to get the lock, we just continue anyway in hopes that the second
470 # build won't corrupt the first, which is arguably better than killing the
471 # request.
472 3 ▶ my $lock= try { $self->_get_lock($self->lock_file, $self->max_lock_wait); };
Subroutine __ANON__: called CC=1 SCAR=0.0
473 # ---
474
475 3 $self->build_asset($opt);
476
477 3 ▶ $self->_app->log->debug(
Branch: 1 branch
BranchTrueFalseTotal
if $self->_app->debug 0 3 3
478 "Built asset: " . $self->base_path . '/' . $self->asset_name .
479 ' in ' . sprintf("%.3f", tv_interval($start) ) . 's'
480 ) if ($self->_app->debug);
481  
482 # Release the lock and return:
483 3 $self->_persist_state;
484 3 return 1;
485 }
486  
487 sub build_asset {
488 3 ▶ my ($self, $opt) = @_;
Subroutine build_asset: called CC=9 SCAR=26.8
Pod undocumented
489
490 3 ▶ my $files = $opt->{files} || $self->get_include_files;
Condition33%1/3
$opt->{'files'} || $self->get_include_files
l!l&&r!l&&!r
300
MC/DC0%0/2
$opt->{'files'} || $self->get_include_files
$opt->{'files'} $self->get_include_files
Truth table33%1/3
A || B
A= $opt->{'files'}B= $self->get_include_files
ABresult
1X1
011
000
491 3 ▶ my $inc_mtimes = $opt->{inc_mtimes} || $self->get_inc_mtime_concat($files);
Condition33%1/3
$opt->{'inc_mtimes'} || $self->get_inc_mtime_concat($files)
l!l&&r!l&&!r
300
MC/DC0%0/2
$opt->{'inc_mtimes'} || $self->get_inc_mtime_concat($files)
$opt->{'inc_mtimes'} $self->get_inc_mtime_concat($files)
Truth table33%1/3
A || B
A= $opt->{'inc_mtimes'}B= $self->get_inc_mtime_concat($files)
ABresult
1X1
011
000
492 3 ▶ my $built_mtime = $opt->{built_mtime} || $self->get_built_mtime;
Condition33%1/3
$opt->{'built_mtime'} || $self->get_built_mtime
l!l&&r!l&&!r
003
MC/DC0%0/2
$opt->{'built_mtime'} || $self->get_built_mtime
$opt->{'built_mtime'} $self->get_built_mtime
Truth table33%1/3
A || B
A= $opt->{'built_mtime'}B= $self->get_built_mtime
ABresult
1X1
011
000
493
494 # Check the fingerprint to see if we can avoid a full rebuild (if mtimes changed
495 # but the actual content hasn't by comparing the fingerprint/checksum):
496 3 my $fingerprint = $self->calculate_fingerprint($files);
497 3 my $cur_fingerprint = $self->current_fingerprint;
498 3 ▶ if($fingerprint && $cur_fingerprint && $cur_fingerprint eq $fingerprint) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fingerprint and $cur_fingerprint and $cur_fingerprint eq $fingerprint) 0 3 3
Condition33%2/6
$fingerprint and $cur_fingerprint and $cur_fingerprint eq $fingerprint
!ll&&!rl&&r
300
$fingerprint and $cur_fingerprint
!ll&&!rl&&r
030
MC/DC0%0/3
$fingerprint and $cur_fingerprint and $cur_fingerprint eq $fingerprint
$fingerprint $cur_fingerprint $cur_fingerprint eq $fingerprint
Truth table25%1/4
A and B and C
A= $fingerprintB= $cur_fingerprintC= $cur_fingerprint eq $fingerprint
ABCresult
0XX0
10X0
1100
1111
499 # If the mtimes changed but the fingerprint matches we don't need to regenerate.
500 # This will happen if another process just built the files while we were waiting
501 # for the lock and on the very first time after the application starts up
502 0 $self->inc_mtimes($inc_mtimes);
503 0 $self->built_mtime($built_mtime);
504 0 $self->_persist_state;
505 0 return 1;
506 }
507  
508 ### Ok, we really need to do a full rebuild:
509  
510 3 ▶ my $fd = $self->built_file->openw or die $!;
Branch: 1 branch
BranchTrueFalseTotal
my $fd = $self->built_file->openw or die $! 0 3 3
511 3 binmode $fd;
512 3 $self->write_built_file($fd,$files);
513 3 ▶ $fd->close if ($fd->opened);
Branch: 1 branch
BranchTrueFalseTotal
if $fd->opened 2 1 3
514
515 # Update the fingerprint (global) and cached mtimes (specific to the current process)
516 3 $self->inc_mtimes($opt->{inc_mtimes});
517 3 $self->built_mtime($self->get_built_mtime);
518 # we're calculating the fingerprint again because the built_file, which was just
519 # regenerated, is included in the checksum data. This could probably be optimized,
520 # however, this only happens on rebuild which rarely happens (should never happen)
521 # in production so an extra second is no big deal in this case.
522 3 $self->calculate_save_fingerprint($opt->{files});
523 }
524  
525 sub file_checksum {
526 6 ▶ my $self = shift;
Subroutine file_checksum: called CC=4 SCAR=14.0
Pod undocumented
527 6 ▶ my $files = ref $_[0] eq 'ARRAY' ? $_[0] : \@_;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'ARRAY' ? : 0 6 6
528
529 6 my $Sha1 = Digest::SHA1->new;
530 6 foreach my $file ( grep { -f $_ } @$files ) {
531 19 ▶ my $fh = $file->openr or die "$! : $file\n";
Branch: 1 branch
BranchTrueFalseTotal
my $fh = $file->openr or die "$! : $file\n" 0 19 19
532 19 binmode $fh;
533 19 $Sha1->addfile($fh);
534 19 $fh->close;
535 }
536  
537 6 return substr $Sha1->hexdigest, 0, $self->sha1_string_length;
538 }
539  
540 8 ▶sub asset_name { (shift)->current_fingerprint }
Subroutine asset_name: called CC=1 SCAR=0.0
Pod undocumented
541  
542 sub base_path {
543 8 ▶ my $self = shift;
Subroutine base_path: called CC=2 SCAR=7.0
Pod undocumented
544 8 ▶ my $pfx = try{RapidApp->active_request_context->mount_url} || '';
Subroutine __ANON__: called CC=1 SCAR=0.2
Condition50%1/2
&try(sub { || ''
l!l
08
MC/DC0%0/1
&try(sub { 'RapidApp'->active_request_context->mount_url; } ) || ''
&try(sub { 'RapidApp'->active_request_context->mount_url; } )
Truth table50%1/2
A
A= &try(sub { 'RapidApp'->active_request_context->mount_url; } )
Aresult
11
00
545 8 return join('/',$pfx,$self->action_namespace($self->_app));
546 }
547  
548 # this is just used for some internal optimization to avoid calling stat
549 # duplicate times. It is basically me being lazy, adding an internal extra param
550 # to asset_path() without changing its public API/arg list
551 3 has '_asset_path_skip_prepare', is => 'rw', isa => 'Bool', default => 0;
552 before asset_path => sub {
553 8 ▶ my $self = shift;
Subroutine __ANON__: called CC=2 SCAR=7.2
554 8 ▶ $self->prepare_asset(@_) unless ($self->_asset_path_skip_prepare);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_asset_path_skip_prepare 8 0 8
555 3 };
556 sub asset_path {
557 8 ▶ my $self = shift;
Subroutine asset_path: called CC=1 SCAR=0.0
Pod undocumented
558 8 return $self->base_path . '/' . $self->asset_name;
559 }
560  
561 0 ▶sub html_head_tags { undef }
Subroutine html_head_tags: not called CC=1 SCAR=6.9
Pod undocumented
562  
563 # This locks a file or dies trying, and on success, returns a "lock object"
564 # which will release the lock if it goes out of scope. At the moment, this
565 # "object" is just a file handle.
566 #
567 # This lock is specifically *not* inherited by child processes (thanks to
568 # fcntl(FL_CLOEXEC), and in fact, this design principle gives it
569 # cross-platform compatibility that most lock module sdon't have.
570 #
571 sub _get_lock {
572 3 ▶ my ($self, $fname, $timeout)= @_;
Subroutine _get_lock: called CC=8 SCAR=24.1
573 3 my $fh;
574 3 ▶ sysopen($fh, $fname, O_RDWR|O_CREAT|O_EXCL, 0644)
Branch: 1 branch
BranchTrueFalseTotal
sysopen $fh, $fname, 194, 420 or sysopen $fh, $fname, 2 or croak("Unable to create or open $fname") 0 3 3
Condition33%1/3
sysopen $fh, $fname, 194, 420 or sysopen $fh, $fname, 2
l!l&&r!l&&!r
300
MC/DC0%0/2
sysopen $fh, $fname, 194, 420 or sysopen $fh, $fname, 2
sysopen $fh, $fname, 194, 420 sysopen $fh, $fname, 2
Truth table33%1/3
A or B
A= sysopen $fh, $fname, 194, 420B= sysopen $fh, $fname, 2
ABresult
1X1
011
000
575 or sysopen($fh, $fname, O_RDWR)
576 or croak "Unable to create or open $fname";
577
578 3 ▶ try { fcntl($fh, F_SETFD, FD_CLOEXEC) }
Subroutine __ANON__: called CC=1 SCAR=0.0
579 3 ▶ or carp "Failed to set close-on-exec for $fname (see BUGS in Catalyst::Controller::AutoAssets)";
Branch: 1 branch
BranchTrueFalseTotal
&try(sub { fcntl $fh, 2, 1; } ) or carp("Failed to set close-on-exec for $fname (see BUGS in Catalyst::Controller::AutoAssets)") 0 3 3
580
581 # Try to get lock until timeout. We poll because there isn't a sensible
582 # way to wait for the lock. (I don't consider SIGALRM to be very sensible)
583 3 my $deadline= Time::HiRes::time() + $timeout;
584 3 my $locked= 0;
585 3 while (1) {
586 3 ▶ last if flock($fh, LOCK_EX|LOCK_NB);
Branch: 1 branch
BranchTrueFalseTotal
if flock $fh, 6 3 0 3
587 0 ▶ croak "Can't get lock on $fname after $timeout seconds" if Time::HiRes::time() >= $deadline;
Branch: 1 branch
BranchTrueFalseTotal
if &Time::HiRes::time() >= $deadline 0 0 0
588 0 Time::HiRes::sleep(0.4);
589 }
590
591 # Succeeded in getting the lock, so write our pid.
592 3 my $data= "$$";
593 3 ▶ syswrite($fh, $data, length($data)) or croak "Failed to write pid to $fname";
Branch: 1 branch
BranchTrueFalseTotal
syswrite $fh, $data, length $data or croak("Failed to write pid to $fname") 0 3 3
594 3 ▶ truncate($fh, length($data)) or croak "Failed to resize $fname";
Branch: 1 branch
BranchTrueFalseTotal
truncate $fh, length $data or croak("Failed to resize $fname") 0 3 3
595
596 3 return $fh;
597 }
598  
599 3 1;
600  
601 __END__
602  
603 =pod
604  
605 =head1 NAME
606  
607 Catalyst::Controller::AutoAssets::Handler - Handler type Role and default namespace
608  
609 =head1 DESCRIPTION
610  
611 This is the base Role for C<Catalyst::Controller::AutoAssets> Handler classes and is
612 where the majority of the work is done for the AutoAssets module. The Handler class is
613 specified in the 'type' config param and is relative to this namespace. Absolute class
614 names can also be specified with the '+' prefix, so the following are equivalent:
615  
616 type => 'Directory'
617
618 type => '+Catalyst::Controller::AutoAssets::Handler::Directory'
619  
620 Custom Handler classes can be written and used as long as they consume this Role. For examples
621 of how to write custom Handlers, see the existing Handlers below for reference.
622  
623 =head1 TYPE HANDLERS
624  
625 These are the current built in handler classes:
626  
627 =over
628  
629 =item L<Catalyst::Controller::AutoAssets::Handler::Directory>
630  
631 =item L<Catalyst::Controller::AutoAssets::Handler::CSS>
632  
633 =item L<Catalyst::Controller::AutoAssets::Handler::JS>
634  
635 =item L<Catalyst::Controller::AutoAssets::Handler::ImageSet>
636  
637 =item L<Catalyst::Controller::AutoAssets::Handler::IconSet>
638  
639 =back
640  
641 =head1 AUTHOR
642  
643 Henry Van Styn <vanstyn@cpan.org>
644  
645 =head1 COPYRIGHT AND LICENSE
646  
647 This software is copyright (c) 2013 by IntelliTree Solutions llc.
648  
649 This is free software; you can redistribute it and/or modify it under
650 the same terms as the Perl 5 programming language system itself.
651  
652 =cut
653  
654  
↑ Summary Controller/AutoAssets/Handler/Directory.pm »