Controller/AutoAssets/Handler.pm

filter: Statementstmt 81.8% 252 / 308 Branchbran 52.0% 51 / 98 Conditioncond 42.6% 32 / 75 MC/DCmcdc 7.5% 3 / 40 Subroutinesub 81.0% 60 / 74 Podpod 0.0% 0 / 26 totaltotal 64.0% 398 / 621 CC 81 SCAR 16.9
CC
81
Cov
70%
build_asset
14.6
client_current_etag
12.0
__ANON__
12.0
CRAP 5.4
« Plugin/AutoAssets.pm ↑ Summary Controller/AutoAssets/Handler/ImageSet.pm »
1 package Catalyst::Controller::AutoAssets::Handler;
2 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 # VERSION
6  
7 4 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 4 requires qw(
11 asset_request
12 write_built_file
13 );
14  
15 4 ▶use Cwd;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 4 ▶use Path::Class 0.32 qw( dir file );
Subroutine BEGIN: called CC=1 SCAR=0.0
17 4 ▶use Fcntl qw( :DEFAULT :flock );
Subroutine BEGIN: called CC=1 SCAR=0.0
18 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 4 ▶use File::stat qw(stat);
Subroutine BEGIN: called CC=1 SCAR=0.0
20 4 ▶use Catalyst::Utils;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 4 ▶use Time::HiRes qw(gettimeofday tv_interval);
Subroutine BEGIN: called CC=1 SCAR=0.0
22 4 ▶use Storable qw(store retrieve);
Subroutine BEGIN: called CC=1 SCAR=0.0
23 4 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 4 ▶use Data::Dumper::Concise 'Dumper';
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 4 require Digest::SHA1;
27 4 require MIME::Types;
28 4 require Module::Runtime;
29  
30 4 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 4 has 'include', is => 'ro', isa => 'ScalarRef|Str|ArrayRef[ScalarRef|Str]', required => 1;
38  
39 # Optional regex to require files to match to be included
40 4 has 'include_regex', is => 'ro', isa => 'Maybe[Str]', default => undef;
41  
42 # Optional regex to exclude files
43 4 has 'exclude_regex', is => 'ro', isa => 'Maybe[Str]', default => undef;
44  
45 # Whether or not to use qr/$regex/i or qr/$regex/
46 4 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 4 has 'current_redirect', is => 'ro', isa => 'Bool', default => 1;
51  
52 # What string to use for the 'current' redirect
53 4 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 4 has 'allow_static_requests', is => 'ro', isa => 'Bool', default => 0;
58  
59 # What string to use for the 'static' path
60 4 has 'static_alias', is => 'ro', isa => 'Str', default => 'static';
61  
62 # Extra custom response headers for current/static requests
63 4 ▶has 'current_response_headers', is => 'ro', isa => 'HashRef', default => sub {{}};
Subroutine __ANON__: called CC=1 SCAR=0.0
64 4 ▶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 4 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 4 ▶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 4 has 'max_lock_wait', is => 'ro', isa => 'Int', default => 120;
76  
77 has 'cache_control_header', is => 'ro', isa => 'Str',
78 4 ▶ 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 4 ▶has 'persist_state', is => 'ro', isa => 'Bool', default => sub{0};
Subroutine __ANON__: called CC=1 SCAR=0.0
82  
83 # Optional shorter checksum
84 3 ▶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 4 ▶has 'include_relative_dir', isa => 'Path::Class::Dir', is => 'ro', lazy => 1, default => sub {
Subroutine __ANON__: called CC=3 SCAR=12.0
89 4 my $self = shift;
90 4 my $home = $self->_app->config->{home};
91 4 ▶ $home = $home && -d $home ? $self->_app->config->{home} : cwd();
Branch: 1 branch
BranchTrueFalseTotal
$home && -d $home ? : 0 4 4
Condition33%1/3
$home and -d $home
!ll&&!rl&&r
400
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 4 return dir( $home );
93 4 };
94  
95  
96  
97 ######################################
98  
99  
100 0 ▶sub BUILD {}
Subroutine BUILD: not called CC=1 SCAR=0.0
Pod undocumented
101 4 ▶before BUILD => sub {
Subroutine __ANON__: called CC=5 SCAR=17.1
102 4 my $self = shift;
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 4 ▶ $self->_restore_state if($self->persist_state);
Branch: 1 branch
BranchTrueFalseTotal
if $self->persist_state 0 4 4
107  
108 # init includes
109 4 $self->includes;
110
111 Catalyst::Exception->throw("Must include at least one file/directory")
112 4 ▶ unless (scalar @{$self->includes} > 0);
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @{$self->includes;} > 0 0 4 4
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 4 ▶ 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 4 4
Condition33%1/3
$self->sha1_string_length >= 5 and $self->sha1_string_length <= 40
!ll&&!rl&&r
004
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 4 $self->work_dir->mkpath($self->_app->debug);
121 4 $self->work_dir->resolve;
122
123 4 $self->prepare_asset;
124 4 };
125  
126 # Main code entry point:
127 18 ▶sub request {
Subroutine request: called CC=4 SCAR=14.5
Pod undocumented
128 18 my ( $self, $c, @args ) = @_;
129 18 my $sha1 = $args[0];
130
131 18 ▶ return $self->current_request($c, @args) if (
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_current_request_arg(@args) 4 14 18
132 $self->is_current_request_arg(@args)
133 );
134
135 14 ▶ return $self->static_request($c, @args) if (
Branch: 1 branch
BranchTrueFalseTotal
if $self->allow_static_requests and $self->static_alias eq $sha1 0 14 14
Condition33%1/3
$self->allow_static_requests and $self->static_alias eq $sha1
!ll&&!rl&&r
1400
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 14 return $self->handle_asset_request($c, @args);
141 }
142  
143 5 ▶sub is_current_request_arg {
Subroutine is_current_request_arg: called CC=2 SCAR=6.9
Pod undocumented
144 5 my ($self, $arg) = @_;
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 4 ▶sub current_request {
Subroutine current_request: called CC=2 SCAR=6.9
Pod undocumented
149 4 my ( $self, $c, $arg, @args ) = @_;
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 0 ▶sub static_request {
Subroutine static_request: not called CC=2 SCAR=17.9
Pod undocumented
160 0 my ( $self, $c, $arg, @args ) = @_;
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 14 ▶sub handle_asset_request {
Subroutine handle_asset_request: called CC=3 SCAR=12.3
Pod undocumented
172 14 my ( $self, $c, $arg, @args ) = @_;
173
174 14 $self->prepare_asset(@args);
175
176 14 ▶ 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 14 14
Condition33%1/3
$self->use_etags and $self->client_current_etag($c, $arg, @args)
!ll&&!rl&&r
1400
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 14 $self->asset_request($c, $arg, @args);
182 }
183 12 return $c->detach;
184 }
185  
186 0 ▶sub client_current_etag {
Subroutine client_current_etag: not called CC=3 SCAR=24.8
Pod undocumented
187 0 my ( $self, $c, $arg, @args ) = @_;
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 0 ▶sub etag_value {
Subroutine etag_value: not called CC=1 SCAR=6.9
Pod undocumented
196 0 my $self = shift;
197 0 return '"' . join('/',$self->asset_name,@_) . '"';
198 }
199  
200  
201 ############################
202  
203  
204 0 ▶has 'work_dir', is => 'ro', isa => 'Path::Class::Dir', lazy => 1, default => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
205 0 my $self = shift;
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 4 };
213  
214 4 ▶has 'built_file', is => 'ro', isa => 'Path::Class::File', lazy => 1, default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
215 4 my $self = shift;
216 4 my $filename = 'built_file';
217 4 return file($self->work_dir,$filename);
218 4 };
219  
220 1 ▶has 'scratch_dir', is => 'ro', isa => 'Path::Class::Dir', lazy => 1, default => sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
221 1 my $self = shift;
222
223 1 my $Dir = dir($self->work_dir,'_scratch');
224 1 ▶ $Dir->rmtree if (-d $Dir);
Branch: 1 branch
BranchTrueFalseTotal
if -d $Dir 0 1 1
225 1 $Dir->mkpath;
226
227 1 return $Dir
228 4 };
229  
230 4 ▶has 'fingerprint_file', is => 'ro', isa => 'Path::Class::File', lazy => 1, default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
231 4 my $self = shift;
232 4 return file($self->work_dir,'fingerprint');
233 4 };
234  
235 4 ▶has 'lock_file', is => 'ro', isa => 'Path::Class::File', lazy => 1, default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
236 4 my $self = shift;
237 4 return file($self->work_dir,'lockfile');
238 4 };
239  
240  
241 4 ▶has 'includes', is => 'ro', isa => 'ArrayRef', lazy => 1, default => sub {
Subroutine __ANON__: called CC=7 SCAR=19.6
242 4 my $self = shift;
243 4 my $rel = $self->include_relative_dir;
244  
245 4 ▶ my @list = ((ref $self->include)||'') eq 'ARRAY' ? @{$self->include} : $self->include;
Branch: 1 branch
BranchTrueFalseTotal
(ref $self->include || '') eq 'ARRAY' ? : 4 0 4
Condition50%1/2
ref $self->include || ''
l!l
40
MC/DC0%0/1
ref $self->include || ''
ref $self->include
Truth table50%1/2
A
A= ref $self->include
Aresult
11
00
246 4 my $i = 0;
247 return [ map {
248 4 my $inc; $i++;
249 4 ▶ if((ref($_)||'') eq 'SCALAR') {
Branch: 1 branch
BranchTrueFalseTotal
if ((ref $_ || '') eq 'SCALAR') { } 1 3 4
Condition100%2/2
ref $_ || ''
l!l
13
MC/DC100%1/1
ref $_ || ''
ref $_
Truth table100%2/2
A
A= ref $_
Aresult
11
00
250 # New support for ScalarRef includes ... we pre-dump them to a temp file
251 1 $inc = file( $self->scratch_dir, join('','_generated_include_file_',$i) );
252 1 $inc->spew(iomode => '>:raw', $$_);
253 }
254 else {
255 3 $inc = file($_);
256 }
257 4 ▶ $inc = $rel->file($inc) unless ($inc->is_absolute);
Branch: 1 branch
BranchTrueFalseTotal
unless $inc->is_absolute 0 4 4
258 4 ▶ $inc = dir($inc) if (-d $inc); #<-- convert to Path::Class::Dir
Branch: 1 branch
BranchTrueFalseTotal
if -d $inc 2 2 4
259 4 $inc->resolve
260 } @list ];
261 4 };
262  
263  
264  
265  
266 18 ▶sub get_include_files {
Subroutine get_include_files: called CC=4 SCAR=13.9
Pod undocumented
267 18 my $self = shift;
268
269 18 my @excluded = ();
270 18 my @files = ();
271 18 for my $inc (@{$self->includes}) {
272 18 ▶ if($inc->is_dir) {
Branch: 1 branch
BranchTrueFalseTotal
if ($inc->is_dir) { } 12 6 18
273 $inc->recurse(
274 preorder => 1,
275 depthfirst => 1,
276 41 ▶ callback => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
277 41 my $child = shift;
278 41 ▶ $self->_valid_include_file($child)
Branch: 1 branch
BranchTrueFalseTotal
$self->_valid_include_file($child) ? : 27 14 41
279 ? push @files, $child->absolute
280 : push @excluded, $child->absolute;
281 }
282 12 );
283 }
284 else {
285 6 ▶ $self->_valid_include_file($inc)
Branch: 1 branch
BranchTrueFalseTotal
$self->_valid_include_file($inc) ? : 6 0 6
286 ? push @files, $inc->absolute
287 : push @excluded, $inc->absolute;
288 }
289 }
290
291 # Some handlers (like Directory) need to know about excluded files
292 18 $self->_record_excluded_files(\@excluded);
293
294 # force consistent ordering of files:
295 18 return [sort @files];
296 }
297  
298 # optional hook for excluded files:
299 17 ▶sub _record_excluded_files {}
Subroutine _record_excluded_files: called CC=1 SCAR=0.0
300  
301  
302 has '_include_regexp', is => 'ro', isa => 'Maybe[RegexpRef]',
303 0 ▶ lazy => 1, init_arg => undef, default => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
304 0 my $self = shift;
305 0 ▶ my $str = $self->include_regex or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $str = $self->include_regex or return undef 0 0 0
306 0 ▶ return $self->regex_ignore_case ? qr/$str/i : qr/$str/;
Branch: 1 branch
BranchTrueFalseTotal
$self->regex_ignore_case ? : 0 0 0
307 4 };
308 has '_exclude_regexp', is => 'ro', isa => 'Maybe[RegexpRef]',
309 0 ▶ lazy => 1, init_arg => undef, default => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
310 0 my $self = shift;
311 0 ▶ my $str = $self->exclude_regex or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $str = $self->exclude_regex or return undef 0 0 0
312 0 ▶ return $self->regex_ignore_case ? qr/$str/i : qr/$str/;
Branch: 1 branch
BranchTrueFalseTotal
$self->regex_ignore_case ? : 0 0 0
313 4 };
314  
315 47 ▶sub _valid_include_file {
Subroutine _valid_include_file: called CC=6 SCAR=20.7
316 47 my ($self, $file) = @_;
317 return (
318 47 ▶ $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 33 47
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
14033
$file->is_dir or $self->include_regex and not $file =~ $self->_include_regexp
l!l&&r!l&&!r
14033
$self->include_regex and not $file =~ $self->_include_regexp
!ll&&!rl&&r
3300
$self->exclude_regex and $file =~ $self->_exclude_regexp
!ll&&!rl&&r
3300
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
319 || ($self->include_regex && ! ($file =~ $self->_include_regexp))
320 || ($self->exclude_regex && $file =~ $self->_exclude_regexp)
321 ) ? 0 : 1;
322 }
323  
324 4 ▶has 'last_fingerprint_calculated', is => 'rw', isa => 'Maybe[Int]', default => sub{undef};
Subroutine __ANON__: called CC=1 SCAR=0.0
325  
326 4 ▶has 'built_mtime', is => 'rw', isa => 'Maybe[Str]', default => sub{undef};
Subroutine __ANON__: called CC=1 SCAR=0.0
327 37 ▶sub get_built_mtime {
Subroutine get_built_mtime: called CC=2 SCAR=6.9
Pod undocumented
328 37 my $self = shift;
329 37 ▶ return -f $self->built_file ? $self->built_file->stat->mtime : undef;
Branch: 1 branch
BranchTrueFalseTotal
-f $self->built_file ? : 29 8 37
330 }
331  
332 # inc_mtimes are the mtime(s) of the include files. For directory assets
333 # this is *only* the mtime of the top directory (see subfile_meta below)
334 4 has 'inc_mtimes', is => 'rw', isa => 'Maybe[Str]', default => undef;
335 29 ▶sub get_inc_mtime_concat {
Subroutine get_inc_mtime_concat: called CC=1 SCAR=0.0
Pod undocumented
336 29 my $self = shift;
337 29 my $list = shift;
338 29 return join('-', map { $_->stat->mtime } @$list );
339 }
340  
341  
342 8 ▶sub calculate_fingerprint {
Subroutine calculate_fingerprint: called CC=2 SCAR=7.0
Pod undocumented
343 8 my $self = shift;
344 8 my $list = shift;
345 # include both the include (source) and built (output) in the fingerprint:
346 8 my $sha1 = $self->file_checksum(@$list,$self->built_file);
347 8 ▶ $self->last_fingerprint_calculated(time) if ($sha1);
Branch: 1 branch
BranchTrueFalseTotal
if $sha1 8 0 8
348 8 return $sha1;
349 }
350  
351 26 ▶sub current_fingerprint {
Subroutine current_fingerprint: called CC=2 SCAR=6.9
Pod undocumented
352 26 my $self = shift;
353 26 ▶ return undef unless (-f $self->fingerprint_file);
Branch: 1 branch
BranchTrueFalseTotal
unless -f $self->fingerprint_file 4 22 26
354 22 my $fingerprint = $self->fingerprint_file->slurp(iomode => '<:raw');
355 22 return $fingerprint;
356 }
357  
358 4 ▶sub save_fingerprint {
Subroutine save_fingerprint: called CC=2 SCAR=7.1
Pod undocumented
359 4 my $self = shift;
360 4 ▶ my $fingerprint = shift or die "Expected fingerprint/checksum argument";
Branch: 1 branch
BranchTrueFalseTotal
my $fingerprint = shift() or die "Expected fingerprint/checksum argument" 4 0 4
361 4 return $self->fingerprint_file->spew(iomode => '>:raw', $fingerprint);
362 }
363  
364 4 ▶sub calculate_save_fingerprint {
Subroutine calculate_save_fingerprint: called CC=2 SCAR=7.1
Pod undocumented
365 4 my $self = shift;
366 4 ▶ my $fingerprint = $self->calculate_fingerprint(@_) or return 0;
Branch: 1 branch
BranchTrueFalseTotal
my $fingerprint = $self->calculate_fingerprint(@_) or return 0 4 0 4
367 4 return $self->save_fingerprint($fingerprint);
368 }
369  
370 25 ▶sub fingerprint_calc_current {
Subroutine fingerprint_calc_current: called CC=4 SCAR=18.9
Pod undocumented
371 25 my $self = shift;
372 25 ▶ my $last = $self->last_fingerprint_calculated or return 0;
Branch: 1 branch
BranchTrueFalseTotal
my $last = $self->last_fingerprint_calculated or return 0 25 0 25
373 25 ▶ return 1 if ($self->max_fingerprint_calc_age == 0); # <-- 0 means infinite
Branch: 1 branch
BranchTrueFalseTotal
if $self->max_fingerprint_calc_age == 0 25 0 25
374 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
375 0 return 0;
376 }
377  
378 # -----
379 # Quick and dirty state persistence for faster startup
380 0 ▶has 'persist_state_file', is => 'ro', isa => 'Path::Class::File', lazy => 1, default => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
381 0 my $self = shift;
382 0 return file($self->work_dir,'state.dat');
383 4 };
384  
385 3 ▶has '_persist_attrs', is => 'ro', isa => 'ArrayRef', default => sub{[qw(
Subroutine __ANON__: called CC=1 SCAR=0.0
386 built_mtime
387 inc_mtimes
388 last_fingerprint_calculated
389 4 )]};
390  
391 4 ▶sub _persist_state {
Subroutine _persist_state: called CC=2 SCAR=12.6
392 4 my $self = shift;
393 4 ▶ return undef unless ($self->persist_state);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->persist_state 4 0 4
394 0 my $data = { map { $_ => $self->$_ } @{$self->_persist_attrs} };
395 0 $data->{_module_version} = $self->_module_version;
396 0 $data->{_build_params} = $self->_build_params;
397 0 store $data, $self->persist_state_file;
398 0 return $data;
399 }
400  
401 0 ▶sub _restore_state {
Subroutine _restore_state: not called CC=2 SCAR=17.9
402 0 my $self = shift;
403 0 ▶ return 0 unless (-f $self->persist_state_file);
Branch: 1 branch
BranchTrueFalseTotal
unless -f $self->persist_state_file 0 0 0
404 0 my $data;
405 0 ▶ try {
Subroutine __ANON__: not called CC=3 SCAR=24.8
406 0 $data = retrieve $self->persist_state_file;
407 0 ▶ if($self->_valid_state_data($data)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_valid_state_data($data)) 0 0 0
408 0 $self->$_($data->{$_}) for (@{$self->_persist_attrs});
409 }
410 }
411 0 ▶ catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
412 0 $self->clear_asset; #<-- make sure no partial state data is used
413 0 $self->_app->log->warn(
414 'Failed to restore state from ' . $self->persist_state_file
415 );
416 0 };
417 0 return $data;
418 }
419  
420 0 ▶sub _valid_state_data {
Subroutine _valid_state_data: not called CC=3 SCAR=24.8
421 0 my ($self, $data) = @_;
422
423 # Make sure the version and config params hasn't changed
424 return (
425 $self->_module_version eq $data->{_module_version}
426 && Dumper($self->_build_params) eq Dumper($data->{_build_params})
427 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
428 }
429 # -----
430  
431  
432 # force rebuild on next request/prepare_asset
433 0 ▶sub clear_asset {
Subroutine clear_asset: not called CC=1 SCAR=6.9
Pod undocumented
434 0 my $self = shift;
435 0 $self->inc_mtimes(undef);
436 }
437  
438 29 ▶sub _build_required {
Subroutine _build_required: called CC=8 SCAR=22.3
439 29 my ($self, $d) = @_;
440 return (
441 $self->inc_mtimes && $self->built_mtime &&
442 $d->{inc_mtimes} && $d->{built_mtime} &&
443 $self->inc_mtimes eq $d->{inc_mtimes} &&
444 $self->built_mtime eq $d->{built_mtime} &&
445 29 ▶ $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 ? : 25 4 29
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
4025
$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
4025
$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
4025
$self->inc_mtimes and $self->built_mtime and $d->{"inc_mtimes"} and $d->{"built_mtime"}
!ll&&!rl&&r
4025
$self->inc_mtimes and $self->built_mtime and $d->{"inc_mtimes"}
!ll&&!rl&&r
4025
$self->inc_mtimes and $self->built_mtime
!ll&&!rl&&r
4025
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
446 ) ? 0 : 1;
447 }
448  
449  
450 # Gets the data used throughout the prepare_asset process:
451 17 ▶sub get_prepare_data {
Subroutine get_prepare_data: called CC=1 SCAR=0.0
Pod undocumented
452 17 my $self = shift;
453
454 17 my $files = $self->get_include_files;
455 17 my $inc_mtimes = $self->get_inc_mtime_concat($files);
456 17 my $built_mtime = $self->get_built_mtime;
457
458 return {
459 17 files => $files,
460 inc_mtimes => $inc_mtimes,
461 built_mtime => $built_mtime
462 };
463 }
464  
465 17 ▶sub before_prepare_asset {}
Subroutine before_prepare_asset: called CC=1 SCAR=0.0
Pod undocumented
466  
467 29 ▶sub prepare_asset {
Subroutine prepare_asset: called CC=3 SCAR=11.0
Pod undocumented
468 29 my $self = shift;
469 29 my $start = [gettimeofday];
470  
471 # Optional hook:
472 29 $self->before_prepare_asset(@_);
473  
474 29 my $opt = $self->get_prepare_data;
475 29 ▶ return 1 unless $self->_build_required($opt);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_build_required($opt) 25 4 29
476  
477 #### -----
478 #### The code above this line happens on every request and is designed
479 #### to be as fast as possible
480 ####
481 #### The code below this line is (comparatively) expensive and only
482 #### happens when a rebuild is needed which should be rare--only when
483 #### content is modified, or on app startup (unless 'persist_state' is set)
484 #### -----
485  
486 ### Do a rebuild:
487  
488 # --- Blocks for up to max_lock_wait seconds waiting to get an exclusive lock
489 # The lock is held until it goes out of scope.
490 # If we fail to get the lock, we just continue anyway in hopes that the second
491 # build won't corrupt the first, which is arguably better than killing the
492 # request.
493 4 ▶ my $lock= try { $self->_get_lock($self->lock_file, $self->max_lock_wait); };
Subroutine __ANON__: called CC=1 SCAR=0.0
494 # ---
495
496 4 $self->build_asset($opt);
497
498 4 ▶ $self->_app->log->debug(
Branch: 1 branch
BranchTrueFalseTotal
if $self->_app->debug 0 4 4
499 "Built asset: " . $self->base_path . '/' . $self->asset_name .
500 ' in ' . sprintf("%.3f", tv_interval($start) ) . 's'
501 ) if ($self->_app->debug);
502  
503 # Release the lock and return:
504 4 $self->_persist_state;
505 4 return 1;
506 }
507  
508 4 ▶sub build_asset {
Subroutine build_asset: called CC=9 SCAR=26.8
Pod undocumented
509 4 my ($self, $opt) = @_;
510
511 4 ▶ my $files = $opt->{files} || $self->get_include_files;
Condition33%1/3
$opt->{'files'} || $self->get_include_files
l!l&&r!l&&!r
400
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
512 4 ▶ 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
400
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
513 4 ▶ 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
004
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
514
515 # Check the fingerprint to see if we can avoid a full rebuild (if mtimes changed
516 # but the actual content hasn't by comparing the fingerprint/checksum):
517 4 my $fingerprint = $self->calculate_fingerprint($files);
518 4 my $cur_fingerprint = $self->current_fingerprint;
519 4 ▶ if($fingerprint && $cur_fingerprint && $cur_fingerprint eq $fingerprint) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fingerprint and $cur_fingerprint and $cur_fingerprint eq $fingerprint) 0 4 4
Condition33%2/6
$fingerprint and $cur_fingerprint and $cur_fingerprint eq $fingerprint
!ll&&!rl&&r
400
$fingerprint and $cur_fingerprint
!ll&&!rl&&r
040
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
520 # If the mtimes changed but the fingerprint matches we don't need to regenerate.
521 # This will happen if another process just built the files while we were waiting
522 # for the lock and on the very first time after the application starts up
523 0 $self->inc_mtimes($inc_mtimes);
524 0 $self->built_mtime($built_mtime);
525 0 $self->_persist_state;
526 0 return 1;
527 }
528  
529 ### Ok, we really need to do a full rebuild:
530  
531 4 ▶ my $fd = $self->built_file->openw or die $!;
Branch: 1 branch
BranchTrueFalseTotal
my $fd = $self->built_file->openw or die $! 4 0 4
532 4 binmode $fd;
533 4 $self->write_built_file($fd,$files);
534 4 ▶ $fd->close if ($fd->opened);
Branch: 1 branch
BranchTrueFalseTotal
if $fd->opened 3 1 4
535
536 # Update the fingerprint (global) and cached mtimes (specific to the current process)
537 4 $self->inc_mtimes($opt->{inc_mtimes});
538 4 $self->built_mtime($self->get_built_mtime);
539 # we're calculating the fingerprint again because the built_file, which was just
540 # regenerated, is included in the checksum data. This could probably be optimized,
541 # however, this only happens on rebuild which rarely happens (should never happen)
542 # in production so an extra second is no big deal in this case.
543 4 $self->calculate_save_fingerprint($opt->{files});
544 }
545  
546 8 ▶sub file_checksum {
Subroutine file_checksum: called CC=4 SCAR=14.0
Pod undocumented
547 8 my $self = shift;
548 8 ▶ my $files = ref $_[0] eq 'ARRAY' ? $_[0] : \@_;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'ARRAY' ? : 0 8 8
549
550 8 my $Sha1 = Digest::SHA1->new;
551 8 foreach my $file ( grep { -f $_ } @$files ) {
552 22 ▶ my $fh = $file->openr or die "$! : $file\n";
Branch: 1 branch
BranchTrueFalseTotal
my $fh = $file->openr or die "$! : $file\n" 22 0 22
553 22 binmode $fh;
554 22 $Sha1->addfile($fh);
555 22 $fh->close;
556 }
557  
558 8 return substr $Sha1->hexdigest, 0, $self->sha1_string_length;
559 }
560  
561 8 ▶sub asset_name { (shift)->current_fingerprint }
Subroutine asset_name: called CC=1 SCAR=0.0
Pod undocumented
562  
563 8 ▶sub base_path {
Subroutine base_path: called CC=2 SCAR=7.0
Pod undocumented
564 8 my $self = shift;
565 8 ▶ my $pfx = try{RapidApp->active_request_context->mount_url} || '';
Subroutine __ANON__: called CC=1 SCAR=0.2
Condition50%1/2
&try(sub { 'RapidApp'->active_request_context->mount_url; } ) || ''
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
566 8 return join('/',$pfx,$self->action_namespace($self->_app));
567 }
568  
569 # this is just used for some internal optimization to avoid calling stat
570 # duplicate times. It is basically me being lazy, adding an internal extra param
571 # to asset_path() without changing its public API/arg list
572 4 has '_asset_path_skip_prepare', is => 'rw', isa => 'Bool', default => 0;
573 8 ▶before asset_path => sub {
Subroutine __ANON__: called CC=2 SCAR=7.2
574 8 my $self = shift;
575 8 ▶ $self->prepare_asset(@_) unless ($self->_asset_path_skip_prepare);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_asset_path_skip_prepare 8 0 8
576 4 };
577 8 ▶sub asset_path {
Subroutine asset_path: called CC=1 SCAR=0.0
Pod undocumented
578 8 my $self = shift;
579 8 return $self->base_path . '/' . $self->asset_name;
580 }
581  
582 0 ▶sub html_head_tags { undef }
Subroutine html_head_tags: not called CC=1 SCAR=6.9
Pod undocumented
583  
584 # This locks a file or dies trying, and on success, returns a "lock object"
585 # which will release the lock if it goes out of scope. At the moment, this
586 # "object" is just a file handle.
587 #
588 # This lock is specifically *not* inherited by child processes (thanks to
589 # fcntl(FL_CLOEXEC), and in fact, this design principle gives it
590 # cross-platform compatibility that most lock module sdon't have.
591 #
592 4 ▶sub _get_lock {
Subroutine _get_lock: called CC=8 SCAR=24.1
593 4 my ($self, $fname, $timeout)= @_;
594 4 my $fh;
595 4 ▶ 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") 4 0 4
Condition33%1/3
sysopen $fh, $fname, 194, 420 or sysopen $fh, $fname, 2
l!l&&r!l&&!r
400
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
596 or sysopen($fh, $fname, O_RDWR)
597 or croak "Unable to create or open $fname";
598
599 4 ▶ try { fcntl($fh, F_SETFD, FD_CLOEXEC) }
Subroutine __ANON__: called CC=1 SCAR=0.0
600 4 ▶ 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)") 4 0 4
601
602 # Try to get lock until timeout. We poll because there isn't a sensible
603 # way to wait for the lock. (I don't consider SIGALRM to be very sensible)
604 4 my $deadline= Time::HiRes::time() + $timeout;
605 4 my $locked= 0;
606 4 while (1) {
607 4 ▶ last if flock($fh, LOCK_EX|LOCK_NB);
Branch: 1 branch
BranchTrueFalseTotal
if flock $fh, 6 4 0 4
608 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
609 0 Time::HiRes::sleep(0.4);
610 }
611
612 # Succeeded in getting the lock, so write our pid.
613 4 my $data= "$$";
614 4 ▶ 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") 4 0 4
615 4 ▶ truncate($fh, length($data)) or croak "Failed to resize $fname";
Branch: 1 branch
BranchTrueFalseTotal
truncate $fh, length $data or croak("Failed to resize $fname") 4 0 4
616
617 4 return $fh;
618 }
619  
620 4 1;
621  
622 __END__
« Plugin/AutoAssets.pm ↑ Summary Controller/AutoAssets/Handler/ImageSet.pm »