Application.pm

filter: Statementstmt 90.0% 18 / 20 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 7 / 7 Podpod n/a totaltotal 92.5% 25 / 27 CC 1 SCAR 0.4
CC
1
Cov
90%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« Application/Template/PreCacheEndpoint.pm ↑ Summary
1 package Progressive::Web::Application;
2 1 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Carp qw//;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Colouring::In;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 0 ▶use Image::Scale;
Subroutine BEGIN: called CC=1 SCAR=2.6
9 use Cwd qw/abs_path/;
10 our $VERSION = '0.09';
11 our (%TOOL, %MANIFEST_SCHEMA);
12 BEGIN {
13 %TOOL = (
14 tainted => qr/^([\d\/\-\@\w.]+)$/,
15 array_check => sub {
16 my $ref = ref $_[0];
17 Carp::croak(sprintf q/Value is not an ARRAY for field %s/, $_[1]) if !$ref || ref $_[0] ne 'ARRAY';
18 return $_[0];
19 },
20 scalar_check => sub {
21 Carp::croak(sprintf q/Value is not a scalar for field %s/, $_[1]) if ref $_[0];
22 return $_[0];
23 },
24 colour_check => sub { Colouring::In->new($_[0])->toCSS; },
25 JSON => JSON->new->utf8->pretty,
26 to_json => sub { $TOOL{JSON}->encode($_[0]); },
27 from_json => sub { return $TOOL{JSON}->decode($_[0]); },
28 make_path => sub {
29 my $path = abs_path();
30 for (split '/', $_[0]) {
31 $path .= "/$_";
32 $path =~ $TOOL{tainted};
33 if (! -d $1) {
34 mkdir $1 or Carp::croak(qq/
35 Cannot open file for writing $!
36 /);
37 }
38 }
39 return $path;
40 },
41 remove_abs => sub {
42 my $abs = abs_path();
43 $_[0] =~ s/$abs//;
44 return $_[0];
45 },
46 abs => sub {
47 return $_[0] if ($_[0] =~ m/^\//);
48 return sprintf "%s/%s", abs_path(), $_[0];
49 },
50 write_file => sub {
51 $TOOL{abs}->($_[0]) =~ $TOOL{tainted};
52 open my $fh, '>', $1 or Carp::croak(qq/
53 Cannot open file for writing $!
54 /);
55 print $fh $_[1];
56 close $fh;
57 },
58 read_file => sub {
59 $TOOL{abs}->($_[0]) =~ $TOOL{tainted};
60 open my $fh, '<', $1 or Carp::croak(qq/
61 Cannot open file for reading $!
62 /);
63 my $content = do { local $/; <$fh> };
64 close $fh;
65 chomp($content);
66 return $content;
67 },
68 remove_file => sub { $TOOL{remove_directory}->($TOOL{abs}->($_[0])); },
69 read_directory => sub {
70 my ($val, %opts) = $TOOL{parse_params}->(@_);
71 $val = $TOOL{abs}->($val);
72 $val =~ $TOOL{tainted};
73 opendir(my $DIR, $1) or die "Can't open $1$!";
74 my @files;
75 for my $file (grep {$_ !~ /^\./} readdir($DIR)) {
76 my $path = sprintf "%s/%s", $val, $file;
77 $path =~ $opts{blacklist_regex} && next if $opts{blacklist_regex};
78 $path =~ $opts{whitelist_regex} || next if $opts{whitelist_regex};
79 push @files, $opts{recurse} && -d $path
80 ? map {
81 sprintf "%s/%s", $file, $_;
82 } $TOOL{read_directory}->($path, %opts)
83 : $path;
84 }
85 closedir ($DIR) or die "Cant close $1$!";
86 return @files;
87 },
88 remove_directory => sub {
89 $TOOL{abs}->($_[0]) =~ $TOOL{tainted};
90 if (-d $1) {
91 my $d = $1;
92 opendir(my $DIR, $1) or Carp::croak "Can't open $1$!";
93 for my $file (grep { $_ !~ /^\./} readdir($DIR)) {
94 my $path = sprintf "%s/%s", $_[0], $file;
95 $TOOL{remove_directory}->($path);
96 }
97 closedir($DIR) or Carp::craok( "Can't close $1$!");
98 rmdir($d);
99 } else {
100 unlink $1 or Carp::croak(qq/
101 Cannot remove file $1$!
102 /);
103 }
104 return 1;
105 },
106 valid_icon_sizes => { map {
107 $_ => 1
108 } "36x36", "48x48", "57x57", "60x60", "70x70", "72x72", "76x76",
109 "96x96", "114x114", "120x120", "128x128", "150x150", "144x144",
110 "152x152", "180x180", "192x192", "310x310" },
111 valid_icon_types => { map {
112 $_ => 1
113 } "image/png" },
114 generate_icons => sub {
115 my ($icon, %options) = $TOOL{parse_params}->(@_);
116 Carp::croak('No initial icon passed to generate_icons') if !$icon;
117 my $img = Image::Scale->new($icon);
118 $options{outpath} = substr($icon, 0, rindex($icon, '/')) if !$options{outpath};
119 $options{outpath} =~ s/\/$//;
120 $TOOL{make_path}->($options{outpath});
121 my @iconSizes = sort { $b->{width} <=> $a->{width} }
122 map {
123 my @s = split 'x', $_;
124 { width => $s[0] + 0, height => $s[0] + 0 }
125 } keys %{ $TOOL{valid_icon_sizes} };
126 my @files;
127 for my $size (@iconSizes) {
128 $img->resize_gd_fixed_point({
129 width => $size->{width},
130 height => $size->{width},
131 keep_aspect => 1
132 });
133 my $file = sprintf(
134 '%s/%sx%s-%s.png',
135 $options{outpath},
136 $size->{width},
137 $size->{height},
138 $options{icon_name} || q|icon|
139 );
140 $TOOL{write_file}->(
141 $file,
142 $img->as_png()
143 );
144 push @files, {
145 sizes => sprintf(q|%sx%s|, $size->{width}, $size->{height}),
146 src => q|/| . $file,
147 type => q|image/png|
148 };
149 }
150 return @files;
151 },
152 identify_icon_size => sub {
153 my $img = Image::Scale->new($TOOL{abs}->($_[0]));
154 my $size = sprintf "%sx%s", $img->width(), $img->height();
155 return $TOOL{valid_icon_sizes}->{$size} ? $size : undef;;
156 },
157 identify_icon_information => sub {
158 my $root = shift;
159 my @files;
160 for my $file (@_) {
161 my $type = sprintf( "image/%s", substr($file, rindex($file, q|.|) + 1));
162 my $size;
163 next unless ($TOOL{valid_icon_types}->{$type});
164 next unless $size = $TOOL{identify_icon_size}->($file);
165 $root ? $file =~ s/(.*$root)// : $TOOL{remove_abs}->($file);
166 $file = q|/| . $file if $file !~ m/^\//;
167 push @files, {
168 src => $file,
169 type => $type,
170 sizes => $size
171 };
172 }
173 return @files;
174 },
175 validate_icon_information => sub {
176 for (qw/src sizes type/) {
177 if (! defined $_[0]->{$_} ) {
178 Carp::croak(
179 sprintf
180 q/Required field not passed for icon missing %s for %s/,
181 $_,
182 $TOOL{to_json}->($_[0])
183 );
184 }
185 $TOOL{scalar_check}->($_[0]->{$_}, $_);
186 }
187 $_[0]->{src} = q|/| . $_[0]->{src} unless $_[0]->{src} =~ m/^\//;
188 $TOOL{valid_icon_sizes}->{$_[0]->{sizes}}
189 || Carp::croak(
190 sprintf
191 q/Invalid size %s for icon %s/,
192 $_[0]->{sizes},
193 $TOOL{to_json}->($_[0])
194 );
195 $TOOL{valid_icon_types}->{$_[0]->{type}}
196 || Carp::croak(
197 sprintf
198 q/Invalid icon type %s for icon %s/,
199 $_[0]->{types},
200 $TOOL{to_json}->($_[0])
201 );
202 return $_[0];
203 },
204 parse_params => sub {
205 return ( shift, (ref($_[0]) || "") eq q|HASH| ? %{$_[0]} : @_ );
206 },
207 identify_files_to_cache => sub {
208 my ($directory, %options) = $TOOL{parse_params}->(@_);
209 if (!$directory) {
210 Carp::croak(
211 q/no directory passed into identify_files_to_cache/
212 );
213 }
214 my @files_to_cache;
215 for my $dir (ref $directory ? @{$directory} : $directory) {
216 push @files_to_cache, sort { $a cmp $b } map {
217 $options{root} ? $_ =~ s/(.*$options{root})// : $TOOL{remove_abs}->($_);
218 $_ = q|/| . $_ if $_ !~ m/^\//;
219 $_ = sprintf("/%s%s", $options{pathpart}, $_) if $options{pathpart};
220 $_;
221 } $TOOL{read_directory}->($dir, %options);
222 }
223 return @files_to_cache;
224 },
225 valid_orientation => {
226 any => 1,
227 natural => 1,
228 landscape => 1,
229 q|landscape-primary| => 1,
230 q|landscape-secondary| => 1,
231 portrait => 1,
232 q|portrait-primary| => 1,
233 q|portrait-secondary| => 1
234 }
235 );
236 %MANIFEST_SCHEMA = (
237 name => $TOOL{scalar_check},
238 short_name => $TOOL{scalar_check},
239 description => $TOOL{scalar_check},
240 lang => $TOOL{scalar_check}, #TODO,
241 dir => sub {
242 $TOOL{scalar_check}->(@_);
243 $_[0] =~ m/^(auto|ltr|rtl)$/;
244 return $1 ? $_[0] : Carp::croak(sprintf q|Invalid display value passed %s|, $_[0]);
245 },
246 orientation => sub {
247 $TOOL{scalar_check}->(@_);
248 return $TOOL{valid_orientation}->{$_[0]} ? $_[0] : Carp::croak(sprintf q|Invalid orientation value passed %s|, $_[0]);
249 },
250 prefer_related_applications => sub { return (ref $_[0] ? ${$_[0]} : $_[0]) ? \1 : \0; },
251 related_applications => sub {
252 $TOOL{array_check}->(@_);
253 for my $app (@{$_[0]}) {
254 Carp::croak(q|related_applicaiton is not a HASH|) unless ref $app eq q|HASH|;
255 for (qw/platform url/, ($app->{platform} || "") =~ m/play/i ? 'id' : ()) {
256 Carp::croak(sprintf
257 q|Missing required param %s in related_application %s|,
258 $_,
259 $TOOL{to_json}->($app)
260 ) unless $app->{$_};
261 }
262 }
263 return $_[0];
264 },
265 iarc_rating_id => $TOOL{scalar_check}, # TODO parenting
266 scope => $TOOL{scalar_check}, # TODO should validate a path
267 screenshots => sub {
268 $TOOL{array_check}->(@_);
269 for my $screenshot (@{$_[0]}) {
270 Carp::croak(q|screenshot is not a HASH|) unless ref $screenshot eq q|HASH|;
271 map {
272 Carp::croak(sprintf
273 q|Missing required param %s in screenshot %s|,
274 $_,
275 $TOOL{to_json}->($screenshot)
276 ) unless $screenshot->{$_};
277 } qw/src sizes type/;
278 }
279 # TODO - once known to be fully supported...
280 # Mechanize->screenshot
281 # validate src sizes type can likely reuse icons
282 return $_[0];
283 },
284 categories => $TOOL{array_check},
285 start_url => $TOOL{scalar_check}, # TODO should validate a path
286 icons => sub {
287 my ($ref, @icons) = ref $_[0];
288 push @icons, !$ref
289 ? $TOOL{identify_icon_information}->(
290 $_[2], sort { $b cmp $a } $TOOL{read_directory}->($_[0])
291 )
292 : $ref eq q|ARRAY|
293 ? map {
294 $MANIFEST_SCHEMA{icons}->($_);
295 } @{$_[0]}
296 : $ref eq q|HASH|
297 ? ($_[0]->{file}
298 ? $TOOL{generate_icons}->($_[0]->{file},
299 root => $_[2], %{$_[0]}
300 )
301 : $TOOL{validate_icon_information}->($_[0])
302 )
303 : map { $TOOL{validate_icon_information}->($_) } $_[0]->(\%TOOL);
304 if ($_[3]) {
305 @icons = map {
306 $_->{src} = q|/| . $_[3] . $_->{src} if $_->{src} !~ m/^\/$_[3]/;
307 $_;
308 } @icons;
309 }
310 return wantarray ? @icons : \@icons;
311 },
312 display => sub {
313 $TOOL{scalar_check}->($_[0], q|display|);
314 Carp::croak(sprintf
315 q/Invalid display value passed %s must be one of standalone, minimal-ui, fullscreen or browser/, $_[0]
316 ) unless $_[0] =~ m/^(standalone|minimal\-ui|fullscreen|browser)$/;
317 return $_[0];
318 },
319 background_color => $TOOL{colour_check},
320 theme_color => $TOOL{colour_check},
321 # TODO: once available https://developer.mozilla.org/en-US/docs/Web/Manifest/serviceworker
322 );
323 }
324  
325 sub new {
326 my ($package, %args) = $TOOL{parse_params}->(@_);
327 my $new = bless {}, $package;
328 for (qw/root pathpart/) { $new->{$_} = $args{$_} if exists $args{$_}; }
329 $new->set_manifest($args{manifest}) if $args{manifest};
330 $new->set_params($args{params}) if $args{params};
331 $new->set_template(%args);
332 return $new;
333 }
334  
335 sub set_template {
336 my ($self, %args) = $TOOL{parse_params}->(@_);
337 $self->{template_class} = sprintf(
338 q|Progressive::Web::Application::Template::%s|,
339 $args{template} || q|General|
340 );
341 eval "require $self->{template_class}" || Carp::croak $@;
342 $self->{template} = $self->{template_class}->new();
343 }
344  
345 sub set_pathpart { $_[0]->{pathpart} = $_[-1]; }
346  
347 sub set_root { $_[0]->{root} = $_[-1]; }
348  
349 sub has_manifest { $_[0]->{manifest} ? 1 : 0 }
350  
351 sub manifest { $_[1] ? $TOOL{to_json}->($_[0]->{manifest}) : $_[0]->{manifest}; }
352  
353 sub set_manifest {
354 my ($self, %args) = $TOOL{parse_params}->(@_);
355 for my $key (keys %args) {
356 my $validate = $MANIFEST_SCHEMA{$key}
357 or Carp::croak(sprintf q/Invalid key passed to setup manifest %s/, $key);
358 my $val = $validate->($args{$key}, $key, $self->{root}, $self->{pathpart});
359 $self->{manifest}{$key} = $val;
360 }
361 $self->{manifest};
362 }
363  
364 sub clear_manifest { delete $_[0]->{manifest}; }
365  
366 sub has_params { $_[0]->{params} ? 1 : 0 }
367  
368 sub params { $_[1] ? $TOOL{to_json}->($_[0]->{params}) : $_[0]->{params}; }
369  
370 sub set_params {
371 my ($self, %args) = $TOOL{parse_params}->(@_);
372 my $ftc = delete $args{files_to_cache};
373 $self->{params} = {%{$self->{params} || {}}, %args};
374 if ($ftc) {
375 my %map = (
376 ARRAY => sub { @{$ftc} },
377 CODE => sub { @{$ftc->(\%TOOL)} },
378 HASH => sub {
379 $TOOL{identify_files_to_cache}->(
380 $ftc->{directory},
381 root => $self->{root},
382 pathpart => $self->{pathpart},
383 %{$ftc}
384 );
385 }
386 );
387 my $ref = ref $ftc;
388 Carp::croak(q|currently set_params files_to_cache cannot handle | . $ref) unless $map{$ref};
389 my @filesToCache = $map{$ref}->();
390 unshift @filesToCache, $self->{params}{offline_path} if $self->{params}{offline_path};
391 $self->{params}{files_to_cache} = \@filesToCache;
392 }
393 $self->{params}{cache_name} ||= q|Set-a-cache-name-v1|;
394 $self->{params};
395 }
396  
397 sub clear_params { delete $_[0]->{params}; }
398  
399 sub template { $_[0]->{template} }
400  
401 sub templates { $_[0]->{template}->render($_[0]->{params}); }
402  
403 sub compile {
404 my ($self, $root) = @_;
405 $root ||= $self->{root};
406 Carp::croak(
407 q/No root directory provided to compile manifest and service worker/
408 ) unless $root;
409 $TOOL{make_path}->($root);
410 my %build = (
411 ($self->has_manifest ? (manifest => $self->manifest(1)) : ()),
412 ($self->has_params ? (templates => $self->templates()) : ()),
413 );
414 $TOOL{write_file}->(sprintf("%s/manifest.json", $root), $build{manifest}) if $build{manifest};
415 if ($build{templates}) {
416 for my $template (keys %{$build{templates}}) {
417 $TOOL{write_file}->(
418 sprintf("%s/%s", $root, $template),
419 $build{templates}{$template}
420 );
421 }
422 }
423 }
424  
425 sub tools { return \%TOOL; }
426  
427 sub manifest_schema { return \%MANIFEST_SCHEMA; }
428  
429 __END__
« Application/Template/PreCacheEndpoint.pm ↑ Summary