lib/Dancer/Plugin/DynamicConfig.pm

filter: Statementstmt 93.8% 76 / 81 Branchbran 79.1% 19 / 24 Conditioncond 100% 3 / 3 MC/DCmcdc 100% 2 / 2 Subroutinesub 89.4% 17 / 19 Podpod 33.3% 1 / 3 totaltotal 89.3% 118 / 132 CC 15 SCAR 14.3
CC
15
Cov
91%
__ANON__
8.3
initialize
7.0
dc_stat
2.1
CRAP 4.2
↑ Summary
1 package Dancer::Plugin::DynamicConfig;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Dancer ':syntax';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Dancer::FileUtils qw(read_file_content);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Dancer::ModuleLoader;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Dancer::Plugin;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Encode qw(encode);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use JSON::XS qw(decode_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Time::HiRes;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 1 our $VERSION = '0.07';
14  
15 my @file_types = ({
16 type => 'json',
17 re => qr{\.json$},
18 7 ▶ parser => sub {
Subroutine __ANON__: called CC=1 SCAR=0.1
19 7 my ($filename) = @_;
20 7 my $json;
21 7 ▶ try {
Subroutine __ANON__: called CC=1 SCAR=0.2
22 7 read_file_content($filename) =~ /(.*)/s; # untaint
23 7 ▶ try {
Subroutine __ANON__: called CC=1 SCAR=0.0
24 7 $json = decode_json(encode('UTF-8', $1));
25 0 ▶ } catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
26 0 warning "couldn't parse json file ($filename): $_\n";
27 7 };
28 0 ▶ } catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
29 0 warning "couldn't find file to parse ($filename): $_\n";
30 7 };
31 7 return $json;
32 },
33 1 });
34  
35 1 my $dynamic_config;
36  
37 9 ▶register dynamic_config => sub {
Subroutine __ANON__: called CC=8 SCAR=21.2
38 9 my ($file_key) = @_;
39  
40 9 ▶ initialize() unless $dynamic_config;
Branch: 1 branch
BranchTrueFalseTotal
unless $dynamic_config 1 8 9
41  
42 9 ▶ if (my $fileinfo = $dynamic_config->{$file_key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $fileinfo = $dynamic_config->{$file_key}) { } 8 1 9
43 8 my $mtime = (dc_stat($fileinfo->{path}))[9];
44  
45 8 ▶ if (not defined $fileinfo->{data} or $mtime > $fileinfo->{mtime}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $fileinfo->{"data"} or $mtime > $fileinfo->{"mtime"}) 7 1 8
Condition100%3/3
not defined $fileinfo->{"data"} or $mtime > $fileinfo->{"mtime"}
l!l&&r!l&&!r
521
MC/DC100%2/2
not defined $fileinfo->{"data"} or $mtime > $fileinfo->{"mtime"}
not defined $fileinfo->{"data"} $mtime > $fileinfo->{"mtime"}
Truth table100%3/3
not(A) or B
A= not defined $fileinfo->{"data"}B= $mtime > $fileinfo->{"mtime"}
ABresult
1X1
011
000
46 7 my $parsed = $fileinfo->{parser}->($fileinfo->{path});
47  
48 7 ▶ if (defined $parsed) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $parsed) { } 7 0 7
49 7 $fileinfo->{mtime} = $mtime;
50 7 ▶ $fileinfo->{data} = $fileinfo->{rewrite_class} ? $fileinfo->{rewrite_class}->rewrite($parsed) : $parsed;
Branch: 1 branch
BranchTrueFalseTotal
$fileinfo->{'rewrite_class'} ? : 3 4 7
51 } else {
52 0 warning "could not parse $fileinfo->{path}";
53 0 ▶ warning "$@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
54 }
55 }
56  
57 8 return $fileinfo->{data};
58 } else {
59 1 warning "unknown dynamic_config key [$file_key]";
60 }
61  
62 1 return '';
63 1 };
64  
65 3 ▶sub reinitialize { undef $dynamic_config; initialize() }
Subroutine reinitialize: called CC=1 SCAR=0.0
Pod documented
66  
67 4 ▶sub initialize {
Subroutine initialize: called CC=7 SCAR=19.5
Pod undocumented
68 4 ▶ SETTING: while (my ($file_key, $etc) = each %{ plugin_setting() }) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{plugin_setting();}) 19 4 23
69 19 my ($path, $rewrite_class);
70 19 ▶ if (ref $etc) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $etc) { } 8 11 19
71 8 $path = $etc->{path};
72 8 $rewrite_class = $etc->{rewrite_class};
73 } else {
74 11 $path = $etc;
75 }
76  
77 19 foreach my $ft (@file_types) {
78 19 ▶ if ($path =~ $ft->{re}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($path =~ /$ft->{'re'}/) { } 18 1 19
79 $dynamic_config->{$file_key} = {
80 path => $path,
81 mtime => undef,
82 data => undef,
83 type => $ft->{type},
84 parser => $ft->{parser},
85 18 rewrite_class => $rewrite_class,
86 };
87  
88 18 ▶ if ($rewrite_class) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rewrite_class) 8 10 18
89 8 ▶ Dancer::ModuleLoader->load($rewrite_class) or die "Could not load $rewrite_class";
Branch: 1 branch
BranchTrueFalseTotal
"Dancer::ModuleLoader"->load($rewrite_class) or die "Could not load $rewrite_class" 8 0 8
90 }
91 18 next SETTING;
92 } else {
93 1 warning "ignoring config key [$file_key]: unknown filetype for [$path]";
94 1 warn "ignoring config key [$file_key]: unknown filetype for [$path]";
95 # should this die? your config is so broken we can't start up
96 }
97 }
98 }
99  
100 4 return;
101 }
102  
103 8 ▶sub dc_stat {
Subroutine dc_stat: called CC=2 SCAR=7.6
Pod undocumented
104 8 my ($filething) = @_;
105  
106 8 ▶ if (defined &Time::HiRes::stat) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined &Time::HiRes::stat) { } 8 0 8
107 8 return Time::HiRes::stat($filething);
108 } else {
109 0 return stat($filething);
110 }
111 }
112  
113 1 register_plugin;
114  
115 1 1;
116  
117 __END__
↑ Summary